Go Down

Topic: Digital Audio through Arduino DUE to PC (Read 372 times) previous topic - next topic

konni941

Hey there,
I´m new to this forum, but have been reading for quite a while now.

I have browsed the audio section for a while now, but haven´t found a solution for my problem, yet.

I want to use a MEMS-based analog microphone to record audio to my PC by using an Arduino DUE as a "bridge". I relay the analog signal through an external ADC and into the Arduino´s DI. That works fine and I was also able to save this data through hterm into a binary file.

My problem now is, that I want to convert that file into an audio format (.wav) so I can analyse and filter it with MATLAB or Audacity or any audio software that will work.

I´m not sure if this is the correct forum for this, since it is not a problem with the Arduino, but maybe there are other ways (with the Arduino) to get the job done.

Recording, saving and analysing the file is fine for the early stages of my project, but my aim is to get real time audio and analyse/filter it on the fly. Is it possible to transmit the audio over USB?

I don´t need good quality, I just want to be able to recognice a tapping pattern for example.

I hope You can give me some ideas to think about.

Thank you :)

PieterP

Since a .WAV file is basically a list of amplitude values with the right headers, you could write a script (in Python, for example) to read in the binary file, and convert it to a .WAV file.
SciPy has a function to write an array of samples to a .WAV file: https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html

Writing USB firmware is not easy, and I can't seem to find a library that does what you want on a Arduino Due. Here's a dead thread, for example, where someone claims that he got it working, but he posted no code: https://forum.arduino.cc/index.php?topic=202365.0.

I'd recommend getting a Teensy 3.x (or 4.0?), it has great software support for audio stuff (including USB and some DSP): https://www.pjrc.com/teensy/td_libs_Audio.html

Pieter

konni941

Hi Pieter,

thank you for the fast reply.

I thought about writing a Python script but didn´t want to go down that rabbit hole ;). Now it seems like it is the next logical step.

Nice, I tried a similar program to convert into .wav but it didn´t work, let´s see if this works, thank you.

Yes, I didn´t find a library either, but for now it is not that important.

Actually I already ordered one (3.6) together with the Audio Adaptor Board but I wanted to try getting it to

work with the Arduino, but I already saw in other forums, that the Teensy seems to be the go-to-board when it comes to audio.

Again, thank you, I will let you know if one of your solutions worked or if I find one myself


Konni

DVDdoug

#3
Aug 19, 2019, 07:46 pm Last Edit: Aug 19, 2019, 07:57 pm by DVDdoug
Here is the WAV file format.  This seems like the easiest part of the project...

The WAV header is only 44 bytes and if your format never changes all of your headers will be the same except for the "chunk size" fields.  You can open an existing WAV file with a hex editor to look at the header or copy it or use it as a template.

For the data, its just a matter of getting the correct byte sequence/format.   8-bit mono is the easiest because it's simply a series of unsigned bytes.*   16-bits or higher uses signed integers (or floating-point) with  little-endian byte-sequence.    With stereo files, the left & right channels alternate with left coming first.

If you are recording and writhing a WAV file in real time, I assume the standard practice is to write the header first with a chunk size of zero.  Then, go-back and update size when recording is stopped.

Quote
My problem now is, that I want to convert that file into an audio format (.wav) so I can analyse and filter it with MATLAB or Audacity or any audio software that will work.
Audacity can import "raw"  uncompressed audio data files but you have to type-in all of the information it normally gets from the file header such as the bit depth, sample rate, number of channels, etc.   Then, you can optionally export to WAV (or other standard audio format). 

I'm sure MATLAB can do that too, since MATLAB isn't strictly an audio program, but it may be more work for you to re-format the raw bytes into audio samples (if it's not simple 8-bit audio).




* Since the Arduino ADC can't read negative values, the input is biased giving you biased A(unsigned) data.  For signed formats, you'll have to subtract-out the bias before writing the WAV file.   

And of course, you'll need to scale the ADC data (12-bits form the Due) up or down to 8 or 16 bits for a standard audio file.

konni941

Hi DVDdoug thank you, for your reply.

I was reading that exact site before, but I didn`t get it. But your idea of copying or at least looking at an existing header is gold, because now I have an example to work with.

Audacity can import "raw"  uncompressed audio data files but you have to type-in all of the information it normally gets from the file header such as the bit depth, sample rate, number of channels, etc.   Then, you can optionally export to WAV (or other standard audio format). 

Yes, I tried this function of Audacity, but I didn`t get a sound file that made sense(it was only noise).

I'm sure MATLAB can do that too, since MATLAB isn't strictly an audio program, but it may be more work for you to re-format the raw bytes into audio samples (if it's not simple 8-bit audio).

I haven`t tried MATLAB, yet. A friend recommended it to me. As I understand it it´s a very powerfull tool and basically capable of everything, but it also seems to be a massive rabbit hole to go down to.

* Since the Arduino ADC can't read negative values, the input is biased giving you biased A(unsigned) data.  For signed formats, you'll have to subtract-out the bias before writing the WAV file.   

And of course, you'll need to scale the ADC data (12-bits form the Due) up or down to 8 or 16 bits for a standard audio file.

I read about this quite a lot when I was first looking into audio with Arduino. As I understand it the Arduino simply reads nothing when it gets "hit" by a negative value. And this can be solved on the hardware side if I´m not mistaken.

To scale the data down to 8 bits I can use analogReadResolution( 8 ), discarding the least significant bits, can`t I?


But I have to admit, I cheated already ;)
I simply connected the microphone to a 3.5mm jack and used my computers soundcard to record the files. Now I can at least analyse my data, but I still feel kind of bad for not doing it with the Arduino ;)

I also think, I went at this the wrong way. I was thinking I could filter my audio for peaks, then write a program that utilizes that peaks to start a sequence. But by writing my project in this forum I realized, that what I want is basically a sort of clap-switch, which as I understand is much simpler to implement.

But I can still use the audio-analysing to test different base-materials for the microphone, so it wasn´t all for nothing.

Go Up