USB Speak with Digital Volume Control
Dependencies: AD5206 USBDevice mbed
Fork of USBAUDIO_speaker by
main.cpp@2:c3edc567ae33, 2011-11-30 (annotated)
- Committer:
- samux
- Date:
- Wed Nov 30 10:29:23 2011 +0000
- Revision:
- 2:c3edc567ae33
- Parent:
- 1:0335b5c18618
- Child:
- 3:41b10e311100
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:5176b3dfccd6 | 1 | // Hello World example for the USBMIDI library |
samux | 0:5176b3dfccd6 | 2 | |
samux | 0:5176b3dfccd6 | 3 | #include "mbed.h" |
samux | 0:5176b3dfccd6 | 4 | #include "USBAudio.h" |
samux | 0:5176b3dfccd6 | 5 | #include "CircBuffer.h" |
samux | 0:5176b3dfccd6 | 6 | |
samux | 0:5176b3dfccd6 | 7 | Serial pc(USBTX, USBRX); |
samux | 0:5176b3dfccd6 | 8 | |
samux | 0:5176b3dfccd6 | 9 | // frequency: 48 kHz |
samux | 0:5176b3dfccd6 | 10 | #define FREQ 48000 |
samux | 0:5176b3dfccd6 | 11 | |
samux | 0:5176b3dfccd6 | 12 | // 1 channel: mono |
samux | 0:5176b3dfccd6 | 13 | #define NB_CHA 1 |
samux | 0:5176b3dfccd6 | 14 | |
samux | 0:5176b3dfccd6 | 15 | // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1 |
samux | 2:c3edc567ae33 | 16 | #define AUDIO_LENGTH_PACKET 48 * 2 * 1 |
samux | 0:5176b3dfccd6 | 17 | |
samux | 0:5176b3dfccd6 | 18 | // USBAudio |
samux | 0:5176b3dfccd6 | 19 | USBAudio audio(FREQ, NB_CHA, 0x7180, 0x7500); |
samux | 0:5176b3dfccd6 | 20 | |
samux | 0:5176b3dfccd6 | 21 | // speaker connected to the AnalogOut output. The audio stream received over USb will be sent to the speaker |
samux | 0:5176b3dfccd6 | 22 | AnalogOut speaker(p18); |
samux | 0:5176b3dfccd6 | 23 | |
samux | 0:5176b3dfccd6 | 24 | // ticker to send data to the speaker at the good frequency |
samux | 0:5176b3dfccd6 | 25 | Ticker tic; |
samux | 0:5176b3dfccd6 | 26 | |
samux | 1:0335b5c18618 | 27 | // buffer where will be store one audio packet (LENGTH_AUDIO_PACKET/2 because we are storing int16 and not uint8) |
samux | 2:c3edc567ae33 | 28 | int16_t buf[AUDIO_LENGTH_PACKET/2]; |
samux | 0:5176b3dfccd6 | 29 | |
samux | 0:5176b3dfccd6 | 30 | // show if an audio packet is available |
samux | 0:5176b3dfccd6 | 31 | volatile bool available = false; |
samux | 0:5176b3dfccd6 | 32 | |
samux | 0:5176b3dfccd6 | 33 | // index of the value which will be send to the speaker |
samux | 0:5176b3dfccd6 | 34 | int index_buf = 0; |
samux | 0:5176b3dfccd6 | 35 | |
samux | 0:5176b3dfccd6 | 36 | // previous value sent to the speaker |
samux | 0:5176b3dfccd6 | 37 | uint16_t p_val = 0; |
samux | 0:5176b3dfccd6 | 38 | |
samux | 0:5176b3dfccd6 | 39 | // function executed each 1/FREQ s |
samux | 0:5176b3dfccd6 | 40 | void tic_handler() { |
samux | 0:5176b3dfccd6 | 41 | float speaker_value; |
samux | 0:5176b3dfccd6 | 42 | |
samux | 0:5176b3dfccd6 | 43 | if (available) { |
samux | 0:5176b3dfccd6 | 44 | //convert 2 bytes in float |
samux | 1:0335b5c18618 | 45 | speaker_value = (float)(buf[index_buf]); |
samux | 0:5176b3dfccd6 | 46 | |
samux | 0:5176b3dfccd6 | 47 | // speaker_value between 0 and 65535 |
samux | 0:5176b3dfccd6 | 48 | speaker_value += 32768.0; |
samux | 0:5176b3dfccd6 | 49 | |
samux | 0:5176b3dfccd6 | 50 | // adjust according to current volume |
samux | 0:5176b3dfccd6 | 51 | speaker_value *= audio.getVolume(); |
samux | 0:5176b3dfccd6 | 52 | |
samux | 0:5176b3dfccd6 | 53 | // as two bytes has been read, we move the index of two bytes |
samux | 1:0335b5c18618 | 54 | index_buf++; |
samux | 0:5176b3dfccd6 | 55 | |
samux | 0:5176b3dfccd6 | 56 | // if we have read all the buffer, no more data available |
samux | 2:c3edc567ae33 | 57 | if (index_buf == AUDIO_LENGTH_PACKET/2) { |
samux | 0:5176b3dfccd6 | 58 | index_buf = 0; |
samux | 0:5176b3dfccd6 | 59 | available = false; |
samux | 0:5176b3dfccd6 | 60 | } |
samux | 0:5176b3dfccd6 | 61 | } else { |
samux | 0:5176b3dfccd6 | 62 | speaker_value = p_val; |
samux | 0:5176b3dfccd6 | 63 | } |
samux | 0:5176b3dfccd6 | 64 | |
samux | 0:5176b3dfccd6 | 65 | p_val = speaker_value; |
samux | 0:5176b3dfccd6 | 66 | |
samux | 0:5176b3dfccd6 | 67 | // send value to the speaker |
samux | 0:5176b3dfccd6 | 68 | speaker.write_u16((uint16_t)speaker_value); |
samux | 0:5176b3dfccd6 | 69 | } |
samux | 0:5176b3dfccd6 | 70 | |
samux | 0:5176b3dfccd6 | 71 | int main() { |
samux | 0:5176b3dfccd6 | 72 | |
samux | 0:5176b3dfccd6 | 73 | // attach a function executed each 1/FREQ s |
samux | 0:5176b3dfccd6 | 74 | tic.attach_us(tic_handler, 1000000.0/(float)FREQ); |
samux | 0:5176b3dfccd6 | 75 | |
samux | 0:5176b3dfccd6 | 76 | while (1) { |
samux | 0:5176b3dfccd6 | 77 | // read an audio packet |
samux | 1:0335b5c18618 | 78 | audio.read((uint8_t *)buf); |
samux | 0:5176b3dfccd6 | 79 | available = true; |
samux | 0:5176b3dfccd6 | 80 | } |
samux | 0:5176b3dfccd6 | 81 | } |