miniblip play notes with interrupts with variable volume

Dependencies:   mbed

Fork of blip_playnotes by Alberto Piganti

Committer:
JCristobal
Date:
Fri Dec 04 10:11:15 2015 +0000
Revision:
3:2af917fe09c3
Parent:
2:85d8ce475baa
miniblip play notes with interrupts with variable volume

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pighixxx 2:85d8ce475baa 1 // miniblip play notes with interrupts - Not tested with led matrix
pighixxx 2:85d8ce475baa 2 // For documentation see http://mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
pighixxx 2:85d8ce475baa 3
4180_1 0:b2fdf3770282 4 #include "mbed.h"
4180_1 1:2e6ea42675c7 5 #include "SongPlayer.h"
4180_1 0:b2fdf3770282 6
JCristobal 3:2af917fe09c3 7 // Potentiometer to handle the volume
JCristobal 3:2af917fe09c3 8 #define ANALOG_POTENTIOMETER P0_22
JCristobal 3:2af917fe09c3 9 AnalogIn ain(ANALOG_POTENTIOMETER);
JCristobal 3:2af917fe09c3 10
JCristobal 3:2af917fe09c3 11 #define MUTE 0.0
JCristobal 3:2af917fe09c3 12 #define END 1.0
JCristobal 3:2af917fe09c3 13
JCristobal 3:2af917fe09c3 14 //-- Notes and frequencies (Hz)
JCristobal 3:2af917fe09c3 15 #define DO_4 261.626
JCristobal 3:2af917fe09c3 16 #define MI_4 329.628
JCristobal 3:2af917fe09c3 17 #define SOL_4 391.995
JCristobal 3:2af917fe09c3 18 #define SI_4 493.883
JCristobal 3:2af917fe09c3 19 #define DO_5 523.251
JCristobal 3:2af917fe09c3 20
JCristobal 3:2af917fe09c3 21 //-- Tempo
JCristobal 3:2af917fe09c3 22 #define TEMPO 200
JCristobal 3:2af917fe09c3 23
JCristobal 3:2af917fe09c3 24 //-- Default volume
JCristobal 3:2af917fe09c3 25 float VOL = 0.5;
JCristobal 3:2af917fe09c3 26
JCristobal 3:2af917fe09c3 27 //-- Notes for the imperial march. MUTE means silence. END for marking the end
JCristobal 3:2af917fe09c3 28 float notes[] = {MI_4, MUTE, MI_4, MUTE, MI_4, MUTE, DO_4, SOL_4, MI_4, MUTE, DO_4, SOL_4, MI_4, MUTE,
JCristobal 3:2af917fe09c3 29 SI_4, MUTE, SI_4, MUTE, SI_4, MUTE, DO_5, SOL_4, MI_4, MUTE, DO_4, SOL_4, MI_4, END};
JCristobal 3:2af917fe09c3 30
JCristobal 3:2af917fe09c3 31 //-- Notes duration
JCristobal 3:2af917fe09c3 32 float tempo[] = {2 , 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3,
JCristobal 3:2af917fe09c3 33 2 , 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0};
JCristobal 3:2af917fe09c3 34
JCristobal 3:2af917fe09c3 35 //-- Declare speaker object
JCristobal 3:2af917fe09c3 36 PwmOut speaker(P0_8);
JCristobal 3:2af917fe09c3 37
JCristobal 3:2af917fe09c3 38 void play()
JCristobal 3:2af917fe09c3 39 {
JCristobal 3:2af917fe09c3 40 int i=0;
JCristobal 3:2af917fe09c3 41 //-- Reproduce all the notes in the list
JCristobal 3:2af917fe09c3 42 while(notes[i] != END) {
JCristobal 3:2af917fe09c3 43
JCristobal 3:2af917fe09c3 44 //-- If silence, turn of the PWM
JCristobal 3:2af917fe09c3 45 if (notes[i] == MUTE) {
JCristobal 3:2af917fe09c3 46 speaker = 0.0;
JCristobal 3:2af917fe09c3 47 }
JCristobal 3:2af917fe09c3 48 else {
JCristobal 3:2af917fe09c3 49 speaker.period(1.0/notes[i]); //-- Note
JCristobal 3:2af917fe09c3 50 float potenciometro = ain.read() * 0.7f; // Handle the volume with the potentiometer
JCristobal 3:2af917fe09c3 51 speaker = potenciometro; //-- Set the volume
JCristobal 3:2af917fe09c3 52 }
JCristobal 3:2af917fe09c3 53
JCristobal 3:2af917fe09c3 54 //-- Note duration
JCristobal 3:2af917fe09c3 55 wait_ms(TEMPO * tempo[i]);
JCristobal 3:2af917fe09c3 56
JCristobal 3:2af917fe09c3 57 //-- Move to the next note
JCristobal 3:2af917fe09c3 58 i++;
JCristobal 3:2af917fe09c3 59 }
JCristobal 3:2af917fe09c3 60
JCristobal 3:2af917fe09c3 61 //-- Turn off the speacker
JCristobal 3:2af917fe09c3 62 speaker = MUTE;
JCristobal 3:2af917fe09c3 63 }
JCristobal 3:2af917fe09c3 64
4180_1 1:2e6ea42675c7 65
4180_1 0:b2fdf3770282 66 int main()
4180_1 0:b2fdf3770282 67 {
JCristobal 3:2af917fe09c3 68
JCristobal 3:2af917fe09c3 69 while(1){
JCristobal 3:2af917fe09c3 70 // Play the song!!
JCristobal 3:2af917fe09c3 71 play();
JCristobal 3:2af917fe09c3 72 wait_ms(500);
JCristobal 3:2af917fe09c3 73 } ;
4180_1 0:b2fdf3770282 74 }
4180_1 1:2e6ea42675c7 75