天嵌 ARM开发社区

 找回密码
 注册
查看: 3258|回复: 1

ubuntu alsa 录音程序

[复制链接]
zhangxianaa 发表于 2015-3-13 16:19:42 | 显示全部楼层 |阅读模式
  在板子上接好VGA以及鼠标键盘,在桌面上打开终端去运行录音程序可以正常运行,但是通过调试串口去运行就会出错,由于该程序要做到开机运行,于是我尝试过添加到/root/.profile中运行以及/home/linaro/.profile/(这两个文件中都添加过,都运行不正确,设置录音采样率为16KHZ,会报一个PLL -22的错误)
  在桌面上打开的终端为/dev/pts/*,调试串口为/dev/ttySAC0,以及通过ctrl+alt+f1进入的终端为/dev/tty1
  以上三个执行同样的录音程序只有桌面打开的虚拟终端才能正常运行,而且以上三个终端都切换过root用户运行,结果都一样,估计与用户无关,感觉可能是与终端有关。
  程序说明:采样率 16KHZ channels 单声道,打开的设备名:defualt  
  现在的问题:程序需要开机自动运行录音程序,而我尝试过添加都失败了,只能程序起来后用鼠标点开在运行程序。(无语~~~)
  哪位大哥帮给给建议,如果需要贴代码也行。谢谢。。
 楼主| zhangxianaa 发表于 2015-3-13 16:38:16 | 显示全部楼层
自己顶~~ 录音源码
//#define ALSA_PCM_NEW_HW_PARAMS_API

#include <alsa/asoundlib.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
long loops;
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
char *buffer;

/* Open PCM device for recording (capture). */
rc = snd_pcm_open(&handle, "default",
                    SND_PCM_STREAM_CAPTURE, 0);
if (rc < 0) {
    fprintf(stderr,
            "unable to open pcm device: %s\n",
            snd_strerror(rc));
    exit(1);
}
int fd=open("./b.wav",O_CREAT|O_RDWR|O_TRUNC);
if(fd==-1)
{
        printf("open abc.wav error\n");
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(&params);

/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);

/* Set the desired hardware parameters. */

/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params,
                      SND_PCM_ACCESS_RW_INTERLEAVED);

/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params,
                              SND_PCM_FORMAT_S16_LE);

/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 1);

/* 44100 bits/second sampling rate (CD quality) */
val = 16000;
snd_pcm_hw_params_set_rate_near(handle, params,
                                  &val, &dir);
//snd_pcm_hw_params_set_rate(handle, params,
//                                 &val);
/* Set period size to 32 frames. */
frames = 320;
snd_pcm_hw_params_set_period_size_near(handle,
                           params, &frames, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
    fprintf(stderr,
            "unable to set hw parameters: %s\n",
            snd_strerror(rc));
    exit(1);
}
snd_pcm_hw_params_get_channels(params, &val);  
printf("channels = %d\n", val);  
snd_pcm_hw_params_get_rate(params, &val, &dir);  
printf("rate = %d bps\n", val);
snd_pcm_hw_params_get_access(params,
                      &val);  
printf("sample = %x bit\n", val);
snd_pcm_hw_params_get_period_size(params,
                                     &frames, &dir);
size = frames * 2; /* 2 bytes/sample, 2 channels */
buffer = (char *) malloc(size);
printf("size=%d\n",size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params,
                                         &val, &dir);
printf("val=%u\n",val);
loops = 3000000 / val; //录音三秒
/* Use a buffer large enough to hold one period */
while (loops > 0) {
    loops--;
    rc = snd_pcm_readi(handle, buffer, frames);
    if (rc == -EPIPE) {
      /* EPIPE means overrun */
      fprintf(stderr, "overrun occurred\n");
      snd_pcm_prepare(handle);
    } else if (rc < 0) {
      fprintf(stderr,
              "error from read: %s\n",
              snd_strerror(rc));
    } else if (rc != (int)frames) {
      fprintf(stderr, "short read, read %d frames\n", rc);
    }
    rc = write(fd,buffer,size);
    if (rc != size)
      fprintf(stderr,
             "short write: wrote %d bytes\n", rc);
}

snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
close(fd);
return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

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

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

GMT+8, 2024-5-3 07:48 , Processed in 1.078125 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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