audiocontrol.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. #include "audiocontrol.h"
  2. AudioControl::AudioControl()
  3. {
  4. }
  5. int AudioControl::Set(int Audio_Type, int nValue)
  6. {
  7. snd_ctl_elem_id_t *pid = NULL;
  8. snd_ctl_elem_info_t *pinfo = NULL;
  9. snd_ctl_elem_value_t *pcontrol = NULL;
  10. snd_ctl_elem_id_alloca(&pid);
  11. snd_ctl_elem_info_alloca(&pinfo);
  12. snd_ctl_elem_value_alloca(&pcontrol);
  13. int type = 0, size = 0;
  14. if (getpid(Audio_Type, &pid) == 0) {
  15. if (getpinfo(pid, &pinfo) == 0) {
  16. getpcontrol(pid, pinfo, &pcontrol);
  17. get_type(pinfo, &type);
  18. get_size(pinfo, &size);
  19. set_value(pid, pinfo, pcontrol, nValue);
  20. }
  21. }
  22. return 0;
  23. }
  24. int AudioControl::Get(int Audio_Type, int *nValue)
  25. {
  26. snd_ctl_elem_id_t *pid = NULL;
  27. snd_ctl_elem_info_t *pinfo = NULL;
  28. snd_ctl_elem_value_t *pcontrol = NULL;
  29. snd_hctl_elem_t *pelem = NULL;
  30. snd_ctl_elem_id_alloca(&pid);
  31. snd_ctl_elem_info_alloca(&pinfo);
  32. snd_ctl_elem_value_alloca(&pcontrol);
  33. int type = 0, size = 0;
  34. int volume = 0;
  35. if(getpid(Audio_Type, &pid) == 0)
  36. {
  37. if(getpinfo(pid, &pinfo) == 0)
  38. {
  39. getpcontrol(pid, pinfo, &pcontrol);
  40. get_type(pinfo, &type);
  41. get_size(pinfo, &size);
  42. get_value(type, size, pcontrol, &volume);
  43. *nValue = volume;
  44. }
  45. }
  46. return 0;
  47. }
  48. int AudioControl::GetRange(int Audio_Type, int *pMin ,int *pMax)
  49. {
  50. snd_ctl_elem_id_t *pid = NULL;
  51. snd_ctl_elem_info_t *pinfo = NULL;
  52. snd_ctl_elem_value_t *pcontrol = NULL;
  53. snd_hctl_elem_t *pelem = NULL;
  54. snd_ctl_elem_id_alloca(&pid);
  55. snd_ctl_elem_info_alloca(&pinfo);
  56. snd_ctl_elem_value_alloca(&pcontrol);
  57. int type = 0, size = 0;
  58. Data data;
  59. if(getpid(Audio_Type, &pid) == 0)
  60. {
  61. if(getpinfo(pid, &pinfo) == 0)
  62. {
  63. getpelem(pid, &pelem);
  64. getpcontrol(pid, pinfo, &pcontrol);
  65. get_type(pinfo, &type);
  66. get_size(pinfo, &size);
  67. get_range(type, pelem, pinfo, &data);
  68. *pMin = data.min;
  69. *pMax = data.max;
  70. }
  71. }
  72. return 0;
  73. }
  74. static snd_mixer_t * gMixer = NULL;
  75. static snd_mixer_elem_t * gDuckElement = NULL;
  76. #define require_noerr( err, exit ); \
  77. do { \
  78. if (err) { \
  79. goto exit;\
  80. } \
  81. } while (0)
  82. #if( !defined( Max ) )
  83. #define Max( X, Y ) ( ( (X) > (Y) ) ? (X) : (Y) )
  84. #endif
  85. #if( !defined( Min ) )
  86. #define Min( X, Y ) ( ( (X) < (Y) ) ? (X) : (Y) )
  87. #endif
  88. #define Clamp( x, a, b ) Max( (a), Min( (b), (x) ) )
  89. int AudioControl::ALSAMixerOpen(int stream_name, int ctrlname)
  90. {
  91. snd_mixer_selem_id_t *sid;
  92. snd_pcm_t *handle;
  93. snd_mixer_t *lMixer = NULL;
  94. snd_mixer_elem_t *lDuckElement = NULL;
  95. int err, retry_cnt = 0;
  96. const char* stream_audioName = NULL;
  97. const char* stream_ctrlName = NULL;
  98. qDebug()<<"stream_name:"<<stream_name<<"ctrlname:"<<ctrlname;
  99. switch (stream_name) {
  100. case AUDIO_PHONE_MUSIC:{
  101. stream_audioName = "plug:softvol2";
  102. if(ctrlname == CTRL_PHONE_MUSIC){
  103. stream_ctrlName = "softmaster2";
  104. }
  105. break;
  106. }
  107. case AUDIO_PHONE_NAVI:{
  108. stream_audioName = "plug:softvol1";
  109. if(ctrlname == CTRL_PHONE_NAVI){
  110. stream_ctrlName = "softmaster1";
  111. }
  112. break;
  113. }
  114. case AUDIO_PHONE_TELL:{
  115. stream_audioName = "plug:softvol3";
  116. if(ctrlname == CTRL_PHONE_TELL){
  117. stream_ctrlName = "softmaster3";
  118. }
  119. break;
  120. }
  121. case AUDIO_PHONE_TTS:{
  122. stream_audioName = "plug:softvol4";
  123. if(ctrlname == CTRL_PHONE_TTS){
  124. stream_ctrlName = "softmaster4";
  125. }
  126. break;
  127. }
  128. default:{
  129. stream_audioName = "default";
  130. stream_ctrlName = "softmaster";
  131. break;
  132. }
  133. }
  134. qDebug("stream_audioName =%s , stream_ctrlName =%s \n",stream_audioName,stream_ctrlName);
  135. if (gMixer && gDuckElement)
  136. return 0;
  137. err = snd_pcm_open(&handle, stream_audioName, SND_PCM_STREAM_PLAYBACK, 0);
  138. require_noerr(err, exit);
  139. snd_pcm_close(handle);
  140. handle = NULL;
  141. err = snd_mixer_open(&lMixer, 0);
  142. require_noerr(err, exit);
  143. err = snd_mixer_attach(lMixer, "default");
  144. require_noerr(err, exit);
  145. err = snd_mixer_selem_register(lMixer, NULL, NULL);
  146. require_noerr(err, exit);
  147. err = snd_mixer_load(lMixer);
  148. require_noerr(err, exit);
  149. snd_mixer_selem_id_alloca(&sid);
  150. snd_mixer_selem_id_set_name(sid, stream_ctrlName);
  151. snd_mixer_selem_id_set_index(sid, 0);
  152. lDuckElement = snd_mixer_find_selem(lMixer, sid);
  153. if (!lDuckElement) {
  154. goto exit;
  155. }
  156. gMixer = lMixer;
  157. gDuckElement = lDuckElement;
  158. exit:
  159. return err;
  160. }
  161. void AudioControl::ALSAMixerClose()
  162. {
  163. if (gMixer == NULL)
  164. return ;
  165. if (gMixer) {
  166. //snd_mixer_elem_detach(gDuckElement, );
  167. snd_mixer_close(gMixer);
  168. gMixer = NULL;
  169. gDuckElement = NULL;
  170. }
  171. return;
  172. }
  173. int AudioControl::ArkALSADuckMixerGetVolume()
  174. {
  175. int err;
  176. snd_mixer_selem_channel_id_t channel;
  177. int nIndex = (int)channel;
  178. long minVolume, maxVolume, volume;
  179. err = snd_mixer_selem_get_playback_volume_range( gDuckElement, &minVolume, &maxVolume );
  180. if (!err) {
  181. for (nIndex = 0; nIndex < (int)SND_MIXER_SCHN_LAST; ++nIndex) {
  182. if (snd_mixer_selem_has_playback_channel( gDuckElement, (snd_mixer_selem_channel_id_t)nIndex)) {
  183. err = snd_mixer_selem_get_playback_volume(gDuckElement, (snd_mixer_selem_channel_id_t)nIndex, &volume);
  184. if (!err)
  185. break;
  186. }
  187. }
  188. }
  189. exit:
  190. return (volume);
  191. }
  192. void AudioControl::ArkALSADuckMixerSetVolume(int inVolume)
  193. {
  194. int err;
  195. long minVolume, maxVolume, newVolume;
  196. if (!gDuckElement) {
  197. printf("set gDuckElement == NULL\n");
  198. return ;
  199. }
  200. err = snd_mixer_selem_get_playback_volume_range( gDuckElement, &minVolume, &maxVolume );
  201. if (!err) {
  202. newVolume = Clamp( inVolume, minVolume, maxVolume );
  203. snd_mixer_selem_set_playback_volume_all( gDuckElement, newVolume );
  204. }
  205. }
  206. int AudioControl::getpid(int audio_type, snd_ctl_elem_id_t **ppid)
  207. {
  208. char *paudio_id = getstring(audio_type);
  209. if (snd_ctl_ascii_elem_id_parse(*ppid, paudio_id))
  210. {
  211. printf("-- snd_ctl_ascii_elem_id_parse failed--\n");
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. int AudioControl::getpinfo(snd_ctl_elem_id_t *pid, snd_ctl_elem_info_t **ppinfo)
  217. {
  218. int err;
  219. snd_ctl_t *handle = NULL;
  220. if (handle == NULL && (err = snd_ctl_open(&handle, DEFAULT, 0)) < 0)
  221. {
  222. printf("-- snd_ctl_open failed--\n");
  223. return -1;
  224. }
  225. snd_ctl_elem_info_set_id(*ppinfo, pid);
  226. if ((err = snd_ctl_elem_info(handle, *ppinfo)) < 0)
  227. {
  228. snd_ctl_close(handle);
  229. handle = NULL;
  230. printf("-- snd_ctl_elem_info failed--\r\n");
  231. return -1;
  232. }
  233. snd_ctl_close(handle);
  234. handle = NULL;
  235. return 0;
  236. }
  237. int AudioControl::getpelem(snd_ctl_elem_id_t *pid, snd_hctl_elem_t **ppelem)
  238. {
  239. int err;
  240. snd_hctl_t *hctl;
  241. if ((err = snd_hctl_open(&hctl, DEFAULT, 0)) < 0)
  242. {
  243. printf("-- snd_hctl_open failed--\n");
  244. return -1;
  245. }
  246. if ((err = snd_hctl_load(hctl)) < 0)
  247. {
  248. printf("-- snd_hctl_load failed--\n");
  249. return -1;
  250. }
  251. *ppelem = snd_hctl_find_elem(hctl, pid);
  252. snd_hctl_close(hctl);
  253. return 0;
  254. }
  255. int AudioControl::getpcontrol(snd_ctl_elem_id_t *pid, snd_ctl_elem_info_t *pinfo ,snd_ctl_elem_value_t **ppcontrol)
  256. {
  257. int err;
  258. snd_hctl_t *hctl;
  259. snd_hctl_elem_t *elem;
  260. if ((err = snd_hctl_open(&hctl, DEFAULT, 0)) < 0)
  261. {
  262. printf("-- snd_hctl_open failed--\n");
  263. return -1;
  264. }
  265. if ((err = snd_hctl_load(hctl)) < 0)
  266. {
  267. printf("-- snd_hctl_load failed--\n");
  268. return -1;
  269. }
  270. elem = snd_hctl_find_elem(hctl, pid);
  271. if ((err = snd_hctl_elem_info(elem, pinfo)) < 0)
  272. {
  273. printf("-- snd_hctl_elem_info failed --\n");
  274. return -1;
  275. }
  276. snd_ctl_elem_info_is_readable(pinfo);
  277. if ((err = snd_hctl_elem_read(elem, *ppcontrol)) < 0)
  278. {
  279. printf("-- snd_hctl_elem_read failed--\n");
  280. return -1;
  281. }
  282. snd_hctl_close(hctl);
  283. return 0;
  284. }
  285. int AudioControl::get_type(snd_ctl_elem_info_t *pinfo, int *ptype)
  286. {
  287. int err;
  288. int type = -1;
  289. if ((type = snd_ctl_elem_info_get_type(pinfo)) < 0)
  290. {
  291. printf("-- snd_hctl_elem_read failed--\n");
  292. return -1;
  293. }
  294. *ptype = type;
  295. return 0;
  296. }
  297. int AudioControl::get_size(snd_ctl_elem_info_t *pinfo, int *psize)
  298. {
  299. int err;
  300. int size = -1;
  301. if ((size = snd_ctl_elem_info_get_count(pinfo)) < 0)
  302. {
  303. printf("-- snd_hctl_elem_read failed--\n");
  304. return -1;
  305. }
  306. *psize = size;
  307. return 0;
  308. }
  309. int AudioControl::get_range(int type, snd_hctl_elem_t *pelem, snd_ctl_elem_info_t *pinfo, Data *pdata)
  310. {
  311. switch (type)
  312. {
  313. case SND_CTL_ELEM_TYPE_INTEGER:
  314. pdata->min = snd_ctl_elem_info_get_min(pinfo);
  315. pdata->max = snd_ctl_elem_info_get_max(pinfo);
  316. pdata->step = snd_ctl_elem_info_get_step(pinfo);
  317. break;
  318. case SND_CTL_ELEM_TYPE_INTEGER64:
  319. pdata->min = snd_ctl_elem_info_get_min(pinfo);
  320. pdata->max = snd_ctl_elem_info_get_max(pinfo);
  321. pdata->step = snd_ctl_elem_info_get_step(pinfo);
  322. break;
  323. default:
  324. pdata->min = 0;
  325. pdata->max = 0;
  326. pdata->step = 0;
  327. break;
  328. }
  329. return 0;
  330. }
  331. int AudioControl::get_value(int type, int count, snd_ctl_elem_value_t *pcontrol , int *pvalue)
  332. {
  333. int value = 0;
  334. for (int index = 0; index < count; index++) {
  335. switch (type) {
  336. case SND_CTL_ELEM_TYPE_BOOLEAN:
  337. value = snd_ctl_elem_value_get_boolean(pcontrol, index);
  338. break;
  339. case SND_CTL_ELEM_TYPE_INTEGER:
  340. value = snd_ctl_elem_value_get_integer(pcontrol, index);
  341. printf("integer value = %d\r\n", value);
  342. break;
  343. case SND_CTL_ELEM_TYPE_INTEGER64:
  344. value = snd_ctl_elem_value_get_integer64(pcontrol, index);
  345. break;
  346. case SND_CTL_ELEM_TYPE_ENUMERATED:
  347. value = snd_ctl_elem_value_get_enumerated(pcontrol, index);
  348. printf("enumerated value = %d\r\n", value);
  349. break;
  350. case SND_CTL_ELEM_TYPE_BYTES:
  351. value = snd_ctl_elem_value_get_byte(pcontrol, index);
  352. break;
  353. default:
  354. printf("?");
  355. break;
  356. }
  357. }
  358. *pvalue = value;
  359. return 0;
  360. }
  361. int AudioControl::set_value(snd_ctl_elem_id_t *pid, snd_ctl_elem_info_t *pinfo, snd_ctl_elem_value_t *pcontrol, int value)
  362. {
  363. int err;
  364. snd_ctl_t *handle = NULL;
  365. if (handle == NULL && (err = snd_ctl_open(&handle, DEFAULT, 0)) < 0)
  366. {
  367. printf("-- snd_ctl_open failed--\n");
  368. return -1;
  369. }
  370. snd_ctl_elem_value_set_id(pcontrol, pid);
  371. if ((err = snd_ctl_elem_read(handle, pcontrol)) < 0)
  372. {
  373. snd_ctl_close(handle);
  374. handle = NULL;
  375. return -1;
  376. }
  377. char sz_value[20] = {0,};
  378. sprintf(sz_value, "%d", value);
  379. err = snd_ctl_ascii_value_parse(handle, pcontrol, pinfo, sz_value);
  380. if (err < 0)
  381. {
  382. snd_ctl_close(handle);
  383. handle = NULL;
  384. return -1;
  385. }
  386. if ((err = snd_ctl_elem_write(handle, pcontrol)) < 0)
  387. {
  388. snd_ctl_close(handle);
  389. handle = NULL;
  390. return -1;
  391. }
  392. snd_ctl_close(handle);
  393. handle = NULL;
  394. return 0;
  395. }
  396. char* AudioControl::getstring(int type)
  397. {
  398. static char szID[100] = {0,};
  399. //qDebug()<<"+++++++++AudioControl::getpid+++++++++"<<type;
  400. switch (type) {
  401. case LEFT_AUDIO:
  402. strcpy(szID, LEFT_ID);
  403. break;
  404. case RIGHT_AUDIO:
  405. strcpy(szID, RIGHT_ID);
  406. break;
  407. case STRAM_AUDIO:
  408. strcpy(szID, STRAM_ID);
  409. break;
  410. case OUTPUT_AUDIO:
  411. strcpy(szID, OUTPUT_ID);
  412. break;
  413. case AMP_AUDIO:
  414. strcpy(szID, AMP_ID);
  415. break;
  416. case FM_AUDIO:
  417. strcpy(szID, FM_ID);
  418. break;
  419. case IRSTATUS_AUDIO:
  420. strcpy(szID, IRSTATUS_ID);
  421. break;
  422. case IRCHANNEL_AUDIO:
  423. strcpy(szID, IRCHANNEL_ID);
  424. break;
  425. case PAMUTE_AUDIO:
  426. strcpy(szID, PAMUTE_ID);
  427. break;
  428. case PAINPUTSWITCH_AUDIO: {
  429. strcpy(szID, painputswitch_id);
  430. break;
  431. }
  432. case PAOUTPUTVOLUME_AUDIO: {
  433. strcpy(szID, paoutputvolume_id);
  434. break;
  435. }
  436. case PAMUTESWITCH_AUDIO: {
  437. strcpy(szID, pamuteswitch_id);
  438. break;
  439. }
  440. case PAOUTPUTFL_AUDIO: {
  441. strcpy(szID, paoutputfl_id);
  442. break;
  443. }
  444. case PAOUTPUTFR_AUDIO: {
  445. strcpy(szID, paoutputfr_id);
  446. break;
  447. }
  448. case PAOUTPUTRL_AUDIO: {
  449. strcpy(szID, paoutputrl_id);
  450. break;
  451. }
  452. case PAOUTPUTRR_AUDIO: {
  453. strcpy(szID, paoutputrr_id);
  454. break;
  455. }
  456. case PAOUTPUTBASS_AUDIO: {
  457. strcpy(szID, paoutputbass_id);
  458. break;
  459. }
  460. case PAOUTPUTMIDDLE_AUDIO: {
  461. strcpy(szID, paoutputmiddle_id);
  462. break;
  463. }
  464. case PAOUTPUTTREBLE_AUDIO: {
  465. strcpy(szID, paoutputtreble_id);
  466. break;
  467. }
  468. case PAOUTPUTGAIN_AUDIO: {
  469. strcpy(szID, paoutputgain_id);
  470. break;
  471. }
  472. default: {
  473. break;
  474. }
  475. }
  476. return szID;
  477. }