change tone sounds by altering the waveforms

Dependencies:   mbed

Fork of 5_songs by MakingMusicWorkshop

Committer:
maclobdell
Date:
Sat May 21 16:07:43 2016 +0000
Revision:
7:4730853634bf
Parent:
6:406f73a0eb49
Child:
8:77227ecac915
initial version, based on 5_songs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:0fb17f0d39e7 1 #include "mbed.h" // this tells us to load mbed related functions
maclobdell 1:8922cc02aec7 2 #include "tones.h" // list of all the tones and their frequencies
maclobdell 0:0fb17f0d39e7 3
maclobdell 0:0fb17f0d39e7 4 PwmOut buzzer(D3); // our buzzer is a PWM output (pulse-width modulation)
maclobdell 0:0fb17f0d39e7 5
maclobdell 5:7003c9f6d633 6 static int BPM = 80;
maclobdell 0:0fb17f0d39e7 7
maclobdell 0:0fb17f0d39e7 8 static void silence() {
maclobdell 0:0fb17f0d39e7 9 buzzer.write(0.0f); // silence!
maclobdell 0:0fb17f0d39e7 10 }
maclobdell 0:0fb17f0d39e7 11
maclobdell 0:0fb17f0d39e7 12 // this is our function that plays a tone.
maclobdell 0:0fb17f0d39e7 13 // Takes in a tone frequency, and after duration (in ms.) we stop playing again
maclobdell 7:4730853634bf 14 static void play_tone(int tone, int duration) {
maclobdell 7:4730853634bf 15
maclobdell 7:4730853634bf 16 int tone_period_us = 1000000/tone;
maclobdell 7:4730853634bf 17
maclobdell 7:4730853634bf 18 for (long i = 0; i < duration * 1000L; i += tone_period_us * 2) {
maclobdell 7:4730853634bf 19
maclobdell 7:4730853634bf 20 // let's change the buzzer's volume depending on how far we are
maclobdell 7:4730853634bf 21 float pct = (float)i / (float)(duration * 1000L);
maclobdell 7:4730853634bf 22
maclobdell 7:4730853634bf 23 buzzer = (1 - pct) / 4; // high to low
maclobdell 7:4730853634bf 24 wait_us(tone_period_us);
maclobdell 7:4730853634bf 25 buzzer = 0.0f;
maclobdell 7:4730853634bf 26 wait_us(tone_period_us);
maclobdell 7:4730853634bf 27 }
maclobdell 7:4730853634bf 28
maclobdell 0:0fb17f0d39e7 29 }
maclobdell 0:0fb17f0d39e7 30
maclobdell 0:0fb17f0d39e7 31 static void play_song(int notes_left, int* melody, int* duration) {
maclobdell 1:8922cc02aec7 32
maclobdell 7:4730853634bf 33 // melody and duration are pointers, they point to the array of tones and durations we declared earlier
maclobdell 7:4730853634bf 34 // every time we play a note we up these pointers (move one element forward)
maclobdell 7:4730853634bf 35 // so the current tone is always the first element of melody (same for duration)
maclobdell 7:4730853634bf 36
maclobdell 7:4730853634bf 37 int length;
maclobdell 7:4730853634bf 38
maclobdell 7:4730853634bf 39 while(notes_left > 0)
maclobdell 7:4730853634bf 40 {
maclobdell 7:4730853634bf 41
maclobdell 7:4730853634bf 42 int tone = melody[0];
maclobdell 7:4730853634bf 43 // BPM is quarter notes per minute, so length in milliseconds is:
maclobdell 7:4730853634bf 44 length = static_cast<int>(static_cast<float>(1000 / duration[0]) * (60000.0f / static_cast<float>(BPM * 1000)));
maclobdell 7:4730853634bf 45
maclobdell 7:4730853634bf 46 play_tone(tone, length);
maclobdell 7:4730853634bf 47
maclobdell 7:4730853634bf 48 // after the full length of this tone, we up the melody, and down the notes_left
maclobdell 7:4730853634bf 49
maclobdell 7:4730853634bf 50 notes_left--;
maclobdell 7:4730853634bf 51 melody++;
maclobdell 7:4730853634bf 52 duration++;
maclobdell 7:4730853634bf 53
maclobdell 7:4730853634bf 54 }
maclobdell 7:4730853634bf 55
maclobdell 7:4730853634bf 56 // we're done! just finish this note and silence
maclobdell 7:4730853634bf 57 wait_ms(length / 2);
maclobdell 7:4730853634bf 58 silence();
maclobdell 2:610196c7b41c 59
maclobdell 0:0fb17f0d39e7 60 }
maclobdell 0:0fb17f0d39e7 61
maclobdell 0:0fb17f0d39e7 62 // this code runs when the microcontroller starts up
maclobdell 1:8922cc02aec7 63 int main() {
maclobdell 2:610196c7b41c 64
maclobdell 0:0fb17f0d39e7 65 // declare a melody
maclobdell 6:406f73a0eb49 66 int melody[] = {
maclobdell 6:406f73a0eb49 67 NOTE_C4, NOTE_D4, NOTE_DS4, NOTE_C4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4,
maclobdell 6:406f73a0eb49 68 NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4,
maclobdell 6:406f73a0eb49 69 NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4
maclobdell 0:0fb17f0d39e7 70 };
maclobdell 2:610196c7b41c 71
maclobdell 0:0fb17f0d39e7 72 // note durations: 4 = quarter note, 8 = eighth note, etc.:
maclobdell 6:406f73a0eb49 73 // the rapid succession of 16th notes produces a twill effect
maclobdell 0:0fb17f0d39e7 74 int duration[] = {
maclobdell 6:406f73a0eb49 75 2, 8, 4, 4, 16, 16, 16, 16,
maclobdell 6:406f73a0eb49 76 16, 16, 16, 16, 16, 16, 16,
maclobdell 6:406f73a0eb49 77 16, 16, 16, 16, 16
maclobdell 2:610196c7b41c 78 };
maclobdell 7:4730853634bf 79
maclobdell 0:0fb17f0d39e7 80 // melody & duration are on the heap, need to get them on the stack
maclobdell 0:0fb17f0d39e7 81 int *m = new int[sizeof(melody) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 82 memcpy(m, melody, sizeof(melody));
maclobdell 0:0fb17f0d39e7 83 int *d = new int[sizeof(duration) / sizeof(int)];
maclobdell 0:0fb17f0d39e7 84 memcpy(d, duration, sizeof(duration));
maclobdell 0:0fb17f0d39e7 85
maclobdell 0:0fb17f0d39e7 86 if (sizeof(melody) != sizeof(duration)) {
maclobdell 0:0fb17f0d39e7 87 printf("Melody and duration do not have same number of elements! Aborting!\r\n");
maclobdell 2:610196c7b41c 88 return 1;
maclobdell 0:0fb17f0d39e7 89 }
maclobdell 0:0fb17f0d39e7 90
maclobdell 0:0fb17f0d39e7 91 play_song(sizeof(melody) / sizeof(int), m, d);
maclobdell 2:610196c7b41c 92
maclobdell 2:610196c7b41c 93 return 0;
maclobdell 0:0fb17f0d39e7 94 }