#include #include #include #include #include #include #include #include #include using namespace std; #include int mNChans=1; int mBitsXSample = 8; float mSRate=16384; #define sign(x) ((x > 0) - (x < 0)) typedef double SampleType; #if _MSC_VER <= 1200 typedef unsigned short UINT16; typedef unsigned int UINT32; #endif int verbose = 0; bool FlushWAVEHeader(ostream &outfile, const unsigned long DataSize) { long flen; long fpos; fpos = outfile.tellp(); flen = fpos - 1; outfile.seekp(4); outfile.write((const char *)&flen,sizeof(flen)); outfile.seekp(40); outfile.write((const char *)&DataSize,sizeof(DataSize)); return true; } SampleType Decode8Bit(const char &b) { bool positive; SampleType y; if(b & 0x80) positive =true; else positive=false; if(positive) { y=(b & 0x00FF - 0x80); } else y=-(~(b - 0x80)+1); y /= CHAR_MAX; return y; } SampleType Decode16Bit(const short &s) { double y=s; y /= SHRT_MAX; return y; } struct WaveHeader { char RIFF[4]; UINT32 DataLength; char WAVE[4]; char fmt_[4]; UINT32 SubBlockLength; UINT16 formatTag; UINT16 Channels; UINT32 SampFreq; UINT32 BytesPerSec; UINT16 BytesPerSamp; UINT16 BitsPerSamp; char data[4]; UINT32 WaveSize; } wh; void WAVReadHeader(istream &in,WaveHeader &header) { in.read((char*)(&header), sizeof(WaveHeader)); return; } void TestFlags( ios& x ) { cout << "badbit " << ( x.rdstate( ) & ios::badbit ) << endl; cout << "failbit " << ( x.rdstate( ) & ios::failbit ) << endl; cout << "eofbit " << ( x.rdstate( ) & ios::eofbit ) << endl; cout << endl; } bool ReadShort(istream& stmi, short &s) { char b=0,b2=0; short ts; //if((stmi >> b) && (stmi >> b2)) if(cin.read( &b,1) && cin.read( &b2,1)) { s=b2 & 0x00FF; s =(s << 8) & 0xFF00; s = s | (b & 0x00FF); if(verbose){ int v=int(b2 & 0x00FF) << 8 | int(b & 0x00FF) ; cerr <<"0x" << setfill('0') << setw(2*wh.BitsPerSamp/8) << hex << v ; if (!isatty(fileno(stdout))) cerr << endl; else cerr << "\t"; } return true; } else return false; } bool ReadByte(istream& stmi, char &b) { //if((stmi >> b) && (stmi >> b2)) //NO deve essere lettura binary if(cin.read( &b,1)) { if(verbose){ cerr <<"0x" << setfill('0') << setw(2*wh.BitsPerSamp/8) << hex << int(b &0xFF) ; if (!isatty(fileno(stdout))) cerr << endl; else cerr << "\t"; } return true; } else return false; } void help(const char *ProgName) { cerr << ProgName; cerr << " - "; cerr << "******** Convert WAV file to ASCII stream ********\n\t(By Fischetti P.)" << endl; cerr << "Usage:"<< endl; cerr << " " << ProgName; cerr << " [-i(only File Info)] [-n (print newline between Samples in Stereo case)]] [-v (verbose)] < fileWav" << endl; } void printWavHeader(std::ostream &os,WaveHeader &wh){ os << "Format: " << (wh.formatTag == 1?"PCM":"CMP") << endl; os << "Channels: " << wh.Channels << endl; os << "SampFreq: " << wh.SampFreq << endl; os << "BitsPerSamp: " << wh.BitsPerSamp << endl; } int main(int argc, char *argv[]) { //getch(); char *sep="\t"; if ((argc > 1) && (('-' == argv[1][0]) || ('/' == argv[1][0])) && ('?' == argv[1][1])) { help(_strupr(&(argv[0][0]))); return 1; } if (!isatty(fileno(stdin))) { setmode (fileno (stdin), O_BINARY); WAVReadHeader(cin,wh); if ((argc > 1) && (('-' == argv[1][0]) || ('/' == argv[1][0])) &&('i' == argv[1][1])) { printWavHeader(cout, wh); return 0; } if ((argc > 1) && (('-' == argv[1][0]) || ('/' == argv[1][0])) &&('n' == argv[1][1])) { sep="\n"; } if ((argc > 1) && (('-' == argv[1][0]) || ('/' == argv[1][0])) &&('v' == argv[1][1])) { verbose = 1; } printWavHeader(cerr, wh); if (_setmode(_fileno(stdin), _O_BINARY) == -1) cout << "ERROR: while converting cin to binary:" << strerror(errno) << endl; if (8 == wh.BitsPerSamp){ char b; while(ReadByte(cin,b)) cout << std::fixed << Decode8Bit(b) << endl; } else if (16 == wh.BitsPerSamp){ short s; while(ReadShort(cin,s)) cout << std::fixed << Decode16Bit(s) << endl; } else{ cerr << "Error on BitsPerSamp: "<< wh.BitsPerSamp<< endl; return -1; } } return 0; }