This is early stages of my project, the idea of this project is to be able to mix a guitar with windows sounds in reverse such as instrumental background music or trance music perhaps or maybe another fellow guitarist you may have downloaded from the internet. Microphone or guitar pin is p19 I would use a microphone for drums:) and that it for the moment, the code makes the mbed act as usb speaker that excepts a guitar or microphone input, but with a twist it all in reverse like a guitar reverse effects pedal but only you can mix anything you can get from the internet or any windows sound.

Dependencies:   mbed

Committer:
mbed2f
Date:
Sun Jan 08 17:28:24 2012 +0000
Revision:
0:7610d342c76e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed2f 0:7610d342c76e 1 // Hello World example for the USBMIDI library (circular buffer)
mbed2f 0:7610d342c76e 2
mbed2f 0:7610d342c76e 3 #include "mbed.h"
mbed2f 0:7610d342c76e 4 #include "USBAudio.h"
mbed2f 0:7610d342c76e 5 #include "CircBuffer.h"
mbed2f 0:7610d342c76e 6
mbed2f 0:7610d342c76e 7 Serial pc(USBTX, USBRX);
mbed2f 0:7610d342c76e 8
mbed2f 0:7610d342c76e 9 // frequency: 48 kHz
mbed2f 0:7610d342c76e 10 #define FREQ 32000
mbed2f 0:7610d342c76e 11
mbed2f 0:7610d342c76e 12 // 1 channel: mono
mbed2f 0:7610d342c76e 13 #define NB_CHA 1
mbed2f 0:7610d342c76e 14
mbed2f 0:7610d342c76e 15 // length computed: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
mbed2f 0:7610d342c76e 16 #define LENGTH_AUDIO_PACKET 32 * 2 * 1
mbed2f 0:7610d342c76e 17
mbed2f 0:7610d342c76e 18 #define USR_POWERDOWN (0x104)
mbed2f 0:7610d342c76e 19
mbed2f 0:7610d342c76e 20 int semihost_powerdown() {
mbed2f 0:7610d342c76e 21 uint32_t arg;
mbed2f 0:7610d342c76e 22 return __semihost(USR_POWERDOWN, &arg);
mbed2f 0:7610d342c76e 23 }
mbed2f 0:7610d342c76e 24
mbed2f 0:7610d342c76e 25
mbed2f 0:7610d342c76e 26 // USBAudio
mbed2f 0:7610d342c76e 27 USBAudio USBaudio(FREQ, NB_CHA, 0x7186, 0x7507);
mbed2f 0:7610d342c76e 28
mbed2f 0:7610d342c76e 29 // speaker connected to the AnalogOut output. The audio stream received over USb will be sent to the speaker
mbed2f 0:7610d342c76e 30 AnalogOut speaker(p18);
mbed2f 0:7610d342c76e 31
mbed2f 0:7610d342c76e 32 AnalogIn mic(p19);
mbed2f 0:7610d342c76e 33
mbed2f 0:7610d342c76e 34 // ticker to send data to the speaker at the good frequency
mbed2f 0:7610d342c76e 35 Ticker tic;
mbed2f 0:7610d342c76e 36
mbed2f 0:7610d342c76e 37 // circular buffer where will be stored data to be sent to the speaker
mbed2f 0:7610d342c76e 38 CircBuffer<int16_t> cbuf(LENGTH_AUDIO_PACKET);
mbed2f 0:7610d342c76e 39
mbed2f 0:7610d342c76e 40 // previous value sent to the speaker
mbed2f 0:7610d342c76e 41 uint16_t p_val = 0;
mbed2f 0:7610d342c76e 42
mbed2f 0:7610d342c76e 43 // function executed each 1/FREQ s
mbed2f 0:7610d342c76e 44 void tic_handler() {
mbed2f 0:7610d342c76e 45 int16_t val;
mbed2f 0:7610d342c76e 46 float speaker_value;
mbed2f 0:7610d342c76e 47 unsigned short PlaySample;
mbed2f 0:7610d342c76e 48
mbed2f 0:7610d342c76e 49
mbed2f 0:7610d342c76e 50 if (cbuf.available() >= 1) {
mbed2f 0:7610d342c76e 51 cbuf.dequeue(&val);
mbed2f 0:7610d342c76e 52 speaker_value = (float)val;
mbed2f 0:7610d342c76e 53
mbed2f 0:7610d342c76e 54 // speaker_value between 0 and 65535
mbed2f 0:7610d342c76e 55 speaker_value += 32768.0;
mbed2f 0:7610d342c76e 56
mbed2f 0:7610d342c76e 57 // adjust according to current volume
mbed2f 0:7610d342c76e 58 speaker_value *= USBaudio.getVolume();
mbed2f 0:7610d342c76e 59 } else {
mbed2f 0:7610d342c76e 60 speaker_value = p_val;
mbed2f 0:7610d342c76e 61 }
mbed2f 0:7610d342c76e 62 p_val = speaker_value;
mbed2f 0:7610d342c76e 63
mbed2f 0:7610d342c76e 64 /* ADAMGR: Inserting my May 30th reversing code here!!! */
mbed2f 0:7610d342c76e 65 {
mbed2f 0:7610d342c76e 66 static const unsigned int BufferSize = 20 * 640;
mbed2f 0:7610d342c76e 67 static unsigned short Buffer[BufferSize];
mbed2f 0:7610d342c76e 68
mbed2f 0:7610d342c76e 69 static int Index = 0;
mbed2f 0:7610d342c76e 70 static int Direction = 1;
mbed2f 0:7610d342c76e 71 static int Playback = false;
mbed2f 0:7610d342c76e 72 static int ChunkSize = BufferSize;
mbed2f 0:7610d342c76e 73
mbed2f 0:7610d342c76e 74 unsigned short ReadSample;
mbed2f 0:7610d342c76e 75
mbed2f 0:7610d342c76e 76 /* Default PlaySample to the current sample from USB buffer. */
mbed2f 0:7610d342c76e 77 PlaySample = speaker_value;
mbed2f 0:7610d342c76e 78
mbed2f 0:7610d342c76e 79
mbed2f 0:7610d342c76e 80
mbed2f 0:7610d342c76e 81 /* Read out the sample from the buffer to be played back */
mbed2f 0:7610d342c76e 82 if (Playback)
mbed2f 0:7610d342c76e 83 {
mbed2f 0:7610d342c76e 84 PlaySample = Buffer[Index];
mbed2f 0:7610d342c76e 85 }
mbed2f 0:7610d342c76e 86
mbed2f 0:7610d342c76e 87 /* Obtain current audio sample from the USB buffer. */
mbed2f 0:7610d342c76e 88 ReadSample = (unsigned short)speaker_value;
mbed2f 0:7610d342c76e 89
mbed2f 0:7610d342c76e 90 /* Record the sample into the buffer right where a space was freed up from the PlaySample read above */
mbed2f 0:7610d342c76e 91 Buffer[Index] = ReadSample += mic.read_u16();
mbed2f 0:7610d342c76e 92
mbed2f 0:7610d342c76e 93 /* Increment the buffer pointer */
mbed2f 0:7610d342c76e 94 Index += Direction;
mbed2f 0:7610d342c76e 95
mbed2f 0:7610d342c76e 96 /* Check to see if the chunk has been filled */
mbed2f 0:7610d342c76e 97 if (Index < 0)
mbed2f 0:7610d342c76e 98 {
mbed2f 0:7610d342c76e 99 /* Now have a chunk to be played back */
mbed2f 0:7610d342c76e 100 Playback = true;
mbed2f 0:7610d342c76e 101 /* Reverse the direction of playback and recording */
mbed2f 0:7610d342c76e 102 Direction *= -1;//Direction;
mbed2f 0:7610d342c76e 103 Index = 0;
mbed2f 0:7610d342c76e 104 }
mbed2f 0:7610d342c76e 105 else if (Index >= ChunkSize)
mbed2f 0:7610d342c76e 106
mbed2f 0:7610d342c76e 107
mbed2f 0:7610d342c76e 108
mbed2f 0:7610d342c76e 109 {
mbed2f 0:7610d342c76e 110 /* Now have a chunk to be played back */
mbed2f 0:7610d342c76e 111 Playback = true;
mbed2f 0:7610d342c76e 112 /* Reverse the direction of playback and recording */
mbed2f 0:7610d342c76e 113 Direction *= -1;//Direction;
mbed2f 0:7610d342c76e 114 Index = ChunkSize - 1;
mbed2f 0:7610d342c76e 115 }
mbed2f 0:7610d342c76e 116 }
mbed2f 0:7610d342c76e 117
mbed2f 0:7610d342c76e 118
mbed2f 0:7610d342c76e 119 // send value to the speaker
mbed2f 0:7610d342c76e 120 speaker.write_u16((uint16_t)PlaySample);
mbed2f 0:7610d342c76e 121 }
mbed2f 0:7610d342c76e 122
mbed2f 0:7610d342c76e 123 int main() {
mbed2f 0:7610d342c76e 124 int16_t buf[LENGTH_AUDIO_PACKET/2];
mbed2f 0:7610d342c76e 125
mbed2f 0:7610d342c76e 126 semihost_powerdown();
mbed2f 0:7610d342c76e 127
mbed2f 0:7610d342c76e 128 // attach a function executed each 1/FREQ s
mbed2f 0:7610d342c76e 129 tic.attach_us(tic_handler, 1000000.0/(float)FREQ);
mbed2f 0:7610d342c76e 130
mbed2f 0:7610d342c76e 131 while (1) {
mbed2f 0:7610d342c76e 132 // read an audio packet
mbed2f 0:7610d342c76e 133 USBaudio.read((uint8_t *)buf);
mbed2f 0:7610d342c76e 134
mbed2f 0:7610d342c76e 135 // put buffer in the circ buffer
mbed2f 0:7610d342c76e 136 for(int i = 0; i < LENGTH_AUDIO_PACKET/2; i++) {
mbed2f 0:7610d342c76e 137 cbuf.queue(buf[i]);
mbed2f 0:7610d342c76e 138 }
mbed2f 0:7610d342c76e 139 }
mbed2f 0:7610d342c76e 140 }