Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

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

  1. #1
    Senior Member
    Join Date
    Oct 2013
    Posts
    116

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

    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!

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,078
    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.

  3. #3
    Senior Member
    Join Date
    Oct 2013
    Posts
    116
    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/inde...oSynthWaveform 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.

  4. #4
    Senior Member
    Join Date
    Oct 2013
    Posts
    116
    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;
        }
      }
    }

  5. #5
    Senior Member
    Join Date
    Oct 2013
    Posts
    116
    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.

  6. #6
    Senior Member
    Join Date
    Feb 2017
    Posts
    413
    Quote Originally Posted by tetsuo View Post
    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.

  7. #7
    Senior Member
    Join Date
    Oct 2013
    Posts
    116
    Quote Originally Posted by gfvalvo View Post
    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!

  8. #8
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,906
    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 by Frank B; 02-14-2020 at 12:07 PM.

  9. #9
    Senior Member
    Join Date
    Oct 2013
    Posts
    116
    Quote Originally Posted by Frank B View Post
    Indeed, some time ago I saw a BASIC for Teensy.

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




    *could not resisit.
    ALWP... I guess I deserve that for inappropriate use of CAPS!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •