diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2016-03-07 23:45:53 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2016-03-07 23:45:53 +0300 |
commit | e06cd5d38bfc4fc2722be227d14b47e68a6e7367 (patch) | |
tree | dd03397c5c43472f8244aed4a0364a72ba239ec2 /src | |
parent | afba2342677988c11250594520c95936c1747eab (diff) | |
download | atracdenc-e06cd5d38bfc4fc2722be227d14b47e68a6e7367.tar.gz |
autodetect saved fmt by file extension (WAV, AU, AIFF, RAW)
use AU format in case of "-" (stdout) output (something like
"./atracdenc --decode -i /tmp/test.aea -o - | cvlc - " works)
why AU? http://www.mega-nerd.com/libsndfile/FAQ.html#Q017
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 25 | ||||
-rw-r--r-- | src/wav.cpp | 22 |
2 files changed, 37 insertions, 10 deletions
diff --git a/src/main.cpp b/src/main.cpp index 62a42d4..51794ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,6 +55,7 @@ int main(int argc, char* const* argv) { { "bfuidxfast", no_argument, NULL, 2}, { "notransient", optional_argument, NULL, 3}, { "mono", no_argument, NULL, 'm'}, + { "nostdout", no_argument, NULL, 4}, { NULL, 0, NULL, 0} }; @@ -65,17 +66,15 @@ int main(int argc, char* const* argv) { uint32_t bfuIdxConst = 0; //0 - auto, no const bool fastBfuNumSearch = false; bool mono = false; + bool nostdout = false; TAtrac1EncodeSettings::EWindowMode windowMode = TAtrac1EncodeSettings::EWindowMode::EWM_AUTO; uint32_t winMask = 0; //all is long while ((ch = getopt_long(argc, argv, "edhi:o:m", longopts, NULL)) != -1) { switch (ch) { case 'e': - cout << "encode " << endl; mode |= E_ENCODE; break; case 'd': - cout << "decode" << endl; - mode |= E_DECODE; break; case 'i': @@ -83,7 +82,8 @@ int main(int argc, char* const* argv) { break; case 'o': outFile = optarg; - cout << "out: " << outFile<< endl; + if (outFile == "-") + nostdout = true; break; case 'm': mono = true; @@ -113,6 +113,9 @@ int main(int argc, char* const* argv) { ((winMask & 2) ? "short": "long") << ", hi - " << ((winMask & 4) ? "short": "long") << endl; break; + case 4: + nostdout = true; + break; default: printUsage(myName); } @@ -145,18 +148,20 @@ int main(int argc, char* const* argv) { //TODO: recheck it TAeaPtr aeaIO = TAeaPtr(new TAea(outFile, "test", numChannels, numChannels * totalSamples / 512)); pcmEngine = new TPCMEngine<double>(4096, numChannels, TPCMEngine<double>::TReaderPtr(wavIO->GetPCMReader<double>())); - cout << "Input file: " << inFile << "\n Channels: " << numChannels << "\n SampleRate: " << wavIO->GetSampleRate() << "\n TotalSamples: " << totalSamples << endl; + if (!nostdout) + cout << "Input file: " << inFile << "\n Channels: " << numChannels << "\n SampleRate: " << wavIO->GetSampleRate() << "\n TotalSamples: " << totalSamples << endl; atracProcessor = new TAtrac1Processor(move(aeaIO), TAtrac1EncodeSettings(bfuIdxConst, fastBfuNumSearch, windowMode, winMask)); } else if (mode == E_DECODE) { TAeaPtr aeaIO = TAeaPtr(new TAea(inFile)); totalSamples = aeaIO->GetLengthInSamples(); uint32_t length = aeaIO->GetLengthInSamples(); - cout << "Name: " << aeaIO->GetName() << "\n Channels: " << aeaIO->GetChannelNum() << "\n Length: " << length << endl; + if (!nostdout) + cout << "Name: " << aeaIO->GetName() << "\n Channels: " << aeaIO->GetChannelNum() << "\n Length: " << length << endl; wavIO = TWavPtr(new TWav(outFile, aeaIO->GetChannelNum(), 44100)); pcmEngine = new TPCMEngine<double>(4096, aeaIO->GetChannelNum(), TPCMEngine<double>::TWriterPtr(wavIO->GetPCMWriter<double>())); atracProcessor = new TAtrac1Processor(move(aeaIO), TAtrac1EncodeSettings(bfuIdxConst, fastBfuNumSearch, windowMode, winMask)); } else { - cout << "Processing mode was not specified" << endl; + cerr << "Processing mode was not specified" << endl; return 1; } @@ -167,9 +172,11 @@ int main(int argc, char* const* argv) { try { while (totalSamples > (processed = pcmEngine->ApplyProcess(512, atracLambda))) { - printProgress(processed*100/totalSamples); + if (!nostdout) + printProgress(processed*100/totalSamples); } - cout << "\nDone" << endl; + if (!nostdout) + cout << "\nDone" << endl; } catch (TAeaIOError err) { cerr << "Aea IO fatal error: " << err.what() << endl; diff --git a/src/wav.cpp b/src/wav.cpp index 755fcd7..b7757f6 100644 --- a/src/wav.cpp +++ b/src/wav.cpp @@ -1,6 +1,8 @@ #include <functional> #include <memory> #include <cerrno> +#include <algorithm> +#include <string> #include <sys/stat.h> #include <string.h> @@ -8,6 +10,24 @@ #include "wav.h" #include "pcmengin.h" +static int fileext_to_libsndfmt(const std::string& filename) { + int fmt = SF_FORMAT_WAV; //default fmt + if (filename == "-") + return SF_FORMAT_AU; + size_t pos = filename.find_last_of("."); + if (pos == std::string::npos || pos == filename.size() - 1) //no dot or filename. + return fmt; + std::string ext = filename.substr(pos+1); + std::transform(ext.begin(), ext.end(), ext.begin(), ::toupper); + if (ext == "AU") { + fmt = SF_FORMAT_AU; + } else if (ext == "AIFF") { + fmt = SF_FORMAT_AIFF; + } else if (ext == "PCM" || ext == "RAW") { + fmt = SF_FORMAT_RAW; + } + return fmt; +} TWav::TWav(const std::string& filename) : File(SndfileHandle(filename)) { @@ -16,7 +36,7 @@ TWav::TWav(const std::string& filename) } TWav::TWav(const std::string& filename, int channels, int sampleRate) - : File(SndfileHandle(filename, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, channels, sampleRate)) { + : File(SndfileHandle(filename, SFM_WRITE, fileext_to_libsndfmt(filename) | SF_FORMAT_PCM_16, channels, sampleRate)) { //disable scaling short -> [-1.0, 1.0] File.command(SFC_SET_NORM_DOUBLE /*| SFC_SET_NORM_FLOAT*/, nullptr, SF_FALSE); } |