为了熟悉pjmedia的相关函数以及使用方法,这里练习了官网上的一个录音器的例子。

核心函数:

pj_status_t pjmedia_wav_writer_port_create ( pj_pool_t pool,
    const char *  filename,
    unsigned  clock_rate,
    unsigned  channel_count,
    unsigned  samples_per_frame,
    unsigned  bits_per_sample,
    unsigned  flags,
    pj_ssize_t  buff_size,
    pjmedia_port **  p_port 
  )    

Create a media port to record streams to a WAV file. Note that the port must be closed properly (with pjmedia_port_destroy()) so that the WAV header can be filled with correct values (such as the file length). WAV writer port supports for writing audio in uncompressed 16 bit PCM format or compressed G.711 U-law/A-law format, this needs to be specified in flags param.

Parameters
pool Pool to create memory buffers for this port.
filename File name.
clock_rate The sampling rate.
channel_count Number of channels.
samples_per_frame Number of samples per frame.
bits_per_sample Number of bits per sample (eg 16).
flags Port creation flags, see pjmedia_file_writer_option.
buff_size Buffer size to be allocated. If the value is zero or negative, the port will use default buffer size (which is about 4KB).
p_port Pointer to receive the file port instance.
Returns
PJ_SUCCESS on success.
pj_status_t pjmedia_snd_port_create_rec ( pj_pool_t pool,
    int  index,
    unsigned  clock_rate,
    unsigned  channel_count,
    unsigned  samples_per_frame,
    unsigned  bits_per_sample,
    unsigned  options,
    pjmedia_snd_port **  p_port 
  )    

Create unidirectional sound device port for capturing audio streams from the sound device with the specified parameters.

Parameters
pool Pool to allocate sound port structure.
index Device index, or -1 to let the library choose the first available device.
clock_rate Sound device's clock rate to set.
channel_count Set number of channels, 1 for mono, or 2 for stereo. The channel count determines the format of the frame.
samples_per_frame Number of samples per frame.
bits_per_sample Set the number of bits per sample. The normal value for this parameter is 16 bits per sample.
options Options flag.
p_port Pointer to receive the sound device port instance.
Returns
PJ_SUCCESS on success, or the appropriate error code.
pj_status_t pjmedia_endpt_create ( pj_pool_factory pf,
    pj_ioqueue_t ioqueue,
    unsigned  worker_cnt,
    pjmedia_endpt **  p_endpt 
  )    

Create an instance of media endpoint.

Parameters
pf Pool factory, which will be used by the media endpoint throughout its lifetime.
ioqueue Optional ioqueue instance to be registered to the endpoint. The ioqueue instance is used to poll all RTP and RTCP sockets. If this argument is NULL, the endpoint will create an internal ioqueue instance.
worker_cnt Specify the number of worker threads to be created to poll the ioqueue.
p_endpt Pointer to receive the endpoint instance.
Returns
PJ_SUCCESS on success.
pj_status_t pjmedia_snd_port_connect ( pjmedia_snd_port snd_port,
    pjmedia_port port 
  )    

Connect a port to the sound device port. If the sound device port has a sound recorder device, then this will start periodic function call to the port's put_frame() function. If the sound device has a sound player device, then this will start periodic function call to the port's get_frame() function.

For this version of PJMEDIA, the media port MUST have the same audio settings as the sound device port, or otherwise the connection will fail. This means the port MUST have the same clock_rate, channel count, samples per frame, and bits per sample as the sound device port.

Parameters
snd_port The sound device port.
port The media port to be connected.
Returns
PJ_SUCCESS on success, or the appropriate error code.
//capture sound to wav file
//heat nan -test
#include<pjmedia.h>
#include<pjlib.h>
#include<stdio.h>
#include<stdlib.h> #define CLOCK_RATE 44100 //采样率
#define NCHANNELS 2 //通道数
#define SAMPLES_PER_FRAME (NCHANNELS *(CLOCK_RATE*10/1000)) //每帧包含的采样数
#define BITS_PER_SAMPLE 16 //每个抽样的字节数 int main()
{
pj_caching_pool cp;
pjmedia_endpt *med_endpt;
pj_pool_t *pool;
pjmedia_port *file_port;
pjmedia_snd_port *snd_port;
pj_status_t status;
char tmp[10];
char filename[20]; status=pj_init();
if(status!=PJ_SUCCESS)
{
PJ_LOG(3,("failed","pj_init"));
} pj_caching_pool_init(&cp,&pj_pool_factory_default_policy,0);
//创建endpoint
status=pjmedia_endpt_create(&cp.factory,NULL,1,&med_endpt);
if(status!=PJ_SUCCESS)
{
PJ_LOG(3,("failed","pjmedia_endpt_create"));
} pool=pj_pool_create(&cp.factory,
"wav_recoder",4000,4000,NULL);
//输入要保存录音的文件名
PJ_LOG(3,("wav_recorder","please input the file name!"));
gets(filename);
//创建录音端口 把音频流写成wav文件
status=pjmedia_wav_writer_port_create(pool,filename,
CLOCK_RATE,
NCHANNELS,
SAMPLES_PER_FRAME,
BITS_PER_SAMPLE,
0,0,
&file_port //指向接收该实例的pjmedia_port
);
if(status!=PJ_SUCCESS)
{
PJ_LOG(3,("failed","pjmedia_wav_write_port_create"));
}
//创建单向的音频设备端口用来捕获音频
status=pjmedia_snd_port_create_rec(pool,-1,
PJMEDIA_PIA_SRATE(&file_port->info),
PJMEDIA_PIA_CCNT(&file_port->info),
PJMEDIA_PIA_SPF(&file_port->info),
PJMEDIA_PIA_BITS(&file_port->info),
0,&snd_port);
if(status!=PJ_SUCCESS)
{
PJ_LOG(3,("failed","pjmedia_snd_port_create_rec"));
}
//连接两个端口开始录音
status=pjmedia_snd_port_connect(snd_port,file_port);
if(status!=PJ_SUCCESS)
{
PJ_LOG(3,("failed","pjmedia_snd_port_connect"));
} pj_thread_sleep(10);
//显示终止信息
puts("PRESS <ENTER> to stop recording and quit"); if(fgets(tmp,sizeof(tmp),stdin)==NULL)
{
puts("EOF while reading stdin,will quit now..");
}
//养成良好习惯,吃完饭总要刷刷碗吧!
pjmedia_snd_port_destroy(snd_port);
pjmedia_port_destroy(file_port); pj_pool_release(pool);
pjmedia_endpt_destroy(med_endpt); pj_caching_pool_destroy(&cp); pj_shutdown();
return 0;
// pj_thread_sleep(10); }

PJMEDIA之录音器的使用(capture sound to avi file)的更多相关文章

  1. Android 录音器

    Android自带的mediarecoder录音器不含pause暂停功能,解决方法:录制多个音频片段,最后合成一个文件. 参照 : http://blog.csdn.net/a601445984/ar ...

  2. 用swift实现自动录音器

    基本介绍 自动录音与一般录音区别在:不用像微信那样按下录音-松手结束,而是根据说话声音的大小自动判断该录音和该停止的点,然后可以做到结束录音之后马上播放出来.类似于达到会说话的汤姆猫那样的效果. 在自 ...

  3. 录音器 AudioRecorder

    实现录音器有两种方式可以选择: 1.AudioRecord(基于字节流录音) 优点:可以实现语音的实时处理,进行边录边播,对音频的实时处理. 缺点:输出的是PCM的语音数据,如果保存成音频文件是不能被 ...

  4. H5调用手机的相机/摄像/录音等功能 _input:file的capture属性说明

    H5使用input标签调用系统默许相机,摄像,录音功能.使用input:file标签, 去调用系统默认相机,摄像,录音功能,其实是有个capture属性,直接说明需要调用什么功能: <input ...

  5. 经典alsa 录音和播放程序

    这里贴上虚拟机ubuntu下alsa的录音程序(capture.c)和播放程序(playback.c)的源码. 首先要测试一下自己的ubuntu是否打开了声音.这个可以打开/系统/首选项/声音  来调 ...

  6. android 音乐播放器

    本章以音乐播放器为载体,介绍android开发中,通知模式Notification应用.主要涉及知识点Notification,seekbar,service. 1.功能需求 完善音乐播放器 有播放列 ...

  7. DirectX.Capture Class Library

    DirectX.Capture Class Library Brian Low,              29 Mar 2008                                    ...

  8. Android MP3录音实现

    给APP做语音功能,必须考虑到IOS和Android平台的通用性.wav录音质量高,文件太大,AAC和AMR格式在IOS平台却不支持,所以采用libmp3lame把AudioRecord音频流直接转换 ...

  9. IOS 实现 AAC格式 录音 录音后自动播放

    废话不说了 不知道aac可以百度一下 下面直接上代码,一个h文件 一个m文件 搞定! #import <AVFoundation/AVFoundation.h> #import <U ...

随机推荐

  1. 基于FPGA的VGA显示设计(二)

    上一篇:基于FPGA的VGA显示设计(一)     参照 CrazyBingo 的 基于FPGA的VGA可移植模块终极设计代码  的工程代码风格,模块化处理了上一篇的代码,并增加了一点其它图形. 顶层 ...

  2. 笨办法学Python(三)

    习题 3: 数字和数学计算 每一种编程语言都包含处理数字和进行数学计算的方法.不必担心,程序员经常撒谎说他们是多么牛的数学天才,其实他们根本不是.如果他们真是数学天才,他们早就去从事数学相关的行业了, ...

  3. windows cmd窗口,输出UTF-8格式文件,显示乱码

    本文来自网络,参考文档见文档末尾 想在windows cmd窗口中查看utf-8中文,需要先执行以下步骤 chcp 65001 将CMD窗口切换成UTF-8代码页 在命令行标题栏上点击右键,选择&qu ...

  4. Selenium入门19 捕获异常

    脚本出现异常时会中断执行,想要继续执行就要做异常处理: 1 try ... except .... else   遇到异常显示异常信息: 没有异常继续执行else后面的脚本 2 try ... exc ...

  5. UESTC 31 饭卡 card

    dp,答案容易想到是 凑出价格总和≤m-5 + 没被使用的最大价格. dp[i = 前i种价格][j = 价格总和] = 最大没使用的价格下标idx_m. dp[i-1][j]存在的话,则只要更新id ...

  6. poj 2115 扩展欧几里得

    题目链接:http://poj.org/problem?id=2115 题意: 给出一段循环程序,循环体变量初始值为 a,结束不等于 b ,步长为 c,看要循环多少次,其中运算限制在 k位:死循环输出 ...

  7. frcnn_train_data_param的distort_param实现

    frcnn_train_data_param frcnn_train_data_param { source: "./data/train_list.txt" root_folde ...

  8. BCB:如何在BCB中使用CodeGuard

    www.educity.cn 发布者:xjxyj2006 来源:网络转载 发布日期:2013年12月13日 文章评论 发表文章 一. 为什么写这篇东西 自己在使用 BCB5 写一些程序时需要检查很多东 ...

  9. bootstrap中模态框、模态框的属性

    工作中有需要用到模态框的可以看看 <div class="modal fade" id="userModal" tabindex="-1&quo ...

  10. 如何提高mysql的安全性?

    1.如果 MySQL 客户端和服务器端的连接需要跨越并通过不可信任的网络,那么需要使用 ssh 隧道来加密该连接的通信.2.使用 set password 语句来修改用户的密码,先“mysql -u ...