- 论坛徽章:
- 0
|
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#define BUF_LENGTH 2000
#define CHANNELS 0 /*0--single channel;2--double channel*/
#define SAMPLE_RATE 8000
int set_fmt(int audio_fd, int bits, int rate, int channel)
{
int status = -1;
if( -1 == ioctl(audio_fd, SNDCTL_DSP_SAMPLESIZE, &bits))
{
perror("ioctl SNDCTL_DSP_SAMPLESIZE");
exit(1);
}
if(-1 == ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate))
{
perror("ioctl SNDCTL_DSP_SPEED");
exit(1);
}
if(-1 == ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channel))
{
perror("ioctl SNDCTL_DSP_CHANNELS");
exit(1);
}
return(0);
}
main()
{
int audio_fd = 0;
int bytes = 0;
int i = 0;
char buf[BUF_LENGTH];
audio_fd = open("/dev/dsp", O_RDONLY);
if(audio_fd < 0)
{
perror("open /dev/dsp");
exit(1);
}
if(-1 == (set_fmt(audio_fd, AFMT_U8, SAMPLE_RATE, CHANNELS)))
{
perror("set_fmt");
exit(1);
}
for(i=0; i<500; i++)
{
if(-1 == (bytes = read(audio_fd, buf, BUF_LENGTH)))
{
perror("read");
exit(1);
}
}
close(audio_fd);
audio_fd = open("/dev/dsp", O_WRONLY);
if(audio_fd < 0)
{
perror("open /dev/dsp");
exit(1);
}
if(-1 == (set_fmt(audio_fd, AFMT_U8, SAMPLE_RATE, CHANNELS)))
{
perror("set_fmt");
exit(1);
}
for(i=0; i<500; i++)
{
if(-1 == write(audio_fd, buf, bytes))
{
perror("write");
exit(1);
}
}
printf("It'll be closed\n");
close(audio_fd);
}
是这样写马?不行阿,好像打开设备就有问题……程序就是到open这里就运行不了了…… |
|