测试demo
-
#include "stdio.h"
-
#include "stdlib.h"
-
#include "unistd.h"
-
#include "fcntl.h"
-
#include "sys/types.h"
-
#include "sys/ioctl.h"
-
#include "linux/soundcard.h"
-
#include "termios.h"
-
-
#define RATE 8000
-
#define CHANNELS 1
-
#define BITS 16
-
#define SIZE 320
-
-
unsigned char buf[SIZE];
-
-
int main(int argc,char **argv)
-
{
-
int dspw, mixfd;
-
int arg;
-
FILE *outfd;
-
char *name;
-
int vol;
-
int i;
-
-
if(argc!=2)
-
{
-
printf("usage: %s file_name\n",argv[0]);
-
return 0;
-
}
-
name=argv[1];
-
-
//ioctl mixer to set volume
-
mixfd = open("/dev/mixer",O_RDWR);
-
if(mixfd < 0)
-
{
-
perror("open /dev/mixer Read&&Write error\n");
-
return 1;
-
}
-
-
#if 1
-
ioctl(mixfd, SOUND_MIXER_READ_VOLUME, &vol);
-
printf("read vol=0x%x\n", vol);
-
-
vol = 0x3030;
-
ioctl(mixfd, SOUND_MIXER_WRITE_VOLUME, &vol);
-
-
ioctl(mixfd, SOUND_MIXER_READ_VOLUME, &vol);
-
printf("read vol=0x%x\n", vol);
-
#endif
-
-
-
#if 0
-
//printf("SOUND_MIXER_NRDEVICES=%d\n", SOUND_MIXER_NRDEVICES);
-
for (i = 0; i < SOUND_MIXER_NRDEVICES/8; i++)
-
{
-
ioctl(mixfd, MIXER_READ(i), &vol);
-
printf("[%d]:vol=0x%x\n", i, vol);
-
-
vol = 0x3030;
-
ioctl(mixfd, MIXER_WRITE(i), &vol);
-
-
ioctl(mixfd, MIXER_READ(i), &vol);
-
printf("[%d]:vol=0x%x\n", i, vol);
-
}
-
#endif
-
-
//write only /dev/dsp
-
dspw=open("/dev/dsp",O_WRONLY);
-
if(dspw<0)
-
{
-
perror("open /dev/dsp writeonly error\n");
-
return 1;
-
}
-
arg=BITS;
-
if(ioctl(dspw,SOUND_PCM_WRITE_BITS,&arg)==-1)
-
{
-
perror("set writeonly /dev/dsp bits error\n");
-
return 1;
-
}
-
arg=RATE;
-
if(ioctl(dspw,SOUND_PCM_WRITE_RATE,&arg)==-1)
-
{
-
perror("set writeonly /dev/dsp rate error\n");
-
return 1;
-
}
-
arg=CHANNELS;
-
if(ioctl(dspw,SOUND_PCM_WRITE_CHANNELS,&arg)==-1)
-
{
-
perror("set writeonly /dev/dsp channels error\n");
-
return 1;
-
}
-
-
//outfile open
-
outfd=fopen(name,"r");
-
if(outfd==NULL)
-
{
-
printf("open %s error\n",argv[1]);
-
return 1;
-
}
-
-
while(!feof(outfd))
-
{
-
fread(buf,1,sizeof(buf),outfd);
-
write(dspw,buf,sizeof(buf));
-
}
-
-
close(dspw);
-
fclose(outfd);
-
return 0;
-
}
打开/dev/mixer,利用ioctl设置音量0x3030(左右)
打开/dev/dsp, 输入音频数据,进行播放
Makefile:
-
GCC=/work/tq/bak/toolchain/4.3.3/bin/arm-linux-gcc
-
play:play.c
-
${GCC} -g -o $@ $<
-
cp ./play /tmp/
letitgo_16K_16bit_single.wav --> 采样级别:16KHz,采样大小:16Bit,频道:单声道,位速:256kbps
letitgo_16K_16bit_stereo.wav --> 采样级别:16KHz,采样大小:16Bit,频道:立体道,位速:512kbps
letitgo_8K_16bit_sigle.wav --> 采样级别:8KHz,采样大小:16Bit,频道:单声道,位速:128kbps
letitgo_8K_16bit_stereo.wav --> 采样级别:8KHz,采样大小:16Bit,频道:立体道,位速:256kbps
测试结果:
letitgo_16K_16bit_single.wav --> 正常播放
letitgo_16K_16bit_stereo.wav --> 播放慢
letitgo_8K_16bit_sigle.wav --> 播放快
letitgo_8K_16bit_stereo.wav --> 正常播放
阅读(2010) | 评论(0) | 转发(0) |