simple wav 16-bit / 8-bit converter source code?

2019-09-18 02:49发布

could someone provide me a link to source code (preferably C/C++) of a simple WAV 16-bit to 8-bit (and back if possible) converter? I have basic knowledge of C++ and I need to have a resource to understand wav writing and converting values for my new project. At this moment I have a lecture about RIFF chunks structure.

Also any formulas for converting values between different bit depths (also these un standard) appreciated...

I'll prefer it to be simple and command line so I can edit it with notepad and easily compile.

Thanks in advance!

1条回答
乱世女痞
2楼-- · 2019-09-18 03:10

For reading and writing WAV files have a look at the specification:
https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

For converting between 8 and 16 bit you just need to divide or multiply by 256.
For example (converting from 16 to 8 bit in C):

  • You open the 16 bit file and the (not yet existent) 8 bit file using fopen().
  • You read the beginning of the 16 bit file and write the beginning of the 8 bit file according to the specification.
  • Then you do the following for each sample:
    int sample = 0;
    // read 2 bytes (= 16 bits) from the 16 bit file into sample:
    fread (&sample, 2, 1, wav_file_16bit);
    sample /= 256; // divide sample by 256
    // write 1 byte (= 8 bits) to the 8 bit file:
    fwrite (&sample, 1, 1, wav_file_8bit);
  • Don't forget to close your files.

If you need more specific information please ask.

查看更多
登录 后发表回答