Skip to content

Commit 71f570f

Browse files
committed
USB_8 : audio loopback test at 16khz stereo 16bits
- Callback TX and RX interface is used.
1 parent 3a6a5ab commit 71f570f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Playback example with the USBAUDIO library
2+
3+
#include "mbed.h"
4+
#include "USBAudio.h"
5+
6+
// frequency: 48 kHz
7+
#define FREQ_SPK 16000
8+
#define FREQ_MIC 16000
9+
10+
// 2channels: stereo
11+
#define NB_CHA_SPK 2
12+
#define NB_CHA_MIC 2
13+
14+
// length computed: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there are two channels, the length will be 48 * 2 * 2
15+
#define LENGTH_AUDIO_PACKET_SPK (FREQ_SPK / 500) * NB_CHA_SPK
16+
#define LENGTH_AUDIO_PACKET_MIC (FREQ_MIC / 500) * NB_CHA_MIC
17+
18+
// USBAudio object
19+
USBAudio audio(FREQ_SPK, NB_CHA_SPK, FREQ_MIC, NB_CHA_MIC, 0xab45, 0x0378);
20+
int filled;
21+
int ready = 2;
22+
23+
/* buffer 4 packets to avoid */
24+
int buf[4][LENGTH_AUDIO_PACKET_SPK/sizeof(int)];
25+
void tx_audio(void)
26+
{
27+
/* used some buffer in advance */
28+
ready = (filled+2)&0x3;
29+
audio.writeSync((uint8_t *)buf[ready]);
30+
}
31+
32+
33+
void rx_audio(void)
34+
{
35+
int size=0;
36+
int next = (filled + 1)&0x3;
37+
size = audio.readSync((uint8_t *)buf[next]);
38+
if (size ) filled = next;
39+
if ((size) && (size!=LENGTH_AUDIO_PACKET_MIC)) printf("%d\n",size);
40+
}
41+
42+
int main()
43+
{
44+
filled = 0;
45+
memset(&buf[0][0], 0, sizeof (buf));
46+
audio.attachTx(tx_audio);
47+
audio.attachRx(rx_audio);
48+
/* start the tx with a silent packet */
49+
audio.write((uint8_t *)buf[2]);
50+
while(1);
51+
}

tools/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,11 @@
10031003
"source_dir": join(TEST_DIR, "usb", "device", "audio"),
10041004
"dependencies": [MBED_LIBRARIES, USB_LIBRARIES],
10051005
},
1006+
{
1007+
"id": "USB_8", "description": "AUDIO_CB",
1008+
"source_dir": join(TEST_DIR, "usb", "device", "audio_cb"),
1009+
"dependencies": [MBED_LIBRARIES, USB_LIBRARIES],
1010+
},
10061011

10071012
# CMSIS DSP
10081013
{

0 commit comments

Comments
 (0)