OpenAL错误捕获音频

时间:2017-05-10 08:25:03

标签: c++ openal

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

#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

你知道为什么吗?

1 个答案:

答案 0 :(得分:1)

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

然后请查看https://openal.org/documentation/openal-1.1-specification.pdf中的第6.4.2节。

您可以读到alcCaptureOpenDevice失败时返回NULL。您应该检查返回值。

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