OpenAL 捕获音频时出错

OpenAL error capturing audio

我正在使用 OpenAL 框架 (https://github.com/kcat/openal-soft) 从麦克风捕获音频。主文件是(在 Whosebug 上找到):

#include <stdio.h>
#include "AL/al.h"
#include "AL/alc.h"
using namespace std;

const int SRATE = 44100;
const int SSIZE = 1102;

ALbyte buffer[220500];
ALint sample;

int main(int argc, char *argv[]) {
    alGetError();
    ALCdevice *device = alcCaptureOpenDevice(NULL, SRATE, AL_FORMAT_STEREO16, SSIZE);
    if (alGetError() != AL_NO_ERROR) {
        return 0;
    }
    alcCaptureStart(device);

    while (true) {
        alcGetIntegerv(device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample);
        alcCaptureSamples(device, (ALCvoid *)buffer, sample);

        // ... do something with the buffer 
    }

    alcCaptureStop(device);
    alcCaptureCloseDevice(device);
    return 0;
}

编译主文件我使用下面的CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project( SharedLibrary C CXX )

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -fPIC -fpermissive")

set(OPENAL_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/openal/")

set(OPENAL_LIB_DIR "${OPENAL_ROOT}build")
set(OPENAL_INCLUDE_DIR "${OPENAL_ROOT}include/AL")

include_directories(${OPENAL_INCLUDE_DIR})

link_directories(${OPENAL_LIB_DIR})

add_executable( main main.cpp )

target_link_libraries( main openal)

编译工作正常但是当我 运行 ./main 我得到这个:

AL lib: (WW) alGetError: Querying error state on null context (implicitly 0xa004)
AL lib: (WW) jack_msg_handler: Cannot connect to server socket err = No such file or directory
AL lib: (WW) jack_msg_handler: Cannot connect to server request channel
AL lib: (WW) jack_msg_handler: jack server is not running or cannot be started
AL lib: (WW) jack_msg_handler: JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
AL lib: (WW) jack_msg_handler: JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
AL lib: (WW) ALCjackBackendFactory_init: jack_client_open() failed, 0x11
AL lib: (WW) alc_initconfig: Failed to initialize backend "jack"
AL lib: (WW) alGetError: Querying error state on null context (implicitly 0xa004)
AL lib: (EE) alc_cleanup: 1 device not closed

你知道为什么吗?

第一个警告很容易解决:如果之前没有调用过 OpenAL 函数,请不要调用 alGetError

那么请看一下https://openal.org/documentation/openal-1.1-specification.pdf中的6.4.2节。

你可以读到 alcCaptureOpenDevice return 失败时为 NULL。您应该检查 return 值。

此外,这里您将 NULL 作为 alcCaptureOpenDevice 的第一个参数,以回退到您可能没有的默认设备。建议查询可用设备列表,然后打开其中一个(pdf中也有说明)。