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.

409 lines
11KB

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