audiocontrol.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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;
  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 = SND_MIXER_SCHN_FRONT_LEFT;
  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. //printf("******************paudio_id:%s\n",paudio_id);
  210. if (snd_ctl_ascii_elem_id_parse(*ppid, paudio_id))
  211. {
  212. printf("-- snd_ctl_ascii_elem_id_parse failed--\n");
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. int AudioControl::getpinfo(snd_ctl_elem_id_t *pid, snd_ctl_elem_info_t **ppinfo)
  218. {
  219. int err;
  220. snd_ctl_t *handle = NULL;
  221. if (handle == NULL && (err = snd_ctl_open(&handle, DEFAULT, 0)) < 0)
  222. {
  223. printf("-- snd_ctl_open failed--\n");
  224. return -1;
  225. }
  226. snd_ctl_elem_info_set_id(*ppinfo, pid);
  227. if ((err = snd_ctl_elem_info(handle, *ppinfo)) < 0)
  228. {
  229. snd_ctl_close(handle);
  230. handle = NULL;
  231. printf("-- snd_ctl_elem_info failed--\r\n");
  232. return -1;
  233. }
  234. snd_ctl_close(handle);
  235. handle = NULL;
  236. return 0;
  237. }
  238. int AudioControl::getpelem(snd_ctl_elem_id_t *pid, snd_hctl_elem_t **ppelem)
  239. {
  240. int err;
  241. snd_hctl_t *hctl;
  242. if ((err = snd_hctl_open(&hctl, DEFAULT, 0)) < 0)
  243. {
  244. printf("-- snd_hctl_open failed--\n");
  245. return -1;
  246. }
  247. if ((err = snd_hctl_load(hctl)) < 0)
  248. {
  249. printf("-- snd_hctl_load failed--\n");
  250. return -1;
  251. }
  252. *ppelem = snd_hctl_find_elem(hctl, pid);
  253. snd_hctl_close(hctl);
  254. return 0;
  255. }
  256. int AudioControl::getpcontrol(snd_ctl_elem_id_t *pid, snd_ctl_elem_info_t *pinfo ,snd_ctl_elem_value_t **ppcontrol)
  257. {
  258. int err;
  259. snd_hctl_t *hctl;
  260. snd_hctl_elem_t *elem;
  261. if ((err = snd_hctl_open(&hctl, DEFAULT, 0)) < 0)
  262. {
  263. printf("-- snd_hctl_open failed--\n");
  264. return -1;
  265. }
  266. if ((err = snd_hctl_load(hctl)) < 0)
  267. {
  268. printf("-- snd_hctl_load failed--\n");
  269. return -1;
  270. }
  271. elem = snd_hctl_find_elem(hctl, pid);
  272. if ((err = snd_hctl_elem_info(elem, pinfo)) < 0)
  273. {
  274. printf("-- snd_hctl_elem_info failed --\n");
  275. return -1;
  276. }
  277. snd_ctl_elem_info_is_readable(pinfo);
  278. if ((err = snd_hctl_elem_read(elem, *ppcontrol)) < 0)
  279. {
  280. printf("-- snd_hctl_elem_read failed--\n");
  281. return -1;
  282. }
  283. snd_hctl_close(hctl);
  284. return 0;
  285. }
  286. int AudioControl::get_type(snd_ctl_elem_info_t *pinfo, int *ptype)
  287. {
  288. // int err;
  289. int type = -1;
  290. if ((type = snd_ctl_elem_info_get_type(pinfo)) < 0)
  291. {
  292. printf("-- snd_hctl_elem_read failed--\n");
  293. return -1;
  294. }
  295. *ptype = type;
  296. return 0;
  297. }
  298. int AudioControl::get_size(snd_ctl_elem_info_t *pinfo, int *psize)
  299. {
  300. //int err;
  301. int size = -1;
  302. if ((size = snd_ctl_elem_info_get_count(pinfo)) < 0)
  303. {
  304. printf("-- snd_hctl_elem_read failed--\n");
  305. return -1;
  306. }
  307. *psize = size;
  308. return 0;
  309. }
  310. int AudioControl::get_range(int type, snd_hctl_elem_t *pelem, snd_ctl_elem_info_t *pinfo, Data *pdata)
  311. {
  312. Q_UNUSED(pelem);
  313. switch (type)
  314. {
  315. case SND_CTL_ELEM_TYPE_INTEGER:
  316. pdata->min = snd_ctl_elem_info_get_min(pinfo);
  317. pdata->max = snd_ctl_elem_info_get_max(pinfo);
  318. pdata->step = snd_ctl_elem_info_get_step(pinfo);
  319. break;
  320. case SND_CTL_ELEM_TYPE_INTEGER64:
  321. pdata->min = snd_ctl_elem_info_get_min(pinfo);
  322. pdata->max = snd_ctl_elem_info_get_max(pinfo);
  323. pdata->step = snd_ctl_elem_info_get_step(pinfo);
  324. break;
  325. default:
  326. pdata->min = 0;
  327. pdata->max = 0;
  328. pdata->step = 0;
  329. break;
  330. }
  331. return 0;
  332. }
  333. int AudioControl::get_value(int type, int count, snd_ctl_elem_value_t *pcontrol , int *pvalue)
  334. {
  335. int value = 0;
  336. for (int index = 0; index < count; index++) {
  337. switch (type) {
  338. case SND_CTL_ELEM_TYPE_BOOLEAN:
  339. value = snd_ctl_elem_value_get_boolean(pcontrol, index);
  340. break;
  341. case SND_CTL_ELEM_TYPE_INTEGER:
  342. value = snd_ctl_elem_value_get_integer(pcontrol, index);
  343. printf("integer value = %d\r\n", value);
  344. break;
  345. case SND_CTL_ELEM_TYPE_INTEGER64:
  346. value = snd_ctl_elem_value_get_integer64(pcontrol, index);
  347. break;
  348. case SND_CTL_ELEM_TYPE_ENUMERATED:
  349. value = snd_ctl_elem_value_get_enumerated(pcontrol, index);
  350. printf("enumerated value = %d\r\n", value);
  351. break;
  352. case SND_CTL_ELEM_TYPE_BYTES:
  353. value = snd_ctl_elem_value_get_byte(pcontrol, index);
  354. break;
  355. default:
  356. printf("?");
  357. break;
  358. }
  359. }
  360. *pvalue = value;
  361. return 0;
  362. }
  363. int AudioControl::set_value(snd_ctl_elem_id_t *pid, snd_ctl_elem_info_t *pinfo, snd_ctl_elem_value_t *pcontrol, int value)
  364. {
  365. int err;
  366. snd_ctl_t *handle = NULL;
  367. if (handle == NULL && (err = snd_ctl_open(&handle, DEFAULT, 0)) < 0)
  368. {
  369. printf("-- snd_ctl_open failed--\n");
  370. return -1;
  371. }
  372. snd_ctl_elem_value_set_id(pcontrol, pid);
  373. if ((err = snd_ctl_elem_read(handle, pcontrol)) < 0)
  374. {
  375. snd_ctl_close(handle);
  376. handle = NULL;
  377. return -1;
  378. }
  379. char sz_value[20] = {0,};
  380. sprintf(sz_value, "%d", value);
  381. err = snd_ctl_ascii_value_parse(handle, pcontrol, pinfo, sz_value);
  382. if (err < 0)
  383. {
  384. snd_ctl_close(handle);
  385. handle = NULL;
  386. return -1;
  387. }
  388. if ((err = snd_ctl_elem_write(handle, pcontrol)) < 0)
  389. {
  390. snd_ctl_close(handle);
  391. handle = NULL;
  392. return -1;
  393. }
  394. snd_ctl_close(handle);
  395. handle = NULL;
  396. return 0;
  397. }
  398. char* AudioControl::getstring(int type)
  399. {
  400. static char szID[100] = {0,};
  401. //qDebug()<<"+++++++++AudioControl::getpid+++++++++"<<type;
  402. switch (type) {
  403. case LEFT_AUDIO:
  404. strcpy(szID, LEFT_ID);
  405. break;
  406. case RIGHT_AUDIO:
  407. strcpy(szID, RIGHT_ID);
  408. break;
  409. case STRAM_AUDIO:
  410. strcpy(szID, STRAM_ID);
  411. break;
  412. case OUTPUT_AUDIO:
  413. strcpy(szID, OUTPUT_ID);
  414. break;
  415. case AMP_AUDIO:
  416. strcpy(szID, AMP_ID);
  417. break;
  418. case FM_AUDIO:
  419. strcpy(szID, FM_ID);
  420. break;
  421. case IRSTATUS_AUDIO:
  422. strcpy(szID, IRSTATUS_ID);
  423. break;
  424. case IRCHANNEL_AUDIO:
  425. strcpy(szID, IRCHANNEL_ID);
  426. break;
  427. case PAMUTE_AUDIO:
  428. strcpy(szID, PAMUTE_ID);
  429. break;
  430. case PAINPUTSWITCH_AUDIO: {
  431. strcpy(szID, painputswitch_id);
  432. break;
  433. }
  434. case PAOUTPUTVOLUME_AUDIO: {
  435. strcpy(szID, paoutputvolume_id);
  436. break;
  437. }
  438. case PAMUTESWITCH_AUDIO: {
  439. strcpy(szID, pamuteswitch_id);
  440. break;
  441. }
  442. case PAOUTPUTFL_AUDIO: {
  443. strcpy(szID, paoutputfl_id);
  444. break;
  445. }
  446. case PAOUTPUTFR_AUDIO: {
  447. strcpy(szID, paoutputfr_id);
  448. break;
  449. }
  450. case PAOUTPUTRL_AUDIO: {
  451. strcpy(szID, paoutputrl_id);
  452. break;
  453. }
  454. case PAOUTPUTRR_AUDIO: {
  455. strcpy(szID, paoutputrr_id);
  456. break;
  457. }
  458. case PAOUTPUTBASS_AUDIO: {
  459. strcpy(szID, paoutputbass_id);
  460. break;
  461. }
  462. case PAOUTPUTMIDDLE_AUDIO: {
  463. strcpy(szID, paoutputmiddle_id);
  464. break;
  465. }
  466. case PAOUTPUTTREBLE_AUDIO: {
  467. strcpy(szID, paoutputtreble_id);
  468. break;
  469. }
  470. case PAOUTPUTGAIN_AUDIO: {
  471. strcpy(szID, paoutputgain_id);
  472. break;
  473. }
  474. default: {
  475. break;
  476. }
  477. }
  478. return szID;
  479. }