Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1309077
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: LINUX

2011-03-04 22:41:09

【playwav.h】
#ifndef _PLAY_WAV_H
#define _PLAY_WAV_H
#define AUDIO_DEV_FILE "/dev/audio"
#define MAGIC "RIFF"
typedef struct {
        unsigned int  riff_flag;   /* RIFF flag                         */
        unsigned int  file_long;   /* then wav file long                */
        unsigned int  wav_flag;    /* wave flag                         */
        unsigned int  fnt_flag;    /* fnt flag                          */
        unsigned int  pass_byte;   /* ususal be 16 indicate head 44 byte*/
        short         format;      /* 01 is pcm , or not surport        */
  short         channels;    /* the number of sound channel       */
  int          rate;        /* the frequency of get sample       */
        unsigned int  tran_rate;   /* translate rate                    */
        short         date_block;  /* date block adjust                 */
  short         bits;        /* the bit number of numeric         */
        unsigned int  data_flag;   /* data flag                         */
        unsigned int  sound_long;  /* wav data long                     */
} wav_head_t;
int set_wav_arg( wav_head_t head, int dev_fd );
int wait_play_end( int dev_fd );
#endif /* _PLAY_WAV_H */
 
==========================================================================================【playwav.c】
#include
#include
#include
#include
#include
#include "playwav.h"
int set_wav_arg( wav_head_t head, int dev_fd )
{
 
  /* set numeric bits of get sample */
 int arg;
 int retval;
 
 arg = head.bits;
 retval = ioctl( dev_fd, SOUND_PCM_WRITE_BITS, &arg );
 if( retval == -1 ){
  printf("sound_pcm_write_bits failed.\n");
  exit( 1 );
 }
 if( head.bits != arg )
  perror("Unable to set sample bits !");
 
  /* set the number of channel at get sample */
  arg = head.channels;
  retval = ioctl( dev_fd, SOUND_PCM_WRITE_CHANNELS, &arg );
  if( retval == -1 ){
   printf("SOUND_PCM_WRITE_CHANNELS failed.\n");
  exit( 1 ); 
  }
  if( arg != head.channels )
         perror("Unable to set number of channels !");
 
  /* set the frequence of get samples */
  arg = head.rate;
  retval = ioctl(dev_fd,SOUND_PCM_WRITE_RATE,&arg);
  if( retval == -1 ){
   printf("set SOUND_PCM_WRITE_RATE failed.\n");
  exit( 1 );
  }
  if( arg != head.rate )
  perror("Uable to set number of rate");
 
 return 0;
}
int wait_play_end( int dev_fd )
{
  
 int retval ;
     retval = ioctl( dev_fd, SOUND_PCM_SYNC, 0 );
 if( retval == -1 ){
  printf("SOUND_PCM_SYNC failed.\n");
  exit( 1 );
 }
}
 
=====================================================================================
【t1.c】
#include
#include
#include
#include
#include
#include "playwav.h"
#include
int main()
{
 wav_head_t head;
 int fd = open("./sound.wav", O_RDONLY); 
 if(fd < 0)
 {
  perror("open");
  return -1;
 }
 int ret = read(fd, &head, sizeof(head));
 if(ret != sizeof(head))
 {
  return -1;
 }
 int dev_fd = open(AUDIO_DEV_FILE, O_RDWR);
 if(dev_fd < 0)
 {
  perror("dev open");
  return -1;
 }
 set_wav_arg(head, dev_fd);
 printf("采样率: %d \n采样位数: %d \n声道数: %d \n",  head.rate, head.bits, head.channels); 
 int size = (head.bits * head.rate * head.channels) / 8;
 char buf[size];
 memset(buf, 0, size);
 while(1)
 {
  ret = read(fd, buf, size);
  if(ret == 0)
  {
   break;
  }
  write(dev_fd, buf, ret);
 }
 wait_play_end(dev_fd);
 close(dev_fd);
 close(fd);
}

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