如何使用openAL将实时音频输入从麦克风录制到文件中? (里面有C++代码)

Posted

技术标签:

【中文标题】如何使用openAL将实时音频输入从麦克风录制到文件中? (里面有C++代码)【英文标题】:How to record live audio input into file from microphone with openAL? (C++ code inside) 【发布时间】:2010-11-04 12:38:49 【问题描述】:

我有这样的代码,用于在 Windows 上使用 openAL 进行麦克风回声。

我想创建一些CapturedAudioData 文件来写入循环期间捕获的所有音频数据。所以它会像未格式化的 PCM。我需要每秒填充 25 次。

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <al.h>
#include <alc.h>
using namespace std;
int main()

    ALCdevice *dev[2];
    ALCcontext *ctx;
    ALuint source, buffers[3];
    char data[5000]; 
    ALuint buf;
    ALint val;

    dev[0] = alcOpenDevice(NULL);
    ctx = alcCreateContext(dev[0], NULL);
    alcMakeContextCurrent(ctx);

    alGenSources(1, &source);
    alGenBuffers(3, buffers);

    /* Setup some initial silent data to play out of the source */
    alBufferData(buffers[0], AL_FORMAT_MONO16, data, sizeof(data), 22050);
    alBufferData(buffers[1], AL_FORMAT_MONO16, data, sizeof(data), 22050);
    alBufferData(buffers[2], AL_FORMAT_MONO16, data, sizeof(data), 22050);
    alSourceQueueBuffers(source, 3, buffers);

    /* If you don't need 3D spatialization, this should help processing time */
    alDistanceModel(AL_NONE); 

    dev[1] = alcCaptureOpenDevice(NULL, 22050, AL_FORMAT_MONO16, sizeof(data)/2);

    /* Start playback and capture, and enter the audio loop */
    alSourcePlay(source);
    alcCaptureStart(dev[1]);

    while(1) 
    
        /* Check if any queued buffers are finished */
        alGetSourcei(source, AL_BUFFERS_PROCESSED, &val);
        if(val <= 0)
            continue;

        /* Check how much audio data has been captured (note that 'val' is the
        * number of frames, not bytes) */
        alcGetIntegerv(dev[1], ALC_CAPTURE_SAMPLES, 1, &val);

        /* Read the captured audio */
        alcCaptureSamples(dev[1], data, val);

        /* Pop the oldest finished buffer, fill it with the new capture data,
        then re-queue it to play on the source */
        alSourceUnqueueBuffers(source, 1, &buf);
        alBufferData(buf, AL_FORMAT_MONO16, data, val*2 /* bytes here, not
        frames */, 22050);
        alSourceQueueBuffers(source, 1, &buf);

        /* Make sure the source is still playing */
        alGetSourcei(source, AL_SOURCE_STATE, &val);

        if(val != AL_PLAYING)
        

            alSourcePlay(source);
        
    

    /* Shutdown and cleanup */
    alcCaptureStop(dev[1]);
    alcCaptureCloseDevice(dev[1]);

    alSourceStop(source);
    alDeleteSources(1, &source);
    alDeleteBuffers(3, buffers);

    alcMakeContextCurrent(NULL);
    alcDestroyContext(ctx);
    alcCloseDevice(dev[0]); 

    return 0;
 

如何创建这样的东西 - 我必须更改/添加到我的代码中?

【问题讨论】:

【参考方案1】:

看来您需要做的只是将val 样本从data 缓冲区写入文件,而不是将它们发送到输出。即用文件写入替换以“Pop the old...”开头的代码。计算您编写的样本数:每当您达到 (25 sec * 22050 个样本/秒) 值时,它就会被“填满”。

【讨论】:

以上是关于如何使用openAL将实时音频输入从麦克风录制到文件中? (里面有C++代码)的主要内容,如果未能解决你的问题,请参考以下文章

IOS实时录制音频并分割成文件

如何录制网络/浏览器音频输出(不是麦克风音频)

EZAudio 中的音频输入源

如何使用 Auriotouch 将麦克风的音频输入静音并仅识别设备的内部音频

尝试从麦克风录制并实时播放

仅从 iphone 扬声器(不包括麦克风)录制音频输出