Creating a Sampler using an old .c file

Hey guys,

I've found a lot of related threads on this topic and first of all: this is my first post on this forum and i hope i didn't oversee any old threads, but the example codes I've seen were so different than mine.

I want to program my own sampler, which gets its data as a .WAV file and creates a sampled function with discrete values of that .WAV file. Luckily I already have an algorithm which would do that, but it is written in C...

https://raw.githubusercontent.com/PaulStoffregen/Audio/master/extras/wav2sketch/wav2sketch.c

It's called wav2sketch and is exactly the algorithm I'm looking for. Only problem is that it is written in C.

Like i said I found a lot of topics regarding the issue "i want to include an .c file in my sketch", but all of those codes were so short, if you'd put them together they wouldn't be as extensive as the above .c file.

So my question is: How can i use a .c file in my sketch which for itself has a whole bunch of other .c libraries / headers and isn't just a 5 line code segment?

Is the standard "Extern C" approach working here or do I have to convert every header from the above link into a .c++ / .h file first?

Sorry if I'm sounding stupid right now but I'm an electrical engineer and have not too much experience with coding, so for me it just doesn't add up right now on how to include this gigantic file into my sketch...

Thank you all for your patience and help,
Henning

Why not do the conversion on a PC, using Code::Blocks (free, supports both C and C++) or a similar IDE?

jremington:
Why not do the conversion on a PC, using Code::Blocks (free, supports both C and C++) or a similar IDE?

Code::Blocks would allow me to use both .c and .cpp files without any further modification / conversion of the files? Sounds like a good way to solve this issue!
Only thing is I would like to write that code on a microcontroller to get a "standalone" device in the end. Is a teensy microcontroller for example compatible with Code::Blocks? Or do you have some recommendations for a good microcontroller to use Code::Blocks with?
Thanks for your help guys!
Henning

You need to understand the code, and have a plan of action, before you do anything else.

How will a microcontroller collect and store .wav files, and where will it write the result of the conversion?

jremington:
You need to understand the code, and have a plan of action, before you do anything else.

How will a microcontroller collect and store .wav files, and where will it write the result of the conversion?

I thought of taking a teensy x.x microcontroller together with an audio board. Then I'd use a SD-card to collect and store the files + the result of the conversion.

I've made a thread on the teensy/pjrc board, too:

Maybe it gets clear what I have in mind if you read the posts there. The main Problem is like i said I want to have the whole process automated, i don't want to use an OS / type in commandos on a computer for every .WAV file i want to sample ... I understand what the .c file is doing for itself but it's very hard for me right now to understand how the different programming languages are being put together to function as a whole. A .c to .cpp converter would be awesome lol.

Any help / explanation is highly appreciated, thanks guys.

A .c to .cpp converter would be awesome lol.

Wouldn't it just :slight_smile:

Honestly your problem isn't anything to do with C v.s. C++. You can happily write Arduino sketches in a mostly C-like fashion, despite the fact that it is actually a C++ environment.

What's the end goal here? Is it to play the .WAV samples using the Arduino?

What's the end goal here? Is it to play the .WAV samples using the Arduino?

Sorry, I realised you linked to another forum with a more fulsome description.

So, if I understand you correctly, you want to take incoming ("live/on-the-fly"?) .wav data, and then perform a DFT on it?

If that's true, I'm not sure wav2sketch.c is entirely what you want, unless I'm missing something. The C code is reading a .wav file, encoding it as G.711 (by default), and then auto-generating C/C++ code containing the data in a form that the Teensy Audio Library likes (apparently, last bit from the comments in the file).

When I've done fourier transform stuff in the past it has always been with plain old PCM data.

So I think what you want/need is:

  1. Access a .wav file from "somewhere" (SD card? How is it arriving?)
  2. Crib the wav2sketch code to read/handle the wav file header
  3. Crib the wav2sketch code to read (chunks of) the wav file as arrays of PCM data
  4. Poke the PCM data into your DFT code

IOW, unless I have completely the wrong end of the stick (not unusual!) wav2sketch is mostly going to be useful as a reference rather than a direct source of working code for your usecase.

tomparkin:
Sorry, I realised you linked to another forum with a more fulsome description.

So, if I understand you correctly, you want to take incoming ("live/on-the-fly"?) .wav data, and then perform a DFT on it?

If that's true, I'm not sure wav2sketch.c is entirely what you want, unless I'm missing something. The C code is reading a .wav file, encoding it as G.711 (by default), and then auto-generating C/C++ code containing the data in a form that the Teensy Audio Library likes (apparently, last bit from the comments in the file).

When I've done fourier transform stuff in the past it has always been with plain old PCM data.

So I think what you want/need is:

  1. Access a .wav file from "somewhere" (SD card? How is it arriving?)
  2. Crib the wav2sketch code to read/handle the wav file header
  3. Crib the wav2sketch code to read (chunks of) the wav file as arrays of PCM data
  4. Poke the PCM data into your DFT code

IOW, unless I have completely the wrong end of the stick (not unusual!) wav2sketch is mostly going to be useful as a reference rather than a direct source of working code for your usecase.

Thanks a lot for your answer tomparkin.

Finally someone trys to understand the kind of problems i'm going to have to think of before getting the job done. To clarify what exactly i want to achieve in the end, i can just summarize as follows:
I want a real time frequency analysis with numerical data of a signal which is being sent to my microcontroller. It could be one of my synthesizers, a microphone, anything really. So the input is not stored anywhere, it is recorded LIVE. That's why i need to sample it and store the samples (preferably on a SDcard). These discrete numbers / samples, created by the PCM, are then the values i'm going to send into a DFT / FFT function, which will result a discrete signal in frequency domain. Going further i then want to examine the peaks in the spectrum or defined more accurately the loudness of the discrete frequencies, but that is not a target right now. First i need to get control over the samples and transformations.

Like you said it would be an idea to not use the wav2sketch code and try to use another kind of file format (most reasonably PCM / .WAV). However there's a 1024-point FFT program written for the teensy / teensyduino library and it actually provides pretty accurate results ... and it is using the u-law encoded, hexadecimal data which is being generated by the wav2sketch.c file, so that is where this is coming from ...
But thinking about it again actually you're so right: why not try to write my own code and to use the .WAV files / PCM data directly ... hmmm maybe because i just hate coding if it's not a 2 liner lol ... way better to steal everything from someone else :o

The formula for a discrete fourier transformation is not too complicated - i already programmed one using eulers formula. You're absolutely right if you say the standard procedure would make more sense. Remove the first 96 + 192 bits (12 + 24 bytes) from the .WAV and send the remaining PCM data into my DFT function. But I'm still having big problems with 2 things:

  1. what is one of the 32bit integers of the PCM data telling me? Meaning how can i translate those numbers to loudness / decibel values?
  2. same with the resulting spectrum. If I'm transforming a vector of digital integer values what is the resulting spectrum telling me in forms of loudness? If you look at existing equalizers they all have a spectrum analyzer with dB values for the frequency bands

Thank you so much guys for all the help! Cheers
Henning

henning95:
I want a real time frequency analysis with numerical data of a signal which is being sent to my microcontroller. It could be one of my synthesizers, a microphone, anything really. So the input is not stored anywhere, it is recorded LIVE. That's why i need to sample it and store the samples (preferably on a SDcard).

Ah, OK -- so you are feeding an audio stream to the microcontroller, which is sampling it. Yes?

In which case, I note there's not really any need to store to SD card, unless you especially want to do so for posterity.

Also, you've now got different/new problem :slight_smile: which is to say you need to manage the data-collection aspect of sampling the signal in real time.

Like you said it would be an idea to not use the wav2sketch code and try to use another kind of file format (most reasonably PCM / .WAV). However there's a 1024-point FFT program written for the teensy / teensyduino library and it actually provides pretty accurate results ... and it is using the u-law encoded, hexadecimal data which is being generated by the wav2sketch.c file, so that is where this is coming from ...

OK, fair enough. That makes sense then. By all means do the u-law encoding if that's what the FFT library wants.

  1. what is one of the 32bit integers of the PCM data telling me? Meaning how can i translate those numbers to loudness / decibel values?

It's pretty common for PCM audio data to be 16 bit depth (this is the CD audio standard), so the 32 bit integer is actually two 16 bit integers, one for each stereo channel. You can see that wav2c in the wav2sketch code double checks this by reading the .wav file header.

However, this is somewhat academic if you're working directly on samples generated by the microcontroller: you'll need to look into what the MCU's ADC hardware spits out in order to know what bit depth you'll have for your samples.

In terms of loudness, it's just a value relative to "the maximum value". Maximum being 0dB, and anything less than the maximum being -XdB. This article has maths and everything:

https://www.dsprelated.com/freebooks/mdft/Decibels.html

I want a real time frequency analysis with numerical data of a signal which is being sent to my microcontroller.

That, of course, is a completely different problem than what you posted at the start of this thread.

For input to an FFT/FHT/DFT from an audio signal, all that is needed are the ADC output values, stored in an array in memory. Same for "numerical data". For a simple example that runs even on the Arduino Uno, see the Open Music Labs FFT site.

WAV files, etc. have nothing to do with the process.

The program you say you are trying to convert to an Arduino sketch is one that converts a .wav file into an Arudino source file. It won't do you any good on an Arduino since the Arduino IDE doesn't run on an Arduino. You would have to move the generated source file to a PC to build it into a sketch to play with the Teensy3 Audio Library.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.