Browse Source

Handle usb audio buffer overrun more gracefully

teensy4-core
PaulStoffregen 8 years ago
parent
commit
50e173b3fc
1 changed files with 15 additions and 7 deletions
  1. +15
    -7
      teensy3/usb_audio.cpp

+ 15
- 7
teensy3/usb_audio.cpp View File

// means we no longer get receive callbacks from usb_dev. // means we no longer get receive callbacks from usb_dev.
update_responsibility = false; update_responsibility = false;
usb_audio_sync_feedback = 722824; usb_audio_sync_feedback = 722824;
usb_audio_sync_feedback = 723700; // too fast?
//usb_audio_sync_feedback = 722534; // too slow
} }


static void copy_to_buffers(const uint32_t *src, int16_t *left, int16_t *right, unsigned int len) static void copy_to_buffers(const uint32_t *src, int16_t *left, int16_t *right, unsigned int len)
copy_to_buffers(data, left->data + count, right->data + count, len); copy_to_buffers(data, left->data + count, right->data + count, len);
AudioInputUSB::incoming_count = count + len; AudioInputUSB::incoming_count = count + len;
return; return;
} else {
} else if (avail > 0) {
//serial_print("^"); //serial_print("^");
copy_to_buffers(data, left->data + count, right->data + count, avail); copy_to_buffers(data, left->data + count, right->data + count, avail);
data += avail; data += avail;
len -= avail; len -= avail;
if (AudioInputUSB::ready_left) // buffer overrun, PC sending too fast
AudioStream::release(AudioInputUSB::ready_left);
if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) {
// buffer overrun, PC sending too fast
AudioInputUSB::incoming_count = count + avail;
serial_print("!");
return;
}
send:
AudioInputUSB::ready_left = left; AudioInputUSB::ready_left = left;
if (AudioInputUSB::ready_right)
AudioStream::release(AudioInputUSB::ready_right);
AudioInputUSB::ready_right = right; AudioInputUSB::ready_right = right;
if (AudioInputUSB::update_responsibility) AudioStream::update_all();
//if (AudioInputUSB::update_responsibility) AudioStream::update_all();
left = AudioStream::allocate(); left = AudioStream::allocate();
if (left == NULL) { if (left == NULL) {
AudioInputUSB::incoming_left = NULL; AudioInputUSB::incoming_left = NULL;
} else { } else {
count = 0; count = 0;
} }
} else {
if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) return;
goto send; // recover from buffer overrun
} }
} }
AudioInputUSB::incoming_count = count; AudioInputUSB::incoming_count = count;
//serial_print("."); //serial_print(".");
if (!left || !right) { if (!left || !right) {
underflow_flag = 1; underflow_flag = 1;
//serial_print("#"); // buffer underrun - PC sending too slow
serial_print("#"); // buffer underrun - PC sending too slow
} }
if (left) { if (left) {
transmit(left, 0); transmit(left, 0);

Loading…
Cancel
Save