板子上需要控制声音,试过salsa的库(精简版的alsa),功能有些受限,在PC上测试无法正常获取声音大小,打算采用alsa
http://www.alsa-project.org/main/index.php/Download
下载源码,假设是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来获取设置声音
点击(此处)折叠或打开
- #include <unistd.h>
- #include <fcntl.h>
- #ifdef OSSCONTROL
- #define MIXER_DEV "/dev/dsp"
- #include <sys/soundcard.h>
- #include <sys/ioctl.h>
- #include <stdio.h>
- #else
- #include <alsa/asoundlib.h>
- #endif
- #define VOLUME_BOUND 10000
- typedef enum {
- AUDIO_VOLUME_SET,
- AUDIO_VOLUME_GET,
- } audio_volume_action;
- /*
- Drawbacks. Sets volume on both channels but gets volume on one. Can be easily adapted.
- */
- int audio_volume(audio_volume_action action, long* outvol)
- {
- int ret = 0;
- #ifdef OSSCONTROL
- int fd, devs;
- if ((fd = open(MIXER_DEV, O_WRONLY)) > 0)
- {
- if(action == AUDIO_VOLUME_SET) {
- if(*outvol < 0 || *outvol > 100)
- return -2;
- *outvol = (*outvol << 8) | *outvol;
- ioctl(fd, SOUND_MIXER_WRITE_VOLUME, outvol);
- }
- else if(action == AUDIO_VOLUME_GET) {
- ioctl(fd, SOUND_MIXER_READ_VOLUME, outvol);
- *outvol = *outvol & 0xff;
- }
- close(fd);
- return 0;
- }
- return -1;;
- #else
- snd_mixer_t* handle;
- snd_mixer_elem_t* elem;
- snd_mixer_selem_id_t* sid;
- static const char* mix_name = "Master";
- static const char* card = "default";
- static int mix_index = 0;
- long pmin, pmax;
- long get_vol, set_vol;
- float f_multi;
- snd_mixer_selem_id_alloca(&sid);
- //sets simple-mixer index and name
- snd_mixer_selem_id_set_index(sid, mix_index);
- snd_mixer_selem_id_set_name(sid, mix_name);
- if ((snd_mixer_open(&handle, 0)) < 0)
- return -1;
- if ((snd_mixer_attach(handle, card)) < 0) {
- snd_mixer_close(handle);
- return -2;
- }
- if ((snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
- snd_mixer_close(handle);
- return -3;
- }
- ret = snd_mixer_load(handle);
- if (ret < 0) {
- snd_mixer_close(handle);
- return -4;
- }
- elem = snd_mixer_find_selem(handle, sid);
- if (!elem) {
- snd_mixer_close(handle);
- return -5;
- }
- long minv, maxv;
- snd_mixer_selem_get_playback_volume_range (elem, &minv, &maxv);
- fprintf(stderr, "Volume range <%i,%i>\n", minv, maxv);
- if(action == AUDIO_VOLUME_GET) {
- if(snd_mixer_selem_get_playback_volume(elem, 0, outvol) < 0) {
- snd_mixer_close(handle);
- return -6;
- }
- fprintf(stderr, "Get volume %i with status %i\n", *outvol, ret);
- /* make the value bound to 100 */
- *outvol -= minv;
- maxv -= minv;
- minv = 0;
- *outvol = 100 * (*outvol) / maxv; // make the value bound from 0 to 100
- }
- else if(action == AUDIO_VOLUME_SET) {
- if(*outvol < 0 || *outvol > VOLUME_BOUND) // out of bounds
- return -7;
- *outvol = (*outvol * (maxv - minv) / (100)) + minv;
- if(snd_mixer_selem_set_playback_volume(elem, 0, *outvol) < 0) {
- snd_mixer_close(handle);
- return -8;
- }
- if(snd_mixer_selem_set_playback_volume(elem, 1, *outvol) < 0) {
- snd_mixer_close(handle);
- return -9;
- }
- fprintf(stderr, "Set volume %i with status %i\n", *outvol, ret);
- }
- snd_mixer_close(handle);
- return 0;
- #endif
- }
- int main(void)
- {
- long vol = -1;
- printf("Ret %i\n", audio_volume(AUDIO_VOLUME_GET, &vol));
- printf("Master volume is %i\n", vol);
- vol = 100;
- printf("Ret %i\n", audio_volume(AUDIO_VOLUME_SET, &vol));
- return 0;
- }
本文介绍了一种在Linux环境下利用ALSA库控制音量的方法。文中提供了详细的编译安装步骤,并分享了一个C语言示例程序,该程序能够读取和设置音频设备的音量。
1382

被折叠的 条评论
为什么被折叠?



