PDA

View Full Version : How to Record Audio with Customized Frequency?



noneven
08-01-2016, 07:54 AM
Hi,

I am new to teensy. I am using Teensy 3.2 to record audio with a customized sample rate(e.g. 50KHz). The input analog signal is from a microphone which has already been filtered and amplifed with a range of 0 -3.3 V. I want to use the ADC on Teensy to sample the data and send to a mac book via the serial port. I am currently using the ADC library to do that, but I failed to find a good way to set the sample rate using that ADC libaray.

Is there any simple way I can use the Audio libarray to set the sample rate and record the data?


Thanks a lot!

PaulStoffregen
08-01-2016, 03:53 PM
The ADC input range is 0 to 1.2V (a DC bias of 0.6V) when the low-noise internal reference is used, which is the default when using the audio lib. You should plan on that signal range for the best results.

The audio lib is designed for 44.1 kHz. But if you edit the AudioInputAnalog object, you can change the PDB timer to run at a different speed. If you do this, do *not* use any other input or output objects, unless you also edit them to run at precisely the same speed.

The audio lib has a queue object which gives you access to the raw data. See the Recorder example and of course the design tool's left-side panel for documentation.

The USB serial should be fast enough to transmit to your mac, as long as an efficient program is receiving. Use an efficient binary protocol. The USB serial is at best about 1.1 MByte/sec speed, and often you'll get quite a bit less if other USB devices are active, so consider how many bytes you're actually sending per sample.

noneven
08-01-2016, 05:15 PM
Thanks so much for the reply Paul.
What's the maximum range of the ADC input? 0-5 V? Is there any way I can dive into the libarary and rescale the range of 0-1.2V?
I saw there is another library called ADC(https://github.com/pedvide/ADC) which allows me to customized the ADC input.

I am new to the Teensy world. To use the Audio library, shall I first use the sketch tool to connect a adc input with a buffer and export the code to arduino first? This step should help me initialize the objects. Then use thse objects in the loop function to retrieve the information? I am still a bit confused here on how should I get the 44.1 KHz trigger in the code.

Thanks again.


The ADC input range is 0 to 1.2V (a DC bias of 0.6V) when the low-noise internal reference is used, which is the default when using the audio lib. You should plan on that signal range for the best results.

The audio lib is designed for 44.1 kHz. But if you edit the AudioInputAnalog object, you can change the PDB timer to run at a different speed. If you do this, do *not* use any other input or output objects, unless you also edit them to run at precisely the same speed.

The audio lib has a queue object which gives you access to the raw data. See the Recorder example and of course the design tool's left-side panel for documentation.

The USB serial should be fast enough to transmit to your mac, as long as an efficient program is receiving. Use an efficient binary protocol. The USB serial is at best about 1.1 MByte/sec speed, and often you'll get quite a bit less if other USB devices are active, so consider how many bytes you're actually sending per sample.

noneven
08-01-2016, 05:29 PM
I think my temporal plan is to use a voltage divider to scale the input range to 0-1.2V. Then use the audio library for the 44.1 KHz which I can sample and send via the serial port to the laptop. Is there any example code I can borrow on how to get the 44.1 KHz trigger in the code ?

Sorry for the double posting questions.

noneven
08-02-2016, 01:30 AM
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[i] * 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;
}