PCM文件播放器

来源:互联网 发布:如何制作淘宝详情图 编辑:程序博客网 时间:2024/05/20 20:43

    • 测试环境
    • 源代码

1. 测试环境

  OK210开发板
  测试方式:./pcmplayer  test.pcm

2. 源代码

#include <fcntl.h>#include <malloc.h>#include <sys/types.h>#include <sys/ioctl.h>#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <sys/stat.h>#include <linux/soundcard.h>#define LENGTH 1        /* 存储秒数 */#define RATE 16000      /* 采样频率 */#define SIZE 8          /* 量化位数 */#define CHANNELS 1      /* 声道数目 */#define RSIZE    8      /*buf的大小 */void dspInit(int fd,int rate,int sample_size , int sample_channel);int rdOpen();int wrOpen();int pcmPlay(const char * pcmfilename);int main(int argc, char *argv[]){    char * filepath;    if(argc != 2)    { printf("Usage:   ./pcmplayer [your file'path]\n"); printf("Example: ./pcmplayer test.pcm\n"); return 0;    }    filepath =  argv[1];    printf("filepath=%s\n",filepath);    pcmPlay(filepath);    return 0;}void dspInit(int fd,int rate,int sample_size , int sample_channel){    /* 设置采样时的量化位数 */    int arg;                        /* 用于ioctl调用的参数 */    int status;                     /* 系统调用的返回值 */    arg = sample_size;    status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);    if (status == -1)        perror("SOUND_PCM_WRITE_BITS ioctl failed");    if (arg != sample_size)        perror("unable to set sample size");    /* 设置采样时的声道数目 */    arg = sample_channel;    status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);    if (status == -1)        perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");    if (arg != sample_channel)        perror("unable to set number of channels");    /* 设置采样时的采样频率 */    arg = rate;    status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);    if (status == -1)        perror("SOUND_PCM_WRITE_WRITE ioctl failed");    status = ioctl(fd, SOUND_PCM_SYNC, 0);    if (status == -1)        perror("SOUND_PCM_SYNC ioctl failed");}int rdOpen(){    int fd;    fd = open("/dev/dsp", O_RDONLY);    if (fd < 0)    {      perror("open of /dev/dsp failed");      return -1;    }    printf("[rdOpen]fd_dsprd =%d\n",fd);    dspInit(fd ,RATE, (SIZE*2) , CHANNELS);    return fd;}int wrOpen(){    int fd;    fd = open("/dev/dsp", O_WRONLY);    if (fd < 0)    {        perror("[write]open of /dev/dsp failed");        return -1;    }    printf("[wrOpen]fd_dspwr =%d\n",fd);    dspInit(fd ,(RATE/2), (SIZE*2) , CHANNELS);    return fd;}int pcmPlay(const char * pcmfilename){    int fd_f;    unsigned int i ;    unsigned char buf[RSIZE];    unsigned long filesize = -1;    int     fDspwr;    struct stat statbuff;    if(NULL == pcmfilename)return -1;    printf("pcmfilename=%s\n",pcmfilename);    if(stat(pcmfilename, &statbuff) < 0){        printf("Get pcmfilesize failed,please check!\n");        return -1;    }else{        filesize = statbuff.st_size;    }    if((fDspwr = wrOpen()) < 0)return -1;    printf("PlayPCM...filesize=%ld\n",filesize);    if(( fd_f = open(pcmfilename, O_RDONLY,0777))==-1)    {        perror("cannot open the sound file");        return -1;    }    lseek(fd_f,0,SEEK_SET);    for(i=0;i<(filesize)/RSIZE;i++)    {     if (read(fd_f, buf, sizeof(buf)) != sizeof(buf))        perror("pcmfile read wrong number of bytes");     if (write(fDspwr, buf, sizeof(buf)) != sizeof(buf))        perror("pcm write wrong number of bytes");    }    close(fd_f);    ioctl(fDspwr, SOUND_PCM_SYNC, 0);    close(fDspwr);    return 0;}
0 0
原创粉丝点击