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/wav.cpp | |
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/wav.cpp')
-rw-r--r-- | src/wav.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
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); } |