Hi Paul:
Thanks so much for the clarification; I appreciate your help. I hope you can take a few minutes to clear up two more quick questions regarding what I hope to accomplish.
My plan is to transpose the approach/sketch you show in Part_2_03_Samples of your Audio tutorial to my needs. I'd convert my two sound effects (maybe 5 seconds each) to sketch objects (using wav2sketch) to play them from memory through the DAC depending on which button is being triggered (sound plays while button is pressed, stops when released).
Two questions:
1. On the DAC object in the Audio Design Tool I'm seeing:
analogReference(ref);
Configure output voltage range:
INTERNAL selects 1.2 volt peak-to-peak output.
EXTERNAL selects 3.3 volt peak-to-peak output.
Is this something I need to worry about?
2. If I hold down my button for 15 seconds will this Part_2_03_Samples sketch I'm adapting loop my 5-second sound effect as long as the button is pressed, or will I get silence after the effect has played once? I want the sound effect to play and loop for as long as I'm triggering the button.
Best.
Shawn
// 3: Analog DAC - Connect the DAC pin to an amplified speaker
// http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
//
// To configure the output type, first uncomment one of the three
// output objects. If not using the audio shield, comment out
// the sgtl5000_1 lines in setup(), so it does not wait forever
// trying to configure the SGTL5000 codec chip.
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>
#include <AudioSamplePke_fast_loop_22k.h>
#include <AudioSamplePke_slow_loop_22k.h>
// WAV files converted to code by wav2sketch
//#include "AudioSampleSnare.h" // http://www.freesound.org/people/KEVOY/sounds/82583/
//#include "AudioSampleTomtom.h" // http://www.freesound.org/people/zgump/sounds/86334/
//#include "AudioSampleHihat.h" // http://www.freesound.org/people/mhc/sounds/102790/
//#include "AudioSampleKick.h" // http://www.freesound.org/people/DWSD/sounds/171104/
//#include "AudioSampleGong.h" // http://www.freesound.org/people/juskiddink/sounds/86773/
//#include "AudioSampleCashregister.h" // http://www.freesound.org/people/kiddpark/sounds/201159/
// GUItool: begin automatically generated code
AudioPlayMemory playMem1; //xy=349,634
AudioPlayMemory playMem2; //xy=349,701
AudioMixer4 mixer1; //xy=625,691
AudioOutputAnalog dac1; //xy=841,722
AudioConnection patchCord1(playMem1, 0, mixer1, 0);
AudioConnection patchCord2(playMem2, 0, mixer1, 1);
AudioConnection patchCord3(mixer1, dac1);
// GUItool: end automatically generated code
// Create the Audio components. These should be created in the
// order data flows, inputs/sources -> processing -> outputs
//
//AudioPlayMemory sound0;
//AudioPlayMemory sound1; // six memory players, so we can play
//AudioPlayMemory sound2; // all six sounds simultaneously
//AudioPlayMemory sound3;
//AudioPlayMemory sound4;
//AudioPlayMemory sound5;
//AudioMixer4 mix1; // two 4-channel mixers are needed in
//AudioMixer4 mix2; // tandem to combine 6 audio sources
//AudioOutputI2S headphones;
//AudioOutputAnalog dac; // play to both I2S audio board and on-chip DAC
// Create Audio connections between the components
//
//AudioConnection c1(sound0, 0, mix1, 0);
//AudioConnection c2(sound1, 0, mix1, 1);
//AudioConnection c3(sound2, 0, mix1, 2);
//AudioConnection c4(sound3, 0, mix1, 3);
//AudioConnection c5(mix1, 0, mix2, 0); // output of mix1 into 1st input on mix2
//AudioConnection c6(sound4, 0, mix2, 1);
//AudioConnection c7(sound5, 0, mix2, 2);
//AudioConnection c8(mix2, 0, headphones, 0);
//AudioConnection c9(mix2, 0, headphones, 1);
//AudioConnection c10(mix2, 0, dac, 0);
// Create an object to control the audio shield.
//
//AudioControlSGTL5000 audioShield;
// Bounce objects to read six pushbuttons (pins 0-5)
//
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5); // 5 ms debounce time
//Bounce button2 = Bounce(2, 5);
//Bounce button3 = Bounce(3, 5);
//Bounce button4 = Bounce(4, 5);
//Bounce button5 = Bounce(5, 5);
void setup() {
// Configure the pushbutton pins for pullups.
// Each button should connect from the pin to GND.
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
//pinMode(2, INPUT_PULLUP);
//pinMode(3, INPUT_PULLUP);
//pinMode(4, INPUT_PULLUP);
//pinMode(5, INPUT_PULLUP);
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(10);
// turn on the output
//audioShield.enable();
//audioShield.volume(0.5);
// by default the Teensy 3.1 DAC uses 3.3Vp-p output
// if your 3.3V power has noise, switching to the
// internal 1.2V reference can give you a clean signal
//dac.analogReference(INTERNAL);
// reduce the gain on mixer channels, so more than 1
// sound can play simultaneously without clipping
mixer1.gain(0, 2);
mixer1.gain(1, 2);
//mix1.gain(2, 1);
//mix1.gain(3, 1);
//mix2.gain(1, 1);
//mix2.gain(2, 1);
}
void loop() {
// Update all the button objects
button0.update();
button1.update();
//button2.update();
//button3.update();
//button4.update();
//button5.update();
// When the buttons are pressed, just start a sound playing.
// The audio library will play each sound through the mixers
// so any combination can play simultaneously.
//
if (button0.fallingEdge()) {
playMem1.play(AudioSamplePke_fast_loop_22k);
}
if (button1.fallingEdge()) {
playMem2.play(AudioSamplePke_slow_loop_22k);
}
}
Simply put, as long as button 1 is pressed sound effect 1 plays. If button 1 is held down longer than 5 seconds sound effect 1 loops (or restarts once it reaches the end of the clip). If button 1 is released the sound effect stops playing. Same deal for button 2/sound effect 2. There should not be a circumstance where both buttons are pressed at the same time.
Ok, great,
if not somebody else is faster, I'll help you tomorrow evening - it's now 23.15 here, a bit too late
And i just found some Barcardi, Coke, & Ice in the fridge...
void loop() {
// Update all the button objects
button0.update();
button1.update();
if (button0.read() == 0) { //Button 0 pressed:
playMem2.stop(); //Stop playing sound 2
if (!playMem1.isPlaying()) { //If not playing sound 1,
playMem1.play(AudioSamplePke_fast_loop_22k); //Start sound 1
}
} else { //Button 0 NOT pressed:
playMem1.stop(); //Stop playing sound 1
if (button1.read() == 0) { //Button 1 pressed:
if (!playMem2.isPlaying()) { //if not playing sound 2
playMem2.play(AudioSamplePke_slow_loop_22k); //start sound 2
}
} else {
playMem2.stop(); //Button 1 not pressed -> stop sound 2
}
}
}
Like this ?
Code:void loop() { // Update all the button objects button0.update(); button1.update(); if (button0.read() == 0) { //Button 0 pressed: playMem2.stop(); //Stop playing sound 2 if (!playMem1.isPlaying()) { //If not playing sound 1, playMem1.play(AudioSamplePke_fast_loop_22k); //Start sound 1 } } else { //Button 0 NOT pressed: playMem1.stop(); //Stop playing sound 1 if (button1.read() == 0) { //Button 1 pressed: if (!playMem2.isPlaying()) { //if not playing sound 2 playMem2.play(AudioSamplePke_slow_loop_22k); //start sound 2 } } else { playMem2.stop(); //Button 1 not pressed -> stop sound 2 } } }
It was fun
I hope it does what you want.
Frank.
Hi Shawn,
some minutes, maybe ten incl. a short test?
Wow, computer animation is very interesting, but I do not know much about it. I'm only playing with the displays on the Teensy. And in the 90's I wrote some "graphic demos" for the C64. My first computer was a ZX81 - I got it from my parents to my 13th birthday.Later, I had the chance work with one of these little thingies: https://en.wikipedia.org/wiki/CDC_Cyber and i learned some other programming languages. Today my main job are databases and web..