Linux 下录制和播放 声音, record & play audio in linux
2011-03-09 12:14
736 查看
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>
#define LENGTH 3 /* how many seconds of speech to store */
#define RATE 44100 /* the sampling rate */
#define SIZE 16 /* sample size: 8 or 16 bits */
#define CHANNELS 1 /* 1 = mono 2 = stereo */
/* this buffer holds the digitized audio */
//unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];
int main()
{
int fdplay;
int fdrec;
int arg;
int status;
FILE *fp = 0;
int i=0;
int bufferSize = 1024;
short *buf = 0;
/* open sound device */
fdplay = open("/dev/dsp", O_WRONLY);
if (fdplay < 0) {
printf("open of /dev/dsp failed/n");
return 0;
}
fdrec = open("/dev/dsp1", O_RDONLY);
if (fdrec < 0) {
printf("open of /dev/dsp2 failed/n");
exit(1);
}
/* set sampling parameters */
arg = SIZE; /* sample size */
status = ioctl(fdplay, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
{
printf("SOUND_PCM_WRITE_BITS ioctl failed/n");
goto exit;
}
if (arg != SIZE)
{
printf("unable to set play sample size/n");
goto exit;
}
arg = SIZE; /* sample size */
status = ioctl(fdrec, SOUND_PCM_READ_BITS, &arg);
if (status == -1)
{
printf("SOUND_PCM_RAED_BITS ioctl failed/n");
goto exit;
}
if (arg != SIZE)
{
printf("unable to set rec sample size = %d/n", arg);
goto exit;
}
arg = CHANNELS; /* mono or stereo */
status = ioctl(fdplay, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
{
printf("SOUND_PCM_WRITE_CHANNELS ioctl failed/n");
goto exit;
}
if (arg != CHANNELS)
{
printf("unable to set play number of channels/n");
goto exit;
}
arg = CHANNELS; /* mono or stereo */
status = ioctl(fdrec, SOUND_PCM_READ_CHANNELS, &arg);
if (status == -1)
{
printf("SOUND_PCM_RAED_CHANNELS ioctl failed/n");
goto exit;
}
if (arg != CHANNELS)
{
printf("unable to set rec number of channels/n");
goto exit;
}
arg = RATE; /* sampling rate */
status = ioctl(fdplay, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
{
printf("SOUND_PCM_WRITE_WRITE ioctl failed");
goto exit;
}
arg = RATE; /* sampling rate */
status = ioctl(fdrec, SOUND_PCM_READ_RATE, &arg);
if (status == -1)
{
printf("SOUND_PCM_READ_WRITE ioctl failed");
goto exit;
}
//fp = fopen("1.wav", "r");
buf = (short*)malloc(bufferSize*4);
while (1)
{
if(read(fdrec, buf, bufferSize*4) != bufferSize*4)
{
printf("read file failed/n");
break;
}
printf("Play one sample %d/n", i++);
if ((write(fdplay, buf, bufferSize*4)) != bufferSize*4)
{
printf("PLay one buffer %d/n", i++);
break;
}
if(i >100)
break;
//status = ioctl(fd, SOUND_PCM_SYNC, 0);
/*if (status == -1)
{
printf("SOUND_PCM_SYNC ioctl failed");
break;
}*/
}
free(buf);
ioctl(fdplay, SOUND_PCM_SYNC, 0);
exit:
close(fdplay);
close(fdrec);
}
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>
#define LENGTH 3 /* how many seconds of speech to store */
#define RATE 44100 /* the sampling rate */
#define SIZE 16 /* sample size: 8 or 16 bits */
#define CHANNELS 1 /* 1 = mono 2 = stereo */
/* this buffer holds the digitized audio */
//unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];
int main()
{
int fdplay;
int fdrec;
int arg;
int status;
FILE *fp = 0;
int i=0;
int bufferSize = 1024;
short *buf = 0;
/* open sound device */
fdplay = open("/dev/dsp", O_WRONLY);
if (fdplay < 0) {
printf("open of /dev/dsp failed/n");
return 0;
}
fdrec = open("/dev/dsp1", O_RDONLY);
if (fdrec < 0) {
printf("open of /dev/dsp2 failed/n");
exit(1);
}
/* set sampling parameters */
arg = SIZE; /* sample size */
status = ioctl(fdplay, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
{
printf("SOUND_PCM_WRITE_BITS ioctl failed/n");
goto exit;
}
if (arg != SIZE)
{
printf("unable to set play sample size/n");
goto exit;
}
arg = SIZE; /* sample size */
status = ioctl(fdrec, SOUND_PCM_READ_BITS, &arg);
if (status == -1)
{
printf("SOUND_PCM_RAED_BITS ioctl failed/n");
goto exit;
}
if (arg != SIZE)
{
printf("unable to set rec sample size = %d/n", arg);
goto exit;
}
arg = CHANNELS; /* mono or stereo */
status = ioctl(fdplay, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
{
printf("SOUND_PCM_WRITE_CHANNELS ioctl failed/n");
goto exit;
}
if (arg != CHANNELS)
{
printf("unable to set play number of channels/n");
goto exit;
}
arg = CHANNELS; /* mono or stereo */
status = ioctl(fdrec, SOUND_PCM_READ_CHANNELS, &arg);
if (status == -1)
{
printf("SOUND_PCM_RAED_CHANNELS ioctl failed/n");
goto exit;
}
if (arg != CHANNELS)
{
printf("unable to set rec number of channels/n");
goto exit;
}
arg = RATE; /* sampling rate */
status = ioctl(fdplay, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
{
printf("SOUND_PCM_WRITE_WRITE ioctl failed");
goto exit;
}
arg = RATE; /* sampling rate */
status = ioctl(fdrec, SOUND_PCM_READ_RATE, &arg);
if (status == -1)
{
printf("SOUND_PCM_READ_WRITE ioctl failed");
goto exit;
}
//fp = fopen("1.wav", "r");
buf = (short*)malloc(bufferSize*4);
while (1)
{
if(read(fdrec, buf, bufferSize*4) != bufferSize*4)
{
printf("read file failed/n");
break;
}
printf("Play one sample %d/n", i++);
if ((write(fdplay, buf, bufferSize*4)) != bufferSize*4)
{
printf("PLay one buffer %d/n", i++);
break;
}
if(i >100)
break;
//status = ioctl(fd, SOUND_PCM_SYNC, 0);
/*if (status == -1)
{
printf("SOUND_PCM_SYNC ioctl failed");
break;
}*/
}
free(buf);
ioctl(fdplay, SOUND_PCM_SYNC, 0);
exit:
close(fdplay);
close(fdrec);
}
相关文章推荐
- 使用AudioTrack和AudioRecord录制和播放PCM wave文件
- recordMP3实现网页录制声音,上传,播放
- Android通过AudioRecord录制系统内置声音
- Android录制和播放AudioRecord和AudioTrack
- Android音频处理——通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能
- Android音频处理——通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能
- AudioRecord实现"助听器" ,录制和播放功能
- 使用AudioTrack和AudioRecord录制和播放PCM wave文件
- Cocos Studio中声音组件(ComAudio)的获取与播放
- linux播放音乐,录音命令-----arecord,aplay
- Android音频处理——通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能
- Android音频处理之通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能
- Android: AudioRecord 通过蓝牙设备录制声音
- linux下声音的录制与播放 /dev/dsp
- 使用AudioTrack和AudioRecord录制和播放PCM wave文件
- Android2.3-> Audio-> aplay播放的的问题
- Android利用AudioRecord类实现自己的音频录制程序
- 解决A/libc Fatal signal 11 (SIGSEGV)错误,这可能是目前最鲁棒的Android声音录制和播放封装库了
- Linux 下运行Java程序报“Exception in thread "main" java.lang.NoClassDefFoundError”
- Web 播放声音 — AMR(Audio) 篇