c++ - Loading and Saving WAV file -


i'm trying load wave file data structure, save disk copy of original. loading , saving appears work fine copied wave file won't play sound (although open without errors). i'm wondering if has endian-ness?

this data structure hold wave file:

struct wavefile { public:      static const uint16 num_chars = 4;  public:      wavefile() : data(nullptr) {}      ~wavefile() { delete[] data; }      char chunkid[num_chars];      uint32 chunksize;      char format[num_chars];      char subchunkid[num_chars];      uint32 subchunksize;      uint16 audioformat;      uint16 numchannels;      uint32 samplerate;      uint32 byterate;      uint16 blockalign;      uint16 bitspersample;      char subchunk2id[num_chars];      uint32 subchunk2size;      byte* data;  }; 

this how load in:

std::ifstream file(filename, std::ios::binary);  if (file.good()) {     file.read(wavefile.chunkid, wavefile::num_chars);     file.read(reinterpret_cast<char*>(&wavefile.chunksize), size_ui32);     file.read(wavefile.format, wavefile::num_chars);     file.read(wavefile.subchunkid, wavefile::num_chars);     file.read(reinterpret_cast<char*>(&wavefile.subchunksize), size_ui32);     file.read(reinterpret_cast<char*>(&wavefile.audioformat), size_ui16);     file.read(reinterpret_cast<char*>(&wavefile.numchannels), size_ui16);     file.read(reinterpret_cast<char*>(&wavefile.samplerate), size_ui32);     file.read(reinterpret_cast<char*>(&wavefile.byterate), size_ui32);     file.read(reinterpret_cast<char*>(&wavefile.blockalign), size_ui16);     file.read(reinterpret_cast<char*>(&wavefile.bitspersample), size_ui16);     file.read(wavefile.subchunk2id, wavefile::num_chars);     file.read(reinterpret_cast<char*>(&wavefile.subchunk2size), size_ui32);     wavefile.data = new byte[wavefile.subchunk2size];     file.read(reinterpret_cast<char*>(wavefile.data), sizeof(wavefile.subchunk2size));     file.close(); } 

and how write data out file:

std::ofstream file(outfile, std::ios::binary);  if (file.good()) {     file.flush();     file.write(wavefile.chunkid, wavefile::num_chars);     file.write(reinterpret_cast<const char*>(&wavefile.chunksize), size_ui32);     file.write(wavefile.format, wavefile::num_chars);     file.write(wavefile.subchunkid, wavefile::num_chars);     file.write(reinterpret_cast<const char*>(&wavefile.subchunksize), size_ui32);     file.write(reinterpret_cast<const char*>(&wavefile.audioformat), size_ui16);     file.write(reinterpret_cast<const char*>(&wavefile.numchannels), size_ui16);     file.write(reinterpret_cast<const char*>(&wavefile.samplerate), size_ui32);     file.write(reinterpret_cast<const char*>(&wavefile.byterate), size_ui32);     file.write(reinterpret_cast<const char*>(&wavefile.blockalign), size_ui16);     file.write(reinterpret_cast<const char*>(&wavefile.bitspersample), size_ui16);     file.write(wavefile.subchunk2id, wavefile::num_chars);     file.write(reinterpret_cast<const char*>(&wavefile.subchunk2size), size_ui32);     file.write(reinterpret_cast<const char*>(wavefile.data), wavefile.subchunk2size);     file.close();    } 

the size of copy same original also. if wondering, uint16, uint32 , byte typedefs unsigned short, unsigned int , unsigned char. size_ui32 variables this:

static const uint32 size_ui32 = sizeof(uint32); static const uint32 size_ui16 = sizeof(uint16); 

that't it, can't seem find out i've gone wrong.

cheers help.

after posting question i've discovered problem.

when reading data in i've done this:

file.read(reinterpret_cast<char*>(wavefile.data), sizeof(wavefile.subchunk2size)); 

which should be:

file.read(reinterpret_cast<char*>(wavefile.data), wavefile.subchunk2size);   

worked when removed sizeof(). silly me.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -