Seeking BASIC example for AudioSynthWaveform or similar driving Teensy 3.1 DAC pin

Status
Not open for further replies.

tetsuo

Well-known member
I don't need recorded audio (SD card) or even fancy synths. My goal is just to have a reasonably clean output directly from the teensy to generate a tone (sine, square, sawtooth, or triangle) at line output level into an external mono amp driving a subwoofer for the purposes of a classroom experiment. The frequency will be variable between 30Hz-100Hz.

I don't think I need an additional DAC or the audio shield for this and I have 4 Teensy 3.1s sitting around from an abandoned project, so it seemed like AudioSynthWaveform and the DAC pin (A14) are what I wanted, but I can't find a basic hello world for sending such tones out to A14 on the Teensy.

My apologies is this is an overly basic question. I've been away from Teensy tinkering for a few years and this is my first step back in :)

ANY help (either a link to an example or a basic code snippet) would be greatly appreciated. Thanks!
 
If you find any example which uses the audio shield, you can just swap out the I2S output for a DAC output, and delete the SGTL5000 stuff about controlling the shield.

To see how this is done, check out part 2 of the tutorial starting on page 8 of the PDF:

https://www.pjrc.com/store/audio_tutorial_kit.html

For use of oscillators, turn to pages 20-21 of the PDF.

On that tutorial page is a 45 minute full walkthrough video, so if you get stuck on any part, you can watch me & Alysia do it. All the tutorial uses the audio shield with I2S for outputs. But hopefully the tutorial can give you the concept of how you can just select different things for your design and export that into Arduino. If you just draw the same thing but with the DAC output rather than I2S, your sound will be sent to the DAC pin.
 
False alarm! I think I have what I need after playing around with the GUI audio tool. It just seemed like overkill for my very basic needs, but looking at https://www.pjrc.com/teensy/gui/index.html?info=AudioSynthWaveform and putting two elements on the GUI map (dragging a waveform synth and dac) have me enough to get started. The rest should be easy. Sorry for the noise! :)

edit: holy COW that was a fast response Paul! Yours came in while I was typing the above. Thanks for being awesome! Will take a look at that part of the tutorial. Thanks again.
 
Just so that this thread isn't a total waste of space, I'll attach the very basic example I used to scope what was coming out of A14 in case someone else has the same question and is just looking for a copy/paste starting point

Code:
#include <Audio.h>
#include <Wire.h>

// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=418,255
AudioOutputAnalog        dac1;           //xy=582,250
AudioConnection          patchCord1(waveform1, dac1);

// GUItool: end automatically generated code
elapsedMillis sinceFormChange;

// 
//define WAVEFORM_SINE              0
//define WAVEFORM_SAWTOOTH          1
//define WAVEFORM_SQUARE            2
//define WAVEFORM_TRIANGLE          3
//define WAVEFORM_ARBITRARY         4
//define WAVEFORM_PULSE             5
//define WAVEFORM_SAWTOOTH_REVERSE  6
//define WAVEFORM_SAMPLE_HOLD       7
//define WAVEFORM_TRIANGLE_VARIABLE 8

int forms[5] = {WAVEFORM_SINE, WAVEFORM_SAWTOOTH, WAVEFORM_SAWTOOTH_REVERSE, WAVEFORM_TRIANGLE, WAVEFORM_SQUARE};
int currentForm = -1;
void setup() {
  AudioMemory(10);
  sinceFormChange = 2001; // don't wait for the first update
}

void loop() {
  delay(100); // to be replaced with reading pots and switches
  if (sinceFormChange > 2000) {
    // frequency and amplitude have methods for updating after begin(),
    // but there is no such accomodation for type. so call begin() each time the type changes?
    waveform1.begin(forms[++currentForm]);
    waveform1.frequency(100);
    waveform1.amplitude(0.8);
    sinceFormChange = 0;
    if (currentForm >= 4) {
      currentForm = -1;
    }
  }
}
 
One follow-up question... there doesn't appear to be an end() method for AudioSynthWaveForm to correspond to begin() What is the correct method of stopping the output? For now I'm just setting amplitude to 0, but that means I end up saving the last-on amplitude so I can simulate stop/start at a given amplitude.
 
but that means I end up saving the last-on amplitude so I can simulate stop/start at a given amplitude.
That doesn't seem too onerous.

The other option would be run it through an amp block whose gain you change from 1 to 0 to shut down the output.
 
That doesn't seem too onerous.

The other option would be run it through an amp block whose gain you change from 1 to 0 to shut down the output.

:) Not too onerous at all, no. I have a pot controlling gain, and a button toggling on/off, so it feels hacky to have to have the button override the gain reading from the pot. Not really a huge deal - just clunky. I felt like I might be missing something more akin to play/stop.

Thanks for the amp block suggestion though - sounds like good fix!
 
Indeed, some time ago I saw a BASIC for Teensy.

Using the DAC might be something like
Code:
100 POKE dac, value





*could not resist.
 
Last edited:
Status
Not open for further replies.
Back
Top