comparison main/MainWindow.cpp @ 631:2484381b53a1

Add "Export Audio Data" to export audio waveform data into CSV or similar
author Chris Cannam
date Wed, 09 Oct 2013 14:56:48 +0100
parents c0a20cd1a9ff
children f45af8d8091e 6f06094daba0
comparison
equal deleted inserted replaced
627:9680e1056ac6 631:2484381b53a1
520 menu->addAction(iaction); 520 menu->addAction(iaction);
521 521
522 action = new QAction(tr("&Export Audio File..."), this); 522 action = new QAction(tr("&Export Audio File..."), this);
523 action->setStatusTip(tr("Export selection as an audio file")); 523 action->setStatusTip(tr("Export selection as an audio file"));
524 connect(action, SIGNAL(triggered()), this, SLOT(exportAudio())); 524 connect(action, SIGNAL(triggered()), this, SLOT(exportAudio()));
525 connect(this, SIGNAL(canExportAudio(bool)), action, SLOT(setEnabled(bool)));
526 menu->addAction(action);
527
528 action = new QAction(tr("&Export Audio Data..."), this);
529 action->setStatusTip(tr("Export audio from selection into a data file"));
530 connect(action, SIGNAL(triggered()), this, SLOT(exportAudioData()));
525 connect(this, SIGNAL(canExportAudio(bool)), action, SLOT(setEnabled(bool))); 531 connect(this, SIGNAL(canExportAudio(bool)), action, SLOT(setEnabled(bool)));
526 menu->addAction(action); 532 menu->addAction(action);
527 533
528 menu->addSeparator(); 534 menu->addSeparator();
529 535
2336 } 2342 }
2337 2343
2338 void 2344 void
2339 MainWindow::exportAudio() 2345 MainWindow::exportAudio()
2340 { 2346 {
2347 exportAudio(false);
2348 }
2349
2350 void
2351 MainWindow::exportAudioData()
2352 {
2353 exportAudio(true);
2354 }
2355
2356 void
2357 MainWindow::exportAudio(bool asData)
2358 {
2341 if (!getMainModel()) return; 2359 if (!getMainModel()) return;
2342 2360
2343 RangeSummarisableTimeValueModel *model = getMainModel(); 2361 RangeSummarisableTimeValueModel *model = getMainModel();
2344 std::set<RangeSummarisableTimeValueModel *> otherModels; 2362 std::set<RangeSummarisableTimeValueModel *> otherModels;
2345 RangeSummarisableTimeValueModel *current = model; 2363 RangeSummarisableTimeValueModel *current = model;
2397 model = m[item]; 2415 model = m[item];
2398 } 2416 }
2399 } 2417 }
2400 } 2418 }
2401 2419
2402 QString path = getSaveFileName(FileFinder::AudioFile); 2420 QString path;
2421 if (asData) {
2422 path = getSaveFileName(FileFinder::CSVFile);
2423 } else {
2424 path = getSaveFileName(FileFinder::AudioFile);
2425 }
2403 if (path == "") return; 2426 if (path == "") return;
2404 2427
2405 bool ok = false; 2428 bool ok = false;
2406 QString error; 2429 QString error;
2407 2430
2428 2451
2429 if (item == items[0]) selectionToWrite = &ms; 2452 if (item == items[0]) selectionToWrite = &ms;
2430 2453
2431 } else if (selections.size() > 1) { 2454 } else if (selections.size() > 1) {
2432 2455
2433 QStringList items; 2456 bool multiple = false;
2434 items << tr("Export the selected regions into a single audio file") 2457
2435 << tr("Export the selected regions into separate files") 2458 if (!asData) { // Multi-file export not supported for data
2436 << tr("Export the whole audio file"); 2459
2437 2460 QStringList items;
2438 QString item = ListInputDialog::getItem 2461 items << tr("Export the selected regions into a single file")
2439 (this, tr("Select region to export"), 2462 << tr("Export the selected regions into separate files")
2440 tr("Multiple regions of the original audio file are selected.\nWhat do you want to export?"), 2463 << tr("Export the whole file");
2441 items, 0, &ok); 2464
2465 QString item = ListInputDialog::getItem
2466 (this, tr("Select region to export"),
2467 tr("Multiple regions of the original audio file are selected.\nWhat do you want to export?"),
2468 items, 0, &ok);
2442 2469
2443 if (!ok || item.isEmpty()) return; 2470 if (!ok || item.isEmpty()) return;
2444 2471
2445 if (item == items[0]) { 2472 if (item == items[0]) {
2446 2473 selectionToWrite = &ms;
2474 } else if (item == items[1]) {
2475 multiple = true;
2476 }
2477
2478 } else { // asData
2447 selectionToWrite = &ms; 2479 selectionToWrite = &ms;
2448 2480 }
2449 } else if (item == items[1]) { 2481
2450 2482 if (multiple) { // Can only happen when asData false
2451 multiple = true;
2452 2483
2453 int n = 1; 2484 int n = 1;
2454 QString base = path; 2485 QString base = path;
2455 base.replace(".wav", ""); 2486 base.replace(".wav", "");
2456 2487
2482 } 2513 }
2483 } 2514 }
2484 } 2515 }
2485 2516
2486 if (!multiple) { 2517 if (!multiple) {
2487 WavFileWriter writer(path, 2518 if (asData) {
2488 model->getSampleRate(), 2519 CSVFileWriter writer(path, model,
2489 model->getChannelCount(), 2520 ((QFileInfo(path).suffix() == "csv") ?
2490 WavFileWriter::WriteToTemporary); 2521 "," : "\t"));
2491 writer.writeModel(model, selectionToWrite); 2522 if (selectionToWrite) {
2492 ok = writer.isOK(); 2523 writer.writeSelection(selectionToWrite);
2493 error = writer.getError(); 2524 } else {
2525 writer.write();
2526 }
2527 ok = writer.isOK();
2528 error = writer.getError();
2529 } else {
2530 WavFileWriter writer(path,
2531 model->getSampleRate(),
2532 model->getChannelCount(),
2533 WavFileWriter::WriteToTemporary);
2534 writer.writeModel(model, selectionToWrite);
2535 ok = writer.isOK();
2536 error = writer.getError();
2537 }
2494 } 2538 }
2495 2539
2496 if (ok) { 2540 if (ok) {
2497 if (multiple) { 2541 if (multiple) {
2498 emit activity(tr("Export multiple audio files")); 2542 emit activity(tr("Export multiple audio files"));