PassThroughUSB with queues

Status
Not open for further replies.

kk12

Active member
For some reason, following code that was modified from PassThroughUSB -example, doesn't function while using queues. How to make it work? Left channel is flooded with unwanted noise, wheras right channel (without queues) is correct audio from PC.

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

AudioInputUSB            usb1;           //xy=200,69  (must set Tools > USB Type to Audio)
AudioOutputI2S           i2s1;           //xy=365,94

AudioRecordQueue    queue1;
AudioPlayQueue      queue3;

AudioConnection     patchCord1(usb1, 0, queue1, 0);   
AudioConnection     patchCord3(queue3, 0, i2s1, 0);  
AudioConnection     patchCord2(usb1, 1, i2s1, 1);    

AudioControlSGTL5000     sgtl5000_1;     //xy=302,184

short array1[AUDIO_BLOCK_SAMPLES];

void setup() {                
  AudioMemory(80);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
}

void loop() {

 // INPUT
  memcpy(&array1, queue1.readBuffer(), AUDIO_BLOCK_SAMPLES*2);
  queue1.freeBuffer();

  // OUTPUT
  memcpy(queue3.getBuffer(), &array1, AUDIO_BLOCK_SAMPLES*2);
  queue3.playBuffer();
}
 
Constant noise was kind of misleading. Its a constant frequency instead of widespectrum noise.
 
You need to test that there's a queue buffer available before copying data and sending it.

Code:
  if (queue1.available() >= 1) {
    memcpy(array1, queue1.readBuffer(), AUDIO_BLOCK_SAMPLES*2);
    queue1.freeBuffer();
  }

and do a similar thing with queue3 before calling getBuffer and copying array1 into it.

Pete
 
Thanks Pete!

So here goes a working example of USB-passthrough teensy->usb & usb->teensy. It uses queues and could be used as a soundcard for PC. Also bit manipulation is possible because of using queues. Just Teensy, audio shield and microphone needed.

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(9600);
  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[j*128], rqueue1.readBuffer(), 2*AUDIO_BLOCK_SAMPLES);
    rqueue1.freeBuffer();
  
    memcpy(pqueue2.getBuffer(), &array1[j*128], 2*AUDIO_BLOCK_SAMPLES);
    pqueue2.playBuffer();
  }

  
  ret=rqueue3.available();
  //Serial.println(ret);

  if(ret>=1)
  {
    memcpy(&array1[j*128], rqueue3.readBuffer(), 2*AUDIO_BLOCK_SAMPLES);
    rqueue3.freeBuffer();
  
    memcpy(pqueue4.getBuffer(), &array1[j*128], 2*AUDIO_BLOCK_SAMPLES);
    pqueue4.playBuffer();
  }
  
  //delay(100);
}
 
Status
Not open for further replies.
Back
Top