When working with audio, one typically wants as quiet a system as possible -- no one likes listening to hiss. So, when I started working with the Teensy and the Teensy Audio Board, I applied a critical ear to the board's performance. While the Teensy Audio Board was very fun, quite easy to use, and relatively inexpensive, I found that it was noisier than I like. In this post, I'm going to illustrate the noise levels that I recorded and I'll show a workaround that helped remove a lot of that noise.
Setup: For these tests, I used a Teensy 3.6 attached to Teensy Audio Board. I then wrote an Arduino sketch (shared on my GitHub here) that would record audio from the line-in and send it to both the headphone output as well as to the USB Audio output. I recorded the digital audio coming through USB using Audacity (as discussed here). As shown in the picture above, To try to isolate just the self-noise of the system, I shorted the inputs using a jumper across the Teensy Audio Boards line-in terminals. This should be as quiet as it can get.
First Results: The audio that I recorded (shared here) sounded funny to my ear. If I turned it up, I could hear a very annoying unsteady whining noise. Looking at the spectrum (see plot below), I could clearly see some narrowband tones starting around 2 kHz. That's right where one's hearing is most sensitive, which makes this kind of self-noise particularly problematic. It's definitely not what you want if you're trying to make a hearing aid type of device. What could be the cause of this?
Disable the ADC's HP Filter: After a bunch of messing around, I finally stumbled across this post on tthe Teensy forum. Here, "Raj" stated that the SGTL5000 audio codec (which is at the heart of the Teensy Audio Board) has a high-pass filter in its ADC. He said that this HP filter was introducing a lot of noise and he shared the command for disabling that filter. When I put the command "adcHighPassFilterDisable();" into my sketch and re-ran my tests, those annoying narrowband tones in the Teensy Board's self-noise went away (audio file here). Ahhh. That's so much better.
Varying the Gain: The data shown above were taken with the input gain of the Teensy board set to "10" (ie, full-scale input spans 0.56 Vpp). There are other gain settings that one could use. So, I repeated my measurements at other gain levels to see if the input-referred self-noise was better or worse. The results are below.
As can be seen, the noise with the HP filter (the blue line) is noisier in all cases than when the HP filter is disabled (orange line). So, as long as you can handle a little DC offset in your audio data, you should definitely disable that filter.
A second finding is that the apparent (input-referred) self-noise goes down as the gain increases. So, if you have really quiet signals that you're trying to record, turning up the gain might help reveal those signals.
Dynamic Range: The trade-off with turning up the gain is that the maximum signal level that can be recorded without clipping also goes down as the gain is increased. For many applications, therefore, it is just as important to look at the dynamic range for the system. The dynamic range is the difference between the maximum input level and the noise floor. When I run the numbers for the Teensy Audio Board, I get the results below.
Here, I see that, when the HP filter is disabled (the orange line), the Teensy Audio Board has about 81 dB of dynamic range. And, I also see that the dynamic range is basically independent of the gain setting.
Is it Good Enough? So now we're down to the hard question: is 81 dB of dynamic range good enough for my needs? Well, it is a pretty decent number, especially for the price of the Teensy Audio Board. But, for my use as part of an open-source hearing aid, where I need to amplify quiet sounds while not distorting on already-loud sounds, I think that I need a bigger dynamic range. Normal human hearing spans on the order of 120 dB of dynamic range. I think that I need hardware that'll get me at least a little closer to that number.
Motivation for a New Board: The results shown here were my motivation for building the audio interface (the "Tympan") that I introduced in my last post. In my next post, I'll share some details of that audio interface and I'll perform these same self-noise measurements to see if I was able to get a wider dynamic range. Making wide dynamic range systems isn't easy, so it'll be exciting to find out how I did!
Follow-Up: I measured the self-noise and dynamic range of the new Tympan board. It's pretty good! If you're interested, you can see the results here.
Showing posts with label Teensy Audio Board. Show all posts
Showing posts with label Teensy Audio Board. Show all posts
Sunday, March 12, 2017
Tuesday, November 29, 2016
Teensy Audio Board Headphone Level
For my home-brew Teensy Hearing Aid, I'm using the Teensy Audio Board as my audio interface. The heart of the Teensy Audio Board is the Freescale SGTL5000 stereo codec. This codec is very flexible, with lots of virtual knobs for tuning your system to suit your own application. For my hearing aid, it is important for me to understand all settings related to loudness. So, today, I'm going to look at the impact of the headphone volume control.
System Block Diagram: The figure above is taken from the datasheet for the SGTL5000. All of the blocks in yellow are analog gain stages. The headphone volume control is at the end of the chain, on the upper right. It says that the headphone volume can be adjusted from -52 dB to +12 dB.
Teensy Audio Library: Because this SGTL5000 is embedded within the Teensy Audio Board, I control it via the Teensy Audio Library. The library hides many of the details of controlling the SGTL5000. For example, it allows the user to control the headphone volume simply by commanding a value somewhere between 0.0 and 1.0. Presumably, 0.0 maps to the -52 dB setting and 1.0 maps to the +12 dB setting.
Measuring the Response: To see what this means for my system, I wrote a sketch for the Teensy that uses the Teensy Audio Library to continuously generate a nearly full-scale sine wave and to output it via the headphone jack (code is shared here). The volume setting for the headphone output is controlled via potentiometer on the Teensy Audio Board. To see the effect of the headphone volume setting, I measured the RMS voltage produced at the headphone jack using a Fluke handheld digital multi-meter.
Results: The figure above shows the values that I measured. As can be seen, the response in the middle section is linear, which is great. At the low end, my results do deviate from linear, but that's presumably because my multi-meter had difficulty measuring such small signals (-50dBV is only 3.2 mVrms!).
Saturation: The interesting result happens at the high end of my graph where I turn up the volume control to its highest values. Here, my results saturate -- increasing the volume command does not increase the output voltage. The break-over point is at a command of about 0.85. This corresponds to an output signal level of +1.5 dBV, which is 1.2 Vrms, which is 3.4 Vpk-pk. Since the power supply to the SGTL5000 is only 3.3V, it is not surprising that the output saturates when I ask for the output signal to be greater than the power rails.
Best Setting to Use: Saturation will sound bad. It will cause distortion in the audio. To keep my hearing aid sounding good, I want to avoid this saturation. If my full-scale sine wave causes saturation at a volume setting of 0.85, I am going to set my volume to 0.8.
System Block Diagram: The figure above is taken from the datasheet for the SGTL5000. All of the blocks in yellow are analog gain stages. The headphone volume control is at the end of the chain, on the upper right. It says that the headphone volume can be adjusted from -52 dB to +12 dB.
Teensy Audio Library: Because this SGTL5000 is embedded within the Teensy Audio Board, I control it via the Teensy Audio Library. The library hides many of the details of controlling the SGTL5000. For example, it allows the user to control the headphone volume simply by commanding a value somewhere between 0.0 and 1.0. Presumably, 0.0 maps to the -52 dB setting and 1.0 maps to the +12 dB setting.
Measuring the Response: To see what this means for my system, I wrote a sketch for the Teensy that uses the Teensy Audio Library to continuously generate a nearly full-scale sine wave and to output it via the headphone jack (code is shared here). The volume setting for the headphone output is controlled via potentiometer on the Teensy Audio Board. To see the effect of the headphone volume setting, I measured the RMS voltage produced at the headphone jack using a Fluke handheld digital multi-meter.
Results: The figure above shows the values that I measured. As can be seen, the response in the middle section is linear, which is great. At the low end, my results do deviate from linear, but that's presumably because my multi-meter had difficulty measuring such small signals (-50dBV is only 3.2 mVrms!).
Saturation: The interesting result happens at the high end of my graph where I turn up the volume control to its highest values. Here, my results saturate -- increasing the volume command does not increase the output voltage. The break-over point is at a command of about 0.85. This corresponds to an output signal level of +1.5 dBV, which is 1.2 Vrms, which is 3.4 Vpk-pk. Since the power supply to the SGTL5000 is only 3.3V, it is not surprising that the output saturates when I ask for the output signal to be greater than the power rails.
Best Setting to Use: Saturation will sound bad. It will cause distortion in the audio. To keep my hearing aid sounding good, I want to avoid this saturation. If my full-scale sine wave causes saturation at a volume setting of 0.85, I am going to set my volume to 0.8.
Sunday, November 20, 2016
A Teensy Hearing Aid
Hearing aids are totally closed devices -- their inner workings are hidden. Access is limited to only those who work for the hearing aid companies. But if innovation is to accelerate, we need more ideas iterated more quickly. We need more people to participate in hearing aid development. But if the devices are closed, there is no way to try new ideas. So, let's consider the alternative. Let's try to build an open-source hearing aid. Yes, at first, an open-source hearing aid will be absurdly big and ugly. But, you have to start somewhere. I'm going to start here: take one Teensy microcontroller, add some supporting electronics, and VOILA! A Teensy Hearing Aid!
Basic Hearing Aid Elements: While it is unfortunate that they are closed devices, real hearing aids are absolutely amazing pieces of technology. Their minuscule packages are absolutely stuffed with functionality. I, though, am going to start more simply. I will start with some microphones, analog and digital converts, a digital audio processor, some speakers, and a battery. Once this works, I can always add more features later.
Choosing a Processor: For me, the biggest challenge is always with the software. Therefore, I need to choose a digital audio processor that it is easy to program. For me, that means choosing a processor that can be programmed from the hobbyist-friendly Arduino programming environment. Within that universe of processors, I've chosen to use the Teensy 3.6 because it's fast and because it has a nice audio processing library to make it even easier to program for audio.
Supporting Elements: To get audio signals into and out of the processor, the Teensy folks offer an inexpensive Audio Adapter Board that mates directly to the Teensy 3.6. It has the audio ADC and DAC that I need. For the microphones, I'm using a pair of mic breakout boards from Adafruit. For speakers, I'm using whatever headphones or earbuds that I might have on-hand. Finally, for the battery, I'm using a Li-Po battery and Li-Po charger from Adafruit.
Wiring It Up: The figure above gives an overview of how everything was connected together. As you can see, the Audio Board is at the center with everything else connecting to it. The only tricky part of this setup is connecting the battery and charger into the Teensy system. As indicated on the Teensy pinout diagram, you have to cut a trace on the backside of the Teensy so that you can insert the battery connections. Once cut, the Teensy's 5V USB voltage goes to the battery charger and the battery's output goes back to the Teensy's "5V" input pin. Not too bad.
Initial Software: While this post is primarily about the hardware, I did write some basic software in order to see if the hardware is working (see my GitHub repo). I used the Teensy Audio Library to configure the Audio Board to pass the "I2S" inputs (ie, my microphones) to the "I2S" outputs (ie, my headphones). In between, I wrote a simple routine that applies a user-controllable amount of gain to make things louder. I use the blue potentiometer on the Audio Board to set the amount of gain.
First Audio: Of course, the first time that I tried to compile the code, my software didn't work. Does anyone's code ever work the first time? After some iteration, I finally got it to compile and upload. Putting on my headphones, I could hear the audio being picked up by the microphones. Turning the blue pot, I could control its volume. Hardware knobs are so satisfying. My favorite part, though, is being able to use the on-board battery so that I can move around freely. Very fun.
Limitations: Sure, my home-brewed "hearing aid" is ridiculously large -- no one (including me) would ever wear this around in everyday life. But, unlike commercially-available hearing aids, my device is open. Anyone can modify it to make it better. Anyone can try out their own audio processing algorithms to try to improve one's hearing. Will any of us beat the professional hearing aid algorithm designers? Probably not. But, for me at least, I will surely learn a lot by trying a few of the standard approaches. And, I'll get to look really cool sharing pictures of myself with this (not so) Teensy Hearing Aid. Happy hacking!
Follow-Up: I measured the power consumption of my Teensy Hearing Aid. Using the "WFI" command, I found a super-easy way to increase the battery life by 50%. Wow! See my post here.
Follow-Up: I measured how the volume control affects the headphone ouput. See here.
Basic Hearing Aid Elements: While it is unfortunate that they are closed devices, real hearing aids are absolutely amazing pieces of technology. Their minuscule packages are absolutely stuffed with functionality. I, though, am going to start more simply. I will start with some microphones, analog and digital converts, a digital audio processor, some speakers, and a battery. Once this works, I can always add more features later.
Choosing a Processor: For me, the biggest challenge is always with the software. Therefore, I need to choose a digital audio processor that it is easy to program. For me, that means choosing a processor that can be programmed from the hobbyist-friendly Arduino programming environment. Within that universe of processors, I've chosen to use the Teensy 3.6 because it's fast and because it has a nice audio processing library to make it even easier to program for audio.
Supporting Elements: To get audio signals into and out of the processor, the Teensy folks offer an inexpensive Audio Adapter Board that mates directly to the Teensy 3.6. It has the audio ADC and DAC that I need. For the microphones, I'm using a pair of mic breakout boards from Adafruit. For speakers, I'm using whatever headphones or earbuds that I might have on-hand. Finally, for the battery, I'm using a Li-Po battery and Li-Po charger from Adafruit.
Wiring It Up: The figure above gives an overview of how everything was connected together. As you can see, the Audio Board is at the center with everything else connecting to it. The only tricky part of this setup is connecting the battery and charger into the Teensy system. As indicated on the Teensy pinout diagram, you have to cut a trace on the backside of the Teensy so that you can insert the battery connections. Once cut, the Teensy's 5V USB voltage goes to the battery charger and the battery's output goes back to the Teensy's "5V" input pin. Not too bad.
Initial Software: While this post is primarily about the hardware, I did write some basic software in order to see if the hardware is working (see my GitHub repo). I used the Teensy Audio Library to configure the Audio Board to pass the "I2S" inputs (ie, my microphones) to the "I2S" outputs (ie, my headphones). In between, I wrote a simple routine that applies a user-controllable amount of gain to make things louder. I use the blue potentiometer on the Audio Board to set the amount of gain.
First Audio: Of course, the first time that I tried to compile the code, my software didn't work. Does anyone's code ever work the first time? After some iteration, I finally got it to compile and upload. Putting on my headphones, I could hear the audio being picked up by the microphones. Turning the blue pot, I could control its volume. Hardware knobs are so satisfying. My favorite part, though, is being able to use the on-board battery so that I can move around freely. Very fun.
Limitations: Sure, my home-brewed "hearing aid" is ridiculously large -- no one (including me) would ever wear this around in everyday life. But, unlike commercially-available hearing aids, my device is open. Anyone can modify it to make it better. Anyone can try out their own audio processing algorithms to try to improve one's hearing. Will any of us beat the professional hearing aid algorithm designers? Probably not. But, for me at least, I will surely learn a lot by trying a few of the standard approaches. And, I'll get to look really cool sharing pictures of myself with this (not so) Teensy Hearing Aid. Happy hacking!
Follow-Up: I measured the power consumption of my Teensy Hearing Aid. Using the "WFI" command, I found a super-easy way to increase the battery life by 50%. Wow! See my post here.
Follow-Up: I measured how the volume control affects the headphone ouput. See here.
Tuesday, October 25, 2016
Teensy Audio over USB
In my last post, where I sent my first analog audio through a Teensy, I also discovered that it has the ability to exchange digital audio over its USB link. Whoa. This means that, if I'm developing some snazzy new audio-processing algorithm, I can test it 100% digitally without adding any complications of converting back and forth through analog signals. That is a really powerful capability that'll greatly simplify my audio development. Let's see if it really works!
Discovering USB Audio: I only discovered that the Teensy might be able to do USB Audio while using their web-based graphical tool. This is a tool that the Teensy folks ahve created to help people work with their Audio Library. While using that tool, I noticed that there were input/output blocks named "USB". Does this mean that Teensy can exchange audio over USB?!? Really? That's a pretty advanced feature for an electronics board aimed at hobbyists! Does it work?
A Simple USB Audio Chain: To give it a try, I configured an audio chain as shown below: one stereo USB input connected to one stereo USB output. The left channel is just connected straight across -- no processing. For the right channel, though, I've inserted a Biquad filter block just so that I can confirm that the Teensy is indeed doing something to the audio. It's a pretty simple setup.
It Didn't Work: When I exported the audio chain above, loaded it onto the Teensy, and gave it a try, it didn't work. I got silence. I don't know why. So I started messing around...
Making it Work: To get the USB Audio to work, I found that I had to engage the audio board's hardware in some way. One way to make it work is shown below: add a SGTL5000 block for the Audio Board, and add an I2S block to be the output of the Audio Board. I connected the I2S output to be just like the USB output. Now, whatever the Teensy is sending out via USB will also be present at the headphone jack. Sure, it seems like an unnecessary complication if I just want to use USB for my audio, but it's kinda cool that it's so easy to output to multiple targets simultaneously.
Completing the Arduino Code: The graphical web-based tool shown above doesn't produce a complete program to load onto the Teensy -- it just gets you started. When I hit the "Export" button, it gave me all of the code to setup the hardware and make the connections, but I still needed to write the last bit of code to make it run. As usual, I did my coding in the Arduino IDE because it's easiest.
My Code: The screenshot below shows the Arduino sketch that I used for this demo (it's on my GitHub here). At the top is the big block of code that was exported from the web-based tool. Easy. Then, at the bottom, I added the setup() and loop() functions. As you can see, the setup() function configures the SGTL5000 audio board and it sets the biquad filter to be a low-pass filter at 500 Hz. The loop() function does nothing.
Tell the Compiler about USB Audio! The only trick to using the USB audio is that you need to tell the compiler that you want the Teensy to run in USB Audio mode. So, prior to compiling and uploading this Arduino sketch, go under the "Tools" menu, click on "USB Type" and select "Audio". Notice that there are many other choices, including "Serial", "Keyboard, "MIDI", "Audio" and "No USB". Normally, one uses "Serial" so that Serial.println() is able to print messages to the Serial Monitor. But, for this demo, I want "Audio".
Loading onto the Teensy: Once you change the USB setting to Audio, you can compile and upload to the Teensy. The first time you do it, it'll probably upload to the Teensy without any issue. Great! But, once it is in USB Audio mode, future programs won't appear to upload automatically. Why? Well, once the USB link set for Audio, the Teensy can't get reprogramming commands over that same USB link. What's the solution? After the Arduino IDE has finished compiling, simply press the Teensy's reset button (picture below). After a reset, the Teensy knows to check in with the PC prior to switching over to Audio mode. It will automatically get any new program waiting for it. It's a small extra step, but I didn't find it to be a problem at all.
Testing Using Audacity: Once the Teensy is in USB Audio mode, the Teensy appears to your PC as if it is a soundcard. So, you can now send and receive audio data to it using any audio recording program. For this demo, I used Audacity because it is free.
Configuring Audacity: With the Teensy attached, my computer now has two soundcards: (1) its built-in soundcard and (2) the Teensy. Once Audacity launches, I configured Audacity to use the Teensy as the soundcard instead of the built-in one. The screenshot below shows the five settings that I changed in Audacity. On my computer (Windows 7), the Teensy cryptically appeared as "Digital Audio Interface (2-Tee" [sic], which wasn't as quite helpful as informative as it could have been, but it was good enough for me to choose the right item.
First USB Audio: For this test, I'm want to send audio out over USB (to the Teensy) and record audio coming from USB (from the Teensy). So, I need to start by creating a test signal to send out over USB. Under the "Generate" menu, I chose "Chirp" and made a linear frequency sweep from 100 Hz to 4000 Hz. This appeared as a single mono track in Audacity. That's all the preparation that I needed to do. Now, I simply hit the red record button. Audacity started playing out the chirp audio to the Teensy while recording the audio coming back from the Teensy. The result is shown in the screenshot below.
Success! The screen shot shows two audio files. Both files are shown in "spectrogram" view (60 dB dynamic range). The top file contains the original (mono) frequency sweep generated by Audacity. This is what was sent to the Teensy over the USB Audio link. The bottom file shows the stereo audio recorded from the Teensy. The left channel had no processing being performed by the Teensy and it does indeed appear to be a copy of the original signal. The right channel, though, was being processed by the Teensy by its 500 Hz low-pass biquad filter. As can be seen, the high frequencies are attenuated -- the gentle roll-off is as expected from a biquad filter. This was amazingly easy!
Future Use: While I'm very impressed that this capability exists at all, it's not perfect. In additional testing, I occasionally found hiccups or other artifacts in the audio stream coming back from the Teensy. But, most of the time, this USB Audio link worked really well. It'll be great for debugging my audio projects. Anytime I have any question about the negative effects of my analog components, I can simply switch over to USB Audio to avoid the whole problem. It'll be a great tool to have in my audio hacking toolbox.
Discovering USB Audio: I only discovered that the Teensy might be able to do USB Audio while using their web-based graphical tool. This is a tool that the Teensy folks ahve created to help people work with their Audio Library. While using that tool, I noticed that there were input/output blocks named "USB". Does this mean that Teensy can exchange audio over USB?!? Really? That's a pretty advanced feature for an electronics board aimed at hobbyists! Does it work?
A Simple USB Audio Chain: To give it a try, I configured an audio chain as shown below: one stereo USB input connected to one stereo USB output. The left channel is just connected straight across -- no processing. For the right channel, though, I've inserted a Biquad filter block just so that I can confirm that the Teensy is indeed doing something to the audio. It's a pretty simple setup.
It Didn't Work: When I exported the audio chain above, loaded it onto the Teensy, and gave it a try, it didn't work. I got silence. I don't know why. So I started messing around...
Making it Work: To get the USB Audio to work, I found that I had to engage the audio board's hardware in some way. One way to make it work is shown below: add a SGTL5000 block for the Audio Board, and add an I2S block to be the output of the Audio Board. I connected the I2S output to be just like the USB output. Now, whatever the Teensy is sending out via USB will also be present at the headphone jack. Sure, it seems like an unnecessary complication if I just want to use USB for my audio, but it's kinda cool that it's so easy to output to multiple targets simultaneously.
Completing the Arduino Code: The graphical web-based tool shown above doesn't produce a complete program to load onto the Teensy -- it just gets you started. When I hit the "Export" button, it gave me all of the code to setup the hardware and make the connections, but I still needed to write the last bit of code to make it run. As usual, I did my coding in the Arduino IDE because it's easiest.
My Code: The screenshot below shows the Arduino sketch that I used for this demo (it's on my GitHub here). At the top is the big block of code that was exported from the web-based tool. Easy. Then, at the bottom, I added the setup() and loop() functions. As you can see, the setup() function configures the SGTL5000 audio board and it sets the biquad filter to be a low-pass filter at 500 Hz. The loop() function does nothing.
Tell the Compiler about USB Audio! The only trick to using the USB audio is that you need to tell the compiler that you want the Teensy to run in USB Audio mode. So, prior to compiling and uploading this Arduino sketch, go under the "Tools" menu, click on "USB Type" and select "Audio". Notice that there are many other choices, including "Serial", "Keyboard, "MIDI", "Audio" and "No USB". Normally, one uses "Serial" so that Serial.println() is able to print messages to the Serial Monitor. But, for this demo, I want "Audio".
Loading onto the Teensy: Once you change the USB setting to Audio, you can compile and upload to the Teensy. The first time you do it, it'll probably upload to the Teensy without any issue. Great! But, once it is in USB Audio mode, future programs won't appear to upload automatically. Why? Well, once the USB link set for Audio, the Teensy can't get reprogramming commands over that same USB link. What's the solution? After the Arduino IDE has finished compiling, simply press the Teensy's reset button (picture below). After a reset, the Teensy knows to check in with the PC prior to switching over to Audio mode. It will automatically get any new program waiting for it. It's a small extra step, but I didn't find it to be a problem at all.
Testing Using Audacity: Once the Teensy is in USB Audio mode, the Teensy appears to your PC as if it is a soundcard. So, you can now send and receive audio data to it using any audio recording program. For this demo, I used Audacity because it is free.
Configuring Audacity: With the Teensy attached, my computer now has two soundcards: (1) its built-in soundcard and (2) the Teensy. Once Audacity launches, I configured Audacity to use the Teensy as the soundcard instead of the built-in one. The screenshot below shows the five settings that I changed in Audacity. On my computer (Windows 7), the Teensy cryptically appeared as "Digital Audio Interface (2-Tee" [sic], which wasn't as quite helpful as informative as it could have been, but it was good enough for me to choose the right item.
First USB Audio: For this test, I'm want to send audio out over USB (to the Teensy) and record audio coming from USB (from the Teensy). So, I need to start by creating a test signal to send out over USB. Under the "Generate" menu, I chose "Chirp" and made a linear frequency sweep from 100 Hz to 4000 Hz. This appeared as a single mono track in Audacity. That's all the preparation that I needed to do. Now, I simply hit the red record button. Audacity started playing out the chirp audio to the Teensy while recording the audio coming back from the Teensy. The result is shown in the screenshot below.
Success! The screen shot shows two audio files. Both files are shown in "spectrogram" view (60 dB dynamic range). The top file contains the original (mono) frequency sweep generated by Audacity. This is what was sent to the Teensy over the USB Audio link. The bottom file shows the stereo audio recorded from the Teensy. The left channel had no processing being performed by the Teensy and it does indeed appear to be a copy of the original signal. The right channel, though, was being processed by the Teensy by its 500 Hz low-pass biquad filter. As can be seen, the high frequencies are attenuated -- the gentle roll-off is as expected from a biquad filter. This was amazingly easy!
Future Use: While I'm very impressed that this capability exists at all, it's not perfect. In additional testing, I occasionally found hiccups or other artifacts in the audio stream coming back from the Teensy. But, most of the time, this USB Audio link worked really well. It'll be great for debugging my audio projects. Anytime I have any question about the negative effects of my analog components, I can simply switch over to USB Audio to avoid the whole problem. It'll be a great tool to have in my audio hacking toolbox.
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!
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!
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!
Subscribe to:
Posts (Atom)






















