View Full Version : Reading Samples from Mic Input

02-09-2016, 08:22 PM
I'm trying to read samples from mic with audio shield. I'm using readBuffer() to do so, but the data that is displayed for each sample of 128 bit array doesnt make any sense to me. If i'm reading from microphone then shouldn't the sampled data be random? I've posted my code below and the Serial monitor capture of what i'm getting. Any help would be appreciated on what i'm doing wrong. Thank you!

serial output:

my code:

// Microphone to Speaker output

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S Mic; //microphone
AudioRecordQueue MicMem; //queue for microphone
AudioOutputI2S headphones; //output
AudioConnection patchCord1(Mic, 0, MicMem, 0);
AudioConnection patchCord2(Mic, 0, headphones,0);
AudioConnection patchCord3(Mic, 0, headphones,1);
AudioControlSGTL5000 sgtl5000_1; //xy=339.6666564941406,285.6666488647461
// GUItool: end automatically generated code
int16_t data1[128];

const int micInput = AUDIO_INPUT_MIC;

void setup() {

void loop() {
int16_t *data = MicMem.readBuffer();

for(int i = 0; i < 128; i++) {
MicMem.freeBuffer(); // do not use "data" after this!

Frank B
02-09-2016, 10:14 PM
- Please use code tags next time, without indention your code is difficult to read.
- There is an example "Audio/Recorder" under "Examples" which shows you the correct usage of AudioRecordQueue.

02-10-2016, 04:20 PM
Your code isn't checking for readBuffer() to return NULL, which it does until data is actually available.

Then your code is reading whatever data happens to be present at address zero, which is the ARM exception and interrupt vector table in flash memory. Since your program is fairly small, the 32 addresses are all zeros in the upper 16 bits, which is why you're seeing half the data as zeros. Most of the vector table is populated with the address of an "unused" default handler function, which is why you're seeing the same number repeated.

02-11-2016, 06:57 AM
Thank you for the input; it really helped. I was able to get the data that I need and manipulate it. So, now I filled the sampled data into 512 int16_t array, how can I take this array and play it on speaker, or send it to DAC. I've been looking though examples and "simpleplayer" has a "play(data)" object, but that wont play the array that I have. How can I accomplish that? Thank you!