天嵌 ARM开发社区

 找回密码
 注册
查看: 2510|回复: 6

TQ2440 UDA1341 錄音及播放問題附代碼

[复制链接]
haa14618 发表于 2012-2-15 00:02:05 | 显示全部楼层 |阅读模式
本帖最后由 haa14618 于 2012-2-15 00:04 编辑
  1. //錄音
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h>
  5. #include <sys/ioctl.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <linux/soundcard.h>
  9. #include <termios.h>


  10. #define LENGTH        30        
  11. #define RATE        44100     
  12. #define SIZE        16      
  13. #define CHANNELS 2      
  14. #define RSIZE        100
  15. #define AUDIO_DEV_FILE "/dev/dsp"

  16. unsigned char buf[RSIZE];


  17. struct fhead{
  18.         unsigned char a[4];
  19.         long int b;
  20.         unsigned char c[4];
  21.         unsigned char d[4];
  22.         long int e;
  23.         short int f;
  24.         short int g;
  25.         long int h;
  26.         long int i;
  27.         short int j;
  28.         short int k;
  29.         unsigned char p[4];
  30.         long int q;         
  31. } wavehead;            


  32. int set_wav_arg(void)
  33. {
  34.         wavehead.a[0] = 'R';
  35.         wavehead.a[1] = 'I';
  36.         wavehead.a[2] = 'F';
  37.         wavehead.a[3] = 'F';
  38. //        wavehead.b = LENGTH * RATE * CHANNELS * SIZE / 8 - 8;
  39.         wavehead.c[0] = 'W';
  40.         wavehead.c[1] = 'A';
  41.         wavehead.c[2] = 'V';
  42.         wavehead.c[3] = 'E';
  43.         wavehead.d[0] = 'f';
  44.         wavehead.d[1] = 'm';
  45.         wavehead.d[2] = 't';
  46.         wavehead.d[3] = ' ';
  47.         wavehead.e = 16;
  48.         wavehead.f = 2;
  49.         wavehead.g = CHANNELS;
  50.         wavehead.h = RATE;
  51.         wavehead.i = RATE * CHANNELS * SIZE / 8;
  52.         wavehead.j = CHANNELS * SIZE / 8;
  53.         wavehead.k = SIZE;
  54.         wavehead.p[0] = 'd';
  55.         wavehead.p[1] = 'a';
  56.         wavehead.p[2] = 't';
  57.         wavehead.p[3] = 'a';
  58. //        wavehead.q = LENGTH * RATE * CHANNELS * SIZE / 8;
  59. }


  60. int recodred(void)
  61. {
  62.         int status;
  63.         int fd_f;
  64.         int fd_dev_r;

  65.         int arg;
  66.         fd_dev_r = open(AUDIO_DEV_FILE, O_RDONLY, 0777);
  67.         if (fd_dev_r < 0)
  68.         {
  69.                 perror("Cannot open  device");
  70.                 return 1;
  71.         }
  72. /*
  73.         arg = 16;
  74.         ioctl(fd_dev_r,SOUND_PCM_WRITE_BITS, &arg);        
  75.         arg = 2;
  76.         ioctl(fd_dev_r,SOUND_PCM_WRITE_CHANNELS, &arg);
  77.         arg = 44100;
  78.         ioctl(fd_dev_r,SOUND_PCM_WRITE_RATE, &arg);
  79. */
  80. /*
  81.         arg = 16;
  82.         ioctl(fd_dev_r,SNDCTL_DSP_SETFMT, &arg);
  83.         arg = 2;
  84.         ioctl(fd_dev_r,SNDCTL_DSP_CHANNELS, &arg);
  85.         arg = 44100;
  86.         ioctl(fd_dev_r,SNDCTL_DSP_SPEED, &arg);
  87. */

  88.         system("rm -f *.wav");   
  89.         if((fd_f = open("./sound.wav", O_CREAT | O_RDWR, 0777)) == -1)   
  90.         {
  91.                 perror("cannot creat the sound file");
  92.         }
  93.         if((status = write(fd_f, &wavehead, sizeof(wavehead))) == -1)   
  94.         {
  95.                  perror("write to sound'head wrong!!");
  96.         }

  97.         while(1)
  98.         {
  99.                 status = read(fd_dev_r, buf, sizeof(buf));
  100.                 if (status != sizeof(buf))
  101.                 {
  102.                         perror("read wrong number of bytes");
  103.                 }
  104.                
  105.                 if (write(fd_f, buf, status) == -1)
  106.                 {
  107.                         perror("write to sound wrong!!");
  108.                 }
  109.         }

  110.         close(fd_dev_r);           
  111.         close(fd_f);               
  112. }

  113.   
  114. int main(void)
  115. {
  116.         int fd_dev_r;
  117.         set_wav_arg();
  118.         recodred();
  119. }

复制代码


  1. //播放
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h>
  5. #include <sys/ioctl.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <linux/soundcard.h>
  9. #include <termios.h>
  10.         
  11. #define RSIZE    100     
  12. #define AUDIO_DEV_FILE "/dev/dsp"

  13. unsigned char buf[RSIZE];      

  14. int read_wav(void)
  15. {
  16.         int status;
  17.         int fd_f;
  18.         int fd_dev_w;

  19.         fd_dev_w = open(AUDIO_DEV_FILE, O_WRONLY, 0777);
  20.         if (fd_dev_w < 0)
  21.         {
  22.                 perror("Cannot open  device");
  23.                 return 1;
  24.         }
  25.         
  26.         printf("Play...:\n");
  27.         
  28.         if ((fd_f = open("./sound.wav", O_RDONLY, 0777)) == -1)
  29.         {
  30.                 perror("cannot creat the sound file");
  31.         }
  32.    
  33.         lseek(fd_f, 44, SEEK_SET);
  34.    
  35.         while(1)
  36.         {
  37.                 status = read(fd_f, buf, sizeof(buf));   
  38.                 if(status == 0)
  39.                 break;
  40.                 if(status != sizeof(buf))
  41.                 {
  42.                         perror("write wrong number of bytes");
  43.                 }
  44.                 status = write(fd_dev_w, buf, sizeof(buf));
  45.                 if (status != sizeof(buf))
  46.                 {
  47.                         perror("wrote2 wrong number of bytes");
  48.                 }
  49.         }

  50.         close(fd_f);
  51.         close(fd_dev_w);
  52.         return 0;
  53. }

  54. int main(void)
  55. {
  56.         read_wav();
  57.         return 0;
  58. }
复制代码
[/code]

問題1:
試過了幾種方法,設定採樣、量化及聲道發現
SOUND_PCM_WRITE_及SNDCTL_DSP_兩種方式
錄出來的檔案,都無法播放
是不是我那設定錯誤???

問題2:
使用fhead所儲存的檔案是可以播放的
但是問題又來了,
播放的時候會有延遲,要等一段時間才有聲音
後來我用lseek();函數讀取後面的字節
是可以正常的播放
但是換一個錄音檔就會有問題,
不知道是錄音時還是播放的關係???

問題3:
我把在板子上可以播放的檔案
抓到WINDOWS 7 底下播放
會出現格式錯誤的訊息
是還有哪裡設定不對嗎???

求助啊!!!:'(
TQ-lkp 发表于 2012-2-15 08:32:09 | 显示全部楼层
稍等,我发一个可用的代码上去
 楼主| haa14618 发表于 2012-2-15 10:30:44 | 显示全部楼层
好的~~感謝啊!!:):)
TQ-lkp 发表于 2012-2-20 11:03:33 | 显示全部楼层
Hens007 发表于 2013-2-23 12:05:29 | 显示全部楼层
版主给的这个链接无法打开
Real_me゛ 发表于 2013-10-31 20:55:05 | 显示全部楼层
TQ-lkp 发表于 2012-2-20 11:03
已经发布了,http://bbs.embedsky.net/forum.php?mod=viewthread&tid=10920

你好,这个连接打不开啊?
Real_me゛ 发表于 2013-11-2 20:34:17 | 显示全部楼层
TQ-lkp 发表于 2012-2-20 11:03
已经发布了,http://bbs.embedsky.net/forum.php?mod=viewthread&tid=10920

打不开啊,能不能从新发个,我最近在看,没有合适的代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-10-7 04:24 , Processed in 1.042032 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表