Android OpenSL 录制 PCM 音频数据(八)

2021-01-11 09:52:35 浏览数 (1)

前言

本章主要讲述如何通过Android OpenSL 实现录制 PCM 音频数据。开发流程回顾可以看下笔者前面发布过的文章。

开发准备

1.在Android studo下面建立工程,在使用OpenSL ES的API之前,首先需要引入CMakeLists.txt里面添加依赖库和链接库,代码如下:

代码语言:txt复制
find_library( # Sets the name of the path variable.
              log-lib
              android
        # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib
                       OpenSLES
                       android
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

2.同时添加静态库依赖的c 文件

代码语言:txt复制
add_library( # Sets the name of the library.
             native-lib
             # Sets the library as a shared library.
             SHARED
             native-lib.cpp
            )

OpenSL ES 录制 PCM 音频数据流程主要有如下:

1、 新建native-lib.cpp文件,然后引入头文件

代码语言:txt复制
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

声明SLObjectItf接口类型的引擎接口对象engineObject,而后用方法slCreateEngine建立一个引擎接口对象;建立好引擎接口对象后,须要用SLObjectItf的Realize方法来实现engineObject;最后用SLObjectItf的GetInterface方法来初始化SLEngnineItf对象实例。如:

代码语言:txt复制
//引擎接口
static SLObjectItf engineObject = NULL;
//引擎对象
static SLEngineItf engineEngine;
void createEngine()
{
    SLresult result;
    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);

}

2、设置IO设备麦克风,设置buffer队列,设置录制规格。

代码语言:txt复制
 /**
     * 设置IO设备(麦克风)
     */
    SLDataLocator_IODevice loc_dev = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
                                      SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
    SLDataSource audioSrc = {&loc_dev, NULL};
    /**
     * 设置buffer队列
     */
    SLDataLocator_AndroidSimpleBufferQueue loc_bq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
    /**
     * 设置录制规格:PCM、2声道、44100HZ、16bit
     */
    SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 2, SL_SAMPLINGRATE_44_1,
                                   SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
                                   SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, SL_BYTEORDER_LITTLEENDIAN};
    SLDataSink audioSnk = {&loc_bq, &format_pcm};

    const SLInterfaceID id[1] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
    const SLboolean req[1] = {SL_BOOLEAN_TRUE};

3、创建录音器

代码语言:txt复制
 /**
     * 创建录制器
     */
    result = (*engineEngine)->CreateAudioRecorder(engineEngine, &recorderObject, &audioSrc,
                                                  &audioSnk, 1, id, req);
    if (SL_RESULT_SUCCESS != result) {
        return;
    }
    result = (*recorderObject)->Realize(recorderObject, SL_BOOLEAN_FALSE);
    if (SL_RESULT_SUCCESS != result) {
        return;
    }
    result = (*recorderObject)->GetInterface(recorderObject, SL_IID_RECORD, &recorderRecord);
    result = (*recorderObject)->GetInterface(recorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
                                             &recorderBufferQueue);
    finished = false;
    result = (*recorderBufferQueue)->Enqueue(recorderBufferQueue, recordBuffer->getRecordBuffer(),
                                             recorderSize);
    result = (*recorderBufferQueue)->RegisterCallback(recorderBufferQueue, bqRecorderCallback, NULL);

4、设置回调函数

代码语言:txt复制
  result = (*recorderBufferQueue)->RegisterCallback(recorderBufferQueue, bqRecorderCallback, NULL);

void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{
    // for streaming recording, here we would call Enqueue to give recorder the next buffer to fill
    // but instead, this is a one-time buffer so we stop recording

    fwrite(recordBuffer->getNowBuffer(), 1, recorderSize, inputPcmFile);

    if(finished)
    {
        (*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_STOPPED);
        fclose(inputPcmFile);
    } else{
        (*recorderBufferQueue)->Enqueue(recorderBufferQueue, recordBuffer->getRecordBuffer(),
                                        recorderSize);
    }
}

5、开始录音

代码语言:txt复制
 /**
     * 开始录音
     */
    (*recorderRecord)->SetRecordState(recorderRecord, SL_RECORDSTATE_RECORDING);

小结

通过上面五个步骤就完成了Android OpenGL ES录制pcm功能,文章最后会附送上demo链接,demo生成的pcm数据会保存在

/Android/data/com.pengjie0668.opensles.demo/cache/temp.pcm

github demo下载链接

0 人点赞