void Sound::playNote() { if (!this->_enabled) { // Not going to play sound unless explicitly told it's safe to do so. // ALL servos must be detached. return; } // @todo: remove hard coding to the 8 note melody if (_current_note > this->_length) { // Nothing left to play this->disable(); return; } int dur = _noteDurations[_current_note]; int noteDuration = 1000/dur; this->_playing = 1; NewTone(_pin, _melody[_current_note], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; //Serial.print(_melody[_current_note]); //Serial.print(" : "); //Serial.println(pauseBetweenNotes); ++_current_note; _timer.setTimeout(pauseBetweenNotes, sound_caller::stopNote); }
void SoundPlayer::playCoin() { delay(100); NewTone(_speakerPin, NOTE_E6, 250); delay(100); noNewTone(_speakerPin); }
void SoundPlayer::noise(int freq, int var, int duration) { int low = freq - var; int high = freq + var; unsigned long time = millis(); while (millis() - time <= duration) { NewTone(_speakerPin, random(low, high)); } noNewTone(_speakerPin); }
void SoundPlayer::play1UP() { NewTone(_speakerPin, NOTE_E6, 60); //delay(85); NewTone(_speakerPin, NOTE_G6, 60); //delay(85); NewTone(_speakerPin, NOTE_E7, 60); //delay(85); NewTone(_speakerPin, NOTE_C7, 60); //delay(85); NewTone(_speakerPin, NOTE_D7, 60); //delay(85); NewTone(_speakerPin, NOTE_G7, 60); //delay(85); noNewTone(_speakerPin); }
bool Speaker::loop() { if(speakerPin == -1) return false; if(this->count != 0) { if(melody == SIREN) { int wait = 1; int time = 10; if(this->state == 0) this->state = 150; // start at freq 150 if(this->state <= 1800) // go up to 1800 { NewTone(speakerPin, this->state++, time); // Beep pin, freq, time sleep_milli(wait); } else if(this->state > 1800 && this->state < 1800 + 1800 - 150) // go down to 150 { NewTone(speakerPin, 1800 - (this->state - 1800), time); sleep_milli(wait); this->state++; } else { countDown(); } } else if(melody == MELODY1) { int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; // note durations: 4 = quarter note, 8 = eighth note, etc.: int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 }; // stop the tone playing: noNewTone(speakerPin); int thisNote = state++; if(thisNote < 8) // number of notes { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000/noteDurations[thisNote]; NewTone(speakerPin, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; sleep_milli(pauseBetweenNotes); } else { countDown(); } } } return true; }