Linux 需要 ALSA 教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8485553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ALSA tutorial required
提问by yuvaeasy
I am New to audio programming.I want to create small application which is capable of playing and gives volume control . I am using alsa-lib.
我是音频编程的新手。我想创建能够播放并提供音量控制的小型应用程序。我正在使用 alsa-lib。
I want to know what is the purpose of switch (ex.Master Playback switch), enum in mixer elements and what value should i set to those switchs .
我想知道开关的目的是什么(例如主播放开关),混音器元素中的枚举以及我应该为这些开关设置什么值。
Please suggest me some tutorial for mixer settings as well as alsa programming .
请给我一些关于混音器设置和 alsa 编程的教程。
采纳答案by sdaau
Just collecting some here, that have example code:
只是在这里收集一些,其中有示例代码:
- ALSA Programming HOWTO v.1.0.0 [alsamodular.sourceforge.net]
- A tutorial on using the ALSA Audio API [equalarea.com]2002
- A close look at ALSA [volkerschatz.com]
- ALSA API - Sample Programs With Source Code By Aquiles Yanez2005
- Introduction to Sound Programming with ALSA | Linux Journal (pg3 with example code)2004
- ALSA 编程 HOWTO v.1.0.0 [alsamodular.sourceforge.net]
- 使用 ALSA 音频 API [equalarea.com]2002 的教程
- 仔细观察 ALSA [volkerschatz.com]
- ALSA API - 带有源代码的示例程序,作者 Aquiles Yanez2005
- ALSA 声音编程简介 | Linux Journal (pg3 with example code)2004
Note that some of these are old, and API may have changed in the meantime... you can also look up aplay.c
(the source for the command line arecord
and aplay
), but that one is not the easiest to read for starters...
请注意,其中一些是旧的,并且 API 可能在此期间发生了变化……您也可以查找aplay.c
(命令行arecord
和的源代码aplay
),但对于初学者来说,这不是最容易阅读的……
回答by aktungmak
You'll have a tough time finding anything concrete on ALSA, as I have have found from just starting learning it too. The best place to begin is the ALSA project homepagewhere they link to a number of tutorials, the best one being Dr Nagorni's one IMO.
你将很难找到任何关于 ALSA 的具体内容,正如我从刚开始学习它时所发现的那样。最好的起点是ALSA 项目主页,它们链接到许多教程,最好的一个是 Nagorni 博士的 IMO。
From what it sounds like you're trying to do, Hymanwould most likely be a quicker and easier solution, though.
不过,从听起来您正在尝试做的事情来看,Hyman很可能是一个更快、更简单的解决方案。
回答by Samuel Danielson
Check out the docs. There are some good examples.
查看文档。有一些很好的例子。
http://www.alsa-project.org/alsa-doc/alsa-lib/examples.html
http://www.alsa-project.org/alsa-doc/alsa-lib/examples.html
Be aware of the safe alsa subset.
请注意安全的 alsa 子集。
https://www.winehq.org/pipermail/wine-bugs/2009-June/179698.html
https://www.winehq.org/pipermail/wine-bugs/2009-June/179698.html
Here's something small I put together using the various sources I could find. It miiiiiiiiiight be a good starting point.
这是我使用我能找到的各种来源整理的一些小东西。这可能是一个很好的起点。
/* Compile with gcc -lasound -pthread threadaudio.c */
#include <alsa/asoundlib.h>
#include <pthread.h>
#include <stdio.h>
unsigned char audiobuffer[0x400];
pthread_mutex_t audiomutex = PTHREAD_MUTEX_INITIALIZER;
void changeaudio (int volume) {
int i;
pthread_mutex_lock(&audiomutex);
for (i = 0; i < sizeof(audiobuffer); i++)
audiobuffer[i] = (random() & 0xff) * volume / 10;
pthread_mutex_unlock(&audiomutex);
}
void *startaudio (void *param)
{
static char *device = "default";
snd_output_t *output = NULL;
int *audiostop = (int*)param;
int err;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;
changeaudio(5);
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = snd_pcm_set_params(handle,
SND_PCM_FORMAT_U8,
SND_PCM_ACCESS_RW_INTERLEAVED,
1,
48000,
1,
100000)) < 0) { /* 0.1sec */
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
while (!*audiostop) {
err = snd_pcm_wait(handle, 1000);
if (err < 0) {
fprintf (stderr, "poll failed (%d)\n", err);
break;
}
pthread_mutex_lock(&audiomutex);
frames = snd_pcm_writei(handle, audiobuffer, sizeof(audiobuffer));
pthread_mutex_unlock(&audiomutex);
if (frames < 0)
err = snd_pcm_recover(handle, frames, 0);
if (err < 0) {
printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
break;
}
if (frames > 0 && frames < (long)sizeof(audiobuffer))
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(audiobuffer), frames);
}
snd_pcm_close(handle);
}
int main(void)
{
pthread_t audiothread;
int audiostop = 0;
int volume;
pthread_create(&audiothread, NULL, startaudio, &audiostop);
while (1) {
printf("Enter volume 1 through 10. [0 to quit.]: ");
scanf("%d", &volume);
if (volume == 0) break;
changeaudio(volume);
}
audiostop = 1;
pthread_join(audiothread, NULL);
return 0;
}
And after reading the code above you'll probably want to read this article regarding (among other things) not using locks.
阅读完上面的代码后,您可能想阅读这篇关于(除其他外)不使用锁的文章。
http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing
http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing