📄 wave.c
字号:
/*
* =====================================================================================
*
* Filename: wave.c
*
* Description:
*
* Version: 1.0
* Created: 2009年01月17日 20时52分25秒
* Revision: none
* Compiler: gcc
*
* Author: 杨小合 (CH), yang.xiaohe@163.com QQ:475094605
* Company: Student
*
* =====================================================================================
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/ioctl.h>
#include<fcntl.h>
#include<linux/sound.h>
#include<linux/soundcard.h>
#include "option.h"
#include "wave.h"
//信号量
static int openAudio(int fd)
{
return open(AUDIO,O_WRONLY);
}
//信号量
static int closeAudio(int fd)
{
return close(fd);
}
int recordWav(char *file)
{
int fd_dev,fd_d;
int result,ret;
char buf[4096];
struct wave_s wave=
{
"RIFF",
0,
"WAVEfmt",
18,
1,
1,
16000,
16000,
1,
8,
{"data",0}
};
/*
printf("file_length=%d\n",wave.file_length);
printf("nChunkSize=%d\n",wave.nChunkSize);
printf("wFormatTag2=%d\n",wave.wFormatTag2);
printf("nChannels=%d\n",wave.nChannels);
printf("nSamplesPerSec=%d\n",wave.nSamplesPerSec);
printf("nAvgBytesPerSec=%d\n",wave.nAvgBytesPerSec);
printf("nBlockAlign=%d\n",wave.nBlockAlign);
printf("nBitPerSample=%d\n",wave.nBitPerSample);
printf("wave_d nDataBytes=%d\n",wave.data.nDataBytes);
return 0;
*/
if((fd_dev=openAudio(fd_dev))==-1)
{
fprintf(stderr,"Failed to open audio device\n");
return 0;
}
GPRSD_DEBUG("openAUdio OK!\n",NULL);
//打开WAVE文件
if((fd_d=open(file,O_WRONLY | O_CREAT | O_TRUNC))==-1)
{
fprintf(stderr,"Failed to open ./test.wav file\n");
return 0;
}
GPRSD_DEBUG("Open %s OK!\n",file);
// 设置音频通道数
ioctl(fd_dev,SOUND_PCM_WRITE_CHANNELS,wave.nChannels);
// 设置音频采样率
ioctl(fd_dev,SOUND_PCM_WRITE_RATE,wave.nSamplesPerSec);
// 设置音频量化精度
ioctl(fd_dev,SOUND_PCM_WRITE_BITS,wave.nBitPerSample);
result=write(fd_d,&wave,sizeof(struct wave_s));
if(result<0)
{
printf("Write File error.\n");
return 0;
}
while(wave_status)
{
result=read(fd_dev,buf,sizeof(buf));
if(result<0)
{
printf("Read File error.\n");
return 0;
}
ret=write(fd_d,buf,result);
if(ret<0)
{
printf("Write Audio error.\n");
return 0;
}
}
close(fd_d);
closeAudio(fd_dev);
return 0;
}
void *playWav(char *file)
{
int fd_dev,fd_file;
int result,ret;
struct wave_s *wave_head;
char buf[4096];
//打开音频设备文件
if((fd_dev=openAudio(fd_dev))==-1)
{
fprintf(stderr,"Failed to open audio device\n");
return 0;
}
//打开WAVE文件
if((fd_file=open(WAVEFILE,O_RDONLY))==-1)
{
fprintf(stderr,"Failed to open ./test.wav file\n");
return 0;
}
//读取音频文件头信息
result=read(fd_file,buf,sizeof(struct wave_s));
if(result<0)
{
fprintf(stderr,"Read wave file error.\n");
return 0;
}
//固定标记"RIFF"
wave_head=(struct wave_s*)buf;
if((wave_head->format1[0]!='R')||(wave_head->format1[1]!='I')||
(wave_head->format1[2]!='F')||(wave_head->format1[3]!='F'))
{
fprintf(stderr,"File format(1) error. \n");
return 0;
}
printf("File length=%d\n",wave_head->file_length);
// 对比文件格式
wave_head->format2[7]='\0';
if(strcmp(wave_head->format2,"WAVEfmt")!=0)
{
fprintf(stderr,"File format(2) error.\n");
return 0;
}
// 设置音频通道数
printf("Channels=%d\n",wave_head->nChannels);
printf("IO ctrl Channels\n");
ioctl(fd_dev,SOUND_PCM_WRITE_CHANNELS,wave_head->nChannels);
// 设置音频采样率
printf("Samples Per Sec=%d\n",wave_head->nSamplesPerSec);
printf("IO ctrl SamplesPerSec\n");
ioctl(fd_dev,SOUND_PCM_WRITE_RATE,wave_head->nSamplesPerSec);
// 设置音频量化精度
printf("Bits Per Sample=%d\n",wave_head->nBitPerSample);
printf("IO ctrl Bits Per Sample\n");
ioctl(fd_dev,SOUND_PCM_WRITE_BITS,wave_head->nBitPerSample);
// 查看WAVE文件中的音频数据的长度
printf("%s\n",wave_head->data.format3);
if((wave_head->data.format3[0]!='d')||(wave_head->data.format3[1]!='a')||
(wave_head->data.format3[2]!='t')||(wave_head->data.format3[3]!='a'))
{
fprintf(stderr,"File format(3) is Unknown.\n");
}
else
{
fprintf(stderr,"File format(3) is data.\n");
}
printf("Total Data Bytes=%d\n",wave_head->data.nDataBytes);
// 播放音频文件
while(result)
{
result=read(fd_file,buf,sizeof(buf));
if(result<0)
{
printf("Read File error.\n");
return 0;
}
ret=write(fd_dev,buf,result);
if(ret<0)
{
printf("Write Audio error.\n");
return 0;
}
fprintf(stderr,".");
}
close(fd_file);
printf("Close Wave File:%s\n",WAVEFILE);
closeAudio(fd_dev);
printf("Close device File:/dev/audio\n");
return (void*)0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -