alsa record example.c

 
 
/*
A Minimal Capture Program
This program opens an audio interface for capture, configures it for
stereo, 16 bit, 44.1kHz, interleaved conventional read/write
access. Then its reads a chunk of random data from it, and exits. It
isn't meant to be a real program.
From on Paul David's tutorial : http://equalarea.com/paul/alsa-audio.html
Fixes rate and buffer problems
sudo apt-get install libasound2-dev
gcc -o alsa-record-example -lasound alsa-record-example.c && ./alsa-record-example hw:0
*/
 
# include <stdio.h>
# include <stdlib.h>
# include <alsa/asoundlib.h>
main ( int argc, char *argv[])
{
int i;
int err;
char *buffer;
int buffer_frames = 128;
unsigned int rate = 44100;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
 
if ((err = snd_pcm_open (&capture_handle, argv[ 1], SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
argv[ 1],
snd_strerror (err));
exit ( 1);
}
 
fprintf(stdout, "audio interface opened\n");
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit ( 1);
}
 
fprintf(stdout, "hw_params allocated\n");
if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit ( 1);
}
 
fprintf(stdout, "hw_params initialized\n");
if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
exit ( 1);
}
 
fprintf(stdout, "hw_params access setted\n");
if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, format)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit ( 1);
}
 
fprintf(stdout, "hw_params format setted\n");
if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit ( 1);
}
fprintf(stdout, "hw_params rate setted\n");
 
if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit ( 1);
}
 
fprintf(stdout, "hw_params channels setted\n");
if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit ( 1);
}
 
fprintf(stdout, "hw_params setted\n");
snd_pcm_hw_params_free (hw_params);
 
fprintf(stdout, "hw_params freed\n");
if ((err = snd_pcm_prepare (capture_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit ( 1);
}
 
fprintf(stdout, "audio interface prepared\n");
 
buffer = malloc( 128 * snd_pcm_format_width(format) / 8 * 2);
 
fprintf(stdout, "buffer allocated\n");
 
for (i = 0; i < 10; ++i) {
if ((err = snd_pcm_readi (capture_handle, buffer, buffer_frames)) != buffer_frames) {
fprintf (stderr, "read from audio interface failed (%s)\n",
err, snd_strerror (err));
exit ( 1);
}
fprintf(stdout, "read %d done\n", i);
}
 
free(buffer);
 
fprintf(stdout, "buffer freed\n");
snd_pcm_close (capture_handle);
fprintf(stdout, "audio interface closed\n");
 
exit ( 0);
}
### ALSA Framework Overview and Usage ALSA (Advanced Linux Sound Architecture) is a comprehensive framework for handling sound in the Linux operating system. It provides kernel drivers, a user-space library, and applications to facilitate audio and MIDI functionality on Linux systems[^3]. Below is an overview of its usage and key components: #### Key Components of ALSA 1. **Kernel Drivers**: These are responsible for interacting directly with the hardware. They provide low-level access to sound cards and other audio devices. 2. **User-Space Library (libasound)**: This library abstracts the complexities of the kernel drivers, allowing developers to interact with audio devices using higher-level APIs[^4]. 3. **Applications**: Tools like `aplay`, `arecord`, and `alsamixer` are part of the ALSA suite and can be used to test and manage audio devices. #### Basic Usage of ALSA To use ALSA in a program, one typically includes the ALSA headers and links against the ALSA library (`libasound`). Below is an example of playing a PCM stream using ALSA: ```c #include <alsa/asoundlib.h> #include <stdio.h> int main() { snd_pcm_t *handle; snd_pcm_sframes_t frames; unsigned char buffer[320]; // Example buffer // Open PCM device for playback if (snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) { printf("Error opening PCM device.\n"); return -1; } // Set hardware parameters snd_pcm_hw_params_t *hw_params; snd_pcm_hw_params_alloca(&hw_params); snd_pcm_hw_params_any(handle, hw_params); snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(handle, hw_params, SND_PCM_FORMAT_S16_LE); snd_pcm_hw_params_set_rate_near(handle, hw_params, 44100, NULL); snd_pcm_hw_params_set_channels(handle, hw_params, 2); if (snd_pcm_hw_params(handle, hw_params) < 0) { printf("Error setting hardware parameters.\n"); return -1; } // Write data to the PCM device for (int i = 0; i < 10; i++) { frames = snd_pcm_writei(handle, buffer, 80); if (frames < 0) { frames = snd_pcm_recover(handle, frames, 0); } if (frames < 0) { printf("Error writing to PCM device.\n"); return -1; } } snd_pcm_drain(handle); snd_pcm_close(handle); return 0; } ``` This example demonstrates how to open a PCM device, set hardware parameters, and write audio data to it. The `snd_pcm_writei` function writes interleaved samples to the device[^5]. #### Important Concepts - **PCM Devices**: Represent digital audio streams. Each PCM device has its own configuration, such as sample rate, format, and channels. - **Control Interface**: Used to configure and monitor audio hardware settings, such as volume and mute controls. - **Sequencer Interface**: Provides support for MIDI events and allows communication between MIDI applications. #### Compiling and Linking ALSA Programs When compiling programs that use ALSA, ensure the ALSA development libraries are installed. Use the following flags during compilation: ```bash gcc -o alsa_test alsa_test.c -lasound ``` #### Troubleshooting If encountering issues with ALSA, check the following: - Ensure the correct PCM device name is being used (e.g., `default`, `plughw:0,0`). - Verify that the ALSA drivers are properly installed and loaded. - Check permissions for accessing audio devices[^6].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值