Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2150707
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2015-05-14 12:02:42

测试demo
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "unistd.h"
  4. #include "fcntl.h"
  5. #include "sys/types.h"
  6. #include "sys/ioctl.h"
  7. #include "linux/soundcard.h"
  8. #include "termios.h"

  9. #define RATE 8000
  10. #define CHANNELS 1
  11. #define BITS 16
  12. #define SIZE 320

  13. unsigned char buf[SIZE];

  14. int main(int argc,char **argv)
  15. {
  16.     int dspw, mixfd;
  17.     int arg;
  18.     FILE *outfd;
  19.     char *name;
  20.     int vol;
  21.     int i;

  22.     if(argc!=2)
  23.     {
  24.         printf("usage: %s file_name\n",argv[0]);
  25.         return 0;
  26.     }
  27.     name=argv[1];
  28.     
  29.     //ioctl mixer to set volume
  30.     mixfd = open("/dev/mixer",O_RDWR);
  31.     if(mixfd < 0)
  32.     {
  33.         perror("open /dev/mixer Read&&Write error\n");
  34.         return 1;
  35.     }

  36. #if 1
  37.     ioctl(mixfd, SOUND_MIXER_READ_VOLUME, &vol);
  38.     printf("read vol=0x%x\n", vol);

  39.     vol = 0x3030;
  40.     ioctl(mixfd, SOUND_MIXER_WRITE_VOLUME, &vol);

  41.     ioctl(mixfd, SOUND_MIXER_READ_VOLUME, &vol);
  42.     printf("read vol=0x%x\n", vol);
  43. #endif


  44. #if 0
  45.     //printf("SOUND_MIXER_NRDEVICES=%d\n", SOUND_MIXER_NRDEVICES);
  46.     for (i = 0; i < SOUND_MIXER_NRDEVICES/8; i++)
  47.     {
  48.         ioctl(mixfd, MIXER_READ(i), &vol);
  49.         printf("[%d]:vol=0x%x\n", i, vol);

  50.         vol = 0x3030;
  51.         ioctl(mixfd, MIXER_WRITE(i), &vol);

  52.         ioctl(mixfd, MIXER_READ(i), &vol);
  53.         printf("[%d]:vol=0x%x\n", i, vol);
  54.     }
  55. #endif

  56.     //write only /dev/dsp
  57.     dspw=open("/dev/dsp",O_WRONLY);
  58.     if(dspw<0)
  59.     {
  60.         perror("open /dev/dsp writeonly error\n");
  61.         return 1;
  62.     }
  63.     arg=BITS;
  64.     if(ioctl(dspw,SOUND_PCM_WRITE_BITS,&arg)==-1)
  65.     {
  66.         perror("set writeonly /dev/dsp bits error\n");
  67.         return 1;
  68.     }
  69.     arg=RATE;
  70.     if(ioctl(dspw,SOUND_PCM_WRITE_RATE,&arg)==-1)
  71.     {
  72.         perror("set writeonly /dev/dsp rate error\n");
  73.         return 1;
  74.     }
  75.     arg=CHANNELS;
  76.     if(ioctl(dspw,SOUND_PCM_WRITE_CHANNELS,&arg)==-1)
  77.     {
  78.         perror("set writeonly /dev/dsp channels error\n");
  79.         return 1;
  80.     }

  81.     //outfile open
  82.     outfd=fopen(name,"r");
  83.     if(outfd==NULL)
  84.     {
  85.         printf("open %s error\n",argv[1]);
  86.         return 1;
  87.     }

  88.     while(!feof(outfd))
  89.     {
  90.         fread(buf,1,sizeof(buf),outfd);
  91.         write(dspw,buf,sizeof(buf));
  92.     }

  93.     close(dspw);
  94.     fclose(outfd);
  95.     return 0;
  96. }
打开/dev/mixer,利用ioctl设置音量0x3030(左右)
打开/dev/dsp, 输入音频数据,进行播放

Makefile:
  1. GCC=/work/tq/bak/toolchain/4.3.3/bin/arm-linux-gcc
  2. play:play.c
  3.     ${GCC} -g -o $@ $<
  4.     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 --> 正常播放


阅读(2002) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~