Audio USB Out "hello world" program

Status
Not open for further replies.

LanternMaker

New member
Hello,
I'm trying to use the audio features of my Teensy 3.6 and to begin, I am trying to make the most basic program to output sound using the computer's speakers, because I haven't bought the teensy sound card.
I used the design tool to connect one sine wave generator to both inputs of the USB output and pasted the code into Arduino editor. I have added some lines to make the onboard led blink and the code is here:

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

// GUItool: begin automatically generated code
AudioSynthWaveformSine   sine1;          //xy=350.1999969482422,248.1999969482422
AudioOutputUSB           usb1;           //xy=541.2000122070312,254.20001220703125
AudioConnection          patchCord1(sine1, 0, usb1, 0);
AudioConnection          patchCord2(sine1, 0, usb1, 1);
// GUItool: end automatically generated code

//used to blink the led to show that something is happening
int led=13;

void setup() {
  // put your setup code here, to run once:
  sine1.frequency(140);
  sine1.amplitude(0.6);
  pinMode(led, OUTPUT);     
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  sine1.amplitude(0.6);
  delay(1000);
  
   digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  sine1.amplitude(0.0);
  delay(100);
}

And the led blinks, but no sound is output. What am I doing wrong? :confused:
Before you ask :) I'm running windows 10, I have just updated my arduino program (1.8.5) and my Teenyduino (1.42). I have selected "Teensy 3.6", "Audio", "180MHz", "US english","Faster" in the Tools menu.
 

Attachments

  • audio_usb_hello.ino
    977 bytes · Views: 64
Hi,

Have you solved your issue? I'm using Teensy 3.2, and I found I can't make the teensy as a USB recording device.
 
You might also try File > Examples > Audio > HardwareTesting > WavFilePlayerUSB.

Don't forget to edit the SD card defines for Teensy 3.6 builtin SD card.
 
This basic code will let you hear audio form dac and also L&R USB
how are you listening to USB sound.....you might need to setup you PC to use the USB device for Audio instead of linein or mic etc
I get a bit of clicking when I record the USB audio and play it back......for this simple code it just switches from one freq to next and does not worry about zero crossover....

This is just uploaded into an bare teensy3.2 with amp speaker on dac and USB to PC .........

EDITED ..... I had been recording on win10 pc with audacity 2.0 when getting clicks on playback etc. I have now installed latest Audacity 2.3 and it records and the playback is OK no clicks.


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

// GUItool: begin automatically generated code
AudioSynthWaveformSine   sine1;          //xy=244,163
AudioOutputUSB           usb1;           //xy=557,160
AudioConnection          patchCord1(sine1, 0, usb1, 0);
AudioConnection          patchCord2(sine1, 0, usb1, 1);
// GUItool: end automatically generated code

AudioOutputAnalog     dac1;
AudioConnection          patchCord3(sine1, 0, dac1, 0);

 int freq = 100;

void setup() {
  // put your setup code here, to run once:

sine1.frequency(freq);
sine1.amplitude(0.7);

AudioMemory(80);
delay(500);

}

void loop() {
  // put your main code here, to run repeatedly:
freq = freq + 100;
sine1.frequency(freq);
delay(500);
if (freq >= 2000) freq = 100;
}
 
Last edited:
Thanks a lot. You code works on my Teensy 3.2. And my issue is caused by AudioMemory() which I used AudioMemory(20).
 
Hello,
This still doesn't work for me. I tried to change the sine function to a waveform, and the result is the same :

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

// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=378.20001220703125,246.20001220703125
AudioOutputUSB           usb1;           //xy=541.2000122070312,254.20001220703125
AudioOutputAnalog        dac1;           //xy=541.2000122070312,291.20001220703125
AudioConnection          patchCord1(waveform1, 0, usb1, 0);
AudioConnection          patchCord2(waveform1, 0, usb1, 1);
AudioConnection          patchCord3(waveform1, dac1);
// GUItool: end automatically generated code

int freq = 100;
 
//used to blink the led to show that something is happening
int led=13;

void setup() {
  AudioMemory(80);
  waveform1.begin(WAVEFORM_SINE);
  waveform1.frequency(freq);
  waveform1.amplitude(0.7);

delay(500);
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  freq = freq + 100;
  waveform1.frequency(freq);
  delay(500);
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(500);
  if (freq >= 2000) freq = 100;
}

I also checked the configuration of the sound of my computer and it all seems fine. Windows finds the Teensy, etc. but still no output.
 
As this code works for sure, you could first check with an oscilloscope on the DAC output if the Teensy is really producing an audio signal, at least on that output from which we might deduce a correct working USB output, too. Then, you should do a differential diagnose to test if your Windows PC does definitively play sound from USB. So, you need another USB audio source, i.e. a gaming headset with microphone to check. I'd guess that something in the Windows audio configuration (default audio device, usb audio input, windows mixer) is perhaps muted or disabled.
 
Hi. please try your teensy as a USB output device as Theremingenieur said. I think in this way you can confirm your board is OK or not.
 
My original idea of the simple basic sketch was to simplify it, and output audio to DAC and USB at same time. DAC is the most simple and direct from teensy.
In order to hear DAC all that is needed is a small amplified speaker having line-in or Aux-in port. That will make sure the sketch is running and outputting to DAC....forget about USB until you get that working.

Then with DAC working plug USB into your PC as see can you hear audio from PC while listening to DAC audio on small speaker.

If you dont hear PC sound you will have to fiddle with the PC sound settings to get PC to be listening in to USB audio in and Playing out over either PC speakers or Lineout speakers, but listen to DAC audio at same time while setting up PC so you know the teensy sketch is still running.
 
Status
Not open for further replies.
Back
Top