Recording audio on a raspberry Pi

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
benjgorman
 
Posts: 5
Joined: Wed Feb 05, 2014 12:56 pm

Recording audio on a raspberry Pi

Post by benjgorman »

Hello,

I'm trying to record audio on Raspberry Pi using the adafruit microphone breakout board and the 12 bit adc breakout board over the PI's i2c.

I can read in the values from the adc using python and plotting them on a graph shows it's a basic audio wave format. I'm sampling at 1000hz.

My question is, how am I supposed to take these values and make them into an WAV file for instance?

I've looked at pyAudio but I'm still a bit lost as it refers to a stream generally.

Thanks!

Ben

User avatar
adafruit_support_mike
 
Posts: 66315
Joined: Thu Feb 11, 2010 2:51 pm

Re: Recording audio on a raspberry Pi

Post by adafruit_support_mike »

In general, a WAV file is just a list of the values you'd get from the ADC. You can find more information about the file format here: http://en.wikipedia.org/wiki/WAV

PyAudio handles the file encoding for you, but does work with streams, which are a programming IO abstraction with a 'medium' challenge rating. Basically a stream is anything that produces or receives data, but the devil is in the details.

Your biggest challenge for recording audio on a RasPi will be the hardware itself though. A 1kHz sampling rate will only record sounds at 500Hz (slightly higher than a dialtone) and lower. Sounds at higher ferquencies move faster than the sampling system can catch them, so you get what's called an 'aliased' component of the signal. To sample audio for the human hearing spectrum (about 22Hz to 20kHz), your sample rate needs to be at least 40kHz. The industry standard for CDs is 44.1kHz.

In general, you can't do that with a RasPi.

Linux is a time-sliced operating system, meaning the OS gives each program a certain amount of time on the CPU (usually 10 milliseconds) then suspends it and gives some other program a turn. That's fine for plug-and-chug number crunching, and for human-speed things like word processors, but not so good for machine-speed stuff like audio recording. You end up with randomly-spaced gaps in the data.

To make Linux do time-critical work, you need a kernel module which runs outside the OS's time-slicing mechanism.

User avatar
benjgorman
 
Posts: 5
Joined: Wed Feb 05, 2014 12:56 pm

Re: Recording audio on a raspberry Pi

Post by benjgorman »

Right that all makes sense. However I'm fine with the low sampling rate I have, but from the ADS1x15 I just have an output of voltages. I'm not sure where to even start turning this into a wav file, even a raw audio file.

Would it simply be a list of binary values which represent these voltages?

Code: Select all

2736
1227
2037
1782
1488
2712
2535
717
1566
1533
534
3837
3189
3114
537
1458
1932
1761
1917
1836
1812
2181
1389
1800
3207
609
1500
1902
249
3837
3108
2403
1092
1167
1779
2379
1860
1632
1899
2151
1887
1695
2910
1179
1482
1470
723
3834
3078
2220
645
1584
1779
2109
2061
1491
1956
2139
1605
1848
2868
849
1857
1281
291
3837
3420
2736
864
1497
1716
1770
1857
2139
1686
1776
1983
1608

User avatar
pburgess
 
Posts: 4139
Joined: Sun Oct 26, 2008 2:29 am

Re: Recording audio on a raspberry Pi

Post by pburgess »

A different approach would be to use a USB audio adapter like this one:
http://www.adafruit.com/products/1475
WIth the addition of a regular 1/8" recording mic, this is compatible with the 'arecord' utility to produce a WAV file.

User avatar
adafruit_support_mike
 
Posts: 66315
Joined: Thu Feb 11, 2010 2:51 pm

Re: Recording audio on a raspberry Pi

Post by adafruit_support_mike »

benjgorman wrote:Would it simply be a list of binary values which represent these voltages?
Pretty much.. that's the core of what would be called a 'Pulse Code Modulated' (PCM) audio file. The Wikipedia link above explains more about the information the WAV format adds to the PCM section.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”