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
- PCM文件播放器
- PCM文件播放器
- sdl播放pcm文件
- waveout播放pcm文件总结
- python-pcm音频播放器
- 使用directsound播放pcm流文件.txt
- 音频队列Audioqueue播放pcm文件
- 播放PCM
- 使用AudioTrack和AudioRecord录制和播放PCM wave文件
- 非阻塞式播放pcm和mp3文件类
- 使用AudioTrack和AudioRecord录制和播放PCM wave文件
- 使用AudioTrack和AudioRecord录制和播放PCM wave文件
- WaveOutXX 播放PCM流
- ios播放PCM数据
- ios播放PCM数据
- IOS播放PCM数据
- IOS播放PCM数据
- ios播放PCM数据
- Android 分析log
- HDU - 1973 - Prime Path (BFS)
- 逆波兰表达式
- 思维导图OC中数据的操作
- 生活随笔:陪女友逛街好累!为什么女生爱购物?总算明白原因了!
- PCM文件播放器
- 数据库 文件同步工具 java 开源 http://www.symmetricds.org
- Java 弱智猜数游戏
- 插入排序
- 1071
- 要做linux运维工程师的朋友,必须要掌握以下几个工具才行
- vs2010+openCV配置
- 使用SQLite本地数据库
- 优化Android应用内存的若干方法