gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
HexToSox.C
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 
18 /*
19 Author: Matt Flax <flatmax@flatmax.org>
20 Date: 2016.12.27
21 */
22 
23 #include <iostream>
24 #include "OptionParser.H"
25 #include "Sox.H"
26 #include <unistd.h>
27 #include <stdint.h>
28 using namespace std;
29 
30 using namespace std;
31 
32 int printUsage(string name) {
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;
43 
44  Sox<int32_t> sox;
45  vector<string> formats=sox.availableFormats();
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]<<' ';
49  cout<<endl;
50 
51  return 0;
52 }
53 
54 /*int saveAudioToFile(const char *fn, const CrossoverAudio& crossAudio, const int chCnt) {
55  int ret=sox.openWrite(fn, crossAudio.getSampleRate(), chCnt, 1.0);
56  if (ret!=NO_ERROR)
57  return SoxDebug().evaluateError(ret);
58  int written=sox.write(crossAudio.audio);
59  if (written!=crossAudio.audio.rows()*crossAudio.audio.cols()) {
60  cout<<SoxDebug().evaluateError(written)<<endl;
61  cout<<"written "<<written<<endl;
62  cout<<"Output matrix size (rows, cols) = ("<<crossAudio.audio.rows()<<", "<<crossAudio.audio.cols()<<")"<<endl;
63  cout<<"Matrix is a total of "<<crossAudio.audio.rows()*crossAudio.audio.cols()<<" samples"<<endl;
64  cout<<"Error writing, exiting."<<endl;
65  }
66  sox.closeWrite();
67  return ret;
68 }*/
69 
70 int main(int argc, char *argv[]) {
71  OptionParser op;
72 
73  ostringstream msg; msg<<"Format : ";
74 
75  int i=0, dummy;
76  string help;
77  if (op.getArg<string>("h", argc, argv, help, i=0)!=0 || argc<3)
78  return printUsage(argv[0]);
79 
80  bool signd=true;
81  dummy=op.getArg<int>("u", argc, argv, dummy, i=0);
82  if (dummy!=0){
83  signd=false;
84  msg<<" unsigned ";
85  } else
86  msg<<" signed ";
87 
88  int outChCnt=1;
89  op.getArg<int>("o", argc, argv, outChCnt, i=0);
90  msg<<outChCnt<<" ch ";
91 
92  int bits=32;
93  op.getArg<int>("b", argc, argv, bits, i=0);
94  msg<<bits<<" bit ";
95 
96  float fs=48000.;
97  op.getArg<float>("r", argc, argv, fs, i=0);
98  msg<<fs<<" Hz \n";
99 
100  int N=strlen(argv[argc-2]);
101  msg<<"Repeating : "<<argv[argc-2]<<" ("<<N<<" nibbles)";
102 
103  float duration=1.0;
104  op.getArg<float>("d", argc, argv, duration, i=0);
105  msg<<" for "<<duration<<" s";
106 
107  int byteCnt=ceil(fs*duration/N)*N;
108  byteCnt=ceil(byteCnt/sizeof(sox_sample_t))*sizeof(sox_sample_t);
109 
110  cout<<msg.str()<<endl;
111 
112 /* // break the type info up into parameters.
113  bool isLittleEndian=false;
114  std::string token(10,'\0'); // check the endian-ness
115  std::stringstream typeInfo;
116  typeInfo.getline((char*)token.c_str(), 10, ':');
117  if (token.find("le")!=std::string::npos)
118  isLittleEndian=true;
119 */
120  bool switchEndian=false;
121  dummy=op.getArg<int>("s", argc, argv, dummy, i=0);
122  if (dummy!=0){
123  switchEndian=true;
124  cout<<"switching endian in the output file "<<endl;
125  } else
126  cout<<"not switching endian in the output file "<<endl;
127 
128  stringstream hexString;
129  for (int i=0; i*N<byteCnt*2; i++)
130  hexString<<argv[argc-2];
131 
132  hexString.seekg(0, ios::end); // find the number of nibbles in the stream
133  int size = hexString.tellg();
134  hexString.seekg(0, ios::beg);
135 
136  ostringstream charString;
137  unsigned char charLSB, charMSB, charTogether;
138  char chars[2];
139  for (int i=0; i<size/2; i++){
140  hexString>>charMSB;
141  hexString>>charLSB;
142 // cout<<"0x"<<charMSB<<charLSB<<" ";
143  if (charMSB<87) charMSB+=87;
144  if (charLSB<87) charLSB+=87;
145  charTogether=(((charMSB-87)<<4)&0xf0)|((charLSB-87)&0xf);
146  // cout<<(int)charTogether<<'\n';
147  charString<<charTogether;
148  }
149 
150 // cout<<charString.str().c_str()<<endl;
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);
154 
155  // open sox
156  Sox<sox_sample_t> sox;
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);
159  sox.write(audio);
160  sox.closeWrite();
161  return 0;
162 }
size(signal)
virtual int openWrite(const string &fileName, double fs, int channels, double maxVal)
Definition: Sox.H:174
int N
int getArg(string key, int argc, char *argv[], TYPE &ret, int i)
Definition: OptionParser.H:43
int closeWrite(void)
Definition: Sox.C:232
STL namespace.
vector< string > availableFormats(void)
Definition: Sox.C:238
Definition: Sox.H:95
int printUsage(string name)
Definition: HexToSox.C:32
int main(int argc, char *argv[])
Definition: HexToSox.C:70
virtual int write(const vector< vector< FP_TYPE_ > > &audioData)
Definition: Sox.C:203
gtkIOStream: /tmp/gtkiostream/applications/HexToSox.C Source File
GTK+ IOStream  Beta