Skip to content

Commit

Permalink
Fixes repeat playback missing notes, adds repeat delay
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex T committed Jan 7, 2018
1 parent b513438 commit 0d2b2ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ piezo.play({

## Repeat

Repeat the melody 4 times:
Repeat the melody 4 times with a 250ms delay between repetitions:

```js
var piezo = require('rpio-rtttl-piezo');

piezo.play({
pwmOutputPin: 32,
repeat: 4
repeat: 4,
repeatDelay: 250
});
```

Expand Down
54 changes: 35 additions & 19 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ RpioRtttlPiezo.prototype.play = function(opts) {
pwmClockDivider: 16,
pwmClockFreq: 1.2e6,
rtttl: 'Default:d=16,o=6,b=200:d,d#,e,f,f#',
repeat: 2,
dutyCycle: 32,
repeat: 1,
dutyCycle: 2,
freqMultiplier: 3,
playbackCounter: 0
repeatDelay: 250,
playbackCounter: 0,
noteCounter: 0
};
opts = objectAssign(defaults, opts);

Expand All @@ -60,28 +62,42 @@ RpioRtttlPiezo.prototype.play = function(opts) {
* Continues playback of previously called play()
*/
RpioRtttlPiezo.prototype.playNextNote = function(playable) {
var noteCounter = playable.playbackCounter%playable.repeat;
if (noteCounter < playable.tune.melody.length) {
var note = playable.tune.melody[noteCounter];
var freq = parseInt(note.frequency * playable.freqMultiplier);

if (freq) {
rpio.pwmSetRange(playable.pwmOutputPin, playable.pwmClockFreq/freq);
rpio.pwmSetData(playable.pwmOutputPin, (playable.pwmClockFreq/freq)/playable.dutyCycle);
var _this = this;
// Are we within melody repeat loop?
if (playable.playbackCounter < playable.repeat) {
// Are we within notes of melody?
if (playable.noteCounter < playable.tune.melody.length) {

// Get next note and calculate desired output frequency
var note = playable.tune.melody[playable.noteCounter];
var freq = parseInt(note.frequency * playable.freqMultiplier);

// Is a sound desired?
if (freq) {
// Set PWM range based on clock frequency, and PWM data based on duty cycle
rpio.pwmSetRange(playable.pwmOutputPin, playable.pwmClockFreq/freq);
rpio.pwmSetData(playable.pwmOutputPin, (playable.pwmClockFreq/freq)/playable.dutyCycle);
} else {
// Silence
rpio.pwmSetData(playable.pwmOutputPin, 0);
}

// Prepare for next note in melody sequence
playable.noteCounter++;
setTimeout(function(){ _this.playNextNote(playable); }, note.duration);
} else {
// End of melody, set silence
rpio.pwmSetData(playable.pwmOutputPin, 0);

// Prepare for playback repeat
playable.noteCounter = 0;
playable.playbackCounter++;
setTimeout(function(){ _this.playNextNote(playable); }, playable.repeatDelay);
}
} else {
// End of playback, set silence
rpio.pwmSetData(playable.pwmOutputPin, 0);
}

playable.playbackCounter++;
if (playable.playbackCounter < playable.tune.melody.length*playable.repeat) {
var _this = this;
setTimeout(function(){ _this.playNextNote(playable); }, note.duration);
} else {
setTimeout(function(){ rpio.pwmSetData(playable.pwmOutputPin, 0); }, note.duration);
}
};

module.exports = new RpioRtttlPiezo();

0 comments on commit 0d2b2ef

Please sign in to comment.