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

Thread: Audio library sine output duration

  1. #1
    Senior Member pictographer's Avatar
    Join Date
    May 2013
    Location
    San Jose, CA
    Posts
    671

    Audio library sine output duration

    The code below produces a short beep.

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    
    
    // GUItool: begin automatically generated code
    AudioSynthWaveformSine   sine1;          //xy=286,264
    AudioOutputAnalog        dac1;           //xy=478,256
    AudioConnection          patchCord1(sine1, dac1);
    // GUItool: end automatically generated code
    
    
    void setup() {
      sine1.amplitude(0.5);
      sine1.frequency(440);
    }
    
    
    void loop() {
    
    
    }
    How do I control the duration of the beep and make the beep repeat? Hardware is a pair of earbuds wired to A14 and Gnd.

  2. #2
    Senior Member
    Join Date
    Feb 2013
    Posts
    563
    wouldn't it produce a continuous tone?

    anyways, there's an envelope class: effect_envelope

    that'll be one way of going about it (ie controlling the duration). a somewhat more complex use case can be found in the 'PlaySynthMusic' example.
    Last edited by mxxx; 10-09-2014 at 07:06 AM. Reason: spelling

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,497
    It should produce a continuous, never ending tone.

    The DAC pin is not designed to drive headphones. You need an amplifier chip.

    It can drive the input to most (amplified) computer speakers. Those are the easiest way to test, before you go to the trouble of connecting a headphone amp chip. To use computer speakers, a single 10 uF capacitor is recommended, with the + side on A14 and the - side on the speaker input. Of course, connect the grounds together.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,497
    Oh, I see what's wrong. You're missing AudioMemory().

    Try it like this:

    Code:
    void setup() {
      AudioMemory(8);
      sine1.amplitude(0.5);
      sine1.frequency(440);
    }

  5. #5
    Senior Member pictographer's Avatar
    Join Date
    May 2013
    Location
    San Jose, CA
    Posts
    671
    Thanks, Paul! Thanks, mxxx!

    Indeed Paul's modification gets us a continuous tone, with one little mystery: when the AudioOutputAnalog object is constructed there's a brief beep. This was the sound I was hearing originally. At first I thought this was because I omitted AudioNoInterrupts()/AudioInterrupts(), but that's not it.

    And to complete my original question, one head smackingly obvious way to control when the sound is on and off is to use the AudioSynthWavefromSine's amplitude() function to set the amplitude to 0.0. Maybe an envelop filter is better?

    Here's a beeper example. Sounds like a telephone fast busy.

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    
    
    // GUItool: begin automatically generated code
    AudioSynthWaveformSine   sine1;          //xy=286,264
    AudioOutputAnalog        dac1;           //xy=478,256
    AudioConnection          patchCord1(sine1, dac1);
    // GUItool: end automatically generated code
    
    
    void setup() {
      AudioNoInterrupts();
      AudioMemory(8);
      sine1.amplitude(0.2);
      sine1.frequency(440);
      AudioInterrupts();
    }
    
    
    void loop() {
      sine1.amplitude(0.0);
      delay(100);
      sine1.amplitude(0.3);
      delay(100);
    }

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,497
    You could probably get something indistinguishable from a telephone busy signal if you use the info from this page:

    http://nemesis.lonestar.org/referenc...ling/busy.html

    For the dual-tone countries like the USA, use 2 sine waves and a mixer.

  7. #7
    Senior Member pictographer's Avatar
    Join Date
    May 2013
    Location
    San Jose, CA
    Posts
    671
    Neat! I'm really enjoying the Audio System Design Tool http://www.pjrc.com/teensy/gui/. This

    Click image for larger version. 

Name:	Screen Shot 2014-10-10 at 11.20.12 PM.png 
Views:	110 
Size:	13.6 KB 
ID:	2772

    generates the initialization code below. For complex applications, writing it by hand would be tedious.

    Code:
    // Synthesize a US busy signal and output it on Teensy 3.1 pin A14/DAC. See
    // http://nemesis.lonestar.org/reference/telecom/signaling/busy.html
    // This example code is public domain.
    
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    
    
    // GUItool: begin automatically generated code
    AudioSynthWaveformSine   sine1;          //xy=179,301
    AudioSynthWaveformSine   sine2;          //xy=180,363
    AudioMixer4              mixer1;         //xy=346,293
    AudioOutputAnalog        dac1;           //xy=504,301
    AudioConnection          patchCord1(sine1, 0, mixer1, 0);
    AudioConnection          patchCord2(sine2, 0, mixer1, 1);
    AudioConnection          patchCord3(mixer1, dac1);
    // GUItool: end automatically generated code
    
    
    IntervalTimer tick500ms;
    
    
    void setup() {
      AudioNoInterrupts();
      AudioMemory(8);
      sine1.frequency(480);
      sine1.amplitude(0.0);
      sine2.frequency(620);
      sine2.amplitude(0.0);
      tick500ms.begin(doToggleBeep, 500000);
      AudioInterrupts();
    }
    
    
    void doToggleBeep() {
      static int state(0);
      if (state) {
        sine1.amplitude(0.14);
        sine2.amplitude(0.14);
        state = 0;
      } else {
        sine1.amplitude(0.0);
        sine2.amplitude(0.0);
        state = 1;
      }
    }
    
    
    void loop() {}

Posting Permissions

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