Creating a stereo WAV file using C

前端 未结 2 547
难免孤独
难免孤独 2021-02-10 11:49

I am looking at creating a WAV file in C and have seen an example here.

This looks good, but I\'m interested in adding two buffers to make the audio stereo

2条回答
  •  孤独总比滥情好
    2021-02-10 12:28

    I think the problem is with the function "write_little_endian". You shouldn't need to use it on your laptop.

    Endianness is architecture specific. The original example was likely for an Arduino microcontroller board. Arduino boards use Atmel microcontrollers which are big-endian. Thats why the code you cited explicitly needs to convert the 16-bit integers to little-endian format.

    Your laptop on the other hand, uses x86 processors which are already little endian so no conversion is necessary. If you want robust portable code to convert endianness, you can use the function htole16 in Linux. Lookup the man pages to learn more about this function.

    For a quick but non-portable fix, I would say just write out the entire 16bit value.

    Also, I don't think you need to halve the amplitudes to go from mono to stereo.

提交回复
热议问题