You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb_audio.cpp 9.7KB

8 jaren geleden
8 jaren geleden
8 jaren geleden
8 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2016 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "usb_dev.h"
  31. #include "usb_audio.h"
  32. #include "HardwareSerial.h"
  33. #include <string.h> // for memcpy()
  34. #ifdef AUDIO_INTERFACE // defined by usb_dev.h -> usb_desc.h
  35. #if F_CPU >= 20000000
  36. bool AudioInputUSB::update_responsibility;
  37. audio_block_t * AudioInputUSB::incoming_left;
  38. audio_block_t * AudioInputUSB::incoming_right;
  39. audio_block_t * AudioInputUSB::ready_left;
  40. audio_block_t * AudioInputUSB::ready_right;
  41. uint16_t AudioInputUSB::incoming_count;
  42. uint8_t AudioInputUSB::receive_flag;
  43. #define DMABUFATTR __attribute__ ((section(".dmabuffers"), aligned (4)))
  44. uint16_t usb_audio_receive_buffer[AUDIO_RX_SIZE/2] DMABUFATTR;
  45. uint32_t usb_audio_sync_feedback DMABUFATTR;
  46. uint8_t usb_audio_receive_setting=0;
  47. static uint32_t feedback_accumulator = 185042824;
  48. void AudioInputUSB::begin(void)
  49. {
  50. incoming_count = 0;
  51. incoming_left = NULL;
  52. incoming_right = NULL;
  53. ready_left = NULL;
  54. ready_right = NULL;
  55. receive_flag = 0;
  56. // update_responsibility = update_setup();
  57. // TODO: update responsibility is tough, partly because the USB
  58. // interrupts aren't sychronous to the audio library block size,
  59. // but also because the PC may stop transmitting data, which
  60. // means we no longer get receive callbacks from usb_dev.
  61. update_responsibility = false;
  62. //usb_audio_sync_feedback = 722824;
  63. //usb_audio_sync_feedback = 723700; // too fast?
  64. usb_audio_sync_feedback = 722534; // too slow
  65. }
  66. static void copy_to_buffers(const uint32_t *src, int16_t *left, int16_t *right, unsigned int len)
  67. {
  68. uint32_t *target = (uint32_t*) src + len;
  69. while ((src < target) && (((uintptr_t) left & 0x02) != 0)) {
  70. uint32_t n = *src++;
  71. *left++ = n & 0xFFFF;
  72. *right++ = n >> 16;
  73. }
  74. while ((src < target - 2)) {
  75. uint32_t n = *src++;
  76. uint32_t n1 = *src++;
  77. *(uint32_t *)left = n1 & 0xFFFF | ((n & 0xFFFF) << 16);
  78. left+=2;
  79. *(uint32_t *)right = (n1 >> 16) | ((n & 0xFFFF0000)) ;
  80. right+=2;
  81. }
  82. while ((src < target)) {
  83. uint32_t n = *src++;
  84. *left++ = n & 0xFFFF;
  85. *right++ = n >> 16;
  86. }
  87. }
  88. // Called from the USB interrupt when an isochronous packet arrives
  89. // we must completely remove it from the receive buffer before returning
  90. //
  91. void usb_audio_receive_callback(unsigned int len)
  92. {
  93. unsigned int count, avail;
  94. audio_block_t *left, *right;
  95. const uint32_t *data;
  96. AudioInputUSB::receive_flag = 1;
  97. len >>= 2; // 1 sample = 4 bytes: 2 left, 2 right
  98. data = (const uint32_t *)usb_audio_receive_buffer;
  99. count = AudioInputUSB::incoming_count;
  100. left = AudioInputUSB::incoming_left;
  101. right = AudioInputUSB::incoming_right;
  102. if (left == NULL) {
  103. left = AudioStream::allocate();
  104. if (left == NULL) return;
  105. AudioInputUSB::incoming_left = left;
  106. }
  107. if (right == NULL) {
  108. right = AudioStream::allocate();
  109. if (right == NULL) return;
  110. AudioInputUSB::incoming_right = right;
  111. }
  112. while (len > 0) {
  113. avail = AUDIO_BLOCK_SAMPLES - count;
  114. if (len < avail) {
  115. copy_to_buffers(data, left->data + count, right->data + count, len);
  116. AudioInputUSB::incoming_count = count + len;
  117. return;
  118. } else if (avail > 0) {
  119. copy_to_buffers(data, left->data + count, right->data + count, avail);
  120. data += avail;
  121. len -= avail;
  122. if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) {
  123. // buffer overrun, PC sending too fast
  124. AudioInputUSB::incoming_count = count + avail;
  125. //if (len > 0) {
  126. //serial_print("!");
  127. //serial_phex(len);
  128. //}
  129. return;
  130. }
  131. send:
  132. AudioInputUSB::ready_left = left;
  133. AudioInputUSB::ready_right = right;
  134. //if (AudioInputUSB::update_responsibility) AudioStream::update_all();
  135. left = AudioStream::allocate();
  136. if (left == NULL) {
  137. AudioInputUSB::incoming_left = NULL;
  138. AudioInputUSB::incoming_right = NULL;
  139. AudioInputUSB::incoming_count = 0;
  140. return;
  141. }
  142. right = AudioStream::allocate();
  143. if (right == NULL) {
  144. AudioStream::release(left);
  145. AudioInputUSB::incoming_left = NULL;
  146. AudioInputUSB::incoming_right = NULL;
  147. AudioInputUSB::incoming_count = 0;
  148. return;
  149. }
  150. AudioInputUSB::incoming_left = left;
  151. AudioInputUSB::incoming_right = right;
  152. count = 0;
  153. } else {
  154. if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) return;
  155. goto send; // recover from buffer overrun
  156. }
  157. }
  158. AudioInputUSB::incoming_count = count;
  159. }
  160. void AudioInputUSB::update(void)
  161. {
  162. audio_block_t *left, *right;
  163. __disable_irq();
  164. left = ready_left;
  165. ready_left = NULL;
  166. right = ready_right;
  167. ready_right = NULL;
  168. uint16_t c = incoming_count;
  169. uint8_t f = receive_flag;
  170. receive_flag = 0;
  171. __enable_irq();
  172. if (f) {
  173. int diff = AUDIO_BLOCK_SAMPLES/2 - (int)c;
  174. feedback_accumulator += diff;
  175. // TODO: min/max sanity check for feedback_accumulator??
  176. usb_audio_sync_feedback = (feedback_accumulator >> 8) + diff * 3;
  177. //if (diff > 0) {
  178. //serial_print(".");
  179. //} else if (diff < 0) {
  180. //serial_print("^");
  181. //}
  182. }
  183. //serial_phex(c);
  184. //serial_print(".");
  185. if (!left || !right) {
  186. //serial_print("#"); // buffer underrun - PC sending too slow
  187. if (f) feedback_accumulator += 10 << 8;
  188. }
  189. if (left) {
  190. transmit(left, 0);
  191. release(left);
  192. }
  193. if (right) {
  194. transmit(right, 1);
  195. release(right);
  196. }
  197. }
  198. bool AudioOutputUSB::update_responsibility;
  199. audio_block_t * AudioOutputUSB::left_1st;
  200. audio_block_t * AudioOutputUSB::left_2nd;
  201. audio_block_t * AudioOutputUSB::right_1st;
  202. audio_block_t * AudioOutputUSB::right_2nd;
  203. uint16_t AudioOutputUSB::offset_1st;
  204. uint16_t usb_audio_transmit_buffer[AUDIO_TX_SIZE/2] DMABUFATTR;
  205. uint8_t usb_audio_transmit_setting=0;
  206. void AudioOutputUSB::begin(void)
  207. {
  208. update_responsibility = false;
  209. left_1st = NULL;
  210. right_1st = NULL;
  211. }
  212. static void copy_from_buffers(uint32_t *dst, int16_t *left, int16_t *right, unsigned int len)
  213. {
  214. // TODO: optimize...
  215. while (len > 0) {
  216. *dst++ = (*right++ << 16) | (*left++ & 0xFFFF);
  217. len--;
  218. }
  219. }
  220. void AudioOutputUSB::update(void)
  221. {
  222. audio_block_t *left, *right;
  223. left = receiveReadOnly(0); // input 0 = left channel
  224. right = receiveReadOnly(1); // input 1 = right channel
  225. if (usb_audio_transmit_setting == 0) {
  226. if (left) release(left);
  227. if (right) release(right);
  228. if (left_1st) { release(left_1st); left_1st = NULL; }
  229. if (left_2nd) { release(left_2nd); left_2nd = NULL; }
  230. if (right_1st) { release(right_1st); right_1st = NULL; }
  231. if (right_2nd) { release(right_2nd); right_2nd = NULL; }
  232. offset_1st = 0;
  233. return;
  234. }
  235. if (left == NULL) {
  236. if (right == NULL) return;
  237. right->ref_count++;
  238. left = right;
  239. } else if (right == NULL) {
  240. left->ref_count++;
  241. right = left;
  242. }
  243. __disable_irq();
  244. if (left_1st == NULL) {
  245. left_1st = left;
  246. right_1st = right;
  247. offset_1st = 0;
  248. } else if (left_2nd == NULL) {
  249. left_2nd = left;
  250. right_2nd = right;
  251. } else {
  252. // buffer overrun - PC is consuming too slowly
  253. audio_block_t *discard1 = left_1st;
  254. left_1st = left_2nd;
  255. left_2nd = left;
  256. audio_block_t *discard2 = right_1st;
  257. right_1st = right_2nd;
  258. right_2nd = right;
  259. offset_1st = 0; // TODO: discard part of this data?
  260. //serial_print("*");
  261. release(discard1);
  262. release(discard2);
  263. }
  264. __enable_irq();
  265. }
  266. // Called from the USB interrupt when ready to transmit another
  267. // isochronous packet. If we place data into the transmit buffer,
  268. // the return is the number of bytes. Otherwise, return 0 means
  269. // no data to transmit
  270. unsigned int usb_audio_transmit_callback(void)
  271. {
  272. static uint32_t count=5;
  273. uint32_t avail, num, target, offset, len=0;
  274. audio_block_t *left, *right;
  275. if (++count < 9) { // TODO: dynamic adjust to match USB rate
  276. target = 44;
  277. } else {
  278. count = 0;
  279. target = 45;
  280. }
  281. while (len < target) {
  282. num = target - len;
  283. left = AudioOutputUSB::left_1st;
  284. if (left == NULL) {
  285. // buffer underrun - PC is consuming too quickly
  286. memset(usb_audio_transmit_buffer + len, 0, num * 4);
  287. //serial_print("%");
  288. break;
  289. }
  290. right = AudioOutputUSB::right_1st;
  291. offset = AudioOutputUSB::offset_1st;
  292. avail = AUDIO_BLOCK_SAMPLES - offset;
  293. if (num > avail) num = avail;
  294. copy_from_buffers((uint32_t *)usb_audio_transmit_buffer + len,
  295. left->data + offset, right->data + offset, num);
  296. len += num;
  297. offset += num;
  298. if (offset >= AUDIO_BLOCK_SAMPLES) {
  299. AudioStream::release(left);
  300. AudioStream::release(right);
  301. AudioOutputUSB::left_1st = AudioOutputUSB::left_2nd;
  302. AudioOutputUSB::left_2nd = NULL;
  303. AudioOutputUSB::right_1st = AudioOutputUSB::right_2nd;
  304. AudioOutputUSB::right_2nd = NULL;
  305. AudioOutputUSB::offset_1st = 0;
  306. } else {
  307. AudioOutputUSB::offset_1st = offset;
  308. }
  309. }
  310. return target * 4;
  311. }
  312. #endif // F_CPU
  313. #endif // AUDIO_INTERFACE