Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Playback example with the USBAUDIO library
00002 
00003 #include "mbed.h"
00004 #include "USBAudio.h"
00005 
00006 // frequency: 48 kHz
00007 #define FREQ_SPK 16000
00008 #define FREQ_MIC 16000
00009 
00010 // 2channels: stereo
00011 #define NB_CHA_SPK 2
00012 #define NB_CHA_MIC 2
00013 
00014 // length computed: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there are two channels, the length will be 48 * 2 * 2
00015 #define LENGTH_AUDIO_PACKET_SPK (FREQ_SPK / 500) * NB_CHA_SPK
00016 #define LENGTH_AUDIO_PACKET_MIC (FREQ_MIC / 500) * NB_CHA_MIC
00017 
00018 // USBAudio object
00019 USBAudio audio(FREQ_SPK, NB_CHA_SPK, FREQ_MIC, NB_CHA_MIC, 0xab45, 0x0378);
00020 int filled;
00021 int ready = 2;
00022 
00023 /*  buffer 4 packets to avoid  */
00024 int buf[4][LENGTH_AUDIO_PACKET_SPK/sizeof(int)];
00025 void tx_audio(void)
00026 {
00027     /*  used some buffer in advance  */
00028     ready = (filled+2)&0x3;
00029     audio.writeSync((uint8_t *)buf[ready]);
00030 }
00031 
00032 
00033 void rx_audio(void)
00034 {
00035     int size=0;
00036     int next = (filled + 1)&0x3; 
00037     size = audio.readSync((uint8_t *)buf[next]);
00038     if (size ) filled = next;
00039     if ((size) && (size!=LENGTH_AUDIO_PACKET_MIC)) printf("%d\n",size);
00040 }
00041 
00042 int main()
00043 {
00044     filled = 0;
00045     memset(&buf[0][0], 0, sizeof (buf));
00046     audio.attachTx(tx_audio);
00047     audio.attachRx(rx_audio);
00048     /*  start the tx with a silent packet */
00049     audio.write((uint8_t *)buf[2]);
00050     while(1);
00051 }