Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4460542
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: LINUX

2015-07-18 14:23:16

板子上需要控制声音,试过salsa的库(精简版的alsa),功能有些受限,在PC上测试无法正常获取声音大小,打算采用alsa

下载源码,假设是alsa-lib-1.0.9.tar.bz2
tar -xvf  alsa-lib-1.0.9.tar.bz2
cd alsa-lib-1.0.9
./configure --target=arm-linux  --host=x86-64-linux  CC=arm-none-linux-gnueabi-gcc  --prefix=/opt/sun5i
编译
make
安装到/opt/sun5i
sudo make install
PC机是64位系统,所以选择--host=x86-64-linux 

附上网上找的通过alsa获取声音大小的程序,(可以获取,设置好像不行),建议alsa-utils编译产生的工具amixer来获取设置声音

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <fcntl.h>

  3. #ifdef OSSCONTROL
  4. #define MIXER_DEV "/dev/dsp"

  5. #include <sys/soundcard.h>
  6. #include <sys/ioctl.h>
  7. #include <stdio.h>
  8. #else
  9. #include <alsa/asoundlib.h>
  10. #endif

  11. #define VOLUME_BOUND 10000

  12. typedef enum {
  13.     AUDIO_VOLUME_SET,
  14.     AUDIO_VOLUME_GET,
  15. } audio_volume_action;

  16. /*
  17.   Drawbacks. Sets volume on both channels but gets volume on one. Can be easily adapted.
  18.  */
  19. int audio_volume(audio_volume_action action, long* outvol)
  20. {
  21.     int ret = 0;
  22. #ifdef OSSCONTROL
  23.     int fd, devs;

  24.     if ((fd = open(MIXER_DEV, O_WRONLY)) > 0)
  25.     {
  26.         if(action == AUDIO_VOLUME_SET) {
  27.             if(*outvol < 0 || *outvol > 100)
  28.                 return -2;
  29.             *outvol = (*outvol << 8) | *outvol;
  30.             ioctl(fd, SOUND_MIXER_WRITE_VOLUME, outvol);
  31.         }
  32.         else if(action == AUDIO_VOLUME_GET) {
  33.             ioctl(fd, SOUND_MIXER_READ_VOLUME, outvol);
  34.             *outvol = *outvol & 0xff;
  35.         }
  36.         close(fd);
  37.         return 0;
  38.     }
  39.     return -1;;
  40. #else
  41.     snd_mixer_t* handle;
  42.     snd_mixer_elem_t* elem;
  43.     snd_mixer_selem_id_t* sid;

  44.     static const char* mix_name = "Master";
  45.     static const char* card = "default";
  46.     static int mix_index = 0;

  47.     long pmin, pmax;
  48.     long get_vol, set_vol;
  49.     float f_multi;

  50.     snd_mixer_selem_id_alloca(&sid);

  51.     //sets simple-mixer index and name
  52.     snd_mixer_selem_id_set_index(sid, mix_index);
  53.     snd_mixer_selem_id_set_name(sid, mix_name);

  54.         if ((snd_mixer_open(&handle, 0)) < 0)
  55.         return -1;
  56.     if ((snd_mixer_attach(handle, card)) < 0) {
  57.         snd_mixer_close(handle);
  58.         return -2;
  59.     }
  60.     if ((snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
  61.         snd_mixer_close(handle);
  62.         return -3;
  63.     }
  64.     ret = snd_mixer_load(handle);
  65.     if (ret < 0) {
  66.         snd_mixer_close(handle);
  67.         return -4;
  68.     }
  69.     elem = snd_mixer_find_selem(handle, sid);
  70.     if (!elem) {
  71.         snd_mixer_close(handle);
  72.         return -5;
  73.     }

  74.     long minv, maxv;

  75.     snd_mixer_selem_get_playback_volume_range (elem, &minv, &maxv);
  76.     fprintf(stderr, "Volume range <%i,%i>\n", minv, maxv);

  77.     if(action == AUDIO_VOLUME_GET) {
  78.         if(snd_mixer_selem_get_playback_volume(elem, 0, outvol) < 0) {
  79.             snd_mixer_close(handle);
  80.             return -6;
  81.         }

  82.         fprintf(stderr, "Get volume %i with status %i\n", *outvol, ret);
  83.         /* make the value bound to 100 */
  84.         *outvol -= minv;
  85.         maxv -= minv;
  86.         minv = 0;
  87.         *outvol = 100 * (*outvol) / maxv; // make the value bound from 0 to 100
  88.     }
  89.     else if(action == AUDIO_VOLUME_SET) {
  90.         if(*outvol < 0 || *outvol > VOLUME_BOUND) // out of bounds
  91.             return -7;
  92.         *outvol = (*outvol * (maxv - minv) / (100)) + minv;

  93.         if(snd_mixer_selem_set_playback_volume(elem, 0, *outvol) < 0) {
  94.             snd_mixer_close(handle);
  95.             return -8;
  96.         }
  97.         if(snd_mixer_selem_set_playback_volume(elem, 1, *outvol) < 0) {
  98.             snd_mixer_close(handle);
  99.             return -9;
  100.         }
  101.         fprintf(stderr, "Set volume %i with status %i\n", *outvol, ret);
  102.     }

  103.     snd_mixer_close(handle);
  104.     return 0;
  105. #endif
  106.     }

  107. int main(void)
  108. {
  109.     long vol = -1;
  110.     printf("Ret %i\n", audio_volume(AUDIO_VOLUME_GET, &vol));
  111.     printf("Master volume is %i\n", vol);

  112.     vol = 100;
  113.     printf("Ret %i\n", audio_volume(AUDIO_VOLUME_SET, &vol));

  114.     return 0;
  115. }


阅读(4653) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

帅得不敢出门2016-12-15 15:22:35

wytheZ:你好,最近在搞嵌入式的ALSA程序,请教一个问题:

在您的代码中,设置音量时,为什么要除以99呢??我认为应该是100啊!
*outvol = (*outvol * (maxv - minv) / (100-1)) + minv;

正如文中所言,代码是网上找的,你是对的,正确的的确是100。

回复 | 举报

wytheZ2016-10-19 20:00:44

你好,最近在搞嵌入式的ALSA程序,请教一个问题:

在您的代码中,设置音量时,为什么要除以99呢??我认为应该是100啊!
*outvol = (*outvol * (maxv - minv) / (100-1)) + minv;

dallas482015-08-21 17:02:39

你好  
这段程序在pc上可以正常调节音量

请教下
  1、alsa如何暂停播放?

  2、再有一个问题是我写的alsa播放音乐的程序需要 sudo ./paly 这样的形式才能播放音乐  这是为什么呢?

灰常感谢~~