下面是我很早时的一段代码:
#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 30 /* 存储秒数 */
//#define RATE 8000 /* 采样频率 */
#define RATE 44000 /* 采样频率 */
#define SIZE 16 /* 量化位数 */
#define CHANNELS 2 /* 声道数目 */
/* 用于保存数字音频数据的内存缓冲区 */
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];
void availChannel(unsigned int mask) {
if (mask & (1 << SOUND_MIXER_VOLUME))
{
printf("SOUND_MIXER_VOLUME \n");
}
if (mask & (1 << SOUND_MIXER_MIC))
{
printf("SOUND_MIXER_ MIC \n");
}
if (mask & (1 << SOUND_MIXER_BASS))
{
printf("SOUND_MIXER_ BASS \n");
}
}
int main()
{
int fd;/* 声音设备的文件描述符 */
int id; /*声音输出文件描述符*/
//add by gzyjw
int md;
int mask;
int arg;/* 用于ioctl调用的参数 */
int status; /* 系统调用的返回值 */
int i;
int j;
/* 打开声音设备 */
//fd = open("/dev/dsp", O_RDWR);//arm下的音频文件是"/dev/sound/dsp";
fd = open("/dev/dsp", O_WRONLY);//arm下的音频文件是"/dev/sound/dsp";
if (fd < 0) {
perror("open of /dev/dsp failed");
exit(1);
}
md = open("/dev/mixer",O_RDWR);
if(md < 0){
perror("open of /dev/mixer failed");
exit(1);
}
arg =0x6464;
status = ioctl(md, MIXER_WRITE(SOUND_MIXER_VOLUME), &arg);
if (status == -1)
perror("SOUND_MIXER_VOLUME ioctl failed");
printf("the sound mixer is %x \n",arg);
arg =0x6464;
status = ioctl(md, MIXER_WRITE(SOUND_MIXER_PCM), &arg);
if (status == -1)
perror("SOUND_MIXER_PCM ioctl failed");
printf("the sound mixer is %x \n",arg);
// if (arg != 0x3f3f)
// perror("unable to set sound volumn");
/*打开输出文件*/
id=open("shanghai.wav",O_RDWR);
if(id < 0){
perror("open of sound file failed");
exit(1);
}
/* 设置采样时的量化位数 */
arg = SIZE;
//arg = AFMT_U16_NE;
status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
//status = ioctl(fd, SNDCTL_DSP_SETFMT, &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, SOUND_PCM_WRITE_CHANNELS, &arg);
//status = ioctl(fd, SNDCTL_DSP_STEREO, &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, SOUND_PCM_WRITE_RATE, &arg);
// status = ioctl(fd, SNDCTL_DSP_STEREO, &arg);
if (status == -1)
perror("SOUND_PCM_WRITE_WRITE ioctl failed");
//int enable_bits = ~ PCM_ENABLE_OUTPUT;
//ioctl(fd, SNDCTL_DSP_SETTRIGGER, & enable_bits);
// get available mixer mask
if (ioctl(md, SOUND_MIXER_READ_DEVMASK, &mask) == -1)
perror("checking available mixer channels error!");
else {
printf("Available Mixer Channel:\n");
availChannel(mask);
}
// get available recording device(channel)
if (ioctl(md, SOUND_MIXER_READ_RECMASK, &mask) == -1)
perror("checking available recroding channels error!");
else {
printf("Available Recording Channel:\n");
availChannel(mask);
}
// get stereo or mono info, 1 for stereo, 0 for mono
if (ioctl(md, SOUND_MIXER_READ_STEREODEVS, &mask) == -1)
{
perror("checking stereo channels error!");
}
else {
printf("Available Stereo Channel:\n");
availChannel(mask);
}
/* 读取一定数量的音频数据,并将之写到输出文件中去 */
for(i=0;i<10;i++){
j=read(id, buf, sizeof(buf));
if (j > 0){
write(fd, buf, j); /* 放音 */
printf("playback sound \n");
}
}
/* 关闭输入、输出文件 */
close(fd);
close(id);
return 1;
}