📄 oss.c
字号:
void main()
{
int audio_fd;
//打开音频设备
if ((audio_fd = open("/dev/dsp",omode,0)) == -1) {
perror("/dev/dsp");
exit(1);
}
//设置音频格式、声道数、采样频率
int format;
format = AFMT_S16_NE;
if (ioctl(audio_fd,SNDCTL_DSP_SETFMT, &format) == -1) {
perror("SNDCTL_DSP_SETFMT");
exit(1);
}
int channels = 2;
if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, & channels) == -1) {
perror("SNDCTL_DSP_CHANNELS");
exit(1);
}
int speed = 44100; //44.1 KHz
if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &speed) == -1) {
perror("SNDCTL_DSP_SPEED");
exit(1);
}
int music_fd;
signed short applicbuf[2048];
int count;
if (mode == PLAY) {//播放模式
if ((music_fd = open(filename, O_RDONLY, 0)) == -1) {
perror(filename);
exit(1);
}
//从文件中读音频数据,直接发送到音频设备
while ((count = read(music_fd, applicbuf, 2048)) > 0) {
write(audio_fd, applicbuf, count);
}
}
else //录音模式
{
if ((music_fd = open(filename, O_WRONLY | O_CREAT, 0)) == -1) {
perror(filename);
exit(1);
}
int mixer_fd;
if ((mixer_fd = open("/dev/mixer", O_WRONLY)) == -1) {
perror("open /dev/mixer error");
exit(1);
}
//设置录音源
int testchan = SOUND_MIXER_CD;
int recsrc = (1 << testchan);
if (ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC,&recsrc) == -1) {
perror("CD");
exit(1);
}
int totalbyte= speed * channels * 2 * 60 * 3;
int totalword = totalbyte/2;
int total = 0;
while (total != totalword) {
if (totalword - total >= 2048)
count = 2048;
else
count = totalword - total;
read(audio_fd, applicbuf, count);//录音
write(music_fd, applicbuf, count);//写入文件
total += count;
}
close(mixer_fd);
}
close(audio_fd);
close(music_fd);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -