33 name=name.substr(name.find_last_of(
"\\/")+1, name.size());
34 cout<<
"\nWrite hexValues to audio file"<<endl;
35 cout<<
"\nUseage: \n"<<endl;
36 cout<<name<<
" [-o num] [-u num] [-d num] [-r num] [-b bits] hexValues outputFileName.ext : the output file name with ext replaced by a known output format extension (see below)"<<endl;
37 cout<<name<<
" -o num : number of channels to output [2]"<<endl;
38 cout<<name<<
" -u : don't use signed, use unsigned"<<endl;
39 cout<<name<<
" -d num : duration in seconds [1]"<<endl;
40 cout<<name<<
" -r num : sample rate in Hz [48000]"<<endl;
41 cout<<name<<
" -b num : bits [32]"<<endl;
42 cout<<name<<
" -s : switch the endian for the audio file"<<endl;
46 cout<<
"The known output file extensions (output file formats) are the following :"<<endl;
47 for (
int i=0; i<formats.size(); i++)
48 cout<<formats[i]<<
' ';
70 int main(
int argc,
char *argv[]) {
73 ostringstream msg; msg<<
"Format : ";
77 if (op.
getArg<
string>(
"h", argc, argv, help, i=0)!=0 || argc<3)
81 dummy=op.
getArg<
int>(
"u", argc, argv, dummy, i=0);
89 op.
getArg<
int>(
"o", argc, argv, outChCnt, i=0);
90 msg<<outChCnt<<
" ch ";
93 op.
getArg<
int>(
"b", argc, argv, bits, i=0);
97 op.
getArg<
float>(
"r", argc, argv, fs, i=0);
100 int N=strlen(argv[argc-2]);
101 msg<<
"Repeating : "<<argv[argc-2]<<
" ("<<N<<
" nibbles)";
104 op.
getArg<
float>(
"d", argc, argv, duration, i=0);
105 msg<<
" for "<<duration<<
" s";
107 int byteCnt=ceil(fs*duration/N)*
N;
108 byteCnt=ceil(byteCnt/
sizeof(sox_sample_t))*
sizeof(sox_sample_t);
110 cout<<msg.str()<<endl;
120 bool switchEndian=
false;
121 dummy=op.
getArg<
int>(
"s", argc, argv, dummy, i=0);
124 cout<<
"switching endian in the output file "<<endl;
126 cout<<
"not switching endian in the output file "<<endl;
128 stringstream hexString;
129 for (
int i=0; i*N<byteCnt*2; i++)
130 hexString<<argv[argc-2];
132 hexString.seekg(0, ios::end);
133 int size = hexString.tellg();
134 hexString.seekg(0, ios::beg);
136 ostringstream charString;
137 unsigned char charLSB, charMSB, charTogether;
139 for (
int i=0; i<size/2; i++){
143 if (charMSB<87) charMSB+=87;
144 if (charLSB<87) charLSB+=87;
145 charTogether=(((charMSB-87)<<4)&0xf0)|((charLSB-87)&0xf);
147 charString<<charTogether;
151 Eigen::Matrix<sox_sample_t, Eigen::Dynamic, Eigen::Dynamic> audio(byteCnt/
sizeof(sox_sample_t),outChCnt);
152 for (
int i=0; i<outChCnt; i++)
153 memcpy(audio.col(i).data(), charString.str().c_str(), byteCnt);
157 int revBytes=0, revNibbles=0, revBits=0;
158 sox.
openWrite(argv[argc-1], fs, outChCnt, (
double)numeric_limits<sox_sample_t>::max(), bits, switchEndian, revBytes, revNibbles, revBits);
virtual int openWrite(const string &fileName, double fs, int channels, double maxVal)
int getArg(string key, int argc, char *argv[], TYPE &ret, int i)
vector< string > availableFormats(void)
int printUsage(string name)
int main(int argc, char *argv[])
virtual int write(const vector< vector< FP_TYPE_ > > &audioData)