Tuesday, March 14, 2017

Teensy 3.6 Basics: Getting Started with the Audio System Design Tool

Note for Teensy 4.1: 

The examples shown in this post will work with Teensy 4.1, with the following changes: 

1) Instead of using the dac1 object in the design tool, use mqs1 instead: 


2) Instead of using the DAC0 pin as the signal output, use the MQSR for right and MQSL for left signal output instead


3) Ground from the Teensy 4.1 will still need to be connected to ground of the sound card


Overview

The Teensy 3.x is able to use an extensive Audio library for synthesis, playback, mixing, recording, and affecting audio signals as well as interfacing with relevant external chips. The Audio library is included in the Teensyduino software installation.

The infrastructure of using the Audio library as part of Arduino code can be created, edited and viewed visually using the Audio System Design Tool.

Objects such as inputs, outputs, mixers, sample players and synthesiser can be interconnected visually as a system. This visual representation can then be exported as Arduino code, which can then be used as part of a larger Arduino sketch.



Hardware Setup
The audio output signal created by the Audio library can be sent out of the Teensy in a number of ways. The most straightforward method is to use the onboard digital to analog converters (DACs). The onboard DACs are 12-bit. Sound is computed as 16-bit, 44.1KHz audio.

In this example, only one channel of audio is generated. As such, only the first audio output, DAC0, is used. The tip of an audio plug is connected to DAC0. The sleeve of an audio plug is connected to ground. The audio plug is connected to a mixer, sound card or powered speakers.







Software Setup
In general, to use the Audio System Design Tool in conjunction with the Audio library, the following steps are taken:

  • Create a visual patch using the Audio System Design Tool
  • Click on an object to view the inputs and outputs in terms of designation
  • Click on an object to view which functions are available (i.e. which parameters can be changed using Arduino code) 
  • Take note of which objects (by name) are connecting to which objects by double clicking on an object. Multiple instances of the same type of objects will automatically have unique names with a suffixed number
  • Click on the 'Export' button and copy the resulting code. Paste this code into a new Arduino sketch at the top
  • Write Arduino code that interacts with and references the objects by name and the functions of those objects to control the behaviour of each object

The Audio System Design Tool will generate the infrastructure that has been created visually. Importantly, it will not create any methods for controlling or interfacing with the Audio library and objects. 

For example, an Audio System Design Tool patch may include a synthesiser connected to an audio output, but it cannot include any instructions to tell the synthesiser when to play (i.e. note on and note off messages) or how to play (i.e. settings related to frequency, amplitude, timbre etc). Any control instructions or changes must be written as Arduino code. 



Example 1 - Noise Generator 
In this example, the aim is to hear white noise from the output. A noise generator is connected to an output. Dynamic memory is allocated to be used exclusively by the Audio library. The volume of the noise generator is set to 100%










Example 2 - Drum Synth
In this example, the aim is to set up and trigger a drum synth every second. A drum synth is connected to an output. Dynamic memory is allocated to be used exclusively by the Audio library. The frequency of the drum synth is set to 550 Hz. The length of the drum synth is set to 400 ms. A second frequency that is a fifth above the fundamental is added with a mix of 100%. The drum is set so that the frequency neither bends up or down. The drum is then triggered every second. 












Example 3 - Drum Synth Triggered via Button
In this example, the aim is to set up and trigger a drum synth whenever a button is pressed. This can be achieved by combining the Bounce2 library and the Audio library. A button is added to the hardware setup, as shown below. A drum synth is connected to an output. Dynamic memory is allocated to be used exclusively by the Audio library. The frequency of the drum synth is set to 550 Hz. The length of the drum synth is set to 400 ms. A second frequency that is a fifth above the fundamental is added with a mix of 100%. The drum is set so that the frequency neither bends up or down. The drum is then triggered whenever the button is pressed. 


















Summary
The Audio library for the Teensy 3.x microcontroller allows for extensive processing and generation. The Audio System Design Tool can be used to create patches for the Audio library. Arduino code is then used to interact with the Audio library. 

0 comments: