music mixer code
Dependencies: mbed wave_player mbed-rtos SimpleRGB 4DGL-uLCD-SE PinDetect
Diff: main.cpp
- Revision:
- 10:5f5e199e1b9c
- Parent:
- 9:d6343146d23e
--- a/main.cpp Tue Oct 11 03:42:42 2016 +0000 +++ b/main.cpp Mon Dec 09 16:43:04 2019 +0000 @@ -1,207 +1,236 @@ #include "mbed.h" #include "rtos.h" -#include <string> +#include "mpr121.h" +#include "wave_player.h" #include "uLCD_4DGL.h" -#include "SimpleRGB.h" -#include "SDFileSystem.h" -#include "wave_player.h" +#include "PinDetect.h" +#include <vector> + +PwmOut speaker(p25); +Ticker sampletick; +DigitalOut myled(LED1); -#define SECOND 1000 // used with threads -DigitalOut led(LED1); // test led - +#include "dixiehorn.h" +#include "guitarjazz.h" +#include "marimbaAmin.h" +#include "marimbaChimeC.h" +#include "marimbaFmaj.h" +#include "marimbaGmaj.h" +#include "marimbaHighD.h" +#include "marimbaLowC.h" +#include "snare.h" +#include "splash.h" +#include "tomHigh.h" +#include "kick.h" -/* 1 init/declare +#define sample_freq 8000.0 +#define SECOND 1000 +#define RED 0xFF0000 +#define BLACK 0x000000 -Ticker first; -Ticker second; -Ticker third; -Ticker fourth; +using namespace std; + DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); +PinDetect pbRec(p17); +PinDetect pbPlay(p16); +DigitalOut led(p23); -void one() { // attached to ticker object. will be called after time given - led1 = !led1; // flip the value of the led -} // nothing returned +// Create the interrupt receiver object on pin 26 +InterruptIn interrupt(p26); +// Setup the i2c bus on pins 9 and 10 +I2C i2c(p9, p10); +// Setup the Mpr121: +// constructor(i2c object, i2c address of the mpr121) + +int key_code; + +//Bluetooth + +Serial pc(USBTX, USBRX); + +// initialize recording capability +vector<int> sequence; +volatile bool isrec = false; +volatile bool play = false; -void two() { - led2 = !led2; -} +Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); + +uLCD_4DGL uLCD(p13, p14, p29); // lcd used by the following threads +Mutex stdio_mutex; -void three() { - led3 = !led3; -} - -void four() { - led4 = !led4; +void fallInterrupt() +{ + int i=0; + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + // LED demo mod + i=0; + // puts key number out to LEDs for demo + for (i=0; i<12; i++) { + if (((value>>i)&0x01)==1) key_code=i+1; + } + led4=key_code & 0x01; + led3=(key_code>>1) & 0x01; + led2=(key_code>>2) & 0x01; + led1=(key_code>>3) & 0x01; + +// fclose(wave_file); } -*/// - 1 - - -/* 2 init/declare */ - -uLCD_4DGL lcd(p9, p10, p11); // lcd used by the following threads -Mutex stdio_mutex; // mutex lock, ensure safe use of lcd - -// lcd thread -void uLCD_reboot_thread(void const *args) { - // show reboot process, associated with clip of apple restart - while (true) { - // lock, print 'Rebooting!' to uLCD with "progress_bar" underneath - stdio_mutex.lock(); - lcd.text_string("Rebooting", 2, 5, FONT_7X8, WHITE); - // draw progress bar 8 sec - for (int i = 0; i < 8; i++) { - lcd.text_string("|", 2 + i, 6, FONT_7X8, WHITE); - // progress[i+1] += arr[i]; - Thread::wait(SECOND); - } - // draw an ascii 'apple' - lcd.text_string(" .:' ", 2, 4, FONT_7X8, WHITE); - lcd.text_string(" :' ", 2, 5, FONT_7X8, WHITE); - lcd.text_string(" .'` `-' ``. ", 2, 6, FONT_7X8, WHITE); - lcd.text_string(": .-' ", 2, 7, FONT_7X8, WHITE); - lcd.text_string(": : ", 2, 8, FONT_7X8, WHITE); - lcd.text_string(" : `-; ", 2, 9, FONT_7X8, WHITE); - lcd.text_string(" `.__.-.__.' ", 2, 10, FONT_7X8, WHITE); - // unlock, wait for 52 - stdio_mutex.unlock(); - Thread::wait(52 * SECOND); - // clear screen - stdio_mutex.lock(); - lcd.cls(); - stdio_mutex.unlock(); - } -} - -// lcd thread -void uLCD_status_thread(void const *args) { - // show time with smile, frown, sad face at top right - set_time(1476146921); - while (true) { - char buffer[32]; - time_t seconds = time(NULL); - strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); - // lock, update time at top right with smiley at top left - stdio_mutex.lock(); - lcd.text_string(buffer, 8, 1, FONT_7X8, BLUE); - lcd.text_string(":-)", 1, 1, FONT_7X8, WHITE); - // unlock, wait for 20 sec - stdio_mutex.unlock(); - Thread::wait(20 * SECOND); - // lock, change to frown - stdio_mutex.lock(); - lcd.text_string(">:|", 1, 1, FONT_7X8, WHITE); - // unlock, wait for 20 sec - stdio_mutex.unlock(); - Thread::wait(20 * SECOND); - // lock, change to sad - stdio_mutex.lock(); - lcd.text_string(":-(", 1, 1, FONT_7X8, WHITE); - // unlock, wait for 20 sec - stdio_mutex.unlock(); - Thread::wait(20 * SECOND); +void ledRecord(void const *args) { + while(1) { + if(isrec) led = !led; + else led = 0; + Thread::wait(1 * SECOND); } } - -/* extra credit - video */ -void uLCD_video_thread(void const *args) { - lcd.media_init(); - lcd.set_sector_address(0x001D, 0x4C42); - lcd.display_video(0,0); +void pbRec_hit_callback (void) { + if (!isrec) {isrec = true; sequence.clear();} + else if(isrec) {isrec = false;} } -// - extra - -RGBLed rgbLED(p21, p22, p23); // object to use the RGB LED -const LightColor green(0.0, 1.0, 0.0); -const LightColor yellow(1.0, 1.0, 0.0); -const LightColor orange(1.0, 0.6, 0.0); -const LightColor red(1.0,0.0,0.0); -const LightColor violet(1.0, 0.0, 0.8); -const LightColor blue(0.0, 0.0, 1.0); +void pbPlay_hit_callback (void) { + if (!play) {play = true;} + else if(play) {play = false;} +} -// rgb led thread -void rainbow_led_thread(void const *args) { - // reboot with white (6 sec), then cycle through apple colors (54 sec) - while (true) { - rgbLED.write(1.0, 1.0, 1.0); // bright white - Thread::wait(2 * SECOND); - // dim to nothing over 4 seconds - for (float c = 1.0; c > 0.0; c = c - 0.01) { - rgbLED.write(c, c, c); - Thread::wait(40); // 40 * 100 = 4000 = 4 secs - } - // rotate through other colors over 54 seconds - rgbLED = green; - Thread::wait(9 * SECOND); - rgbLED = yellow; - Thread::wait(9 * SECOND); - rgbLED = orange; - Thread::wait(9 * SECOND); - rgbLED = red; - Thread::wait(9 * SECOND); - rgbLED = violet; - Thread::wait(9 * SECOND); - rgbLED = blue; - Thread::wait(9 * SECOND); +void playback(int num) { + switch(num) { //uses a case statement for each individual touch sensor + case 1: + led = 1; + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(0,93,42,124,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_dixiehorn, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 2: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(0,62,42,93,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_guitarjazz, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 3: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(0,31,42,62,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_marimbaA, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 4: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(0,0,42,31,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_marimbaChime, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 5: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(42,93,84,124,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_marimbaF, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 6: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(42,62,84,93,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_marimbaG, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 7: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(42,31,84,62,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_marimbaD, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 8: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(42,0,84,31,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_marimbaC, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 9: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(84,93,127,124,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_snare, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 10: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(84,62,127,93,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_splash, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 11: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(84,31,127,62,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_tomHigh, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; + case 12: + if (isrec) sequence.push_back(num); + stdio_mutex.lock(); + uLCD.filled_rectangle(84,0,127,31,RED); + stdio_mutex.unlock(); + sampletick.attach(&audio_sample_kick, 1.0 / sample_freq); + Thread::wait(.75 * SECOND); + break; } } -SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card with .wav files -AnalogOut DACout(p18); // DAC for producing sound -wave_player waver(&DACout); // wavplayer object -// - 2 +void initialize() { + speaker.period(1.0/250000.0); //PWM freq >10X audio sample rate + pbRec.mode(PullUp); + pbPlay.mode(PullUp); + wait(.001); + pbRec.attach_deasserted(&pbRec_hit_callback); + pbPlay.attach_deasserted(&pbPlay_hit_callback); + pbRec.setSampleFrequency(); + pbPlay.setSampleFrequency(); + interrupt.fall(&fallInterrupt); + interrupt.mode(PullUp); +} -int main() { - /* 1 calls - led1 = 1, led2 = 1, led3 = 1, led4 = 1; - - first.attach(&one, 1.0); // attach the function and when to call it - second.attach(&two, 2.0); - third.attach(&three, 3.0); - fourth.attach(&four, 4.0); - */// - 1 - - - /* 2 calls */ - Thread thread2(uLCD_reboot_thread); - Thread thread3(uLCD_status_thread); - Thread thread4(rainbow_led_thread); - //Thread thread5(uLCD_video_thread); - - FILE *wave_file; - - // - 2 - - - led = 1; - - while(true) { - - /* 2 speaker 'thread' calls */ - // speaker plays mac reboot sound, error sound occasionally - // play reboot sound - wave_file = fopen("/sd/reboot.wav","r"); - waver.play(wave_file); - fclose(wave_file); - // wait 20 - Thread::wait(20 * SECOND); - // error - wave_file = fopen("/sd/error.wav","r"); - waver.play(wave_file); - fclose(wave_file); - // wait 20 - Thread::wait(20 * SECOND); - // error - waver.play(wave_file); - fclose(wave_file); - // wait 20 - Thread::wait(20 * SECOND); - // - 2 - } // interupts will occur automatically -} +int main() +{ + initialize(); +// Thread thread2(bluetooth, NULL, osPriorityNormal, 2000, NULL); + Thread thread2(ledRecord, NULL, osPriorityNormal, 2000, NULL); +// Thread thread3(bluetooth, NULL, osPriorityNormal, 2000, NULL); + //defaultscreen(); + while (1) { + //uLCD.printf("play = %i", (int)play); + if (play) { + for (int j=0; j < sequence.size(); j++) { + if (play) playback(sequence[j]); + else break; + } + } +// Thread thread2(layeredAudio); + playback(key_code); + uLCD.cls(); + } +} \ No newline at end of file