Send raw audio data through the serialport

Status
Not open for further replies.

noneven

Member
I am a newbee to the teensy world. I posted another question earlier about customizing the frequency earlier. I am now using the code below to record the data with 44.1 KHz and trying to send the data via the serialport. Unfortuantely, the data received on the laptop did not look right. I guess the problem is how I read the audiobuffer and convert the bytes array to the string which was then written to the serialport.

What is the right way to write the bytes array to the serial port? Thanks.


Here is the code I am trying to use the Audio library to sample the adc with 44100 KHz. The read value did not look right.

// Record sound as raw data to a SD card, and play it back.
//
// Requires the audio shield:
// http://www.pjrc.com/store/teensy3_audio.html
//
// Three pushbuttons need to be connected:
// Record Button: pin 0 to GND
// Stop Button: pin 1 to GND
// Play Button: pin 2 to GND
//
// This example code is in the public domain.

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

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

// GUItool: begin automatically generated code
AudioInputAnalog adc1(A0); //xy=145,295
AudioRecordQueue queue1; //xy=339,289
AudioConnection patchCord1(adc1, queue1);
// GUItool: end automatically generated code


// which input on the audio shield will be used?
const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;

// Remember which mode we're doing
int mode = 0; // 0=stopped, 1=recording, 2=playing

// The file where data is recorded
File frec;

void setup() {

Serial.begin(9600);

Serial.println("Begin setup");
// Audio connections require memory, and the record queue
// uses this memory to buffer incoming audio.
AudioMemory(60);

}


void loop() {
// First, read the buttons
if (mode == 0) startRecording();
continueRecording();
}


void startRecording() {
Serial.println("startRecording");
queue1.begin();
mode = 1;
}

void continueRecording() {
if (queue1.available() >= 2) {
byte buffer[1024];
// Fetch 2 blocks from the audio library and copy
// into a 512 byte buffer. The Arduino SD library
// is most efficient when full 512 byte sector size
// writes are used.
queue1.freeBuffer();
memcpy(buffer, queue1.readBuffer(), 1024);
for(int i=0; i+2<1024;i=i+2){
unsigned int word = buffer * 256 + buffer[i+1];
Serial.println(word);

}



// queue1.freeBuffer();
// memcpy(buffer+256, queue1.readBuffer(), 256);
// queue1.freeBuffer();

// Uncomment these lines to see how long SD writes
// are taking. A pair of audio blocks arrives every
// 5802 microseconds, so hopefully most of the writes
// take well under 5802 us. Some will take more, as
// the SD library also must write to the FAT tables
// and the SD card controller manages media erase and
// wear leveling. The queue1 object can buffer
// approximately 301700 us of audio, to allow time
// for occasional high SD card latency, as long as
// the average write time is under 5802 us.
//Serial.print("SD write, us=");
//Serial.println(usec);
}
}

void stopRecording() {
Serial.println("stopRecording");
queue1.end();
if (mode == 1) {
while (queue1.available() > 0) {
frec.write((byte*)queue1.readBuffer(), 256);
queue1.freeBuffer();
}
frec.close();
}
mode = 0;
}
 
Status
Not open for further replies.
Back
Top