ARM2410 OSS播放WAV文件与音频采集
来源:互联网 发布:网络教育有没有档案 编辑:程序博客网 时间:2024/06/10 00:05
ARM2410 OSS播放WAV文件与音频采集,完整的源代码、说明文件、编译方法等请到论坛下载。下载地址:http://bbs.rosoo.net/thread-739-1-1.html
1. how to make?
gcc sndtools.c record.c -o test
2. about OpenSnd
Sound driver name is different according to you sound card & OS.
and here are some the typical name:
OSS audio system --> /dev/dsp or /dev/mixer
ALSA --> /dev/snd/pcmC0D0c
3. others
any questions about this, contact me by mailto:jacky@rosoo.net
or visit http://bbs.rosoo.net for discussion
/////////////////////play.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include "sndtools.h"
- #define WAVOUTDEV FMT8K
- typedef unsigned int DWORD;
- typedef unsigned short int WORD;
- struct RIFF_HEADER
- {
- char szRiffID[4]; // 'R','I','F','F'
- DWORD dwRiffSize;
- char szRiffFormat[4]; // 'W','A','V','E'
- };
- struct WAVE_FORMAT
- {
- WORD wFormatTag;
- WORD wChannels;
- DWORD dwSamplesPerSec;
- DWORD dwAvgBytesPerSec;
- WORD wBlockAlign;
- WORD wBitsPerSample;
- };
- struct FMT_BLOCK
- {
- char szFmtID[4]; // 'f','m','t',' '
- DWORD dwFmtSize;
- struct WAVE_FORMAT wavFormat;
- };
- struct FACT_BLOCK
- {
- char szFactID[4]; // 'f','a','c','t'
- DWORD dwFactSize;
- };
- struct DATA_BLOCK
- {
- char szDataID[4]; // 'd','a','t','a'
- DWORD dwDataSize;
- };
- int main()
- {
- char *buf;
- int dwSize;
- int i;
- struct RIFF_HEADER riffheader;
- struct FMT_BLOCK fmtblock;
- struct DATA_BLOCK datablock;
- FILE * fp;
- printf("WORD %d /n",sizeof(WORD));
- printf("DWORD %d/n",sizeof(DWORD));
- if((fp=fopen("test.wav","rw")) == NULL)
- {
- printf("Cannot open test.wav");
- return;
- }
- fread(&riffheader,sizeof(riffheader),1,fp);
- fread(&fmtblock,sizeof(fmtblock),1,fp);
- fread(&datablock,sizeof(datablock),1,fp);
- printf("riff size:%d /n",riffheader.dwRiffSize);
- printf("riff format:%s/n",riffheader.szRiffFormat);
- printf("fmtID: %s /n",fmtblock.szFmtID);
- printf("fmtSize %d /n",fmtblock.dwFmtSize);
- printf("fmtFormat %d/n",fmtblock.wavFormat.wFormatTag);
- printf("fmtChannels %d/n",fmtblock.wavFormat.wChannels);
- printf("SamplePerSec %d/n",fmtblock.wavFormat.dwSamplesPerSec);
- printf("AvgBytesPerSec %d/n",fmtblock.wavFormat.dwAvgBytesPerSec);////////////////////
- printf("BlockAlign %d /n", fmtblock.wavFormat.wBlockAlign);//=2;
- printf("BitsPerSample %d/n",fmtblock.wavFormat.wBitsPerSample);//=16;
- /*
- datablock.szDataID[0]='d';
- datablock.szDataID[1]='a';
- datablock.szDataID[2]='t';
- datablock.szDataID[3]='a';
- */
- printf("DataSize %d /n",datablock.dwDataSize);
- if(!OpenSnd())
- {
- printf("Open sound device error!//n");
- exit(-1);
- }
- SetFormat(fmtblock.wavFormat.wBitsPerSample, fmtblock.wavFormat.dwSamplesPerSec);
- SetChannel(fmtblock.wavFormat.wChannels);
- buf = (char *)malloc(1024);
- if(buf == NULL)
- exit(-1);
- while(!feof(fp))
- {
- //printf("%d /n",i);
- //dwSize = Record(buf, 1024);
- dwSize=fread(buf,1024,1,fp);
- usleep(1);
- dwSize = Play(buf, 1024);
- }
- fclose(fp);
- }
////////sndtools.h
- #ifndef SNDTOOLS_H
- #define SNDTOOLS_H
- #include <linux/soundcard.h>
- #define FMT8BITS AFMT_S8_LE
- #define FMT16BITS AFMT_S16_LE
- #define FMT8K 8000
- #define FMT16K 16000
- #define FMT22K 22000
- #define FMT44K 44000
- #define MONO 1
- #define STERO 2
- #ifndef VAR_STATIC
- extern int devfd;
- extern int CapMask;
- #endif //ifndef VAR_STATIC
- //Open sound device, return 1 if open success
- //else return 0
- int OpenSnd();
- //Close sound device
- int CloseSnd();
- //Set record or playback format, return 1 if success
- //else return 0
- int SetFormat(int bits, int hz);
- //Set record or playback channel, return 1 if success
- //else return 1
- int SetChannel(int chn);
- //Record
- int Record(char *buf, int size);
- //Playback
- int Play(char *buf, int size);
- #endif //ifndef SNDTOOLS_H
///////sndtools.c
- #include <stdio.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <string.h>
- #define VAR_STATIC
- #include "sndtools.h"
- int devfd = 0;
- /*
- * Open Sound device
- * Return 1 if success, else return 0.
- */
- int OpenSnd(/* add by new version */int nWhich)
- {
- if(devfd > 0)
- close(devfd);
- devfd = open("/dev/sound/dsp", O_RDWR);
- if(devfd < 0)
- return 0;
- return 1;
- }
- /*
- * Close Sound device
- * return 1 if success, else return 0.
- */
- int CloseSnd(/* add by new version */int nWhich)
- {
- close(devfd);
- devfd = 0;
- return 1;
- }
- /*
- * Set Record an Playback format
- * return 1 if success, else return 0.
- * bits -- FMT8BITS(8bits), FMT16BITS(16bits)
- * hz -- FMT8K(8000HZ), FMT16K(16000HZ), FMT22K(22000HZ), FMT44K(44000HZ)
- */
- int SetFormat(int bits, int hz)
- {
- int tmp = bits;
- if( -1 == ioctl(devfd, SNDCTL_DSP_SETFMT, &tmp))
- {
- #ifdef DEBUG_WARN
- printf("Set fmt to s16_little faile:%d//n", nWhich);
- #endif
- return 0;
- }
- tmp = hz;
- if( -1 == ioctl(devfd, SNDCTL_DSP_SPEED, &tmp))
- {
- #ifdef DEBUG_WARN
- printf("Set speed to %d:%d//n", hz, nWhich);
- #endif
- return 0;
- }
- return 1;
- }
- /*
- * Set Sound Card Channel
- * return 1 if success, else return 0.
- * chn -- MONO, STERO
- */
- int SetChannel(int chn)
- {
- int tmp = chn;
- if(-1 == ioctl(devfd, SNDCTL_DSP_CHANNELS, &tmp))
- {
- #ifdef DEBUG_WARN
- printf("Set Audio Channel faile:%d//n", nWhich);
- #endif
- return 0;
- }
- return 1;
- }
- /*
- * Record
- * return numbers of byte for read.
- */
- int Record(char *buf, int size)
- {
- return read(devfd, buf, size);
- }
- /*
- * Playback
- * return numbers of byte for write.
- */
- int Play(char *buf, int size)
- {
- return write(devfd, buf, size);
- }
////////////////////////////////////////record.c///////////////////////
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include "sndtools.h"
- #define WAVOUTDEV FMT8K
- typedef unsigned int DWORD;
- typedef unsigned short int WORD;
- struct RIFF_HEADER
- {
- char szRiffID[4]; // 'R','I','F','F'
- DWORD dwRiffSize;
- char szRiffFormat[4]; // 'W','A','V','E'
- };
- struct WAVE_FORMAT
- {
- WORD wFormatTag;
- WORD wChannels;
- DWORD dwSamplesPerSec;
- DWORD dwAvgBytesPerSec;
- WORD wBlockAlign;
- WORD wBitsPerSample;
- };
- struct FMT_BLOCK
- {
- char szFmtID[4]; // 'f','m','t',' '
- DWORD dwFmtSize;
- struct WAVE_FORMAT wavFormat;
- };
- struct FACT_BLOCK
- {
- char szFactID[4]; // 'f','a','c','t'
- DWORD dwFactSize;
- };
- struct DATA_BLOCK
- {
- char szDataID[4]; // 'd','a','t','a'
- DWORD dwDataSize;
- };
- int main()
- {
- char *buf;
- int dwSize;
- int i;
- struct RIFF_HEADER riffheader;
- struct FMT_BLOCK fmtblock;
- struct DATA_BLOCK datablock;
- FILE * fp;
- printf("WORD %d /n",sizeof(WORD));
- printf("DWORD %d/n",sizeof(DWORD));
- riffheader.szRiffID[0]='R';
- riffheader.szRiffID[1]='I';
- riffheader.szRiffID[2]='F';
- riffheader.szRiffID[3]='F';
- riffheader.dwRiffSize=1024*50+8+16+8+4;
- riffheader.szRiffFormat[0]='W';
- riffheader.szRiffFormat[1]='A';
- riffheader.szRiffFormat[2]='V';
- riffheader.szRiffFormat[3]='E';
- fmtblock.szFmtID[0]='f';
- fmtblock.szFmtID[1]='m';
- fmtblock.szFmtID[2]='t';
- fmtblock.szFmtID[3]=' ';
- fmtblock.dwFmtSize=16;
- fmtblock.wavFormat.wFormatTag=0x0001;
- fmtblock.wavFormat.wChannels=1;
- fmtblock.wavFormat.dwSamplesPerSec=8000;
- fmtblock.wavFormat.dwAvgBytesPerSec= 8000*2;////////////////////
- fmtblock.wavFormat.wBlockAlign=2;
- fmtblock.wavFormat.wBitsPerSample=16;
- datablock.szDataID[0]='d';
- datablock.szDataID[1]='a';
- datablock.szDataID[2]='t';
- datablock.szDataID[3]='a';
- datablock.dwDataSize=1024*50;
- if((fp=fopen("test.wav","wb")) == NULL)
- {
- printf("Cannot open test.wav");
- return;
- }
- fwrite(&riffheader,sizeof(riffheader),1,fp);
- fwrite(&fmtblock,sizeof(fmtblock),1,fp);
- fwrite(&datablock,sizeof(datablock),1,fp);
- if(!OpenSnd())
- {
- printf("Open sound device error!//n");
- exit(-1);
- }
- SetFormat(FMT16BITS, FMT8K);
- SetChannel(MONO);
- buf = (char *)malloc(1024);
- if(buf == NULL)
- exit(-1);
- for(i = 0; i <50; i++)
- {
- printf("%d /n",i);
- dwSize = Record(buf, 1024);
- fwrite(buf,dwSize,1,fp);
- //dwSize = Play(buf, dwSize);
- }
- fclose(fp);
- exit(1);
- }
- ARM2410 OSS播放WAV文件与音频采集
- 使用OSS播放WAV音频
- 播放WAV音频
- Java-播放WAV音频
- windows 下wav采集音频
- iOS 实时音频采集与播放
- Wav 格式音频播放实例
- SDL播放wav音频分析
- 使用openal播放WAV音频
- java 播放wav格式音频
- 使用openal播放WAV音频
- linux音频采集技术 ALSA OSS
- 播放wav文件
- 播放WAV文件
- 播放WAV文件
- python 播放wav文件
- 播放 wav 文件
- gstreamer播放wav文件
- 求马氏距离(matlab版)
- C#多线程学习(五) 多线程的自动管理(定时器)
- 关于做Android+JavaEE系统集成开发的一点心得
- hibernate中的悲观锁和乐观锁
- C#多线程学习(六) 互斥对象
- ARM2410 OSS播放WAV文件与音频采集
- 进程之间的数据传递
- C#多线程学习(六) 互斥对象
- WinForm实现窗体上控件的自由拖动
- Linux配置文件系统路径
- mysql使用笔记
- gdb,strace那些不常用的功能
- 创业板指创调整新低 46家公司跌幅超50%
- 人生海海,皆是自己的选择