My teensy is outputting distorted audio

maggot

Member
Here is a video showing what I mean.

This is just a test sketch playing a waveform through an envelope ever 500ms, but I've run some of the example sketches too, and all the audio is coming out distorted. It's a fully charged li-po, and a mini speaker - which I've tested on my laptop and it works fine.

The first few beeps sound ok - although they don't quite sound like sine waves - but then it quickly becomes distorted. Sometimes it sounds different, starting off silent as soon as I turn it on, and then slowly fading into a distorted fuzzy sound with loads of white noise.

I've used Teeny boards and the audio adapter before and not had this issue. This one's been sitting in a drawer for maybe a year or more.

Here's my code:

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

// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=225.11111450195312,1058.3331985473633
AudioEffectEnvelope      envelope1;      //xy=410,1053.3333358764648
AudioOutputI2S           i2s1;           //xy=680.1112117767334,1050.3331985473633
AudioConnection          patchCord1(waveform1, envelope1);
AudioConnection          patchCord2(envelope1, 0, i2s1, 0);
AudioConnection          patchCord3(envelope1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=303.1111145019531,960.333251953125
// GUItool: end automatically generated code


// Timing variables
unsigned long lastTime = 0;

void setup() {
  Serial.begin(9600);
  AudioMemory(10);

  waveform1.begin(WAVEFORM_SINE);
  waveform1.frequency(262);
  waveform1.amplitude(0.8);

  envelope1.attack(10);
  envelope1.decay(200);
  envelope1.sustain(0.25);
  envelope1.release(300);

  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);

  Serial.println("hi there!");
}

void loop() {
  unsigned long currentTime = millis();

  if (currentTime - lastTime >= 500) {
    envelope1.noteOn();
    lastTime = currentTime;
    Serial.println("C");
  }
}
 
@maggot: If you're connecting your mini-speaker to the headphone jack, you may be overloading it (the headphone interface not really made for the low-impedance that a speaker presents). The headphone interface has a false ground that absolutely does not like to be connected to GND (see the warning on the Audio Adapter about not connecting VGND to GND). Try using actual headphones & see if the distortion is any different and/or goes away.

Mark J Culross
KD5RXT
 
I ran your program here on a Teensy 4.1 with Audio shield.

This is the waveform my oscilloscope sees on the line out pin after it's been running for several minutes.

1737552354116.png
 
Thanks Mark - I tried it with headphones and it's still distorted, but it's much quieter.
If I'm going to use a mini speaker in this project, how should I wire it up? Normally what I do is take it apart and solder the input wires directly onto the audio adapter where the headphone jack is
 
Hey Paul - I think that waveform is correct, a sine wave with an envelope with 0.25 sustain, have I misunderstood something?
 
Thanks Mark - I tried it with headphones and it's still distorted, but it's much quieter.
If I'm going to use a mini speaker in this project, how should I wire it up? Normally what I do is take it apart and solder the input wires directly onto the audio adapter where the headphone jack is
@maggot: Your best bet is to connect your mini-speaker to the LINE OUT interface. If the signal level is not high enough to drive your speaker sufficiently, then try adding an inline amplifier. Adafruit has a selection of audio amplifiers <here>. Sparkfun also has a selection of audio amplifiers <here>. Which one is best for your application can only be determined by you, taking into account your specific needs.

Please feel free to ask any further questions . . .

Mark J Culross
KD5RXT
 
The line output voltage is usually plenty for ordinary computer speakers (which have an amplifier built inside).

But if you need more voltage, you can use the SGTL5000 lingOutLevel() function. It's documented here (right side panel).


However, the line out is never meant to directly drive a low impedance speaker or headphone. Even with more voltage, it isn't capable of much current. Line out is really only meant to connect to an amplifier or other audio equipment which takes a line input.
 
Thanks for your help - unfortunately line out is still distorted. Less distorted than the audio jack, but it sounds like a loud square wave rather than a sine wave. At this point, do I just have a faulty audio adapter?
 
Back
Top