|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
-
-
- #include "usb_dev.h"
- #include "usb_audio.h"
- #include "HardwareSerial.h"
- #include <string.h> // for memcpy()
-
- #ifdef AUDIO_INTERFACE
- #if F_CPU >= 20000000
-
- bool AudioInputUSB::update_responsibility;
- audio_block_t * AudioInputUSB::incoming_left;
- audio_block_t * AudioInputUSB::incoming_right;
- audio_block_t * AudioInputUSB::ready_left;
- audio_block_t * AudioInputUSB::ready_right;
- uint16_t AudioInputUSB::incoming_count;
- uint8_t AudioInputUSB::receive_flag;
-
- #define DMABUFATTR __attribute__ ((section(".dmabuffers"), aligned (4)))
- uint16_t usb_audio_receive_buffer[AUDIO_RX_SIZE/2] DMABUFATTR;
- uint32_t usb_audio_sync_feedback DMABUFATTR;
- uint8_t usb_audio_receive_setting=0;
-
- static uint32_t feedback_accumulator = 185042824;
-
- void AudioInputUSB::begin(void)
- {
- incoming_count = 0;
- incoming_left = NULL;
- incoming_right = NULL;
- ready_left = NULL;
- ready_right = NULL;
- receive_flag = 0;
-
-
-
-
-
- update_responsibility = false;
-
-
- usb_audio_sync_feedback = 722534;
- }
-
- static void copy_to_buffers(const uint32_t *src, int16_t *left, int16_t *right, unsigned int len)
- {
-
- while (len > 0) {
- uint32_t n = *src++;
- *left++ = n & 0xFFFF;
- *right++ = n >> 16;
- len--;
- }
- }
-
-
-
-
- void usb_audio_receive_callback(unsigned int len)
- {
- unsigned int count, avail;
- audio_block_t *left, *right;
- const uint32_t *data;
-
- AudioInputUSB::receive_flag = 1;
- len >>= 2;
- data = (const uint32_t *)usb_audio_receive_buffer;
-
- count = AudioInputUSB::incoming_count;
- left = AudioInputUSB::incoming_left;
- right = AudioInputUSB::incoming_right;
- if (left == NULL) {
- left = AudioStream::allocate();
- if (left == NULL) return;
- AudioInputUSB::incoming_left = left;
- }
- if (right == NULL) {
- right = AudioStream::allocate();
- if (right == NULL) return;
- AudioInputUSB::incoming_right = right;
- }
- while (len > 0) {
- avail = AUDIO_BLOCK_SAMPLES - count;
- if (len < avail) {
- copy_to_buffers(data, left->data + count, right->data + count, len);
- AudioInputUSB::incoming_count = count + len;
- return;
- } else if (avail > 0) {
- copy_to_buffers(data, left->data + count, right->data + count, avail);
- data += avail;
- len -= avail;
- if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) {
-
- AudioInputUSB::incoming_count = count + avail;
-
-
-
-
- return;
- }
- send:
- AudioInputUSB::ready_left = left;
- AudioInputUSB::ready_right = right;
-
- left = AudioStream::allocate();
- if (left == NULL) {
- AudioInputUSB::incoming_left = NULL;
- AudioInputUSB::incoming_right = NULL;
- AudioInputUSB::incoming_count = 0;
- return;
- }
- right = AudioStream::allocate();
- if (right == NULL) {
- AudioStream::release(left);
- AudioInputUSB::incoming_left = NULL;
- AudioInputUSB::incoming_right = NULL;
- AudioInputUSB::incoming_count = 0;
- return;
- }
- AudioInputUSB::incoming_left = left;
- AudioInputUSB::incoming_right = right;
- count = 0;
- } else {
- if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) return;
- goto send;
- }
- }
- AudioInputUSB::incoming_count = count;
- }
-
- void AudioInputUSB::update(void)
- {
- audio_block_t *left, *right;
-
- __disable_irq();
- left = ready_left;
- ready_left = NULL;
- right = ready_right;
- ready_right = NULL;
- uint16_t c = incoming_count;
- uint8_t f = receive_flag;
- receive_flag = 0;
- __enable_irq();
- if (f) {
- int diff = AUDIO_BLOCK_SAMPLES/2 - (int)c;
- feedback_accumulator += diff;
-
- usb_audio_sync_feedback = (feedback_accumulator >> 8) + diff * 3;
-
-
-
-
-
- }
-
-
- if (!left || !right) {
-
- if (f) feedback_accumulator += 10 << 8;
- }
- if (left) {
- transmit(left, 0);
- release(left);
- }
- if (right) {
- transmit(right, 1);
- release(right);
- }
- }
-
-
-
-
-
-
-
- bool AudioOutputUSB::update_responsibility;
- audio_block_t * AudioOutputUSB::left_1st;
- audio_block_t * AudioOutputUSB::left_2nd;
- audio_block_t * AudioOutputUSB::right_1st;
- audio_block_t * AudioOutputUSB::right_2nd;
- uint16_t AudioOutputUSB::offset_1st;
-
-
- uint16_t usb_audio_transmit_buffer[AUDIO_TX_SIZE/2] DMABUFATTR;
- uint8_t usb_audio_transmit_setting=0;
-
- void AudioOutputUSB::begin(void)
- {
- update_responsibility = false;
- left_1st = NULL;
- right_1st = NULL;
- }
-
- static void copy_from_buffers(uint32_t *dst, int16_t *left, int16_t *right, unsigned int len)
- {
-
- while (len > 0) {
- *dst++ = (*right++ << 16) | (*left++ & 0xFFFF);
- len--;
- }
- }
-
- void AudioOutputUSB::update(void)
- {
- audio_block_t *left, *right;
-
- left = receiveReadOnly(0);
- right = receiveReadOnly(1);
- if (usb_audio_transmit_setting == 0) {
- if (left) release(left);
- if (right) release(right);
- if (left_1st) { release(left_1st); left_1st = NULL; }
- if (left_2nd) { release(left_2nd); left_2nd = NULL; }
- if (right_1st) { release(right_1st); right_1st = NULL; }
- if (right_2nd) { release(right_2nd); right_2nd = NULL; }
- offset_1st = 0;
- return;
- }
- if (left == NULL) {
- if (right == NULL) return;
- right->ref_count++;
- left = right;
- } else if (right == NULL) {
- left->ref_count++;
- right = left;
- }
- __disable_irq();
- if (left_1st == NULL) {
- left_1st = left;
- right_1st = right;
- offset_1st = 0;
- } else if (left_2nd == NULL) {
- left_2nd = left;
- right_2nd = right;
- } else {
-
- audio_block_t *discard1 = left_1st;
- left_1st = left_2nd;
- left_2nd = left;
- audio_block_t *discard2 = right_1st;
- right_1st = right_2nd;
- right_2nd = right;
- offset_1st = 0;
-
- release(discard1);
- release(discard2);
- }
- __enable_irq();
- }
-
-
-
-
-
-
- unsigned int usb_audio_transmit_callback(void)
- {
- static uint32_t count=5;
- uint32_t avail, num, target, offset, len=0;
- audio_block_t *left, *right;
-
- if (++count < 9) {
- target = 44;
- } else {
- count = 0;
- target = 45;
- }
- while (len < target) {
- num = target - len;
- left = AudioOutputUSB::left_1st;
- if (left == NULL) {
-
- memset(usb_audio_transmit_buffer + len, 0, num * 4);
-
- break;
- }
- right = AudioOutputUSB::right_1st;
- offset = AudioOutputUSB::offset_1st;
-
- avail = AUDIO_BLOCK_SAMPLES - offset;
- if (num > avail) num = avail;
-
- copy_from_buffers((uint32_t *)usb_audio_transmit_buffer + len,
- left->data + offset, right->data + offset, num);
- len += num;
- offset += num;
- if (offset >= AUDIO_BLOCK_SAMPLES) {
- AudioStream::release(left);
- AudioStream::release(right);
- AudioOutputUSB::left_1st = AudioOutputUSB::left_2nd;
- AudioOutputUSB::left_2nd = NULL;
- AudioOutputUSB::right_1st = AudioOutputUSB::right_2nd;
- AudioOutputUSB::right_2nd = NULL;
- AudioOutputUSB::offset_1st = 0;
- } else {
- AudioOutputUSB::offset_1st = offset;
- }
- }
- return target * 4;
- }
-
-
- #endif
- #endif
|