music mixer code

Dependencies:   mbed wave_player mbed-rtos SimpleRGB 4DGL-uLCD-SE PinDetect

Committer:
vamsatoor
Date:
Mon Dec 09 16:43:04 2019 +0000
Revision:
10:5f5e199e1b9c
Parent:
9:d6343146d23e
commit 1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kswanson31 0:51d88b4e2b23 1 #include "mbed.h"
kswanson31 0:51d88b4e2b23 2 #include "rtos.h"
vamsatoor 10:5f5e199e1b9c 3 #include "mpr121.h"
vamsatoor 10:5f5e199e1b9c 4 #include "wave_player.h"
fkhan39 4:4792f0db029a 5 #include "uLCD_4DGL.h"
vamsatoor 10:5f5e199e1b9c 6 #include "PinDetect.h"
vamsatoor 10:5f5e199e1b9c 7 #include <vector>
vamsatoor 10:5f5e199e1b9c 8
vamsatoor 10:5f5e199e1b9c 9 PwmOut speaker(p25);
vamsatoor 10:5f5e199e1b9c 10 Ticker sampletick;
vamsatoor 10:5f5e199e1b9c 11 DigitalOut myled(LED1);
kswanson31 1:ff69d2d97a2c 12
vamsatoor 10:5f5e199e1b9c 13 #include "dixiehorn.h"
vamsatoor 10:5f5e199e1b9c 14 #include "guitarjazz.h"
vamsatoor 10:5f5e199e1b9c 15 #include "marimbaAmin.h"
vamsatoor 10:5f5e199e1b9c 16 #include "marimbaChimeC.h"
vamsatoor 10:5f5e199e1b9c 17 #include "marimbaFmaj.h"
vamsatoor 10:5f5e199e1b9c 18 #include "marimbaGmaj.h"
vamsatoor 10:5f5e199e1b9c 19 #include "marimbaHighD.h"
vamsatoor 10:5f5e199e1b9c 20 #include "marimbaLowC.h"
vamsatoor 10:5f5e199e1b9c 21 #include "snare.h"
vamsatoor 10:5f5e199e1b9c 22 #include "splash.h"
vamsatoor 10:5f5e199e1b9c 23 #include "tomHigh.h"
vamsatoor 10:5f5e199e1b9c 24 #include "kick.h"
kswanson31 8:38419fb4e242 25
vamsatoor 10:5f5e199e1b9c 26 #define sample_freq 8000.0
vamsatoor 10:5f5e199e1b9c 27 #define SECOND 1000
vamsatoor 10:5f5e199e1b9c 28 #define RED 0xFF0000
vamsatoor 10:5f5e199e1b9c 29 #define BLACK 0x000000
kswanson31 1:ff69d2d97a2c 30
vamsatoor 10:5f5e199e1b9c 31 using namespace std;
vamsatoor 10:5f5e199e1b9c 32
kswanson31 0:51d88b4e2b23 33 DigitalOut led1(LED1);
kswanson31 0:51d88b4e2b23 34 DigitalOut led2(LED2);
kswanson31 0:51d88b4e2b23 35 DigitalOut led3(LED3);
kswanson31 0:51d88b4e2b23 36 DigitalOut led4(LED4);
vamsatoor 10:5f5e199e1b9c 37 PinDetect pbRec(p17);
vamsatoor 10:5f5e199e1b9c 38 PinDetect pbPlay(p16);
vamsatoor 10:5f5e199e1b9c 39 DigitalOut led(p23);
kswanson31 0:51d88b4e2b23 40
vamsatoor 10:5f5e199e1b9c 41 // Create the interrupt receiver object on pin 26
vamsatoor 10:5f5e199e1b9c 42 InterruptIn interrupt(p26);
vamsatoor 10:5f5e199e1b9c 43 // Setup the i2c bus on pins 9 and 10
vamsatoor 10:5f5e199e1b9c 44 I2C i2c(p9, p10);
vamsatoor 10:5f5e199e1b9c 45 // Setup the Mpr121:
vamsatoor 10:5f5e199e1b9c 46 // constructor(i2c object, i2c address of the mpr121)
vamsatoor 10:5f5e199e1b9c 47
vamsatoor 10:5f5e199e1b9c 48 int key_code;
vamsatoor 10:5f5e199e1b9c 49
vamsatoor 10:5f5e199e1b9c 50 //Bluetooth
vamsatoor 10:5f5e199e1b9c 51
vamsatoor 10:5f5e199e1b9c 52 Serial pc(USBTX, USBRX);
vamsatoor 10:5f5e199e1b9c 53
vamsatoor 10:5f5e199e1b9c 54 // initialize recording capability
vamsatoor 10:5f5e199e1b9c 55 vector<int> sequence;
vamsatoor 10:5f5e199e1b9c 56 volatile bool isrec = false;
vamsatoor 10:5f5e199e1b9c 57 volatile bool play = false;
kswanson31 0:51d88b4e2b23 58
vamsatoor 10:5f5e199e1b9c 59 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
vamsatoor 10:5f5e199e1b9c 60
vamsatoor 10:5f5e199e1b9c 61 uLCD_4DGL uLCD(p13, p14, p29); // lcd used by the following threads
vamsatoor 10:5f5e199e1b9c 62 Mutex stdio_mutex;
kswanson31 0:51d88b4e2b23 63
vamsatoor 10:5f5e199e1b9c 64 void fallInterrupt()
vamsatoor 10:5f5e199e1b9c 65 {
vamsatoor 10:5f5e199e1b9c 66 int i=0;
vamsatoor 10:5f5e199e1b9c 67 int value=mpr121.read(0x00);
vamsatoor 10:5f5e199e1b9c 68 value +=mpr121.read(0x01)<<8;
vamsatoor 10:5f5e199e1b9c 69 // LED demo mod
vamsatoor 10:5f5e199e1b9c 70 i=0;
vamsatoor 10:5f5e199e1b9c 71 // puts key number out to LEDs for demo
vamsatoor 10:5f5e199e1b9c 72 for (i=0; i<12; i++) {
vamsatoor 10:5f5e199e1b9c 73 if (((value>>i)&0x01)==1) key_code=i+1;
vamsatoor 10:5f5e199e1b9c 74 }
vamsatoor 10:5f5e199e1b9c 75 led4=key_code & 0x01;
vamsatoor 10:5f5e199e1b9c 76 led3=(key_code>>1) & 0x01;
vamsatoor 10:5f5e199e1b9c 77 led2=(key_code>>2) & 0x01;
vamsatoor 10:5f5e199e1b9c 78 led1=(key_code>>3) & 0x01;
vamsatoor 10:5f5e199e1b9c 79
vamsatoor 10:5f5e199e1b9c 80 // fclose(wave_file);
kswanson31 0:51d88b4e2b23 81 }
kswanson31 1:ff69d2d97a2c 82
vamsatoor 10:5f5e199e1b9c 83 void ledRecord(void const *args) {
vamsatoor 10:5f5e199e1b9c 84 while(1) {
vamsatoor 10:5f5e199e1b9c 85 if(isrec) led = !led;
vamsatoor 10:5f5e199e1b9c 86 else led = 0;
vamsatoor 10:5f5e199e1b9c 87 Thread::wait(1 * SECOND);
kswanson31 0:51d88b4e2b23 88 }
kswanson31 0:51d88b4e2b23 89 }
kswanson31 0:51d88b4e2b23 90
vamsatoor 10:5f5e199e1b9c 91 void pbRec_hit_callback (void) {
vamsatoor 10:5f5e199e1b9c 92 if (!isrec) {isrec = true; sequence.clear();}
vamsatoor 10:5f5e199e1b9c 93 else if(isrec) {isrec = false;}
fkhan39 7:f3eb2b2c295d 94 }
kswanson31 8:38419fb4e242 95
vamsatoor 10:5f5e199e1b9c 96 void pbPlay_hit_callback (void) {
vamsatoor 10:5f5e199e1b9c 97 if (!play) {play = true;}
vamsatoor 10:5f5e199e1b9c 98 else if(play) {play = false;}
vamsatoor 10:5f5e199e1b9c 99 }
kswanson31 1:ff69d2d97a2c 100
vamsatoor 10:5f5e199e1b9c 101 void playback(int num) {
vamsatoor 10:5f5e199e1b9c 102 switch(num) { //uses a case statement for each individual touch sensor
vamsatoor 10:5f5e199e1b9c 103 case 1:
vamsatoor 10:5f5e199e1b9c 104 led = 1;
vamsatoor 10:5f5e199e1b9c 105 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 106 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 107 uLCD.filled_rectangle(0,93,42,124,RED);
vamsatoor 10:5f5e199e1b9c 108 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 109 sampletick.attach(&audio_sample_dixiehorn, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 110 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 111 break;
vamsatoor 10:5f5e199e1b9c 112 case 2:
vamsatoor 10:5f5e199e1b9c 113 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 114 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 115 uLCD.filled_rectangle(0,62,42,93,RED);
vamsatoor 10:5f5e199e1b9c 116 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 117 sampletick.attach(&audio_sample_guitarjazz, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 118 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 119 break;
vamsatoor 10:5f5e199e1b9c 120 case 3:
vamsatoor 10:5f5e199e1b9c 121 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 122 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 123 uLCD.filled_rectangle(0,31,42,62,RED);
vamsatoor 10:5f5e199e1b9c 124 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 125 sampletick.attach(&audio_sample_marimbaA, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 126 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 127 break;
vamsatoor 10:5f5e199e1b9c 128 case 4:
vamsatoor 10:5f5e199e1b9c 129 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 130 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 131 uLCD.filled_rectangle(0,0,42,31,RED);
vamsatoor 10:5f5e199e1b9c 132 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 133 sampletick.attach(&audio_sample_marimbaChime, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 134 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 135 break;
vamsatoor 10:5f5e199e1b9c 136 case 5:
vamsatoor 10:5f5e199e1b9c 137 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 138 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 139 uLCD.filled_rectangle(42,93,84,124,RED);
vamsatoor 10:5f5e199e1b9c 140 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 141 sampletick.attach(&audio_sample_marimbaF, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 142 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 143 break;
vamsatoor 10:5f5e199e1b9c 144 case 6:
vamsatoor 10:5f5e199e1b9c 145 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 146 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 147 uLCD.filled_rectangle(42,62,84,93,RED);
vamsatoor 10:5f5e199e1b9c 148 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 149 sampletick.attach(&audio_sample_marimbaG, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 150 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 151 break;
vamsatoor 10:5f5e199e1b9c 152 case 7:
vamsatoor 10:5f5e199e1b9c 153 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 154 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 155 uLCD.filled_rectangle(42,31,84,62,RED);
vamsatoor 10:5f5e199e1b9c 156 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 157 sampletick.attach(&audio_sample_marimbaD, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 158 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 159 break;
vamsatoor 10:5f5e199e1b9c 160 case 8:
vamsatoor 10:5f5e199e1b9c 161 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 162 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 163 uLCD.filled_rectangle(42,0,84,31,RED);
vamsatoor 10:5f5e199e1b9c 164 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 165 sampletick.attach(&audio_sample_marimbaC, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 166 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 167 break;
vamsatoor 10:5f5e199e1b9c 168 case 9:
vamsatoor 10:5f5e199e1b9c 169 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 170 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 171 uLCD.filled_rectangle(84,93,127,124,RED);
vamsatoor 10:5f5e199e1b9c 172 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 173 sampletick.attach(&audio_sample_snare, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 174 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 175 break;
vamsatoor 10:5f5e199e1b9c 176 case 10:
vamsatoor 10:5f5e199e1b9c 177 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 178 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 179 uLCD.filled_rectangle(84,62,127,93,RED);
vamsatoor 10:5f5e199e1b9c 180 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 181 sampletick.attach(&audio_sample_splash, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 182 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 183 break;
vamsatoor 10:5f5e199e1b9c 184 case 11:
vamsatoor 10:5f5e199e1b9c 185 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 186 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 187 uLCD.filled_rectangle(84,31,127,62,RED);
vamsatoor 10:5f5e199e1b9c 188 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 189 sampletick.attach(&audio_sample_tomHigh, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 190 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 191 break;
vamsatoor 10:5f5e199e1b9c 192 case 12:
vamsatoor 10:5f5e199e1b9c 193 if (isrec) sequence.push_back(num);
vamsatoor 10:5f5e199e1b9c 194 stdio_mutex.lock();
vamsatoor 10:5f5e199e1b9c 195 uLCD.filled_rectangle(84,0,127,31,RED);
vamsatoor 10:5f5e199e1b9c 196 stdio_mutex.unlock();
vamsatoor 10:5f5e199e1b9c 197 sampletick.attach(&audio_sample_kick, 1.0 / sample_freq);
vamsatoor 10:5f5e199e1b9c 198 Thread::wait(.75 * SECOND);
vamsatoor 10:5f5e199e1b9c 199 break;
kswanson31 0:51d88b4e2b23 200 }
kswanson31 0:51d88b4e2b23 201 }
kswanson31 9:d6343146d23e 202
vamsatoor 10:5f5e199e1b9c 203 void initialize() {
vamsatoor 10:5f5e199e1b9c 204 speaker.period(1.0/250000.0); //PWM freq >10X audio sample rate
vamsatoor 10:5f5e199e1b9c 205 pbRec.mode(PullUp);
vamsatoor 10:5f5e199e1b9c 206 pbPlay.mode(PullUp);
vamsatoor 10:5f5e199e1b9c 207 wait(.001);
vamsatoor 10:5f5e199e1b9c 208 pbRec.attach_deasserted(&pbRec_hit_callback);
vamsatoor 10:5f5e199e1b9c 209 pbPlay.attach_deasserted(&pbPlay_hit_callback);
vamsatoor 10:5f5e199e1b9c 210 pbRec.setSampleFrequency();
vamsatoor 10:5f5e199e1b9c 211 pbPlay.setSampleFrequency();
kswanson31 0:51d88b4e2b23 212
vamsatoor 10:5f5e199e1b9c 213 interrupt.fall(&fallInterrupt);
vamsatoor 10:5f5e199e1b9c 214 interrupt.mode(PullUp);
vamsatoor 10:5f5e199e1b9c 215 }
kswanson31 1:ff69d2d97a2c 216
vamsatoor 10:5f5e199e1b9c 217 int main()
vamsatoor 10:5f5e199e1b9c 218 {
vamsatoor 10:5f5e199e1b9c 219 initialize();
vamsatoor 10:5f5e199e1b9c 220 // Thread thread2(bluetooth, NULL, osPriorityNormal, 2000, NULL);
vamsatoor 10:5f5e199e1b9c 221 Thread thread2(ledRecord, NULL, osPriorityNormal, 2000, NULL);
vamsatoor 10:5f5e199e1b9c 222 // Thread thread3(bluetooth, NULL, osPriorityNormal, 2000, NULL);
vamsatoor 10:5f5e199e1b9c 223 //defaultscreen();
vamsatoor 10:5f5e199e1b9c 224 while (1) {
vamsatoor 10:5f5e199e1b9c 225 //uLCD.printf("play = %i", (int)play);
vamsatoor 10:5f5e199e1b9c 226 if (play) {
vamsatoor 10:5f5e199e1b9c 227 for (int j=0; j < sequence.size(); j++) {
vamsatoor 10:5f5e199e1b9c 228 if (play) playback(sequence[j]);
vamsatoor 10:5f5e199e1b9c 229 else break;
vamsatoor 10:5f5e199e1b9c 230 }
vamsatoor 10:5f5e199e1b9c 231 }
vamsatoor 10:5f5e199e1b9c 232 // Thread thread2(layeredAudio);
vamsatoor 10:5f5e199e1b9c 233 playback(key_code);
vamsatoor 10:5f5e199e1b9c 234 uLCD.cls();
vamsatoor 10:5f5e199e1b9c 235 }
vamsatoor 10:5f5e199e1b9c 236 }