天嵌 ARM开发社区

 找回密码
 注册
查看: 3060|回复: 2

OSS音频接口写的录音播放小程序,在2440 上测试成功

[复制链接]
hjj742975525 发表于 2013-9-14 10:55:42 | 显示全部楼层 |阅读模式
前两天用OSS音频接口写的一个录音和播放小程序,播放的时候,声音比较小,但是如果大声一点录,放出来还能稍微大点,但还是比较小,什么原因?贴上代码:录音的:(按下板子上的key1开始录音,松开按键录音结束,录音存放在record.wav里面)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <linux/soundcard.h>

  6. #define AUDIO_DEVICE "/dev/dsp"
  7. #define RATE          48000
  8. #define CHANNELS 2
  9. #define SIZE         16
  10. #define PAGE_SIZE 4096

  11. #define RECORD_DIR "/tmp/"

  12. #define KEY_DEVICE "/dev/IRQ-Test"
  13. #define KEY_N   4
  14. #define KEY1        0

  15. int main (int argc, char *argv[])
  16. {
  17.         int keyfd, dspfd;
  18.         char key_buf[KEY_N], record_buf[PAGE_SIZE];
  19.         int arg;

  20.         /*open dsp read only and init*/
  21.         dspfd = open (AUDIO_DEVICE, O_RDONLY);
  22.         if (-1 == dspfd) {
  23.                 perror ("##open dsp erro");
  24.                 exit (1);
  25.         }
  26.         /*set format*/
  27.         arg = SIZE;
  28.         if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_BITS, &arg)) {
  29.                 perror ("##set simple format erro");
  30.                 goto out3;
  31.         }
  32.         /*set channels*/
  33.         arg = CHANNELS;
  34.         if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_CHANNELS, &arg)) {
  35.                 perror ("##set channels erro");
  36.                 goto out3;
  37.         }
  38.         /*set speed*/
  39.         arg = RATE;
  40.         if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_RATE, &arg)) {
  41.                 perror ("##set speed erro");
  42.                 goto out3;
  43.         }

  44.         /*create a file to store the record file*/
  45.         FILE *filep = fopen (RECORD_DIR"record.wav", "w");
  46.         if (NULL == filep) {
  47.                 printf ("##open file to record erro\n");
  48.                 goto out3;
  49.         }

  50.         /*open the key ,read only and nonblock*/
  51.         keyfd = open (KEY_DEVICE, O_RDONLY | O_NONBLOCK);
  52.         if (-1 == keyfd) {
  53.                 perror ("##open key erro");
  54.                 goto out2;
  55.         }

  56.         printf ("<press down the KEY1 to record> \n");
  57.         int record_end_flag = 1;
  58.         while (record_end_flag) {
  59.                 if ((-1 == read (keyfd, key_buf, KEY_N)) && (EAGAIN != errno)) {
  60.                         perror ("##read key vaule erro");
  61.                         goto out1;
  62.                 }

  63.                 /*KEY1 down*/
  64.                 while (key_buf[KEY1] != '0') {
  65.                         if ((-1 == read (keyfd, key_buf, KEY_N)) && (EAGAIN != errno)) {
  66.                                 perror ("##read key vaule erro");
  67.                                 goto out1;
  68.                         }       
  69.                         /*key down*/
  70.                         int num = read (dspfd, record_buf, PAGE_SIZE);
  71.                         int write_n = fwrite (record_buf, sizeof (char), num, filep);
  72.                         if (write_n < num) {
  73.                                 printf ("##write to the record file erro\n");
  74.                                 goto out1;
  75.                         }

  76.                         /*key up, record end*/
  77.                         if (key_buf[KEY1] == '0') {
  78.                                 record_end_flag = 0;
  79.                                 printf ("<record end>\n");
  80.                         }
  81.                 }
  82.         }
  83. out1:
  84.         close (keyfd);
  85. out2:
  86.         fclose (filep);
  87. out3:
  88.         close (dspfd);

  89.         return 0;
  90. }
复制代码


播放的:(播放录音record.wav)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <linux/soundcard.h>

  6. #define AUDIO_DEVICE "/dev/dsp"
  7. #define RATE          48000
  8. #define CHANNELS 2
  9. #define SIZE         16
  10. #define PAGE_SIZE 4096
  11. #define VOLUME         100 //max

  12. int main (int argc, char *argv[])
  13. {
  14.         int dspfd;
  15.         char record_buf[PAGE_SIZE];
  16.         int arg;

  17.         if (argc < 2){
  18.                 printf ("Usage: ./playrec filename\n");
  19.                 exit (1);
  20.         }

  21.         /*open dsp write only and init*/
  22.         dspfd = open (AUDIO_DEVICE, O_WRONLY);
  23.         if (-1 == dspfd) {
  24.                 perror ("##open dsp erro");
  25.                 exit (1);
  26.         }
  27.         /*set format*/
  28.         arg = SIZE;
  29.         if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_BITS, &arg)) {
  30.                 perror ("##set simple format erro");
  31.                 goto out1;
  32.         }
  33.         /*set channels*/
  34.         arg = CHANNELS;
  35.         if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_CHANNELS, &arg)) {
  36.                 perror ("##set channels erro");
  37.                 goto out1;
  38.         }
  39.         /*set speed*/
  40.         arg = RATE;
  41.         if (-1 == ioctl (dspfd, SOUND_PCM_WRITE_RATE, &arg)) {
  42.                 perror ("##set speed erro");
  43.                 goto out1;
  44.         }
  45.         /*set the volume*/
  46.         arg = (VOLUME << 8) | VOLUME;
  47.         if (-1 == ioctl (dspfd, SOUND_MIXER_WRITE_VOLUME, &arg)) {
  48.                 perror ("##set volume erro");
  49.                 goto out1;
  50.         }
  51.         /*open the record file*/
  52.         FILE *filep = fopen (argv[1], "r");
  53.         if (NULL == filep) {
  54.                 printf ("##open the record file erro\n");
  55.                 goto out1;
  56.         }

  57.         /*play the record*/
  58.         int num;
  59.         while ((num = fread (record_buf, sizeof (char), PAGE_SIZE, filep)) > 0) {
  60.                 if (-1 == write (dspfd, record_buf, num)) {
  61.                         perror ("##write dsp erro");
  62.                         goto out2;
  63.                 }
  64.                
  65.         }
  66. out2:
  67.         fclose (filep);
  68. out1:
  69.         close (dspfd);

  70.         return 0;
  71. }
复制代码



亚瑟王 发表于 2013-9-16 10:00:48 | 显示全部楼层

回帖奖励 +1

这个和声卡的通道有关系,我们引出来的通道是line-in通道,应该用mic通道。硬件上需要修改一下,同时软件驱动也需要对应的修改,声音就大了。
(^_^)∠※※ 发表于 2013-12-12 13:29:03 | 显示全部楼层
怎么修改声音会变大啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

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

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

GMT+8, 2024-5-10 06:06 , Processed in 1.031250 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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