Playing a WAV file using ALSA (Advanced Linux Sound Architecture) in C requires reading the WAV file format, extracting audio data, and then using ALSA to send this data to your sound device. To keep things simple, we'll assume the WAV file is standard, containing uncompressed PCM audio data.
Before proceeding, ensure the ALSA library development files are installed on your system. For many Linux distributions, you can install these with the libasound2-dev package.
Here's a basic roadmap of what you need to do:
- Open the WAV file and read its header to get audio format parameters such as sample rate, bit depth, and number of channels.
- Initialize the ALSA playback device with these parameters.
- Read audio samples from the WAV file and write them to the ALSA device for playback.
Example: Playing a WAV File with ALSA
The following is a basic example that demonstrates how to play a WAV file using ALSA in C. This example assumes the WAV file is in a standard format (e.g., 16-bit PCM, stereo or mono) and skips any parsing of the file's header beyond the initial 44 bytes, which is the typical size of a WAV file header.
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#define PCM_DEVICE "default"
// Function to set up ALSA parameters and play the buffer
void play_sound(const char *filename) {
FILE *fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Failed to open file '%s'\n", filename);
return;
}
// Skip the WAV header
fseek(fp, 44, SEEK_SET);
// ALSA variables
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
unsigned int sample_rate = 44100; // Change if your WAV file has a different sample rate
int channels = 2; // Change if your WAV file is mono
int pcm;
// Open the PCM device in playback mode
if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0) < 0) {
fprintf(stderr, "ERROR: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE, snd_strerror(pcm));
fclose(fp);
return;
}
// Allocate parameters object and fill it with default values
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm_handle, params);
// Set parameters
if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
fprintf(stderr, "ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
}
if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE) < 0) {
fprintf(stderr, "ERROR: Can't set format. %s\n", snd_strerror(pcm));
}
if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0) {
fprintf(stderr, "ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
}
if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &sample_rate, 0) < 0) {
fprintf(stderr, "ERROR: Can't set rate. %s\n", snd_strerror(pcm));
}
// Write parameters
if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0) {
fprintf(stderr, "ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
}
// Prepare the device
snd_pcm_prepare(pcm_handle);
// Buffer for the audio data
char *buffer = (char *)malloc(4096);
ssize_t read_bytes;
// Read from the WAV file and write to the PCM device
while ((read_bytes = fread(buffer, 1, 4096, fp)) > 0) {
if (pcm = snd_pcm_writei(pcm_handle, buffer, read_bytes / 4) == -EPIPE) {
snd_pcm_prepare(pcm_handle);
}
else if (pcm < 0) {
fprintf(stderr, "ERROR: Can't write to PCM device. %s\n", snd_strerror(pcm));
}
}
// Cleanup
fclose(fp);
free(buffer);
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <wav file>\n", argv[0]);
return -1;
}
play_sound(argv[1]);
return 0;
}
Compile this program with gcc and link against the ALSA library:
gcc -o playwav playwav.c -lasound
Run your program with the WAV file as an argument:
./playwav yourfile.wav
This example skips detailed error-checking and parsing of the WAV file's header. It assumes the WAV file is in a somewhat standard format with 16-bit sample size. For real-world applications, you'd want to properly parse the WAV header to determine sample rate, bit depth, and channels, and handle various error conditions more gracefully.