Sunday, October 23, 2016

Teensy Audio Board - First Audio

After having good success with the speed of the Teensy products for doing audio-relevant operations, I've decided that it's now time to start playing with actual audio signals.  To do this, I need some sort of interface for getting analog audio signals into and out of the Teensy.  Luckily, Teensy makes an audio board just for this purpose.  Today, I'm going to give the Teensy Audio Board a try.


Buying The Parts:  The first step of a new project is always fun: buying the parts.  I go to Adafruit or Sparkfun, I see what they have in stock, and I order all the stuff that I need.  This is the part of the project where the vision and dream fills one with excitement, without being tempered by any of the actual hardware and frustration that comes with building and trouble-shooting.  And if that wasn't good enough, in a few days, you get a box full of shiny things!


For this project, I used a Teensy 3.2, a Teensy Audio Board, some 14-pin female headers for the Audio Board, a 10K pot to fit into the "volume knob" spot on the Audio Board, and a 3.5 mm stereo audio jack.

Assembling the Audio Board:  This part of the project was pretty straight-forward.  Simply solder on the female headers, solder on the 10K pot, and solder some wires between the "Line-In" holes on the Audio Board and the 3.mm audio jack..  As you can see in the photo below, I could have done a better job with the audio jack, but it's good enough for this project.  (And thanks to Ray for giving it a nice sturdy plastic base!)


Assemble with the Teensy:  After finishing the assembly of the Audio Board, I turned to the Teensy itself.  All that is needed is to solder on some male pin headers.  Then, the Teensy can be connected to the Audio Board.



Audio Software:  To use the Teensy with the Audio Board, you some software that tells the Teensy how to interact with the Audio Board.  Because audio programming can be tricky, PJRC (the Teensy folks) have written a pretty extensive audio library.  It's pretty impressive.  I chose to use this library for my initial trials.  You can get the library from their GitHub repo (as described here) or, even easier, it was probably already installed on your computer when you first installed the drivers for the Teensy itself (via the "Teensyduino" installer).

Audio System Design Tool:  The hardest part of doing something new is getting started.  Where do I begin?  Well, PJRC knows that this is a hurdle, so they help you get started by providing a web-based GUI for configuring your audio processing.  The idea with this GUI is that it will help you build your software for the Teensy.  The Teensy doesn't know about the Audio Board -- you have to tell it everything.  So, as you can see in the screenshot below, I started by dragging in a module called "sgtl5000".  The SGTL5000 is the audio codec at the heart of the Teensy Audio Board, so by using the module, the Teensy will now know about the Teensy Audio Board.


Line-In Pass-Through:  The next step is to configure the audio path that I'd like.  For this demo, I simply want to pass audio from the Line-In input to the headphone output.  From the Teensy's perspective, the Audio Board will provide these signals to the Teensy over the Teeny's I2S bus.  So, in the GUI, I  dragged in an I2S input module and I dragged in an I2S output module.  Finally, I drew lines connecting the inputs to the outputs (the two lines represent the left and right audio channels).  And that's it!

Exporting to the Arudino IDE:  This GUI is merely a tool to help you get started with configuring your audio processing chain.  It doesn't compose all of the software for me.  Instead, I need to export this audio configuration and get it into the Arduino IDE, where I can complete the programming and put it on the Teensy.  So, I clicked on the GUI's "Export" button, at which point it pops up a window with a bunch of Arduino code ready to be copied-and-pasted into a new Arduino sketch.

The GUI-Provided Arduino Code:  The figure below shows a screen-shot of my completed sketch in the Arduino IDE.  The first part of the sketch is the code provided by the web-based GUI tool.  This code imports the required libraries, instantiates the required audio-related objects, and connects them together per the drawing that I made in the GUI.


Completing the Software:  The second half of the program is the code that I had to write myself.  I had to write the "setup()" and "loop()" functions that are the core of every Arduino sketch.  As you can see in my "setup()" function above, I do a couple of things: (1) I allocate some memory for the audio library to use and (2) I issue a few commands to configure the settings within Audio Board.  I stole all of this code from example programs in the Audio library.  After the "setup()" function, I wrote the "loop()" function.  Based on the example code, I don't need anything here.  Once properly configured, the Audio Library handles everything in the background.  My code is on my GitHub here.

First Audio Testing:  For my first audio test, I simply want to inject an audio signal into the Teensy and record the audio signal that it produces in response.  Since the Teensy was programmed to pass the audio through without any manipulations, the output should be the same as the input.  Let's see!

Setup:  I compiled the code and loaded onto the Teensy.  To generate a test signal, I used my computer.  I used Audacity to make a linear frequency sweep (a "chirp") and played it out of the computer's headphone jack.  I connected the headphone output of my computer to the Line-In jack on the Teensy Audio Board.  I recorded the audio output of the Teensy Audio Board by connecting its headphone output to a Roland R-05 handheld audio recorder that I happen to have.  I hit record on the Roland and I hit play in Audacity and, like MAGIC!, I found that I had audio passing through the Teensy.   Yay!


First Data:  After recording the audio chirp with the Roland, I transferred the audio file over to the PC and opened it in Audacity (available here).  As can be seen in the screen shot below, it does indeed look like a chirp, which is good.  I also see, however, that it is not a pure chirp signal -- I see some unexpected lines in the spectrogram.  To me, these spurious lines look like harmonic distortion and some sort of aliasing.  These undesirable signals are not very loud compared to the primary signal, but they are clearly present.  So, while I'm pleased with my initial success with getting this Teensy-based audio system to work, I also see that there is some work ahead to optimize the audio quality.


Next Steps:  My next steps are to investigate other features of the Audio library (the audio library provides for USB-based audio transfer!?!  That's pretty advanced!) and to try the Audio Board with the Teensy 3.6 instead of the Teensy 3.2.  There's fun times ahead with audio hacking!

Follow-Up:  I made my own Teensy Hearing Aid using a Teensy and a Teensy Audio Board.  It's fun!

3 comments:

  1. How did you solder the audio jack to the Teensy? What connects to what??

    ReplyDelete
    Replies
    1. By "audio jack", I assume that you mean my line-in jack that is shown dangling from the Teensy Audio Board. This jack is attached to the "line-in" holes on the Teensy Audio Board.

      The Teensy Audio Board (https://www.pjrc.com/store/teensy3_audio.html) has four holes labeled "Line In": two are ground ("G"), one is for the left channel ("L"), and one is for the right channel ("R").

      My audio jack has three connections: ground, left, and right. The black wire is ground and it is soldered to one of the "G" holes on the Audio Board. The yellow and red wires are the left and right (I don't remember which is which) and they're soldered into the "L" and "R" holes on the Audio Board.

      I hope that this helps!

      Chip

      Delete
  2. This comment has been removed by the author.

    ReplyDelete