GregAC / pico-stuff

Misc things related to the Raspberry Pi Pico / RP2040
29 stars 4 forks source link

Program to convert wav to header (.h) file? #1

Closed pedro-javierf closed 3 years ago

pedro-javierf commented 3 years ago

Hi, which software have you used to convert .wav samples to .h? I've seen it is just a dump of all the samples into an array so shouldn't be difficult to program that, but I wanted to ask in case there is some already available tool to do it easily.

GregAC commented 3 years ago

Hi @pedro-javierf, apologies only just seen this issue.

I used audacity, it has a raw output option (see https://manual.audacityteam.org/man/other_uncompressed_files_export_options.html) which can give you just the samples in a variety of formats without a header (I used the 'unsigned 8-bit PCM encoding'). I then used a very crude python program to dump out the bytes with a little bit of formatting so it could be used as an initializer in C. Here's the code:

audio_file = open('rock.raw', 'rb')

audio_bytes = audio_file.read()

for b in audio_bytes:
  int_b = int(b)
  print(f'{b},')