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 12KB

8 vuotta sitten
8 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. // Uncomment this to work around a limitation in Macintosh adaptive rates
  37. // This is not a perfect solution. Details here:
  38. // https://forum.pjrc.com/threads/34855-Distorted-audio-when-using-USB-input-on-Teensy-3-1
  39. //#define MACOSX_ADAPTIVE_LIMIT
  40. bool AudioInputUSB::update_responsibility;
  41. audio_block_t * AudioInputUSB::incoming_left;
  42. audio_block_t * AudioInputUSB::incoming_right;
  43. audio_block_t * AudioInputUSB::ready_left;
  44. audio_block_t * AudioInputUSB::ready_right;
  45. uint16_t AudioInputUSB::incoming_count;
  46. uint8_t AudioInputUSB::receive_flag;
  47. struct usb_audio_features_struct AudioInputUSB::features = {0,0,FEATURE_MAX_VOLUME/2};
  48. #define DMABUFATTR __attribute__ ((section(".dmabuffers"), aligned (4)))
  49. uint16_t usb_audio_receive_buffer[AUDIO_RX_SIZE/2] DMABUFATTR;
  50. uint32_t usb_audio_sync_feedback DMABUFATTR;
  51. uint8_t usb_audio_receive_setting=0;
  52. static uint32_t feedback_accumulator = 185042824;
  53. void AudioInputUSB::begin(void)
  54. {
  55. incoming_count = 0;
  56. incoming_left = NULL;
  57. incoming_right = NULL;
  58. ready_left = NULL;
  59. ready_right = NULL;
  60. receive_flag = 0;
  61. // update_responsibility = update_setup();
  62. // TODO: update responsibility is tough, partly because the USB
  63. // interrupts aren't sychronous to the audio library block size,
  64. // but also because the PC may stop transmitting data, which
  65. // means we no longer get receive callbacks from usb_dev.
  66. update_responsibility = false;
  67. usb_audio_sync_feedback = feedback_accumulator >> 8;
  68. }
  69. static void copy_to_buffers(const uint32_t *src, int16_t *left, int16_t *right, unsigned int len)
  70. {
  71. uint32_t *target = (uint32_t*) src + len;
  72. while ((src < target) && (((uintptr_t) left & 0x02) != 0)) {
  73. uint32_t n = *src++;
  74. *left++ = n & 0xFFFF;
  75. *right++ = n >> 16;
  76. }
  77. while ((src < target - 2)) {
  78. uint32_t n1 = *src++;
  79. uint32_t n = *src++;
  80. *(uint32_t *)left = (n1 & 0xFFFF) | ((n & 0xFFFF) << 16);
  81. left+=2;
  82. *(uint32_t *)right = (n1 >> 16) | ((n & 0xFFFF0000)) ;
  83. right+=2;
  84. }
  85. while ((src < target)) {
  86. uint32_t n = *src++;
  87. *left++ = n & 0xFFFF;
  88. *right++ = n >> 16;
  89. }
  90. }
  91. // Called from the USB interrupt when an isochronous packet arrives
  92. // we must completely remove it from the receive buffer before returning
  93. //
  94. void usb_audio_receive_callback(unsigned int len)
  95. {
  96. unsigned int count, avail;
  97. audio_block_t *left, *right;
  98. const uint32_t *data;
  99. AudioInputUSB::receive_flag = 1;
  100. len >>= 2; // 1 sample = 4 bytes: 2 left, 2 right
  101. data = (const uint32_t *)usb_audio_receive_buffer;
  102. count = AudioInputUSB::incoming_count;
  103. left = AudioInputUSB::incoming_left;
  104. right = AudioInputUSB::incoming_right;
  105. if (left == NULL) {
  106. left = AudioStream::allocate();
  107. if (left == NULL) return;
  108. AudioInputUSB::incoming_left = left;
  109. }
  110. if (right == NULL) {
  111. right = AudioStream::allocate();
  112. if (right == NULL) return;
  113. AudioInputUSB::incoming_right = right;
  114. }
  115. while (len > 0) {
  116. avail = AUDIO_BLOCK_SAMPLES - count;
  117. if (len < avail) {
  118. copy_to_buffers(data, left->data + count, right->data + count, len);
  119. AudioInputUSB::incoming_count = count + len;
  120. return;
  121. } else if (avail > 0) {
  122. copy_to_buffers(data, left->data + count, right->data + count, avail);
  123. data += avail;
  124. len -= avail;
  125. if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) {
  126. // buffer overrun, PC sending too fast
  127. AudioInputUSB::incoming_count = count + avail;
  128. //if (len > 0) {
  129. //serial_print("!");
  130. //serial_phex(len);
  131. //}
  132. return;
  133. }
  134. send:
  135. AudioInputUSB::ready_left = left;
  136. AudioInputUSB::ready_right = right;
  137. //if (AudioInputUSB::update_responsibility) AudioStream::update_all();
  138. left = AudioStream::allocate();
  139. if (left == NULL) {
  140. AudioInputUSB::incoming_left = NULL;
  141. AudioInputUSB::incoming_right = NULL;
  142. AudioInputUSB::incoming_count = 0;
  143. return;
  144. }
  145. right = AudioStream::allocate();
  146. if (right == NULL) {
  147. AudioStream::release(left);
  148. AudioInputUSB::incoming_left = NULL;
  149. AudioInputUSB::incoming_right = NULL;
  150. AudioInputUSB::incoming_count = 0;
  151. return;
  152. }
  153. AudioInputUSB::incoming_left = left;
  154. AudioInputUSB::incoming_right = right;
  155. count = 0;
  156. } else {
  157. if (AudioInputUSB::ready_left || AudioInputUSB::ready_right) return;
  158. goto send; // recover from buffer overrun
  159. }
  160. }
  161. AudioInputUSB::incoming_count = count;
  162. }
  163. void AudioInputUSB::update(void)
  164. {
  165. audio_block_t *left, *right;
  166. __disable_irq();
  167. left = ready_left;
  168. ready_left = NULL;
  169. right = ready_right;
  170. ready_right = NULL;
  171. uint16_t c = incoming_count;
  172. uint8_t f = receive_flag;
  173. receive_flag = 0;
  174. __enable_irq();
  175. if (f) {
  176. int diff = AUDIO_BLOCK_SAMPLES/2 - (int)c;
  177. feedback_accumulator += diff / 3;
  178. uint32_t feedback = (feedback_accumulator >> 8) + diff * 100;
  179. #ifdef MACOSX_ADAPTIVE_LIMIT
  180. if (feedback > 722698) feedback = 722698;
  181. #endif
  182. usb_audio_sync_feedback = feedback;
  183. //if (diff > 0) {
  184. //serial_print(".");
  185. //} else if (diff < 0) {
  186. //serial_print("^");
  187. //}
  188. }
  189. //serial_phex(c);
  190. //serial_print(".");
  191. if (!left || !right) {
  192. //serial_print("#"); // buffer underrun - PC sending too slow
  193. //if (f) feedback_accumulator += 10 << 8;
  194. }
  195. if (left) {
  196. transmit(left, 0);
  197. release(left);
  198. }
  199. if (right) {
  200. transmit(right, 1);
  201. release(right);
  202. }
  203. }
  204. bool AudioOutputUSB::update_responsibility;
  205. audio_block_t * AudioOutputUSB::left_1st;
  206. audio_block_t * AudioOutputUSB::left_2nd;
  207. audio_block_t * AudioOutputUSB::right_1st;
  208. audio_block_t * AudioOutputUSB::right_2nd;
  209. uint16_t AudioOutputUSB::offset_1st;
  210. uint16_t usb_audio_transmit_buffer[AUDIO_TX_SIZE/2] DMABUFATTR;
  211. uint8_t usb_audio_transmit_setting=0;
  212. void AudioOutputUSB::begin(void)
  213. {
  214. update_responsibility = false;
  215. left_1st = NULL;
  216. right_1st = NULL;
  217. }
  218. static void copy_from_buffers(uint32_t *dst, int16_t *left, int16_t *right, unsigned int len)
  219. {
  220. // TODO: optimize...
  221. while (len > 0) {
  222. *dst++ = (*right++ << 16) | (*left++ & 0xFFFF);
  223. len--;
  224. }
  225. }
  226. void AudioOutputUSB::update(void)
  227. {
  228. audio_block_t *left, *right;
  229. left = receiveReadOnly(0); // input 0 = left channel
  230. right = receiveReadOnly(1); // input 1 = right channel
  231. if (usb_audio_transmit_setting == 0) {
  232. if (left) release(left);
  233. if (right) release(right);
  234. if (left_1st) { release(left_1st); left_1st = NULL; }
  235. if (left_2nd) { release(left_2nd); left_2nd = NULL; }
  236. if (right_1st) { release(right_1st); right_1st = NULL; }
  237. if (right_2nd) { release(right_2nd); right_2nd = NULL; }
  238. offset_1st = 0;
  239. return;
  240. }
  241. if (left == NULL) {
  242. if (right == NULL) return;
  243. right->ref_count++;
  244. left = right;
  245. } else if (right == NULL) {
  246. left->ref_count++;
  247. right = left;
  248. }
  249. __disable_irq();
  250. if (left_1st == NULL) {
  251. left_1st = left;
  252. right_1st = right;
  253. offset_1st = 0;
  254. } else if (left_2nd == NULL) {
  255. left_2nd = left;
  256. right_2nd = right;
  257. } else {
  258. // buffer overrun - PC is consuming too slowly
  259. audio_block_t *discard1 = left_1st;
  260. left_1st = left_2nd;
  261. left_2nd = left;
  262. audio_block_t *discard2 = right_1st;
  263. right_1st = right_2nd;
  264. right_2nd = right;
  265. offset_1st = 0; // TODO: discard part of this data?
  266. //serial_print("*");
  267. release(discard1);
  268. release(discard2);
  269. }
  270. __enable_irq();
  271. }
  272. // Called from the USB interrupt when ready to transmit another
  273. // isochronous packet. If we place data into the transmit buffer,
  274. // the return is the number of bytes. Otherwise, return 0 means
  275. // no data to transmit
  276. unsigned int usb_audio_transmit_callback(void)
  277. {
  278. static uint32_t count=5;
  279. uint32_t avail, num, target, offset, len=0;
  280. audio_block_t *left, *right;
  281. if (++count < 9) { // TODO: dynamic adjust to match USB rate
  282. target = 44;
  283. } else {
  284. count = 0;
  285. target = 45;
  286. }
  287. while (len < target) {
  288. num = target - len;
  289. left = AudioOutputUSB::left_1st;
  290. if (left == NULL) {
  291. // buffer underrun - PC is consuming too quickly
  292. memset(usb_audio_transmit_buffer + len, 0, num * 4);
  293. //serial_print("%");
  294. break;
  295. }
  296. right = AudioOutputUSB::right_1st;
  297. offset = AudioOutputUSB::offset_1st;
  298. avail = AUDIO_BLOCK_SAMPLES - offset;
  299. if (num > avail) num = avail;
  300. copy_from_buffers((uint32_t *)usb_audio_transmit_buffer + len,
  301. left->data + offset, right->data + offset, num);
  302. len += num;
  303. offset += num;
  304. if (offset >= AUDIO_BLOCK_SAMPLES) {
  305. AudioStream::release(left);
  306. AudioStream::release(right);
  307. AudioOutputUSB::left_1st = AudioOutputUSB::left_2nd;
  308. AudioOutputUSB::left_2nd = NULL;
  309. AudioOutputUSB::right_1st = AudioOutputUSB::right_2nd;
  310. AudioOutputUSB::right_2nd = NULL;
  311. AudioOutputUSB::offset_1st = 0;
  312. } else {
  313. AudioOutputUSB::offset_1st = offset;
  314. }
  315. }
  316. return target * 4;
  317. }
  318. struct setup_struct {
  319. union {
  320. struct {
  321. uint8_t bmRequestType;
  322. uint8_t bRequest;
  323. union {
  324. struct {
  325. uint8_t bChannel; // 0=main, 1=left, 2=right
  326. uint8_t bCS; // Control Selector
  327. };
  328. uint16_t wValue;
  329. };
  330. union {
  331. struct {
  332. uint8_t bIfEp; // type of entity
  333. uint8_t bEntityId; // UnitID, TerminalID, etc.
  334. };
  335. uint16_t wIndex;
  336. };
  337. uint16_t wLength;
  338. };
  339. };
  340. };
  341. int usb_audio_get_feature(void *stp, uint8_t *data, uint32_t *datalen)
  342. {
  343. struct setup_struct setup = *((struct setup_struct *)stp);
  344. if (setup.bmRequestType==0xA1) { // should check bRequest, bChannel, and UnitID
  345. if (setup.bCS==0x01) { // mute
  346. data[0] = AudioInputUSB::features.mute; // 1=mute, 0=unmute
  347. *datalen = 1;
  348. return 1;
  349. }
  350. else if (setup.bCS==0x02) { // volume
  351. if (setup.bRequest==0x81) { // GET_CURR
  352. data[0] = AudioInputUSB::features.volume & 0xFF;
  353. data[1] = (AudioInputUSB::features.volume>>8) & 0xFF;
  354. }
  355. else if (setup.bRequest==0x82) { // GET_MIN
  356. //serial_print("vol get_min\n");
  357. data[0] = 0; // min level is 0
  358. data[1] = 0;
  359. }
  360. else if (setup.bRequest==0x83) { // GET_MAX
  361. data[0] = FEATURE_MAX_VOLUME & 0xFF; // max level, for range of 0 to MAX
  362. data[1] = (FEATURE_MAX_VOLUME>>8) & 0x0F;
  363. }
  364. else if (setup.bRequest==0x84) { // GET_RES
  365. data[0] = 1; // increment vol by by 1
  366. data[1] = 0;
  367. }
  368. else { // pass over SET_MEM, etc.
  369. return 0;
  370. }
  371. *datalen = 2;
  372. return 1;
  373. }
  374. }
  375. return 0;
  376. }
  377. int usb_audio_set_feature(void *stp, uint8_t *buf)
  378. {
  379. struct setup_struct setup = *((struct setup_struct *)stp);
  380. if (setup.bmRequestType==0x21) { // should check bRequest, bChannel and UnitID
  381. if (setup.bCS==0x01) { // mute
  382. if (setup.bRequest==0x01) { // SET_CUR
  383. AudioInputUSB::features.mute = buf[0]; // 1=mute,0=unmute
  384. AudioInputUSB::features.change = 1;
  385. return 1;
  386. }
  387. }
  388. else if (setup.bCS==0x02) { // volume
  389. if (setup.bRequest==0x01) { // SET_CUR
  390. AudioInputUSB::features.volume = buf[0] + (buf[1]<<8);
  391. AudioInputUSB::features.change = 1;
  392. return 1;
  393. }
  394. }
  395. }
  396. return 0;
  397. }
  398. #endif // F_CPU
  399. #endif // AUDIO_INTERFACE