6

I wired up the MCP3008 ADC chip to an Electret Microphone and to my pi. I'm reading the input using bit-banging in python, and I'm getting an integer from 0-1024.

I followed this tutorial to do the bit-banging: https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/connecting-the-cobbler-to-a-mcp3008

My question is how do I take this integer and convert it to something meaningful? Can I somehow write these bytes to a file in python to get the raw audio data that Audacity can play? Right now when I try to write the values they just show up as the integer instead of binary. I'm really new to python, and I've found this link for converting the raw data, but I'm having trouble generating the raw data first:Python open raw audio data file

I'm not even sure what these values represent, are they PCM data that I have to do math with related to time?

Community
  • 1
  • 1
Jack Freeman
  • 1,414
  • 11
  • 18
  • Well I found out how to properly write binary data in python here: http://stackoverflow.com/questions/846038/convert-a-python-int-into-a-big-endian-string-of-bytes – Jack Freeman Feb 01 '15 at 07:01
  • Just curios, did you ever get this working, and how? I'm reading data from an arduino and giving that data directly to the Raspberry pi, but have little idea what to do with this data. Do you just save those int's as bytes into a file and audacity can pick that up? Pretty clueless on this one – Ricky Hartmann May 23 '15 at 04:09
  • Jack, people are asking if you have gotten this to work and can share anything. Thanks – Drew Mar 09 '16 at 17:43

1 Answers1

4

What you are doing here is sampling a time-varying analogue signal. so yes, the values you obtain are PCM - but with a huge caveat (see below). If you write them as a WAV file (possibly using this to help you), you will be able to open them in Audacity. You could either convert the values to unsigned 8-bit (by truncation and) or to 16-bit signed with a shift and subtraction.

The caveat is that PCM is the modulation of a sample clock with the signal. The clock signal in your case is the frequency with which you bit-bang the ADC. Practically, it is very difficult to arrange for this to be regular in software - and particularly when bit-banging the device from a high-level language such as Python. You need to sample at twice the bandwidth of the signal (Nyquist's law) - so realistically, 8kHz for telephone speech quality.

An irregular sample clock will also result in significant artefacts - which you will hear as distortion.

marko
  • 9,029
  • 4
  • 30
  • 46
  • Thanks for this info. Turns out Audacity can play the raw files, but as you mentioned python is no good for this. I have to re-write the code in C to achieve a fast enough sampling rate. Python tops out at about 6khz and is highly irregular due to unknown garbage collection times. – Jack Freeman Feb 03 '15 at 00:51