- 积分
- 1
贡献0
飞刀0 FD
注册时间2010-3-22
在线时间2 小时
扫一扫,手机访问本帖
|
我现在用ok2440III跑linux,用光盘上的linux镜像文件启动系统,用光盘上的record.c文件录制声音文件,但是执行到status = read(fd2, buf, 960)时就退出程序了,没有报任何错误,请问这是什么原因,附record.c文件:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/soundcard.h>
#include <errno.h>
#define max(a, b) ((a) > (b)? (a):(b))
#define LENGTH 3
#define RATE 8000
#define SIZE 8
#define CHANNELS 2 //立体声的时候没有“嗡嗡”声
int open_dsp(const char *dev_name)
{
int fd;
int arg;
int status;
fd = open(dev_name, O_RDWR);
if (fd < 0) {
return fd;
}
arg =
arg = CHANNELS;
status = ioctl(fd, SNDCTL_DSP_CHANNELS, &arg);
if (status < 0)
perror("SNDCTL_DSP_CHANNELS ioctl failed");
status = ioctl(fd, SOUND_PCM_READ_CHANNELS, &arg);
if(status < 0)
perror("ioctl :SNDCTL_DSP_CHANNELS error!\n");
if (arg != CHANNELS)
perror("unable to set number of channels!\n");
arg = RATE;
status = ioctl(fd, SNDCTL_DSP_SPEED, &arg);
if (status < 0)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");
if(arg != RATE) {
perror("unable to set DSP speed!\n");
}
return fd;
}
int main(int argc, char **argv)
{
int i, j;
int fd2=0;
int maxfd;
fd_set rset;
int status;
unsigned char buf[9600];
unsigned short *pp;
char dev_name[256];
static unsigned short k=0;
int fd;
fd = open("audio.wav", O_RDWR | O_CREAT);
if(fd < 0) {
printf("wrong when creat file!\n");
exit(0);
}
sprintf(dev_name, "/dev/dsp");
if(argc == 2)
{
strncpy(dev_name, argv[1], 128);
}
printf("dev_name = [%s]\n", dev_name);
fd2 = open_dsp(dev_name);
if (fd2 < 0) {
perror("open of /dev/dsp failed");
return fd2;
}
i = 0;
for(;;)
{
//read from /dev/dsp
status = read(fd2, buf, 960);
if(status <= 0) {
printf("read from /dev/dsp wrong!\n");
close(fd2);
break;
// write to file
status = write(fd, buf, 960);
if(status < 0) {
printf("write to file wrong!\n");
}
}
return 0;
} |
|