Library to play tones and melodies over the DAC.
Dependents: 1620_Project_Template 1620_App_Board_DAC 1620_App_Board_DAC_Melody 1620_Project_Template ... more
Revision 1:f17240b3e85e, committed 2018-04-20
- Comitter:
- eencae
- Date:
- Fri Apr 20 13:59:18 2018 +0000
- Parent:
- 0:836cdf67dbdb
- Commit message:
- Added set_bpm method.
Changed in this revision
Tone.cpp | Show annotated file Show diff for this revision Revisions of this file |
Tone.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/Tone.cpp Wed Mar 08 17:15:42 2017 +0000 +++ b/Tone.cpp Fri Apr 20 13:59:18 2018 +0000 @@ -30,15 +30,21 @@ } +void Tone::set_bpm(float bpm) +{ + _bpm = bpm; +} + + void Tone::play(float frequency,float duration) { // calculate time step between samples float dt = 1.0/(frequency*_n); // start from beginning of LUT _sample = 0; - + // setup ticker and timeout to stop ticker - + // the ticker repeats every dt to plat each sample in turn ticker->attach(callback(this, &Tone::ticker_isr), dt); // the timeout stops the ticker after the required duration @@ -51,9 +57,9 @@ _bpm = bpm; _notes = notes; // pointer for array _durations = durations; // pointer for array - _melody_length = length; - _repeat = repeat; - + _melody_length = length; + _repeat = repeat; + _note = 0; // start from first note play_next_note(); // play the next note in the melody @@ -63,17 +69,17 @@ void Tone::play_next_note() { // _note is the note index to play - + // calculate the duration and frequency of the note float duration = 60.0/(_bpm*_durations[_note]); float frequency = float(_notes[_note]); //printf("[%i] f = %f d = %f\n",_note,frequency,duration); - + // check if the note is not a space and if not then play the note if (frequency > 0) { - play(frequency,duration); + play(frequency,duration); } - + // the timeout goes to the next note in the melody // double the duration to leave a bit of a gap in between notes to be better // able to distinguish them @@ -84,10 +90,10 @@ void Tone::note_timeout_isr() { _note++; // go onto next note - + // if in repeat mode then reset the note counter when get to end of melody if (_repeat && _note == _melody_length) { - _note=0; + _note=0; } // check if note is within the melody
--- a/Tone.h Wed Mar 08 17:15:42 2017 +0000 +++ b/Tone.h Fri Apr 20 13:59:18 2018 +0000 @@ -21,6 +21,9 @@ // play array of notes and duration, must send length and beats per minute void play_melody(int length,const int *notes,const int *durations,float bpm,bool repeat); + + // change the BPM + void set_bpm(float bpm); private: