audio.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. #include "includes.h"
  2. #include "asm/includes.h"
  3. #include "app_config.h"
  4. #include "system/timer.h"
  5. #include "device/ioctl_cmds.h"
  6. #include "device_drive.h"
  7. #if TCFG_HOST_AUDIO_ENABLE
  8. #include "usb/host/usb_host.h"
  9. #include "usb_ctrl_transfer.h"
  10. #include "usb_bulk_transfer.h"
  11. #include "audio.h"
  12. #include "usb_config.h"
  13. #define LOG_TAG_CONST USB
  14. #define LOG_TAG "[AUDIO]"
  15. #define LOG_ERROR_ENABLE
  16. #define LOG_DEBUG_ENABLE
  17. #define LOG_INFO_ENABLE
  18. /* #define LOG_DUMP_ENABLE */
  19. #define LOG_CLI_ENABLE
  20. #include "debug.h"
  21. /* #define USB_AUDIO_PLAY_TEST */
  22. struct usb_audio_play {
  23. u32 sample_rate;
  24. u8 Cur_AlternateSetting;
  25. u8 src_channel;
  26. u8 play_state;
  27. u8 mute;
  28. u8 *send_buf;
  29. u8 *usb_audio_play_buf;
  30. u8 *usb_audio_play_buf2;
  31. cbuffer_t usb_audio_play_cbuf;
  32. u32 usb_audio_remain_len;
  33. OS_SEM sem;
  34. int (*put_buf)(void *ptr, u32 len);
  35. };
  36. struct usb_audio_mic {
  37. u8 Cur_AlternateSetting;
  38. u32 sample_rate;
  39. u8 record_state;
  40. u8 *usb_audio_record_buf;
  41. u8 *usb_audio_record_buf2;
  42. cbuffer_t usb_audio_record_cbuf;
  43. int *(*get_buf)(void *ptr, u32 len);
  44. };
  45. struct usb_audio_info {
  46. usb_dev usb_id;
  47. struct usb_audio_play player;
  48. struct usb_audio_mic microphone;
  49. };
  50. enum {
  51. AUDIO_PLAY_IDLE = 0,
  52. AUDIO_PLAY_START,
  53. AUDIO_PLAY_STOP,
  54. AUDIO_PLAY_PAUSE,
  55. };
  56. enum {
  57. AUDIO_RECORD_IDLE = 0,
  58. AUDIO_RECORD_START,
  59. AUDIO_RECORD_STOP,
  60. AUDIO_RECORD_PAUSE,
  61. };
  62. #define EP_MAX_PACKET_SIZE (192)
  63. struct usb_audio_info _usb_audio_info = {0};
  64. #define __this (&_usb_audio_info)
  65. struct audio_device_t audio_device[USB_MAX_HW_NUM][MAX_HOST_INTERFACE];
  66. static u8 ep_in_dma_buf[256] __attribute__((aligned(4)));
  67. static u8 ep_out_dma_buf[256] __attribute__((aligned(4)));
  68. static int set_power(struct usb_host_device *host_dev, u32 value)
  69. {
  70. const usb_dev usb_id = host_device2id(host_dev);
  71. return DEV_ERR_NONE;
  72. }
  73. static int get_power(struct usb_host_device *host_dev, u32 value)
  74. {
  75. return DEV_ERR_NONE;
  76. }
  77. static const struct interface_ctrl uac_ctrl = {
  78. .interface_class = USB_CLASS_AUDIO,
  79. .set_power = set_power,
  80. .get_power = get_power,
  81. .ioctl = NULL,
  82. };
  83. static const struct usb_interface_info _uac_if[USB_MAX_HW_NUM][MAX_HOST_INTERFACE] = {
  84. {
  85. {
  86. .ctrl = (struct interface_ctrl *) &uac_ctrl,
  87. .dev.audio = &audio_device[0][0],
  88. },
  89. {
  90. .ctrl = (struct interface_ctrl *) &uac_ctrl,
  91. .dev.audio = &audio_device[0][1],
  92. },
  93. {
  94. .ctrl = (struct interface_ctrl *) &uac_ctrl,
  95. .dev.audio = &audio_device[0][2],
  96. },
  97. {
  98. .ctrl = (struct interface_ctrl *) &uac_ctrl,
  99. .dev.audio = &audio_device[0][3],
  100. },
  101. },
  102. };
  103. int usb_audio_parser(struct usb_host_device *host_dev, u8 interface_num, const u8 *pBuf)
  104. {
  105. struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)pBuf;
  106. const usb_dev usb_id = host_device2id(host_dev);
  107. const struct usb_interface_info *usb_if = &_uac_if[usb_id][interface_num];
  108. struct audio_streaming_t *as_t = NULL;
  109. memset(usb_if->dev.p, 0, sizeof(struct audio_device_t));
  110. host_dev->interface_info[interface_num] = usb_if;
  111. usb_if->dev.audio->parent = host_dev;
  112. if (interface->bInterfaceSubClass == USB_SUBCLASS_AUDIOCONTROL) {
  113. log_info("audio control interface : %d\n", interface_num);
  114. pBuf += sizeof(struct usb_interface_descriptor);
  115. usb_if->dev.audio->subclass = interface->bInterfaceSubClass;
  116. usb_if->dev.audio->interface_num = interface_num;
  117. return sizeof(struct usb_interface_descriptor);
  118. }
  119. if (interface->bInterfaceSubClass == USB_SUBCLASS_AUDIOSTREAMING) {
  120. usb_if->dev.audio->subclass = interface->bInterfaceSubClass;
  121. usb_if->dev.audio->interface_num = interface_num;
  122. if (interface->bNumEndpoints == 0) {
  123. pBuf += sizeof(struct usb_interface_descriptor);
  124. do {
  125. struct usb_interface_descriptor *as_interface = (struct usb_interface_descriptor *)pBuf;
  126. if (as_interface->bNumEndpoints == 0 || as_interface->bInterfaceClass != USB_CLASS_AUDIO) {
  127. break;
  128. }
  129. log_info("audio streaming interface : %d ep_num:%d Altersetting:%d", interface_num, as_interface->bNumEndpoints, as_interface->bAlternateSetting);
  130. as_t = &usb_if->dev.audio->as[as_interface->bAlternateSetting - 1];
  131. as_t->bNumEndpoints = as_interface->bNumEndpoints;
  132. pBuf += (USB_DT_INTERFACE_SIZE + UAC_DT_AS_HEADER_SIZE);
  133. //解析format
  134. struct uac_format_type_i_discrete_descriptor *uac_format_desc = (struct uac_format_type_i_discrete_descriptor *)pBuf;
  135. if (uac_format_desc->bDescriptorSubtype == UAC_FORMAT_TYPE) {
  136. as_t->bFormatType = uac_format_desc->bFormatType;
  137. as_t->bNrChannels = uac_format_desc->bNrChannels;
  138. as_t->bSubframeSize = uac_format_desc->bSubframeSize;
  139. as_t->bBitResolution = uac_format_desc->bBitResolution;
  140. as_t->bSamFreqType = uac_format_desc->bSamFreqType;
  141. for (u8 i = 0; i < as_t->bSamFreqType; i++) {
  142. memcpy(&as_t->tSamFreq[i], &uac_format_desc->tSamFreq[i], 3);
  143. log_info("as bNrChannels:%d bBitResolution:%d tSamFreq : %d", as_t->bNrChannels, as_t->bBitResolution, as_t->tSamFreq[i]);
  144. }
  145. //Endpointdescriptor
  146. pBuf += uac_format_desc->bLength;
  147. /* for (int i = 0; i < as_t->bNumEndpoints; i++) { */
  148. struct usb_endpoint_descriptor *endpoint = (struct usb_endpoint_descriptor *)pBuf;
  149. if (endpoint->bDescriptorType == USB_DT_ENDPOINT) {
  150. as_t->ep_Interval = endpoint->bInterval;
  151. as_t->ep_max_packet_size = endpoint->wMaxPacketSize;
  152. if (endpoint->bEndpointAddress & USB_DIR_IN) {
  153. as_t->ep = endpoint->bEndpointAddress & 0xf;
  154. log_info("ep in : %x\n", as_t->ep);
  155. usb_if->dev.audio->support = MICROPHONE_SUPPORTED;
  156. } else {
  157. as_t->ep = endpoint->bEndpointAddress;
  158. log_info("ep out : %x\n", as_t->ep);
  159. usb_if->dev.audio->support = HEADPHONE_SUPPORTED;
  160. }
  161. pBuf += (USB_DT_ENDPOINT_AUDIO_SIZE + UAC_ISO_ENDPOINT_DESC_SIZE);
  162. }
  163. /* } */
  164. } else {
  165. log_error("uac_format_desc->bDescriptorSubtype err!!\n");
  166. goto __exit;
  167. }
  168. } while (1);
  169. /* log_info("lennnn:%d\n",pBuf - (u8 *)interface); */
  170. return pBuf - (u8 *)interface ;
  171. } else {
  172. log_info("audio streaming interface : %d ep_num:%d Altersetting:%d\n", interface_num, interface->bNumEndpoints, interface->bAlternateSetting);
  173. }
  174. }
  175. __exit:
  176. return USB_DT_INTERFACE_SIZE;
  177. }
  178. static struct audio_device_t *__find_microphone_interface(const struct usb_host_device *host_dev)
  179. {
  180. struct audio_device_t *audio = NULL;
  181. for (u8 i = 0; i < MAX_HOST_INTERFACE; i++) {
  182. const struct usb_interface_info *usb_if = host_dev->interface_info[i];
  183. if (usb_if &&
  184. (usb_if->ctrl->interface_class == USB_CLASS_AUDIO)) {
  185. audio = usb_if->dev.audio;
  186. if (audio->subclass == USB_SUBCLASS_AUDIOSTREAMING &&
  187. audio->support == MICROPHONE_SUPPORTED) {
  188. // find microphone
  189. return audio;
  190. }
  191. }
  192. }
  193. return NULL;
  194. }
  195. static struct audio_device_t *__find_headphone_interface(const struct usb_host_device *host_dev)
  196. {
  197. struct audio_device_t *audio = NULL;
  198. for (u8 i = 0; i < MAX_HOST_INTERFACE; i++) {
  199. const struct usb_interface_info *usb_if = host_dev->interface_info[i];
  200. if (usb_if &&
  201. (usb_if->ctrl->interface_class == USB_CLASS_AUDIO)) {
  202. audio = usb_if->dev.audio;
  203. if (audio->subclass == USB_SUBCLASS_AUDIOSTREAMING &&
  204. audio->support == HEADPHONE_SUPPORTED) {
  205. // find headphone
  206. return audio;
  207. }
  208. }
  209. }
  210. return NULL;
  211. }
  212. static u32 play_vol_convert(u16 v)
  213. {
  214. //固定音量表,更换声卡需要修改音量表
  215. const u16 vol_table[] = {
  216. //0-100
  217. 0xd300, //0
  218. 0xd58f, 0xd7bf, 0xd9a8, 0xdb5b, 0xdce1, 0xde45, 0xdf8a, 0xe0b6, 0xe1cd, 0xe2d1,
  219. 0xe3c5, 0xe4ab, 0xe583, 0xe651, 0xe714, 0xe7cd, 0xe87f, 0xe928, 0xe9ca, 0xea66,
  220. 0xeafc, 0xeb8d, 0xec18, 0xec9e, 0xed20, 0xed9e, 0xee18, 0xee8e, 0xef00, 0xef6f,
  221. 0xefdc, 0xf045, 0xf0ab, 0xf10f, 0xf171, 0xf1d0, 0xf22c, 0xf287, 0xf2e0, 0xf336,
  222. 0xf38b, 0xf3de, 0xf42f, 0xf47e, 0xf4cc, 0xf518, 0xf563, 0xf5ad, 0xf5f5, 0xf63c,
  223. 0xf681, 0xf6c6, 0xf709, 0xf74b, 0xf78c, 0xf7cb, 0xf80a, 0xf848, 0xf885, 0xf8c1,
  224. 0xf8fc, 0xf936, 0xf96f, 0xf9a8, 0xf9df, 0xfa16, 0xfa4c, 0xfa81, 0xfab6, 0xfaea,
  225. 0xfb1d, 0xfb50, 0xfb82, 0xfbb3, 0xfbe4, 0xfc14, 0xfc43, 0xfc72, 0xfca0, 0xfcce,
  226. 0xfcfc, 0xfd28, 0xfd55, 0xfd80, 0xfdab, 0xfdd6, 0xfe01, 0xfe2a, 0xfe54, 0xfe7d,
  227. 0xfea5, 0xfece, 0xfef5, 0xff1d, 0xff43, 0xff6a, 0xff90, 0xffb6, 0xffdb, 0x0000,
  228. };
  229. if (v <= 100) {
  230. return vol_table[v];
  231. }
  232. for (int i = 0; i < sizeof(vol_table) / 2; i++) {
  233. if (v <= vol_table[i]) {
  234. return i;
  235. }
  236. }
  237. return 0;
  238. }
  239. void set_usb_audio_play_volume(u16 vol)
  240. {
  241. const struct usb_host_device *host_dev = host_id2device(__this->usb_id);
  242. u8 featureUnitID = 6;
  243. usb_audio_volume_control(host_dev, featureUnitID, 1, play_vol_convert(vol));
  244. usb_audio_volume_control(host_dev, featureUnitID, 2, play_vol_convert(vol));
  245. if (vol == 0) {
  246. __this->player.mute = 1;
  247. usb_audio_mute_control(host_dev, featureUnitID, __this->player.mute); //mute
  248. } else {
  249. if (__this->player.mute == 1) {
  250. __this->player.mute = 0;
  251. usb_audio_mute_control(host_dev, featureUnitID, __this->player.mute);
  252. }
  253. }
  254. }
  255. #ifdef USB_AUDIO_PLAY_TEST
  256. static const s16 sin_48k[] = {
  257. 0, 2139, 4240, 6270, 8192, 9974, 11585, 12998,
  258. 14189, 15137, 15826, 16244, 16384, 16244, 15826, 15137,
  259. 14189, 12998, 11585, 9974, 8192, 6270, 4240, 2139,
  260. 0, -2139, -4240, -6270, -8192, -9974, -11585, -12998,
  261. -14189, -15137, -15826, -16244, -16384, -16244, -15826, -15137,
  262. -14189, -12998, -11585, -9974, -8192, -6270, -4240, -2139
  263. };
  264. #endif
  265. static void usb_audio_tx_isr(struct usb_host_device *host_dev, u32 ep)
  266. {
  267. const usb_dev usb_id = host_device2id(host_dev);
  268. struct audio_device_t *audio = NULL;
  269. struct audio_streaming_t *as_t = NULL;
  270. u16 ep_max_packet_size = 0;
  271. u8 channel = 0;
  272. u32 rlen = 0;
  273. static u32 usb_audio_tx_len = 0;
  274. if (__this->player.play_state != AUDIO_PLAY_START) {
  275. return;
  276. }
  277. audio = __find_headphone_interface(host_dev);
  278. if (!audio) {
  279. log_error("no find headphone interface!");
  280. return;
  281. }
  282. as_t = &audio->as[__this->player.Cur_AlternateSetting - 1];
  283. /* ep_max_packet_size = as_t->ep_max_packet_size; */
  284. ep_max_packet_size = EP_MAX_PACKET_SIZE;
  285. channel = as_t->bNrChannels;
  286. //iso send
  287. #ifdef USB_AUDIO_PLAY_TEST
  288. //For Test
  289. int tx_len = 0;
  290. #if 1 // 单声道双声道输出
  291. s16 buf[240 / 2];
  292. for (u8 i = 0, j = 0; i < 240 / 2; i += 2) {
  293. buf[i] = sin_48k[j];
  294. buf[i + 1] = sin_48k[j];
  295. j++;
  296. if (j >= sizeof(sin_48k) / sizeof(sin_48k[0])) {
  297. j = 0;
  298. }
  299. }
  300. #else
  301. //单声道直接输出
  302. u8 buf[248];
  303. do {
  304. memcpy(&buf[tx_len], sin_48k, sizeof(sin_48k));
  305. tx_len += sizeof(sin_48k);
  306. } while (tx_len < ep_max_packet_size);
  307. #endif
  308. usb_h_ep_write_async(usb_id, ep, ep_max_packet_size, as_t->ep, buf, ep_max_packet_size, USB_ENDPOINT_XFER_ISOC, 0);
  309. #else
  310. if (__this->player.usb_audio_remain_len == 0) {
  311. cbuf_read_alloc(&__this->player.usb_audio_play_cbuf, &__this->player.usb_audio_remain_len);
  312. usb_audio_tx_len = 0;
  313. }
  314. if (__this->player.usb_audio_remain_len) {
  315. if (usb_audio_tx_len == 0) {
  316. rlen = cbuf_read(&__this->player.usb_audio_play_cbuf, __this->player.usb_audio_play_buf2, __this->player.usb_audio_remain_len);
  317. if (!rlen) {
  318. __this->player.usb_audio_remain_len = 0;
  319. usb_audio_tx_len = 0;
  320. putchar('C');
  321. usb_h_ep_write_async(usb_id, ep, ep_max_packet_size, as_t->ep, NULL, ep_max_packet_size, USB_ENDPOINT_XFER_ISOC, 1);
  322. os_sem_post(&__this->player.sem);
  323. return;
  324. }
  325. os_sem_post(&__this->player.sem);
  326. }
  327. u8 *send_buf = __this->player.send_buf;
  328. u8 *play_buf = __this->player.usb_audio_play_buf2;
  329. if (channel == 2) {
  330. if (__this->player.src_channel == 1) {
  331. //源数据是单声道数据,转双声道输出
  332. int j = 0;
  333. for (u8 i = 0; i < ep_max_packet_size; i += 4) {
  334. //left
  335. *(send_buf + i) = *(play_buf + (usb_audio_tx_len + j));
  336. *(send_buf + i + 1) = *(play_buf + (usb_audio_tx_len + j + 1));
  337. //right
  338. *(send_buf + i + 2) = *(play_buf + (usb_audio_tx_len + j));
  339. *(send_buf + i + 3) = *(play_buf + (usb_audio_tx_len + j + 1));
  340. j += 2;
  341. }
  342. usb_audio_tx_len += j;
  343. usb_h_ep_write_async(usb_id, ep, ep_max_packet_size, as_t->ep, send_buf, ep_max_packet_size, USB_ENDPOINT_XFER_ISOC, 0);
  344. } else if (__this->player.src_channel == 2) {
  345. //源数据是双声道数据,直接双声道输出
  346. usb_h_ep_write_async(usb_id, ep, ep_max_packet_size, as_t->ep, play_buf + usb_audio_tx_len, ep_max_packet_size, USB_ENDPOINT_XFER_ISOC, 0);
  347. usb_audio_tx_len += ep_max_packet_size;
  348. }
  349. } else if (channel == 1) {
  350. }
  351. if (usb_audio_tx_len >= __this->player.usb_audio_remain_len) {
  352. __this->player.usb_audio_remain_len = 0;
  353. usb_audio_tx_len = 0;
  354. }
  355. } else {
  356. //audio buf null ,send null packet
  357. putchar('E');
  358. usb_h_ep_write_async(usb_id, ep, ep_max_packet_size, as_t->ep, NULL, ep_max_packet_size, USB_ENDPOINT_XFER_ISOC, 1);
  359. }
  360. #endif
  361. }
  362. void set_vol_test(void *p)
  363. {
  364. struct usb_host_device *host_dev = (struct usb_host_device *)p;
  365. static u16 vol = 100;
  366. set_usb_audio_play_volume(vol);
  367. /* static u8 f = 0; */
  368. /* void usb_audio_pause_play(void); */
  369. /* void usb_audio_resume_play(void); */
  370. /* if (!f) { */
  371. /* usb_audio_pause_play(); */
  372. /* } else { */
  373. /* usb_audio_resume_play(); */
  374. /* } */
  375. /* f = !f; */
  376. vol -= 10;
  377. }
  378. void audio_play_task(void *p)
  379. {
  380. log_info(">>> Enter usb audio play task");
  381. struct usb_host_device *host_dev = (struct usb_host_device *)p;
  382. const usb_dev usb_id = host_device2id(host_dev);
  383. u8 *ptr = NULL;
  384. u32 wlen = 0;
  385. u32 ret = 0;
  386. struct audio_device_t *audio = NULL;
  387. audio = __find_headphone_interface(host_dev);
  388. struct audio_streaming_t *as_t = &audio->as[__this->player.Cur_AlternateSetting - 1];
  389. /* u32 ep_max_packet_size = as_t->ep_max_packet_size; */
  390. u32 ep_max_packet_size = EP_MAX_PACKET_SIZE;
  391. log_info("ep max packet : %d\n", ep_max_packet_size);
  392. if (__this->player.send_buf) {
  393. free(__this->player.send_buf);
  394. __this->player.send_buf = NULL;
  395. }
  396. __this->player.send_buf = zalloc(ep_max_packet_size);
  397. u32 usb_audio_buf_size = ep_max_packet_size * 5; //预留5个包的缓存
  398. /* sys_timer_add(host_dev,set_vol_test,5000); */
  399. os_sem_create(&__this->player.sem, 0);
  400. u32 host_ep = as_t->host_ep;
  401. __this->player.play_state = AUDIO_PLAY_START;
  402. //分配双缓存
  403. // 一个缓存保存读卡的数据,一个用于usb发送
  404. if (!__this->player.usb_audio_play_buf) {
  405. __this->player.usb_audio_play_buf = zalloc(usb_audio_buf_size);
  406. cbuf_init(&__this->player.usb_audio_play_cbuf, __this->player.usb_audio_play_buf, usb_audio_buf_size);
  407. usb_h_ep_write_async(usb_id, host_ep, ep_max_packet_size, as_t->ep, NULL, ep_max_packet_size, USB_ENDPOINT_XFER_ISOC, 1); //启动iso传输
  408. }
  409. if (!__this->player.usb_audio_play_buf2) {
  410. __this->player.usb_audio_play_buf2 = zalloc(usb_audio_buf_size);
  411. }
  412. while (1) {
  413. if (__this->player.Cur_AlternateSetting == 0 || __this->player.play_state != AUDIO_PLAY_START) {
  414. putchar('C');
  415. os_time_dly(50);
  416. continue;
  417. }
  418. ptr = cbuf_write_alloc(&__this->player.usb_audio_play_cbuf, &wlen);
  419. if (wlen) {
  420. putchar('R');
  421. ret = __this->player.put_buf(ptr, wlen);
  422. if (ret != wlen) {
  423. __this->player.play_state = AUDIO_PLAY_STOP;
  424. goto __task_exit;
  425. }
  426. cbuf_write_updata(&__this->player.usb_audio_play_cbuf, wlen);
  427. } else {
  428. log_w("usb audio play buf not enough!\n");
  429. }
  430. __task_exit:
  431. os_sem_pend(&__this->player.sem, 0);
  432. }
  433. }
  434. void usb_audio_start_play(const usb_dev usb_id, u8 channel, u8 bit_reso, u32 sample_rate)
  435. {
  436. log_info(" usb audio play\n");
  437. const struct usb_host_device *host_dev = host_id2device(usb_id);
  438. struct audio_device_t *audio = NULL;
  439. struct audio_streaming_t *as_t = NULL;
  440. u8 *ep_buffer = ep_out_dma_buf;
  441. u8 find_alternatesetting = 0;
  442. audio = __find_headphone_interface(host_dev);
  443. if (!audio) {
  444. log_error("no find headphone interface!");
  445. return;
  446. }
  447. for (u8 i = 0; i < ARRAY_SIZE(audio->as); i++) {
  448. as_t = &audio->as[i];
  449. if (as_t->bBitResolution == bit_reso) {
  450. for (u8 j = 0; j < as_t->bSamFreqType; j++) {
  451. if (as_t->tSamFreq[j] == sample_rate) {
  452. find_alternatesetting = i + 1;
  453. break;
  454. }
  455. }
  456. }
  457. }
  458. if (!find_alternatesetting) {
  459. log_e("can not find Alternatesetting,please check bit_reso and sample_rate\n");
  460. return;
  461. }
  462. __this->usb_id = usb_id;
  463. //端点分配
  464. u32 host_ep = usb_get_ep_num(usb_id, USB_DIR_OUT, USB_ENDPOINT_XFER_ISOC);
  465. ASSERT(host_ep != -1, "ep not enough");
  466. __this->player.Cur_AlternateSetting = find_alternatesetting; //选择Alternatesetting
  467. __this->player.sample_rate = sample_rate; //选择采样率
  468. __this->player.src_channel = channel;
  469. as_t = &audio->as[find_alternatesetting - 1];
  470. u8 target_ep = as_t->ep;
  471. u8 ep_interval = as_t->ep_Interval;
  472. as_t->host_ep = host_ep;
  473. usb_set_interface(host_dev, audio->interface_num, find_alternatesetting); //interface Alternatesetting
  474. usb_audio_sampling_frequency_control(host_dev, target_ep, sample_rate);//设置采样率
  475. //设置音量
  476. /* usb_audio_volume_control(host_dev, 6, 1, vol_convert(5)); */
  477. /* usb_audio_volume_control(host_dev, 6, 2, vol_convert(5)); */
  478. log_info("H2D ep: %x --> %x interval: %d", host_ep, target_ep, ep_interval);
  479. usb_h_set_ep_isr(host_dev, host_ep, usb_audio_tx_isr, host_dev);
  480. usb_h_ep_config(usb_id, host_ep, USB_ENDPOINT_XFER_ISOC, 1, ep_interval, ep_buffer, sizeof(ep_out_dma_buf));
  481. task_create(audio_play_task, host_dev, "uac_play");
  482. }
  483. void usb_audio_stop_play(const usb_dev usb_id)
  484. {
  485. const struct usb_host_device *host_dev = host_id2device(usb_id);
  486. usb_h_set_ep_isr(NULL, 0, NULL, NULL);
  487. __this->player.put_buf(NULL, 0);
  488. __this->player.Cur_AlternateSetting = 0;
  489. __this->player.sample_rate = 0;
  490. __this->player.src_channel = 0;
  491. if (__this->player.usb_audio_play_buf) {
  492. free(__this->player.usb_audio_play_buf);
  493. __this->player.usb_audio_play_buf = NULL;
  494. }
  495. if (__this->player.usb_audio_play_buf2) {
  496. free(__this->player.usb_audio_play_buf2);
  497. __this->player.usb_audio_play_buf2 = NULL;
  498. }
  499. if (__this->player.send_buf) {
  500. free(__this->player.send_buf);
  501. __this->player.send_buf = NULL;
  502. }
  503. printf("\n[ debug ]--func=%s line=%d\n", __func__, __LINE__);
  504. task_kill("uac_play");
  505. printf("\n[ debug ]--func=%s line=%d\n", __func__, __LINE__);
  506. }
  507. void usb_audio_pause_play(void)
  508. {
  509. __this->player.play_state = AUDIO_PLAY_PAUSE;
  510. }
  511. void usb_audio_resume_play(void)
  512. {
  513. const struct usb_host_device *host_dev = host_id2device(__this->usb_id);
  514. struct audio_device_t *audio = __find_headphone_interface(host_dev);
  515. struct audio_streaming_t *as_t = &audio->as[__this->player.Cur_AlternateSetting - 1];
  516. __this->player.play_state = AUDIO_PLAY_START;
  517. usb_h_ep_write_async(__this->usb_id, as_t->host_ep, as_t->ep_max_packet_size, as_t->ep, NULL, as_t->ep_max_packet_size, USB_ENDPOINT_XFER_ISOC, 1); //重新启动传输
  518. }
  519. static u32 record_vol_convert(u16 v)
  520. {
  521. //固定音量表,更换声卡需要修改音量表
  522. const u16 vol_table[] = {
  523. //0-100
  524. 0xf400,
  525. 0xf479, 0xf4ee, 0xf560, 0xf5cf, 0xf63a, 0xf6a3, 0xf709, 0xf76c, 0xf7cd, 0xf82b,
  526. 0xf887, 0xf8e1, 0xf939, 0xf98f, 0xf9e4, 0xfa36, 0xfa87, 0xfad6, 0xfb23, 0xfb6f,
  527. 0xfbba, 0xfc03, 0xfc4b, 0xfc91, 0xfcd6, 0xfd1a, 0xfd5d, 0xfd9f, 0xfde0, 0xfe1f,
  528. 0xfe5e, 0xfe9b, 0xfed8, 0xff14, 0xff4e, 0xff88, 0xffc1, 0xfff9, 0x0003, 0x0069,
  529. 0x00a3, 0x00de, 0x0119, 0x0155, 0x0193, 0x01d1, 0x0210, 0x0251, 0x0292, 0x02d5,
  530. 0x0318, 0x035d, 0x03a3, 0x03eb, 0x0434, 0x047e, 0x04c9, 0x0517, 0x0565, 0x05b5,
  531. 0x0607, 0x065b, 0x06b1, 0x0708, 0x0762, 0x07bd, 0x081b, 0x087c, 0x08de, 0x0943,
  532. 0x09ab, 0x0a16, 0x0a84, 0x0af4, 0x0b69, 0x0be1, 0x0c5c, 0x0cdc, 0x0d60, 0x0de9,
  533. 0x0e77, 0x0f0a, 0x0fa2, 0x1041, 0x10e7, 0x1195, 0x124a, 0x1308, 0x13d0, 0x14a3,
  534. 0x1582, 0x166e, 0x176a, 0x1877, 0x1998, 0x1ad0, 0x1c24, 0x1d98, 0x1f33, 0x2100,
  535. };
  536. if (v <= 100) {
  537. return vol_table[v];
  538. }
  539. for (int i = 0; i < sizeof(vol_table) / 2; i++) {
  540. if (v <= vol_table[i]) {
  541. return i;
  542. }
  543. }
  544. return 0;
  545. }
  546. void set_usb_audio_record_volume(u16 vol)
  547. {
  548. const struct usb_host_device *host_dev = host_id2device(__this->usb_id);
  549. u8 featureUnitID = 5;
  550. usb_audio_volume_control(host_dev, featureUnitID, 0, record_vol_convert(vol));
  551. }
  552. static u32 write_file_len = 0;
  553. static void usb_audio_rx_isr(struct usb_host_device *host_dev, u32 ep)
  554. {
  555. u8 buffer[192] = {0};
  556. u8 *ptr = NULL;
  557. int rlen, wlen = 0;
  558. usb_dev usb_id = host_device2id(host_dev);
  559. struct audio_device_t *audio = NULL;
  560. struct audio_streaming_t *as_t = NULL;
  561. audio = __find_microphone_interface(host_dev);
  562. if (!audio) {
  563. log_error("no find microphone interface!");
  564. return;
  565. }
  566. if (__this->microphone.record_state != AUDIO_RECORD_START) {
  567. return;
  568. }
  569. as_t = &audio->as[__this->microphone.Cur_AlternateSetting - 1];
  570. u8 channel = as_t->bNrChannels;
  571. u32 rx_len = usb_h_ep_read_async(usb_id, ep, as_t->ep, buffer, sizeof(buffer), USB_ENDPOINT_XFER_ISOC, 0);
  572. /* g_printf("RX:%d\n",rx_len); */
  573. /* printf_buf(buffer, rx_len); */
  574. cbuf_write(&__this->microphone.usb_audio_record_cbuf, buffer, rx_len);
  575. cbuf_write_alloc(&__this->microphone.usb_audio_record_cbuf, &wlen);
  576. if (wlen == 0) {
  577. putchar('O');
  578. if (write_file_len) {
  579. log_w("write againnnnn\n");
  580. }
  581. /* [> printf("R:%d W:%d\n", rx_len,wlen); <] */
  582. cbuf_read_alloc(&__this->microphone.usb_audio_record_cbuf, &rlen);
  583. cbuf_read(&__this->microphone.usb_audio_record_cbuf, __this->microphone.usb_audio_record_buf2, rlen);
  584. write_file_len = rlen;
  585. os_taskq_post_msg("uac_record", 2, 0x01, rlen);
  586. /* return; */
  587. }
  588. usb_h_ep_read_async(usb_id, ep, as_t->ep, NULL, 0, USB_ENDPOINT_XFER_ISOC, 1); //触发下一个接收中断
  589. }
  590. void audio_record_task(void *p)
  591. {
  592. log_info(">>> Enter usb audio record task");
  593. struct usb_host_device *host_dev = (struct usb_host_device *)p;
  594. const usb_dev usb_id = host_device2id(host_dev);
  595. u8 *ptr = NULL;
  596. u32 rlen = 0;
  597. u32 ret = 0;
  598. int msg[16];
  599. struct audio_device_t *audio = NULL;
  600. audio = __find_microphone_interface(host_dev);
  601. struct audio_streaming_t *as_t = &audio->as[__this->microphone.Cur_AlternateSetting - 1];
  602. /* u32 ep_max_packet_size = as_t->ep_max_packet_size; */
  603. u32 ep_max_packet_size = EP_MAX_PACKET_SIZE;
  604. log_info("ep max packet : %d\n", ep_max_packet_size);
  605. u32 usb_audio_buf_size = ep_max_packet_size * 50;
  606. u32 host_ep = as_t->host_ep;
  607. u8 target_ep = as_t->ep;
  608. //分配双缓存
  609. // 一个缓存写卡的数据,一个用于usb接收
  610. if (!__this->microphone.usb_audio_record_buf) {
  611. __this->microphone.usb_audio_record_buf = zalloc(usb_audio_buf_size);
  612. cbuf_init(&__this->microphone.usb_audio_record_cbuf, __this->microphone.usb_audio_record_buf, usb_audio_buf_size);
  613. }
  614. if (!__this->microphone.usb_audio_record_buf2) {
  615. __this->microphone.usb_audio_record_buf2 = zalloc(usb_audio_buf_size);
  616. }
  617. usb_h_ep_read_async(usb_id, host_ep, target_ep, NULL, 0, USB_ENDPOINT_XFER_ISOC, 1); //启动iso
  618. while (1) {
  619. ret = os_taskq_pend(NULL, msg, ARRAY_SIZE(msg));
  620. if (ret == OS_TASKQ) {
  621. switch (msg[1]) {
  622. case 0x01:
  623. ptr = __this->microphone.usb_audio_record_buf2;
  624. rlen = msg[2];
  625. putchar('W');
  626. __this->microphone.get_buf(ptr, rlen);
  627. write_file_len = 0;
  628. break;
  629. }
  630. }
  631. }
  632. }
  633. void usb_audio_start_record(const usb_dev usb_id, u8 bit_reso, u32 sample_rate)
  634. {
  635. log_info(" usb audio record\n");
  636. const struct usb_host_device *host_dev = host_id2device(usb_id);
  637. struct audio_device_t *audio = NULL;
  638. struct audio_streaming_t *as_t = NULL;
  639. u8 *ep_buffer = ep_in_dma_buf;
  640. u8 find_alternatesetting = 0;
  641. audio = __find_microphone_interface(host_dev);
  642. if (!audio) {
  643. log_error("no find microphone interface!");
  644. return;
  645. }
  646. for (u8 i = 0; i < ARRAY_SIZE(audio->as); i++) {
  647. as_t = &audio->as[i];
  648. if (as_t->bBitResolution == bit_reso) {
  649. for (u8 j = 0; j < as_t->bSamFreqType; j++) {
  650. if (as_t->tSamFreq[j] == sample_rate) {
  651. find_alternatesetting = i + 1;
  652. break;
  653. }
  654. }
  655. }
  656. }
  657. if (!find_alternatesetting) {
  658. log_e("can not find Alternatesetting,please check bit_reso and sample_rate\n");
  659. return;
  660. }
  661. //端点分配
  662. u32 host_ep = usb_get_ep_num(usb_id, USB_DIR_IN, USB_ENDPOINT_XFER_ISOC);
  663. ASSERT(host_ep != -1, "ep not enough");
  664. __this->usb_id = usb_id;
  665. host_ep = host_ep | USB_DIR_IN;
  666. __this->microphone.Cur_AlternateSetting = find_alternatesetting; //选择Alternatesetting
  667. __this->microphone.sample_rate = sample_rate; //选择采样率
  668. as_t = &audio->as[find_alternatesetting - 1];
  669. u8 target_ep = as_t->ep;
  670. u8 ep_interval = as_t->ep_Interval;
  671. as_t->host_ep = host_ep;
  672. usb_set_interface(host_dev, audio->interface_num, find_alternatesetting); //interface 1 Alternatesetting 1
  673. usb_audio_sampling_frequency_control(host_dev, target_ep, sample_rate);//设置采样率
  674. //设置音量
  675. /* usb_audio_volume_control(host_dev, 6, 1, vol_convert(5)); */
  676. /* usb_audio_volume_control(host_dev, 6, 2, vol_convert(5)); */
  677. log_info("D2H ep: %x --> %x", target_ep, host_ep);
  678. usb_h_set_ep_isr(host_dev, host_ep, usb_audio_rx_isr, host_dev);
  679. usb_h_ep_config(usb_id, host_ep, USB_ENDPOINT_XFER_ISOC, 1, ep_interval, ep_buffer, sizeof(ep_in_dma_buf));
  680. task_create(audio_record_task, host_dev, "uac_record");
  681. __this->microphone.record_state = AUDIO_RECORD_START;
  682. }
  683. void usb_audio_stop_record(const usb_dev usb_id)
  684. {
  685. const struct usb_host_device *host_dev = host_id2device(usb_id);
  686. usb_h_set_ep_isr(NULL, 0, NULL, NULL);
  687. __this->microphone.get_buf(NULL, 0);
  688. __this->microphone.Cur_AlternateSetting = 0;
  689. __this->microphone.sample_rate = 0;
  690. if (__this->microphone.usb_audio_record_buf) {
  691. free(__this->microphone.usb_audio_record_buf);
  692. __this->microphone.usb_audio_record_buf = NULL;
  693. }
  694. if (__this->microphone.usb_audio_record_buf2) {
  695. free(__this->microphone.usb_audio_record_buf2);
  696. __this->microphone.usb_audio_record_buf2 = NULL;
  697. }
  698. printf("\n[ debug ]--func=%s line=%d\n", __func__, __LINE__);
  699. task_kill("uac_record");
  700. printf("\n[ debug ]--func=%s line=%d\n", __func__, __LINE__);
  701. }
  702. void usb_audio_pause_record(void)
  703. {
  704. __this->microphone.record_state = AUDIO_RECORD_PAUSE;
  705. }
  706. void usb_audio_resume_record(void)
  707. {
  708. const struct usb_host_device *host_dev = host_id2device(__this->usb_id);
  709. struct audio_device_t *audio = __find_microphone_interface(host_dev);
  710. struct audio_streaming_t *as_t = &audio->as[__this->microphone.Cur_AlternateSetting - 1];
  711. __this->microphone.record_state = AUDIO_RECORD_START;
  712. usb_h_ep_read_async(__this->usb_id, as_t->host_ep, as_t->ep, NULL, 0, USB_ENDPOINT_XFER_ISOC, 1); //重新启动接收
  713. }
  714. void usb_audio_start_process(u32 id)
  715. {
  716. usb_audio_start_play(id, 1, 16, 48000); //开启headphone
  717. usb_audio_start_record(id, 16, 48000); //开启microphone
  718. }
  719. void usb_audio_stop_process(u32 id)
  720. {
  721. usb_audio_stop_play(id);
  722. usb_audio_stop_record(id);
  723. }
  724. static void usb_audio_event_handler(struct sys_event *event, void *priv)
  725. {
  726. const char *audio = NULL;
  727. switch (event->type) {
  728. case SYS_DEVICE_EVENT:
  729. if ((u32)event->arg == DEVICE_EVENT_FROM_USB_HOST) {
  730. if ((event->u.dev.event == DEVICE_EVENT_IN) ||
  731. (event->u.dev.event == DEVICE_EVENT_CHANGE)) {
  732. audio = (const char *)event->u.dev.value;
  733. usb_audio_start_process(audio[5] - '0');
  734. } else if (event->u.dev.event == DEVICE_EVENT_OUT) {
  735. log_error("device out %x", event->u.dev.value);
  736. usb_audio_stop_process(audio[5] - '0');
  737. }
  738. break;
  739. }
  740. }
  741. }
  742. void usb_host_audio_init(int (*put_buf)(void *ptr, u32 len), int *(*get_buf)(void *ptr, u32 len))
  743. {
  744. memset(__this, 0, sizeof(struct usb_audio_info));
  745. __this->player.put_buf = put_buf;
  746. __this->microphone.get_buf = get_buf;
  747. register_sys_event_handler(SYS_DEVICE_EVENT, DEVICE_EVENT_FROM_USB_HOST, 2,
  748. usb_audio_event_handler);
  749. }
  750. void usb_host_audio_exit()
  751. {
  752. unregister_sys_event_handler(usb_audio_event_handler);
  753. __this->player.put_buf = NULL;
  754. __this->microphone.get_buf = NULL;
  755. }
  756. #endif