易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
解决mini2440声卡全双工问题 实现同时录音及播放
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>
#include <pthread.h>
#define LENGTH 3
#define RATE 8000
#define SIZE 8
#define CHANNELS 1
unsigned
char
buf[LENGTH * RATE * SIZE * CHANNELS /8];
int
main()
{
int
fd_r,fd_w;
int
arg;
int
status;
pid_t pid;
int
recing = 1,playing = 0;
pid = fork();
if
(pid < 0){
perror(
"error in fork "
);
}
if
(pid == 0){
fd_r = open(
"/dev/dsp"
,O_RDONLY);
if
(fd_r < 0){
perror(
"open read of /dev/dsp failed"
);
exit(1);
}
arg = SIZE;
status = ioctl(fd_r,SOUND_PCM_WRITE_BITS,&arg);
if
(status == -1)
perror(
"SOUND_PCM_WRITE_BITS ioctl failed"
);
if
(arg != SIZE)
perror(
"unable to set sample size"
);
arg = CHANNELS;
status = ioctl(fd_r,SOUND_PCM_WRITE_CHANNELS,&arg);
if
(status == -1)
perror(
"SOUND_PCM_WRITE_CHANNELS ioctl failed"
);
if
(arg != CHANNELS)
perror(
"unable to set number of channels"
);
arg = RATE;
status = ioctl(fd_r,SOUND_PCM_WRITE_RATE,&arg);
if
(status == -1)
perror(
"SOUND_PCM_WRITE_WRITE ioctl failed"
);
while
(1)
{
if
(playing== 0){
recing = 1;
printf(
"Say something:"
);
status = read(fd_r,buf,
sizeof
(buf));
/* recording*/
if
(status !=
sizeof
(buf))
perror(
"read wrong number of bytes"
);
recing = 0;
}
}
}
else
{
fd_w = open(
"/dev/dsp"
,O_WRONLY);
if
(fd_w < 0)
{
perror(
"open write of /dev/dsp failed"
);
exit(1);
}
arg = SIZE;
status = ioctl(fd_w,SOUND_PCM_WRITE_BITS,&arg);
if
(status == -1)
perror(
"SOUND_PCM_WRITE_BITS ioctl failed"
);
if
(arg != SIZE)
perror(
"unable to set sample size"
);
arg = CHANNELS;
status = ioctl(fd_w,SOUND_PCM_WRITE_CHANNELS,&arg);
if
(status == -1)
perror(
"SOUND_PCM_WRITE_CHANNELS ioctl failed"
);
if
(arg != CHANNELS)
perror(
"unable to set number of channels"
);
arg = RATE;
status = ioctl(fd_w,SOUND_PCM_WRITE_RATE,&arg);
if
(status == -1)
perror(
"SOUND_PCM_WRITE_WRITE ioctl failed"
);
while
(1)
{
if
(recing == 0){
playing = 1;
status = write(fd_w,buf,
sizeof
(buf));
/* playback */
if
(status !=
sizeof
(buf))
perror(
"wrote wrong number of bytes"
);
status = ioctl(fd_w,SOUND_PCM_SYNC,0);
if
(status == -1)
perror(
"SOUND_PCM_SYNC ioctl failed"
);
playing = 0;
}
}
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图