19 #if !defined(_MSC_VER) && !defined(__CYGWIN__) 24 #include "SoxWindows.H" 32 using namespace Eigen;
34 template<
typename FP_TYPE_>
36 sox_globals.output_message_handler = output_message;
37 sox_globals.verbosity = 1;
39 assert(sox_init() == SOX_SUCCESS);
43 template<
typename FP_TYPE_>
50 template<
typename FP_TYPE_>
55 if (sox_close(in)!=SOX_SUCCESS)
61 if (sox_close(out)!=SOX_SUCCESS)
69 template<
typename FP_TYPE_>
74 in = sox_open_read(fileName.c_str(), NULL, NULL, NULL);
79 typedef std::numeric_limits<double> Info;
80 maxVal=Info::quiet_NaN();
81 ifstream maxFile((fileName+
".max").c_str());
86 maxVal=(float)::atof(maxValStr);
93 template<
typename FP_TYPE_>
102 in = sox_open_mem_read((
void*)buffer, len, NULL, NULL, NULL);
107 typedef std::numeric_limits<double> Info;
108 maxVal=Info::quiet_NaN();
112 template<
typename FP_TYPE_>
114 char const *
const str[] = {
"FAIL",
"WARN",
"INFO",
"DBUG"};
115 if (sox_globals.verbosity >= level) {
117 sox_basename(base_name,
sizeof(base_name), filename);
119 fprintf(stderr,
"%s %s: ", str[((level - 1) <= (3) ? (level - 1) : (3))], base_name);
121 vfprintf(stderr, fmt, ap);
122 fprintf(stderr,
"\n");
126 template<
typename FP_TYPE_>
127 int Sox<FP_TYPE_>::openWrite(
const string &fileName,
double fs,
int channels,
double maxVal,
unsigned int wordSize,
bool switchEndian,
int revBytes,
int revNibbles,
int revBits){
129 bool inputFile=
false;
135 si.channels=channels;
136 si.precision=wordSize;
140 sox_encodinginfo_t encoding;
141 sox_init_encodinginfo(&encoding);
142 encoding.encoding=SOX_ENCODING_UNKNOWN;
143 encoding.bits_per_sample=wordSize;
144 encoding.opposite_endian=(switchEndian)?sox_true:sox_false;
145 encoding.reverse_bytes=(revBytes==0)?sox_option_no:((revBytes==1)?sox_option_yes:sox_option_default);
146 encoding.reverse_nibbles=(revNibbles==0)?sox_option_no:((revNibbles==1)?sox_option_yes:sox_option_default);
147 encoding.reverse_bits=(revBits==0)?sox_option_no:((revBits==1)?sox_option_yes:sox_option_default);
150 out=sox_open_write(fileName.c_str(), &si, &encoding, NULL, NULL, NULL);
163 ofstream outf((fileName+
".max").c_str());
165 outf<< scientific << outputMaxVal;
170 template<
typename FP_TYPE_>
171 int Sox<FP_TYPE_>::openMemWrite(
void *buffer,
size_t len,
double fs,
int channels,
double maxVal,
unsigned int wordSize,
bool switchEndian,
int revBytes,
int revNibbles,
int revBits){
173 bool inputFile=
false;
179 si.channels=channels;
180 si.precision=wordSize;
184 sox_encodinginfo_t encoding;
185 sox_init_encodinginfo(&encoding);
186 encoding.encoding=SOX_ENCODING_UNKNOWN;
187 encoding.bits_per_sample=wordSize;
188 encoding.opposite_endian=(switchEndian)?sox_true:sox_false;
189 encoding.reverse_bytes=(revBytes==0)?sox_option_no:((revBytes==1)?sox_option_yes:sox_option_default);
190 encoding.reverse_nibbles=(revNibbles==0)?sox_option_no:((revNibbles==1)?sox_option_yes:sox_option_default);
191 encoding.reverse_bits=(revBits==0)?sox_option_no:((revBits==1)?sox_option_yes:sox_option_default);
194 out=sox_open_mem_write(buffer, len, &si, &encoding, NULL, NULL);
202 template<
typename FP_TYPE_>
206 if (out->signal.channels!=audioData.size())
209 int ch=out->signal.channels;
210 int len=audioData[0].size();
212 if (outputBuffer.size()<total)
213 outputBuffer.resize(total);
214 for (
int i=0; i<audioData.size(); i++)
215 for (
int j=0; j<len; j++)
216 outputBuffer[j*ch+i]=(sox_sample_t)((double)audioData[i][j]/outputMaxVal*(
double)numeric_limits<sox_sample_t>::max());
217 size_t writeCount=sox_write(out, &outputBuffer[0], total);
225 template<
typename FP_TYPE_>
228 return close(inputFile);
231 template<
typename FP_TYPE_>
233 bool inputFile=
false;
234 return close(inputFile);
237 template<
typename FP_TYPE_>
240 char const *
const * names;
241 vector<string> formatExts;
244 for (i = 0; sox_format_fns[i].fn; ++i)
245 char const *
const *names = sox_format_fns[i].fn()->names;
247 printf(
"AUDIO FILE FORMATS:");
248 for (i = 0; sox_format_fns[i].fn; ++i) {
249 sox_format_handler_t
const * handler = sox_format_fns[i].fn();
250 if (!(handler->flags & SOX_FILE_DEVICE))
251 for (names = handler->names; *names; ++names)
252 if (!strchr(*names,
'/'))
253 formatExts.push_back(*names);
255 std::sort(formatExts.begin(), formatExts.end());
259 template<
typename FP_TYPE_>
261 printf(
"The known output file extensions (output file formats) are the following :\n");
262 vector<string> formats=availableFormats();
263 for (
int i=0; i<formats.size(); i++)
264 printf(
"%s ",formats[i].c_str());
268 #ifndef HAVE_EMSCRIPTEN 275 #ifdef HAVE_EMSCRIPTEN 277 template<
typename FP_TYPE_>
281 typedef std::numeric_limits<double> Info;
282 return Info::quiet_NaN();
284 if (c>=audio.cols()) {
286 typedef std::numeric_limits<double> Info;
287 return Info::quiet_NaN();
292 template<
typename FP_TYPE_>
297 template<
typename FP_TYPE_>
300 template<
typename FP_TYPE_>
302 return read(audio, count);
305 #include <emscripten/bind.h> 306 EMSCRIPTEN_BINDINGS(Sox_ex) {
307 emscripten::class_<Sox<double>>(
"Sox")
313 .function(
"openRead", emscripten::select_overload<
int(intptr_t,
size_t)>(&
Sox<double>::openRead), emscripten::allow_raw_pointers())
#define SOX_READ_MAXSCALE_ERROR
Sox couldn't open the filename.max to read the rescale value for the audio file.
virtual int openWrite(const string &fileName, double fs, int channels, double maxVal)
int openRead(string fileName)
virtual int evaluateError(int errorNum)
#define SOX_COL_BOUNDS_ERROR
Error when trying to access a col out of bounds (Emscripten case)
int openMemWrite(void *buffer, size_t len, double fs, int channels, double maxVal)
vector< string > availableFormats(void)
#define SOX_ROW_BOUNDS_ERROR
Error when trying to access a row out of bounds (Emscripten case)
#define SOX_WRITE_FILE_OPEN_ERROR
Error when libsox couldn't open the output file.
#define NO_ERROR
There is no error.
#define SOX_READ_FILE_OPEN_ERROR
Error when libsox couldn't open the input file.
#define SOX_WRITE_FILE_NOT_OPENED_ERROR
Error when the output file hasn't already been opened using Sox::openRead.
static void output_message(unsigned level, const char *filename, const char *fmt, va_list ap)
#define SOX_CLOSE_FILE_ERROR
Error when libsox couldn't close the file.
int close(bool inputFile)
#define SOX_WRITE_OUT_CHANNEL_MISMATCH
Error when trying to write data and the channel count doesn't match.
virtual ~Sox()
Destructor.
virtual int write(const vector< vector< FP_TYPE_ > > &audioData)