96kHz-16bit USB Audio Output interface with UDA1345 and 24.576MHz Xtal.
Dependencies: I2S USBDevice mbed
Fork of USBAudioPlayback by
main.cpp@4:a3aee5fc768a, 2014-02-04 (annotated)
- Committer:
- edy555
- Date:
- Tue Feb 04 15:35:52 2014 +0000
- Revision:
- 4:a3aee5fc768a
- Parent:
- 3:44343b9dd8b6
- Child:
- 5:ca2316551f3d
remote input channel, reduce buffer size, change used api from readWrite to read
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:eb53799c0b97 | 1 | // Playback example with the USBAUDIO library |
samux | 0:eb53799c0b97 | 2 | |
samux | 0:eb53799c0b97 | 3 | #include "mbed.h" |
samux | 0:eb53799c0b97 | 4 | #include "USBAudio.h" |
edy555 | 3:44343b9dd8b6 | 5 | #include "I2S.h" |
samux | 0:eb53799c0b97 | 6 | |
samux | 0:eb53799c0b97 | 7 | // frequency: 48 kHz |
edy555 | 3:44343b9dd8b6 | 8 | #define FREQ_SPK 96000 |
edy555 | 3:44343b9dd8b6 | 9 | #define FREQ_MIC 8000 |
samux | 0:eb53799c0b97 | 10 | |
samux | 0:eb53799c0b97 | 11 | // 2channels: stereo |
samux | 0:eb53799c0b97 | 12 | #define NB_CHA_SPK 2 |
edy555 | 4:a3aee5fc768a | 13 | #define NB_CHA_MIC 0 |
samux | 0:eb53799c0b97 | 14 | |
samux | 0:eb53799c0b97 | 15 | // length computed: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there are two channels, the length will be 48 * 2 * 2 |
samux | 0:eb53799c0b97 | 16 | #define LENGTH_AUDIO_PACKET_SPK (FREQ_SPK / 500) * NB_CHA_SPK |
samux | 0:eb53799c0b97 | 17 | #define LENGTH_AUDIO_PACKET_MIC (FREQ_MIC / 500) * NB_CHA_MIC |
samux | 0:eb53799c0b97 | 18 | |
samux | 0:eb53799c0b97 | 19 | // USBAudio object |
samux | 0:eb53799c0b97 | 20 | USBAudio audio(FREQ_SPK, NB_CHA_SPK, FREQ_MIC, NB_CHA_MIC, 0xab45, 0x0378); |
samux | 0:eb53799c0b97 | 21 | |
edy555 | 3:44343b9dd8b6 | 22 | DigitalOut myled(LED1); |
edy555 | 3:44343b9dd8b6 | 23 | |
edy555 | 3:44343b9dd8b6 | 24 | I2S i2s(I2S_TRANSMIT, p5, p6, p7); |
edy555 | 3:44343b9dd8b6 | 25 | I2S i2srx(I2S_RECEIVE, p8, p29, p15); |
edy555 | 3:44343b9dd8b6 | 26 | |
edy555 | 4:a3aee5fc768a | 27 | #define CHUNKS 6 |
edy555 | 3:44343b9dd8b6 | 28 | |
edy555 | 3:44343b9dd8b6 | 29 | int buf_out[LENGTH_AUDIO_PACKET_MIC/sizeof(int)]; |
edy555 | 3:44343b9dd8b6 | 30 | int16_t buf_in[CHUNKS][LENGTH_AUDIO_PACKET_SPK/sizeof(int16_t)]; |
edy555 | 3:44343b9dd8b6 | 31 | int16_t * stream_in = NULL; |
edy555 | 3:44343b9dd8b6 | 32 | int16_t * stream_in_tail = NULL; |
edy555 | 3:44343b9dd8b6 | 33 | int read_pos = 0; |
edy555 | 3:44343b9dd8b6 | 34 | int write_pos = 0; |
edy555 | 3:44343b9dd8b6 | 35 | |
edy555 | 3:44343b9dd8b6 | 36 | void genwave() |
edy555 | 3:44343b9dd8b6 | 37 | { |
edy555 | 3:44343b9dd8b6 | 38 | int buf[8]; |
edy555 | 3:44343b9dd8b6 | 39 | if (stream_in == NULL) { |
edy555 | 3:44343b9dd8b6 | 40 | buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = buf[7] = 0; |
edy555 | 3:44343b9dd8b6 | 41 | i2s.write(buf, 6); |
edy555 | 3:44343b9dd8b6 | 42 | return; |
edy555 | 3:44343b9dd8b6 | 43 | } |
edy555 | 3:44343b9dd8b6 | 44 | |
edy555 | 3:44343b9dd8b6 | 45 | myled = 1; |
edy555 | 3:44343b9dd8b6 | 46 | buf[0] = stream_in[0]; |
edy555 | 3:44343b9dd8b6 | 47 | buf[1] = stream_in[1]; |
edy555 | 3:44343b9dd8b6 | 48 | buf[2] = stream_in[2]; |
edy555 | 3:44343b9dd8b6 | 49 | buf[3] = stream_in[3]; |
edy555 | 3:44343b9dd8b6 | 50 | buf[4] = stream_in[4]; |
edy555 | 3:44343b9dd8b6 | 51 | buf[5] = stream_in[5]; |
edy555 | 3:44343b9dd8b6 | 52 | buf[6] = stream_in[6]; |
edy555 | 3:44343b9dd8b6 | 53 | buf[7] = stream_in[7]; |
edy555 | 3:44343b9dd8b6 | 54 | i2s.write(buf, 8); |
edy555 | 3:44343b9dd8b6 | 55 | stream_in += 8; |
edy555 | 3:44343b9dd8b6 | 56 | if (stream_in >= stream_in_tail) { |
edy555 | 3:44343b9dd8b6 | 57 | write_pos++; |
edy555 | 3:44343b9dd8b6 | 58 | write_pos %= CHUNKS; |
edy555 | 3:44343b9dd8b6 | 59 | stream_in = buf_in[write_pos]; |
edy555 | 3:44343b9dd8b6 | 60 | stream_in_tail = &buf_in[write_pos][LENGTH_AUDIO_PACKET_SPK/sizeof(int16_t)]; |
edy555 | 3:44343b9dd8b6 | 61 | } |
edy555 | 3:44343b9dd8b6 | 62 | myled = 0; |
edy555 | 3:44343b9dd8b6 | 63 | } |
edy555 | 3:44343b9dd8b6 | 64 | |
samux | 0:eb53799c0b97 | 65 | int main() { |
edy555 | 3:44343b9dd8b6 | 66 | i2srx.masterslave(I2S_SLAVE); |
edy555 | 3:44343b9dd8b6 | 67 | i2srx.frequency(96000); |
edy555 | 3:44343b9dd8b6 | 68 | i2srx.start(); |
edy555 | 3:44343b9dd8b6 | 69 | i2s.attach(genwave); |
edy555 | 3:44343b9dd8b6 | 70 | i2s.masterslave(I2S_MASTER); |
edy555 | 3:44343b9dd8b6 | 71 | i2s.wordsize(16); |
edy555 | 3:44343b9dd8b6 | 72 | i2s.frequency(96000); |
edy555 | 3:44343b9dd8b6 | 73 | i2s.set_interrupt_fifo_level(0); |
edy555 | 3:44343b9dd8b6 | 74 | i2s.start(); |
samux | 0:eb53799c0b97 | 75 | |
samux | 0:eb53799c0b97 | 76 | while (1) { |
edy555 | 4:a3aee5fc768a | 77 | audio.read((uint8_t *)buf_in[read_pos++]); |
edy555 | 3:44343b9dd8b6 | 78 | read_pos %= CHUNKS; |
edy555 | 3:44343b9dd8b6 | 79 | if (stream_in == NULL && read_pos > CHUNKS / 2) { |
edy555 | 3:44343b9dd8b6 | 80 | stream_in = buf_in[write_pos]; |
edy555 | 3:44343b9dd8b6 | 81 | stream_in_tail = &buf_in[write_pos][LENGTH_AUDIO_PACKET_SPK/sizeof(int16_t)]; |
edy555 | 3:44343b9dd8b6 | 82 | } |
samux | 0:eb53799c0b97 | 83 | } |
samux | 0:eb53799c0b97 | 84 | } |