11 years, 4 months ago.

Analog.read_u16/Audio/Wave

I am trying to sample raw audio data from a mic "mic came from sparkfun(ELECTRET)" using the .read_u16. i need help understanding how to get back to the 12bit adc value. My intent is to print the bytes out to a file and place a WAV header file on the top and play the file. Then the ending goal is to send it over wifi to a android mobile phone for play back.

This what we have so far

#include "mbed.h"
DigitalOut myled(LED1);
DigitalOut myled1(LED2);
AnalogIn soundin(p17);
AnalogOut soundout(p18);
Serial pc(USBTX, USBRX); // tx, rx
Ticker sample;
void sample_sound(void);

volatile short int sound;

//extern void ADPCMEncoderInit(void);
//extern uint8_t ADPCMEncoder( int16_t i16_sample);
//extern int16_t ADPCMDecoder(uint8_t u8_code);
//extern void ADPCMDecoderInit(void);
#define Raw
double mic_mean = 0.0;
double mic_value;
char stream[16000];
bool ready=false;

int main()
{
    pc.baud(9600);


    sample.attach_us(sample_sound, 125);
    //uint16_t u16_sound= 0xABCD;
    while (1) {

        //end= ((u16_sound&0x00FF)<<8);//lower bits
        //send+=(uint8_t)((u16_sound>>8)&0x00FF);// upper 8bits
        //printf("val %X",send);
        //for (int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
        //buf[i] = (mic.read_u16() >> 3) - mic_mean;
        //if (i != AUDIO_LENGTH_PACKET/2) {
        //wait_us(80);
        // }
        //}
        //audio.write((uint8_t *)buf);
        //if(ready) {
            //myled1=1;
            //stream[16000]='\0';
            //for(int i =0; i<sizeof(stream); i++) {
                 //pc.putc(stream[i]);
            //}
            //myled=0;
            //ready=false;
        //}

    }
}
float s;
uint16_t u16_sound;
uint8_t sendl,sendu;

int count =0;
void sample_sound(void)
{
    //soundout.write_u16(static_cast<uint16_t>(ADPCMDecoder(ADPCMEncoder(static_cast<int16_t>(soundin.read()*3.3)))));
    myled=1;
    //soundout.write_u16(ADPCMDecoder(ADPCMEncoder((soundin.read_u16()>>3)-2048))+2048);
    u16_sound =((soundin.read_u16()));
    pc.putc((uint8_t)((u16_sound&0x00FF)));//lower bits
    count++;
    pc.putc((uint8_t)((u16_sound>>8)&0x00FF));// upper 8bits
    count++;
    
    if(count > 16000) {
        sample.detach();
        myled =0;
        //pc.printf("%s",sendl);
        //pc.printf("%s",sendu);
        ready=true;
        //sample.detach();
    }    


}

This is the wav header im almost positive this is right

52 49 46 46 78 3e 00 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 01 00 40 1f 00 00 80 3e 00 00 02 00 10 00 64 61 74 61 80 3e 00 00

if i got back to the original 12bit adc value the i could simply -2048 to get a signed value for the wav player. I have tried to minus 2^15 from the 16bit normalized value but that didn't work either. The commented out lines where previous attempts to making this work along with allot of deleted ones.

Thanks Ryan

Be the first to answer this question.