PDA

View Full Version : Reading Samples from Mic Input



snirtikam
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:
6336

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() {
Serial.begin(9600);
AudioMemory(20);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
sgtl5000_1.inputSelect(micInput);
sgtl5000_1.micGain(36);
delay(1000);
}

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

for(int i = 0; i < 128; i++) {
Serial.println(data[i]);
}
MicMem.freeBuffer(); // do not use "data" after this!
MicMem.end();
delay(500);
}

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.

PaulStoffregen
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.

snirtikam
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!