💾 Archived View for freeside.wntrmute.net › music › teensyaudio.gmi captured on 2023-07-10 at 13:21:02. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

 _____                            _             _ _       
|_   _|__  ___ _ __  ___ _   _   / \  _   _  __| (_) ___  
  | |/ _ \/ _ \ '_ \/ __| | | | / _ \| | | |/ _` | |/ _ \ 
  | |  __/  __/ | | \__ \ |_| |/ ___ \ |_| | (_| | | (_) |
  |_|\___|\___|_| |_|___/\__, /_/   \_\__,_|\__,_|_|\___/ 
                         |___/                            

The TeensyAudio library was pretty painful to get started with, so I figured I would write up some notes about getting started with it. The GUI tool is neat and will probably be helpful but it doesn't give you a complete, ready to run bit of code. This is mostly about the code you need to get started and some tips for how to get sound out.

I'm using PlatformIO to do as much of this as I can, and while I'm using a bunch of platforms, I wanted to get started with the TeensyAudio shield.

Your platformio.ini doesn't need any changes to work, as far as I can tell; I *think* you can get by with just using the SD library, for example.

A bare minimum main.cc that plays a single sine tone:

#include <Arduino.h>
#include <Audio.h>

AudioSynthWaveformSine   sine1;
AudioOutputI2S 	         headphones;
AudioConnection          patchCord1(sine1, headphones);
AudioControlSGTL5000	 audioShield;


void
setup()
{
	AudioMemory(2);

	sine1.amplitude(0.5);
	sine1.frequency(4500);
	audioShield.enable();
	audioShield.volume(0.5);
}


void
loop()
{
}

The big thing for me to figure out was the appropriate output to get sound out of the headphone jack; I was trying to use the adc output. The right output to use for the TeensyAudio headphone jack is the I2S output. For the NeoTrellis, it's AudioOutputAnalogStereo. Unless you're doing MIDI.

In the setup, you should allocate AudioMemory, which seems like kind of a black art in which you basically guess how much memory you need, then at some point call AudioMemoryUsageMax to figure out how much you're actually using. The audio memory is blocks of 128 samples, which is about 2.9ms of sound. This example is trivial, so you could probably get away with 1 block. One of the NeoTrellis synth examples I was playing with allocates 120 blocks. I'm not sure what the limits are.

TeensyAudio page on AudioMemory

Another thing not to forget to do in the setup is enable the audioshield and turn up the volume.

The AudioConnection bit describes how the audio flows between different audio objects. You can think of it as a virtual patch cable.

This should be enough to get up and running.