| 412 | } |
| 413 | |
| 414 | ExportResult FLACExportProcessor::Process(ExportProcessorDelegate& delegate) |
| 415 | { |
| 416 | delegate.SetStatusString(context.status); |
| 417 | |
| 418 | auto exportResult = ExportResult::Success; |
| 419 | |
| 420 | auto cleanup2 = finally( [&] { |
| 421 | if (exportResult == ExportResult::Cancelled || exportResult == ExportResult::Error) { |
| 422 | #ifndef LEGACY_FLAC |
| 423 | context.f.Detach(); // libflac closes the file |
| 424 | #endif |
| 425 | context.encoder.finish(); |
| 426 | } |
| 427 | } ); |
| 428 | |
| 429 | ArraysOf<FLAC__int32> tmpsmplbuf{ context.numChannels, SAMPLES_PER_RUN, true }; |
| 430 | |
| 431 | while (exportResult == ExportResult::Success) { |
| 432 | auto samplesThisRun = context.mixer->Process(); |
| 433 | if (samplesThisRun == 0) //stop encoding |
| 434 | break; |
| 435 | |
| 436 | for (size_t i = 0; i < context.numChannels; i++) { |
| 437 | auto mixed = context.mixer->GetBuffer(i); |
| 438 | if (context.format == int24Sample) { |
| 439 | for (decltype(samplesThisRun) j = 0; j < samplesThisRun; j++) { |
| 440 | tmpsmplbuf[i][j] = ((const int *)mixed)[j]; |
| 441 | } |
| 442 | } |
| 443 | else { |
| 444 | for (decltype(samplesThisRun) j = 0; j < samplesThisRun; j++) { |
| 445 | tmpsmplbuf[i][j] = ((const short *)mixed)[j]; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | if (! context.encoder.process( |
| 450 | reinterpret_cast<FLAC__int32**>( tmpsmplbuf.get() ), |
| 451 | samplesThisRun) ) { |
| 452 | // TODO: more precise message |
| 453 | throw ExportDiskFullError(context.fName); |
| 454 | } |
| 455 | exportResult = ExportPluginHelpers::UpdateProgress( |
| 456 | delegate, *context.mixer, context.t0, context.t1); |
| 457 | } |
| 458 | |
| 459 | if (exportResult != ExportResult::Cancelled && exportResult != ExportResult::Error) { |
| 460 | #ifndef LEGACY_FLAC |
| 461 | context.f.Detach(); // libflac closes the file |
| 462 | #endif |
| 463 | if (!context.encoder.finish()) |
| 464 | { |
| 465 | return ExportResult::Error; |
| 466 | } |
| 467 | #ifdef LEGACY_FLAC |
| 468 | if (!context.f.Flush() || !context.f.Close()) |
| 469 | { |
| 470 | return ExportResult::Error; |
| 471 | } |