WeekendWarrior1 / XTronical_XT_DAC_Audio_Mirror

Mirror of XTronical's excellent XT_DAC_Audio library for ease of integration into platformio projects
GNU General Public License v3.0
10 stars 18 forks source link

How to play sounds once, with no repeat? #2

Closed bigplik closed 3 years ago

bigplik commented 3 years ago

Hi is there any keyword or command list to this library? I am trying to figure out how to use music samples once, and almost all codes from examples makes some loop and repeat sounds. Also if I add few sounds and want to play them when I select them (eg. after button press) and they play all the time without stop.

Command list would help a lot, or some explanation.

WeekendWarrior1 commented 3 years ago

A simple modification of this example: https://github.com/WeekendWarrior1/XTronical_XT_DAC_Audio_Mirror/blob/master/PlayWav/PlayWav.ino, which plays the demo sound on a button press on pin 13:

// Playing a digital WAV recording repeatadly using the XTronical DAC Audio library
// prints out to the serial monitor numbers counting up showing that the sound plays 
// independently of the main loop
// See www.xtronical.com for write ups on sound, the hardware required and how to make
// the wav files and include them in your code

#include "SoundData.h"
#include "XT_DAC_Audio.h"

XT_Wav_Class ForceWithYou(Force);     // create an object of type XT_Wav_Class that is used by 
                                      // the dac audio class (below), passing wav data as parameter.

XT_DAC_Audio_Class DacAudio(25,0);    // Create the main player class object. 
                                      // Use GPIO 25, one of the 2 DAC pins and timer 0

uint32_t DemoCounter=0;               // Just a counter to use in the serial monitor
                                      // not essential to playing the sound

void setup() {
  Serial.begin(115200);               // Not needed for sound, just to demo printing to the serial
                                      // Monitor whilst the sound plays, ensure your serial monitor
                                      // speed is set to this speed also.

  pinMode(13, INPUT);              // sets the digital pin 13 as input
}

void loop() {
  DacAudio.FillBuffer();                // Fill the sound buffer with data
  if(ForceWithYou.Playing==false && digitalRead(13)) {       // if not playing, and button 13 pressed
    DacAudio.Play(&ForceWithYou);    // play it
  }
  Serial.println(DemoCounter++);        // Showing that the sound will play as well as your code running here.
}