audiocontrol.cpp 13 KB

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