Teensy4.1 USB Audio

bdoan

Well-known member
I am trying to create a USB audio application for the 4.1 using the Audio Shield (Rev D)

I used the Audio System Design Tool and created this code for Arduino IDE 2.3.2

What code needs to go in the main loop?


void setup() {
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
AudioInputUSB usb1; //xy=383.8541564941406,352.60418701171875
AudioInputI2S2 i2s2_1; //xy=388.8541603088379,231.3541603088379
AudioOutputUSB usb2; //xy=763.8541603088379,357.6041603088379
AudioOutputI2S i2s1; //xy=773.8541870117188,233.8541717529297
AudioConnection patchCord1(usb1, 0, i2s1, 0);
AudioConnection patchCord2(usb1, 1, i2s1, 1);
AudioConnection patchCord3(i2s2_1, 0, usb2, 0);
AudioConnection patchCord4(i2s2_1, 1, usb2, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=542.0833282470703,475.0833282470703
}
void loop() {
// put your main code here, to run repeatedly:
}


I am getting this compilation error

Compilation error: 'AudioInputUSB' was not declared in this scope; did you mean 'AudioInputPDM'?
 
Specifically with regard to the compilation error: did you select "Audio" or "Serial + MIDI + Audio" or "Serial + MIDIx16 + Audio" for the "USB Type" in the Ardiuno IDE "Tools" pulldown menu ?? As for what to put in the main loop, the answer to that depends heavily upon what you want your USB audio device to do.

Mark J Culross
KD5RXT
 
Initially, I just want to bridge the Analog audio (in and out) from the SGTL5000 to USB audio.
At some later date, I will add GPIO, ADC measurements and user interface (non-blocking) to complete the application.
 
I am still getting a compilation error, apparently from one of the include libraries.

:\users\user\appdata\local\arduino15\packages\teensy\hardware\avr\1.59.0\libraries\sdfat\src\common\fsstructs.h:47:49: error: a function-definition is not allowed here before '{' token
47 | inline void setLe32(uint8_t* dst, uint32_t src) {
 
It's really helpful to use the Code tag when posting your code, as it preserves the formatting - click the </> button on the toolbar...

A fair few things not been mentioned as yet:
  • You've put all the Design Tool output inside the setup() function, rather than at the top of the file in global scope
  • There's no AudioMemory allocated
  • You've used AudioInputI2S2, which isn't connected to the Audio Adaptor - use AudioInputI2S
  • You haven't initialised the SGTL5000
The following is completely untested (apart from compiling it!), but should be an improvement:
C++:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioInputUSB usb1; //xy=383.8541564941406,352.60418701171875
AudioInputI2S i2s2_1; //xy=388.8541603088379,231.3541603088379
AudioOutputUSB usb2; //xy=763.8541603088379,357.6041603088379
AudioOutputI2S i2s1; //xy=773.8541870117188,233.8541717529297
AudioConnection patchCord1(usb1, 0, i2s1, 0);
AudioConnection patchCord2(usb1, 1, i2s1, 1);
AudioConnection patchCord3(i2s2_1, 0, usb2, 0);
AudioConnection patchCord4(i2s2_1, 1, usb2, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=542.0833282470703,475.0833282470703

void setup()
{
    AudioMemory(10);
    
    sgtl5000_1.enable();
    sgtl5000_1.volume(0.8f);
}

void loop()
{
}
 
It's really helpful to use the Code tag when posting your code, as it preserves the formatting - click the </> button on the toolbar...

A fair few things not been mentioned as yet:
  • You've put all the Design Tool output inside the setup() function, rather than at the top of the file in global scope
  • There's no AudioMemory allocated
  • You've used AudioInputI2S2, which isn't connected to the Audio Adaptor - use AudioInputI2S
  • You haven't initialised the SGTL5000
The following is completely untested (apart from compiling it!), but should be an improvement:
C++:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioInputUSB usb1; //xy=383.8541564941406,352.60418701171875
AudioInputI2S i2s2_1; //xy=388.8541603088379,231.3541603088379
AudioOutputUSB usb2; //xy=763.8541603088379,357.6041603088379
AudioOutputI2S i2s1; //xy=773.8541870117188,233.8541717529297
AudioConnection patchCord1(usb1, 0, i2s1, 0);
AudioConnection patchCord2(usb1, 1, i2s1, 1);
AudioConnection patchCord3(i2s2_1, 0, usb2, 0);
AudioConnection patchCord4(i2s2_1, 1, usb2, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=542.0833282470703,475.0833282470703

void setup()
{
    AudioMemory(10);
   
    sgtl5000_1.enable();
    sgtl5000_1.volume(0.8f);
}

void loop()
{
}
Ok. Got it working. Thanks.
Now how do I speed up the Arduino 2 compiler?
 
Back
Top