#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include "wav.h"
int play_wav(char *wav_name)
{
struct WAVhdr_str WAVhdr;
char *audiobuf;
int fd, audio, abuf_size, samplesize, stereo, speed, to_read, n;
if ((audio = open("/dev/dsp", O_WRONLY)) == -1) {
perror("/dev/dsp");
return 0;
}
if ((fd = open(wav_name, O_RDONLY)) == -1) {
perror("open");
return 0;
}
read (fd, &WAVhdr, 44);
ioctl(audio, SNDCTL_DSP_GETBLKSIZE, &abuf_size);
audiobuf = malloc(abuf_size);
samplesize = (int)WAVhdr.bits_per_sample;
ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &samplesize);
stereo = (WAVhdr.channels == 2) ? 1 : 0;
ioctl(audio, SNDCTL_DSP_STEREO, &stereo);
speed = WAVhdr.samp_rate;
ioctl(audio, SNDCTL_DSP_SPEED, &speed);
to_read = WAVhdr.data_length;
while (to_read) {
n = (to_read > abuf_size) ? abuf_size : to_read;
read(fd, audiobuf, n);
write(audio, audiobuf, n);
to_read -= n;
}
close(audio);
close(fd);
free(audiobuf);
return 1;
}