convert wav to h file

Camilo Leal's icon

Hi.
I'm trying to create an app in which you can drag wav or mp3 files (samples) on a buffer, so I can then export it to another device. The thing is, so far, my options for a device require the files to be converted into .h files (I think that's C, for arduino and other microcontrollers). Is there a way to make such a conversion with the objects in Max/MSP? I mean, I know you can create a text file or a file with a given format, but I am not an engineer or the like and have no clue on the math (or other) to convert raw wav into .h

I'd appreciate any ideas.

Thanks

Source Audio's icon

I have done it for arduino good while ago,
using peek~ and storing samples into text,
I'll digg in old HDs , should be arround,
if I don't find it, I'll recreate it again.

Source Audio's icon

here is the patch

Max Patch
Copy patch and select New From Clipboard in Max.

It is 1 channel only
Select sample type, and the rest is auto done.
At the end write the plain text file.
At the top of the text file is header
followed by samples array

Camilo Leal's icon

Awesome!!! Thank you so much!!!!!
I really appreciate it Source Audio!

Camilo Leal's icon

Hi again.
So, some of the elements in your patcher did not work for me, but I was able to adapt it. I also had to add some stuff to get it to translate the file into hex. It is not perfect and I am not sure the product file works, but it looks decent.

3.h.Archivador.maxpat
text/plain 94.37 KB

conversor.maxpat
text/plain 41.22 KB

hex.txt
text/plain 2.89 KB

Source Audio's icon

Well it's fine you get that working for your needs.
By the way I made this in max 4.6, and just adapted it for max 6.
I see now that timing is a bit different on max 8.
here is a fix for header getting cleared in text file, just a bit different order of
messages.

Max Patch
Copy patch and select New From Clipboard in Max.


--------
From the file paths I see you are using windows.
Try this app first to see if the microprocessor that you want to use works
with produced files, including HEX and INT syntax.


https://colinjs.com/software.htm#t_WAVToCode

Than you can check how to replicate it in max..

Sprintf could have done the hex conversion if that is what you need.
to get int to OXXX hex format
sprintf symout OX%02X,
outputs 0X00, (including comma)


In arduinos I used, int just worked fine, but that probably depends on libs used.

Camilo Leal's icon

This I made with the old version you sent. I tried to reduce the sampling rate to 1/4 by dividing total samples by 4 and letting pass the sample number every 4 counts to the peek~

Not sure if any of this makes any sense, but I am having fun figuring out.

Thanks again, I'll check your recent post.

3.h.Archivador1.maxpat
text/plain 94.40 KB

Source Audio's icon

If you really want to do this with peek~...
I would run the samples
through zl.group 4 -> zl.median
for smoother downsampling
just take care to bang zl.group at the end
because there would be some leftovers