Audio library sine output duration

Status
Not open for further replies.

pictographer

Well-known member
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.
 
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:
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.
 
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);
}
 
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);
}
 
Neat! I'm really enjoying the Audio System Design Tool http://www.pjrc.com/teensy/gui/. This

Screen Shot 2014-10-10 at 11.20.12 PM.png

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() {}
 
Status
Not open for further replies.
Back
Top