Im using Teensy as USB-soundcard, so its both playback and record device. Applying readBuffer(), playBuffer() and queues to write/read the data into short-arrays.
So I created a square wave in Audacity (44,1kHz, 16-bit PCM) to test if Teensy can read it correctly (sending back to PC in serial terminal). There should be only values e.g. 32767 and -32768. For some reason they don't keep stable:
...32269,32269,32272,32274,32275,32275,32278,32278,32282,32282,32285,
-32287,-32286,-32290,-32292,-32293,-32294,-32297,-32295,-32299,-32300,-32302,-32305,-32305,-32309,-32308,-32311,-32311,-32313...
Why is this and is it correct behaviour?
So I created a square wave in Audacity (44,1kHz, 16-bit PCM) to test if Teensy can read it correctly (sending back to PC in serial terminal). There should be only values e.g. 32767 and -32768. For some reason they don't keep stable:
...32269,32269,32272,32274,32275,32275,32278,32278,32282,32282,32285,
-32287,-32286,-32290,-32292,-32293,-32294,-32297,-32295,-32299,-32300,-32302,-32305,-32305,-32309,-32308,-32311,-32311,-32313...
Why is this and is it correct behaviour?
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
AudioControlSGTL5000 sgtl5000_1; //xy=302,184
AudioInputUSB usb1; //xy=200,69 (must set Tools > USB Type to Audio)
AudioOutputI2S i2s1; //xy=365,94
AudioRecordQueue rqueue1;
AudioPlayQueue pqueue2;
AudioConnection patchCord1(usb1, 0, rqueue1, 0);
AudioConnection patchCord2(pqueue2, 0, i2s1, 0);
AudioInputI2S i2s2; //xy=105,63
AudioOutputUSB usb2;
AudioRecordQueue rqueue3;
AudioPlayQueue pqueue4;
AudioConnection patchCord3(i2s2, 0, rqueue3, 0);
AudioConnection patchCord4(pqueue4, 0, usb2, 0);
const int myInput = AUDIO_INPUT_MIC;
int ret=0;
int i=0;
int j=0;
short array1[AUDIO_BLOCK_SAMPLES];
short array2[AUDIO_BLOCK_SAMPLES];
void setup() {
AudioMemory(24);
sgtl5000_1.enable();
sgtl5000_1.inputSelect(myInput);
sgtl5000_1.volume(0.6);
Serial.begin(19200);
rqueue1.begin();
rqueue3.begin();
Serial.println("Begin");
}
void loop() {
// read the PC's volume setting
float vol = usb1.volume();
// scale to a nice range (not too loud)
// and adjust the audio shield output volume
if (vol > 0) {
// scale 0 = 1.0 range to:
// 0.3 = almost silent
// 0.8 = really loud
vol = 0.3 + vol * 0.5;
}
// use the scaled volume setting. Delete this for fixed volume.
//sgtl5000_1.volume(vol);
ret=rqueue1.available();
//Serial.println(ret);
if(ret>=1)
{
memcpy(&array1[0], rqueue1.readBuffer(), 2*AUDIO_BLOCK_SAMPLES);
rqueue1.freeBuffer();
unsigned int loop=0;
while(loop<128)
{
Serial.print(array1[j], DEC);
Serial.print(",");
loop++;
}
Serial.println("");
memcpy(pqueue2.getBuffer(), &array1[0], 2*AUDIO_BLOCK_SAMPLES);
pqueue2.playBuffer();
}
ret=rqueue3.available();
//Serial.println(ret);
if(ret>=1)
{
memcpy(&array1[0], rqueue3.readBuffer(), 2*AUDIO_BLOCK_SAMPLES);
rqueue3.freeBuffer();
memcpy(pqueue4.getBuffer(), &array1[0], 2*AUDIO_BLOCK_SAMPLES);
pqueue4.playBuffer();
}
//delay(100);
}
Last edited: