关于录音和放音问题,帮忙修改一下
#include <stdlib.h>#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#include <termios.h>
#include<sys/stat.h>;
#include<sys/sysinfo.h>;
#include<sys/select.h>;
#define LENGTH 10//录音时间,秒
#define RATE 11025 //采样频率
#define SIZE 8 //量化位数
#define CHANNELS 1 //声道数目
int fd_dev_r;
int fd_dev_w;
int status;
int arg;
int init_dsp()
{
fd_dev_r= open("/dev/dsp", O_RDONLY,0777);
if (fd_dev_r < 0)
{
perror("Cannot open /dev/dsp device");
return 1;
}
arg = SIZE;
status = ioctl(fd_dev_r, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
{
perror("Cannot set SOUND_PCM_WRITE_BITS ");
return 1;
}
arg = CHANNELS;
status = ioctl(fd_dev_r, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
{
perror("Cannot set SOUND_PCM_WRITE_CHANNELS");
return 1;
}
arg = RATE;
status = ioctl(fd_dev_r, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
{
perror("Cannot set SOUND_PCM_WRITE_WRITE");
return 1;
}
}
int init_dsp1()
{
//打开声卡设备,只写方式;并对声卡进行设置
fd_dev_w = open("/dev/dsp", O_WRONLY,0777);
if (fd_dev_w < 0)
{
perror("Cannot open0 /dev/dsp device");
return 1;
}
arg = SIZE;
status = ioctl(fd_dev_w, SOUND_PCM_WRITE_BITS, &arg);//设置量化位数
if (status == -1)
{
perror("Cannot set0 SOUND_PCM_WRITE_BITS ");
return 1;
}
arg = CHANNELS;
status = ioctl(fd_dev_w, SOUND_PCM_WRITE_CHANNELS, &arg);//设置声道数
if (status == -1)
{
perror("Cannot set0 SOUND_PCM_WRITE_CHANNELS");
return 1;
}
arg = RATE;
status = ioctl(fd_dev_w, SOUND_PCM_WRITE_RATE, &arg);//设置采样率
if (status == -1)
{
perror("Cannot set0 SOUND_PCM_WRITE_WRITE");
return 1;
}
}
int main()
{
int mm=LENGTH*RATE*SIZE*CHANNELS/8;
char buf;
while(1)
{
init_dsp();
status = read(fd_dev_r, buf, sizeof(buf));
if (status != sizeof(buf))
{
perror("read wrong number of bytes");
}
close(fd_dev_r);
init_dsp1();
status = write(fd_dev_w, buf, sizeof(buf));//送声卡播放
if (status != sizeof(buf))
{
perror("wrote wrong number of bytes");
{
close(fd_dev_w);
bzero(buf,mm);
}
return 0;
} 求指教 修改代码不在技术支持范围
页:
[1]