Simple mbed amplifier for quarter-inch audio input with volume, delay, and distortion control from smartphone app via bluetooth.
main.cpp
00001 //Library includes 00002 #include "mbed.h" 00003 #include "rtos.h" 00004 00005 #define MAX_DELAY 10000 //Max size of delay effect sample buffer(too large can crash on compile from memory limit) 00006 00007 //mbed pin declarations 00008 00009 AnalogIn pot(p20); 00010 DigitalOut led(LED1); 00011 DigitalOut led2(LED2); 00012 AnalogIn input(p15); 00013 AnalogOut output(p18); 00014 Serial pc(USBTX, USBRX); 00015 Serial blue(p13,p14); 00016 00017 //global variable declarations 00018 00019 int inv_gain = 3; //scale factor for decreasing delay volume 00020 int delay = 0; //store value for delay from smartphone app 00021 float volume = .5; //store value for volume from smartphone app 00022 float distortion = 1.0; //store value for distortion from smartphone app 00023 unsigned short buffer[MAX_DELAY]; //declare array for storing samples for delay effect 00024 int scaleFactor = 30; //global to convert int values from smartphone app sliders 00025 00026 /******************************************************* 00027 Bluetooth thread for reading input from smartphone app 00028 ********************************************************/ 00029 void threadBluetooth(void const *args) 00030 { 00031 while(1) 00032 { 00033 if(blue.readable()) //check if new value is available 00034 { 00035 int changeVal; //variable to hold value from smartphone app 00036 changeVal = blue.getc(); //get new value from app via bluetooth 00037 00038 if(changeVal <= scaleFactor) //check if new value falls in volume slider range 00039 { 00040 //convert volume to float value in range [0.0-1.0] 00041 volume = (float)changeVal/((float)scaleFactor+3); 00042 } 00043 else if(changeVal <= scaleFactor*2) //check if value is in delay range 00044 { 00045 //convert delay to float in range (0.0 - 1.0] 00046 delay = ((float)changeVal-((float)scaleFactor+1))/((float)scaleFactor)*MAX_DELAY; 00047 00048 if(changeVal == 31) //if new value is bottom of delay slider 00049 { 00050 delay = 0; //set delay to zero (disable effect) 00051 } 00052 } 00053 else if(changeVal <= scaleFactor*3) //check if value is in distortion range 00054 { 00055 //convert distortion to float in range (3.0 - 4.0) 00056 distortion = 3.0 + ((float)changeVal-(2*(float)scaleFactor+1))/((float)scaleFactor); 00057 00058 if(changeVal == 61) //if new value is bottom of distortion slider 00059 { 00060 distortion = 1; //set distortion to one (disable effect) 00061 } 00062 } 00063 } 00064 Thread::wait(100); 00065 } //end bluetooth thread 00066 00067 /***** 00068 Main 00069 *****/ 00070 00071 int main(void) 00072 { 00073 Thread threadBlue(threadBluetooth); //startup Bluetooth thread for smartphone app 00074 int oldDelay; //global for delay comparison 00075 00076 while(1) 00077 { 00078 int i; //for loop count variable 00079 00080 if(delay > 0) //check if delay effect is on 00081 { 00082 if(delay != oldDelay) //if delay value has changed re-buffer 00083 { 00084 for (i = 0; i < delay; i++) //buffer loop 00085 { 00086 buffer[i] += input.read_u16(); 00087 oldDelay = delay; 00088 } 00089 } 00090 while(delay > 0) //while delay is on 00091 { 00092 buffer[i] = buffer[i]/inv_gain + input.read_u16(); //scale buffer value and add to input 00093 output.write_u16(distortion * volume * buffer[i]); //write to output 00094 i = (i+1) % delay; //traverse buffer array(use delay number of values) 00095 } 00096 } 00097 else 00098 { 00099 //with no delay write input value scaled by volume and distortion 00100 output.write_u16(distortion * volume * input.read_u16()); 00101 wait_us(23); //wait time based on sample rate 00102 } 00103 } 00104 } //end main
Generated on Sat Jul 16 2022 01:46:28 by
1.7.2