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

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include "sndtools.h"  
  5. #define WAVOUTDEV FMT8K  
  6. typedef unsigned int DWORD;  
  7. typedef unsigned short int WORD;   
  8. struct RIFF_HEADER  
  9. {  
  10. char szRiffID[4]; // 'R','I','F','F'  
  11. DWORD dwRiffSize;  
  12. char szRiffFormat[4]; // 'W','A','V','E'  
  13. };  
  14. struct WAVE_FORMAT  
  15. {  
  16. WORD wFormatTag;  
  17. WORD wChannels;  
  18. DWORD dwSamplesPerSec;  
  19. DWORD dwAvgBytesPerSec;  
  20. WORD wBlockAlign;  
  21. WORD wBitsPerSample;  
  22. };  
  23. struct FMT_BLOCK  
  24. {  
  25. char szFmtID[4]; // 'f','m','t',' '  
  26. DWORD dwFmtSize;  
  27. struct WAVE_FORMAT wavFormat;  
  28. };  
  29. struct FACT_BLOCK  
  30. {  
  31. char szFactID[4]; // 'f','a','c','t'  
  32. DWORD dwFactSize;  
  33. };  
  34. struct DATA_BLOCK  
  35. {  
  36. char szDataID[4]; // 'd','a','t','a'  
  37. DWORD dwDataSize;  
  38. };  
  39. int main()  
  40. {  
  41.  
  42. char *buf;  
  43. int dwSize;  
  44. int i;  
  45. struct RIFF_HEADER riffheader;  
  46. struct FMT_BLOCK fmtblock;  
  47. struct DATA_BLOCK datablock;   
  48. FILE * fp;  
  49. printf("WORD %d /n",sizeof(WORD));  
  50. printf("DWORD %d/n",sizeof(DWORD));  
  51.  
  52. if((fp=fopen("test.wav","rw")) == NULL)  
  53. {  
  54.    printf("Cannot open test.wav");  
  55.    return;  
  56. }  
  57.  
  58. fread(&riffheader,sizeof(riffheader),1,fp);  
  59. fread(&fmtblock,sizeof(fmtblock),1,fp);  
  60. fread(&datablock,sizeof(datablock),1,fp);  
  61.  
  62.  
  63. printf("riff size:%d /n",riffheader.dwRiffSize);  
  64. printf("riff format:%s/n",riffheader.szRiffFormat);  
  65.  
  66.  
  67. printf("fmtID: %s /n",fmtblock.szFmtID);  
  68.  
  69. printf("fmtSize %d /n",fmtblock.dwFmtSize);  
  70. printf("fmtFormat %d/n",fmtblock.wavFormat.wFormatTag);  
  71. printf("fmtChannels %d/n",fmtblock.wavFormat.wChannels);  
  72. printf("SamplePerSec %d/n",fmtblock.wavFormat.dwSamplesPerSec);  
  73. printf("AvgBytesPerSec %d/n",fmtblock.wavFormat.dwAvgBytesPerSec);////////////////////  
  74.    printf("BlockAlign %d /n", fmtblock.wavFormat.wBlockAlign);//=2;  
  75. printf("BitsPerSample %d/n",fmtblock.wavFormat.wBitsPerSample);//=16;  
  76. /*  
  77. datablock.szDataID[0]='d';  
  78. datablock.szDataID[1]='a';  
  79. datablock.szDataID[2]='t';  
  80. datablock.szDataID[3]='a';  
  81. */ 
  82. printf("DataSize %d /n",datablock.dwDataSize);  
  83.  
  84. if(!OpenSnd())  
  85.     {  
  86. printf("Open sound device error!//n");  
  87. exit(-1);  
  88. }  
  89. SetFormat(fmtblock.wavFormat.wBitsPerSample, fmtblock.wavFormat.dwSamplesPerSec);  
  90. SetChannel(fmtblock.wavFormat.wChannels);  
  91. buf = (char *)malloc(1024);  
  92. if(buf == NULL)  
  93. exit(-1);  
  94. while(!feof(fp))  
  95. {  
  96. //printf("%d /n",i);  
  97. //dwSize = Record(buf, 1024);  
  98. dwSize=fread(buf,1024,1,fp);  
  99. usleep(1);  
  100. dwSize = Play(buf, 1024);  
  101. }  
  102. fclose(fp);  

////////sndtools.h

  1. #ifndef SNDTOOLS_H  
  2. #define SNDTOOLS_H  
  3. #include <linux/soundcard.h>  
  4. #define FMT8BITS AFMT_S8_LE  
  5. #define FMT16BITS AFMT_S16_LE  
  6. #define FMT8K 8000  
  7. #define FMT16K 16000  
  8. #define FMT22K 22000  
  9. #define FMT44K 44000  
  10. #define MONO 1  
  11. #define STERO 2  
  12. #ifndef VAR_STATIC  
  13. extern int devfd;  
  14. extern int CapMask;  
  15. #endif //ifndef VAR_STATIC  
  16. //Open sound device, return 1 if open success  
  17. //else return 0  
  18. int OpenSnd();  
  19. //Close sound device  
  20. int CloseSnd();  
  21. //Set record or playback format, return 1 if success  
  22. //else return 0  
  23. int SetFormat(int bits, int hz);  
  24. //Set record or playback channel, return 1 if success  
  25. //else return 1  
  26. int SetChannel(int chn);  
  27. //Record  
  28. int Record(char *buf, int size);  
  29. //Playback  
  30. int Play(char *buf, int size);  
  31. #endif //ifndef SNDTOOLS_H 

///////sndtools.c

  1. #include <stdio.h>  
  2. #include <fcntl.h>  
  3. #include <unistd.h>  
  4. #include <sys/ioctl.h>  
  5. #include <string.h>  
  6. #define VAR_STATIC  
  7. #include "sndtools.h"  
  8. int devfd = 0;  
  9. /*  
  10. * Open Sound device  
  11. * Return 1 if success, else return 0.  
  12. */ 
  13. int OpenSnd(/* add by new version */int nWhich)  
  14. {  
  15. if(devfd > 0)  
  16. close(devfd);  
  17. devfd = open("/dev/sound/dsp", O_RDWR);  
  18. if(devfd < 0)  
  19. return 0;  
  20. return 1;  
  21. }  
  22. /*  
  23. * Close Sound device  
  24. * return 1 if success, else return 0.  
  25. */ 
  26. int CloseSnd(/* add by new version */int nWhich)  
  27. {  
  28. close(devfd);  
  29. devfd = 0;  
  30. return 1;  
  31. }  
  32. /*  
  33. * Set Record an Playback format  
  34. * return 1 if success, else return 0.  
  35. * bits -- FMT8BITS(8bits), FMT16BITS(16bits)  
  36. * hz -- FMT8K(8000HZ), FMT16K(16000HZ), FMT22K(22000HZ), FMT44K(44000HZ)  
  37. */ 
  38. int SetFormat(int bits, int hz)  
  39. {  
  40. int tmp = bits;  
  41. if( -1 == ioctl(devfd, SNDCTL_DSP_SETFMT, &tmp))  
  42. {  
  43. #ifdef DEBUG_WARN  
  44. printf("Set fmt to s16_little faile:%d//n", nWhich);  
  45. #endif  
  46. return 0;  
  47. }  
  48. tmp = hz;  
  49. if( -1 == ioctl(devfd, SNDCTL_DSP_SPEED, &tmp))  
  50. {  
  51. #ifdef DEBUG_WARN  
  52. printf("Set speed to %d:%d//n", hz, nWhich);  
  53. #endif  
  54. return 0;  
  55. }  
  56. return 1;  
  57. }  
  58. /*  
  59. * Set Sound Card Channel  
  60. * return 1 if success, else return 0.  
  61. * chn -- MONO, STERO  
  62. */ 
  63. int SetChannel(int chn)  
  64. {  
  65. int tmp = chn;  
  66. if(-1 == ioctl(devfd, SNDCTL_DSP_CHANNELS, &tmp))  
  67. {  
  68. #ifdef DEBUG_WARN  
  69. printf("Set Audio Channel faile:%d//n", nWhich);  
  70. #endif  
  71. return 0;  
  72. }  
  73. return 1;  
  74. }  
  75. /*  
  76. * Record  
  77. * return numbers of byte for read.  
  78. */ 
  79. int Record(char *buf, int size)  
  80. {  
  81. return read(devfd, buf, size);  
  82. }  
  83. /*  
  84. * Playback  
  85. * return numbers of byte for write.  
  86. */ 
  87. int Play(char *buf, int size)  
  88. {  
  89. return write(devfd, buf, size);  

////////////////////////////////////////record.c///////////////////////

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <unistd.h>  
  4. #include "sndtools.h"  
  5. #define WAVOUTDEV FMT8K  
  6. typedef unsigned int DWORD;  
  7. typedef unsigned short int WORD;   
  8. struct RIFF_HEADER  
  9. {  
  10. char szRiffID[4]; // 'R','I','F','F'  
  11. DWORD dwRiffSize;  
  12. char szRiffFormat[4]; // 'W','A','V','E'  
  13. };  
  14. struct WAVE_FORMAT  
  15. {  
  16. WORD wFormatTag;  
  17. WORD wChannels;  
  18. DWORD dwSamplesPerSec;  
  19. DWORD dwAvgBytesPerSec;  
  20. WORD wBlockAlign;  
  21. WORD wBitsPerSample;  
  22. };  
  23. struct FMT_BLOCK  
  24. {  
  25. char szFmtID[4]; // 'f','m','t',' '  
  26. DWORD dwFmtSize;  
  27. struct WAVE_FORMAT wavFormat;  
  28. };  
  29. struct FACT_BLOCK  
  30. {  
  31. char szFactID[4]; // 'f','a','c','t'  
  32. DWORD dwFactSize;  
  33. };  
  34. struct DATA_BLOCK  
  35. {  
  36. char szDataID[4]; // 'd','a','t','a'  
  37. DWORD dwDataSize;  
  38. };  
  39. int main()  
  40. {  
  41. char *buf;  
  42. int dwSize;  
  43. int i;  
  44. struct RIFF_HEADER riffheader;  
  45. struct FMT_BLOCK fmtblock;  
  46. struct DATA_BLOCK datablock;   
  47. FILE * fp;  
  48. printf("WORD %d /n",sizeof(WORD));  
  49. printf("DWORD %d/n",sizeof(DWORD));  
  50. riffheader.szRiffID[0]='R';  
  51. riffheader.szRiffID[1]='I';  
  52. riffheader.szRiffID[2]='F';  
  53. riffheader.szRiffID[3]='F';  
  54. riffheader.dwRiffSize=1024*50+8+16+8+4;  
  55. riffheader.szRiffFormat[0]='W';  
  56. riffheader.szRiffFormat[1]='A';  
  57. riffheader.szRiffFormat[2]='V';  
  58. riffheader.szRiffFormat[3]='E';  
  59.  
  60. fmtblock.szFmtID[0]='f';  
  61. fmtblock.szFmtID[1]='m';  
  62. fmtblock.szFmtID[2]='t';  
  63. fmtblock.szFmtID[3]=' ';  
  64. fmtblock.dwFmtSize=16;  
  65. fmtblock.wavFormat.wFormatTag=0x0001;  
  66. fmtblock.wavFormat.wChannels=1;  
  67. fmtblock.wavFormat.dwSamplesPerSec=8000;  
  68. fmtblock.wavFormat.dwAvgBytesPerSec= 8000*2;////////////////////  
  69. fmtblock.wavFormat.wBlockAlign=2;  
  70. fmtblock.wavFormat.wBitsPerSample=16;  
  71.  
  72. datablock.szDataID[0]='d';  
  73. datablock.szDataID[1]='a';  
  74. datablock.szDataID[2]='t';  
  75. datablock.szDataID[3]='a';  
  76.  
  77. datablock.dwDataSize=1024*50;  
  78.  
  79.  
  80. if((fp=fopen("test.wav","wb")) == NULL)  
  81. {  
  82.    printf("Cannot open test.wav");  
  83.    return;  
  84. }  
  85. fwrite(&riffheader,sizeof(riffheader),1,fp);  
  86. fwrite(&fmtblock,sizeof(fmtblock),1,fp);  
  87. fwrite(&datablock,sizeof(datablock),1,fp);  
  88.     
  89.  
  90. if(!OpenSnd())  
  91.     {  
  92. printf("Open sound device error!//n");  
  93. exit(-1);  
  94. }  
  95. SetFormat(FMT16BITS, FMT8K);  
  96. SetChannel(MONO);  
  97. buf = (char *)malloc(1024);  
  98. if(buf == NULL)  
  99. exit(-1);  
  100. for(i = 0; i <50; i++)  
  101. {  
  102. printf("%d /n",i);  
  103. dwSize = Record(buf, 1024);  
  104. fwrite(buf,dwSize,1,fp);  
  105. //dwSize = Play(buf, dwSize);  
  106. }  
  107. fclose(fp);  
  108. exit(1);