OpenAL:如何创建简单的“麦克风回声”程序?

发布于 2024-09-30 03:09:02 字数 106 浏览 3 评论 0原文

所以我想知道从默认麦克风读取数据并输出到默认扬声器的最短(就有效线路而言)开放 AL 代码是什么?

我正在 Visual Studio 2008 下的 Windows 7 上进行开发

So I wonder what would be shortest (in terms of effective lines) open AL code for reading data from default microphone and outputting on to default loudspeakers?

I am developing on windows 7 under Visual Studio 2008

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二手情话 2024-10-07 03:09:02

这是一个古老的问题,但这里有一个答案。如果我们真的追求简洁,它肯定可以被修剪,但这比 100 行有效行少一点:

#include <AL/al.h>    // OpenAL header files
#include <AL/alc.h>

#include <list>

using std::list;

#define FREQ 22050   // Sample rate
#define CAP_SIZE 2048 // How much to capture at a time (affects latency)

int main(int argC,char* argV[])
{
    list<ALuint> bufferQueue; // A quick and dirty queue of buffer objects

    ALenum errorCode=0;
    ALuint helloBuffer[16], helloSource[1];
    ALCdevice* audioDevice = alcOpenDevice(NULL); // Request default audio device
    errorCode = alcGetError(audioDevice);
    ALCcontext* audioContext = alcCreateContext(audioDevice,NULL); // Create the audio context
    alcMakeContextCurrent(audioContext);
    errorCode = alcGetError(audioDevice);
    // Request the default capture device with a half-second buffer
    ALCdevice* inputDevice = alcCaptureOpenDevice(NULL,FREQ,AL_FORMAT_MONO16,FREQ/2);
    errorCode = alcGetError(inputDevice);
    alcCaptureStart(inputDevice); // Begin capturing
    errorCode = alcGetError(inputDevice);

    alGenBuffers(16,&helloBuffer[0]); // Create some buffer-objects
    errorCode = alGetError();

    // Queue our buffers onto an STL list
    for (int ii=0;ii<16;++ii) {
        bufferQueue.push_back(helloBuffer[ii]);
    }

  alGenSources (1, &helloSource[0]); // Create a sound source
    errorCode = alGetError();

    short buffer[FREQ*2]; // A buffer to hold captured audio
    ALCint samplesIn=0;  // How many samples are captured
    ALint availBuffers=0; // Buffers to be recovered
    ALuint myBuff; // The buffer we're using
    ALuint buffHolder[16]; // An array to hold catch the unqueued buffers
    bool done = false;
    while (!done) { // Main loop
        // Poll for recoverable buffers
        alGetSourcei(helloSource[0],AL_BUFFERS_PROCESSED,&availBuffers);
        if (availBuffers>0) {
            alSourceUnqueueBuffers(helloSource[0],availBuffers,buffHolder);
            for (int ii=0;ii<availBuffers;++ii) {
                // Push the recovered buffers back on the queue
                bufferQueue.push_back(buffHolder[ii]);
            }
        }
        // Poll for captured audio
        alcGetIntegerv(inputDevice,ALC_CAPTURE_SAMPLES,1,&samplesIn);
        if (samplesIn>CAP_SIZE) {
            // Grab the sound
            alcCaptureSamples(inputDevice,buffer,CAP_SIZE);

            //***** Process/filter captured data here *****//
            //for (int ii=0;ii<CAP_SIZE;++ii) {
            //  buffer[ii]*=0.1; // Make it quieter
            //}

            // Stuff the captured data in a buffer-object
            if (!bufferQueue.empty()) { // We just drop the data if no buffers are available
                myBuff = bufferQueue.front(); bufferQueue.pop_front();
                alBufferData(myBuff,AL_FORMAT_MONO16,buffer,CAP_SIZE*sizeof(short),FREQ);

                // Queue the buffer
                alSourceQueueBuffers(helloSource[0],1,&myBuff);

                // Restart the source if needed
                // (if we take too long and the queue dries up,
                //  the source stops playing).
                ALint sState=0;
                alGetSourcei(helloSource[0],AL_SOURCE_STATE,&sState);
                if (sState!=AL_PLAYING) {
                    alSourcePlay(helloSource[0]);
                }
            }
        }
    }
    // Stop capture
    alcCaptureStop(inputDevice);
    alcCaptureCloseDevice(inputDevice);

    // Stop the sources
    alSourceStopv(1,&helloSource[0]);
    for (int ii=0;ii<1;++ii) {
        alSourcei(helloSource[ii],AL_BUFFER,0);
    }
    // Clean-up 
    alDeleteSources(1,&helloSource[0]); 
    alDeleteBuffers(16,&helloBuffer[0]);
    errorCode = alGetError();
    alcMakeContextCurrent(NULL);
    errorCode = alGetError();
    alcDestroyContext(audioContext);
    alcCloseDevice(audioDevice);

    return 0;
}

An ancient question, but here's an answer. It could definitely be trimmed if we were really going for conciseness, but this is a little bit less than 100 effective lines:

#include <AL/al.h>    // OpenAL header files
#include <AL/alc.h>

#include <list>

using std::list;

#define FREQ 22050   // Sample rate
#define CAP_SIZE 2048 // How much to capture at a time (affects latency)

int main(int argC,char* argV[])
{
    list<ALuint> bufferQueue; // A quick and dirty queue of buffer objects

    ALenum errorCode=0;
    ALuint helloBuffer[16], helloSource[1];
    ALCdevice* audioDevice = alcOpenDevice(NULL); // Request default audio device
    errorCode = alcGetError(audioDevice);
    ALCcontext* audioContext = alcCreateContext(audioDevice,NULL); // Create the audio context
    alcMakeContextCurrent(audioContext);
    errorCode = alcGetError(audioDevice);
    // Request the default capture device with a half-second buffer
    ALCdevice* inputDevice = alcCaptureOpenDevice(NULL,FREQ,AL_FORMAT_MONO16,FREQ/2);
    errorCode = alcGetError(inputDevice);
    alcCaptureStart(inputDevice); // Begin capturing
    errorCode = alcGetError(inputDevice);

    alGenBuffers(16,&helloBuffer[0]); // Create some buffer-objects
    errorCode = alGetError();

    // Queue our buffers onto an STL list
    for (int ii=0;ii<16;++ii) {
        bufferQueue.push_back(helloBuffer[ii]);
    }

  alGenSources (1, &helloSource[0]); // Create a sound source
    errorCode = alGetError();

    short buffer[FREQ*2]; // A buffer to hold captured audio
    ALCint samplesIn=0;  // How many samples are captured
    ALint availBuffers=0; // Buffers to be recovered
    ALuint myBuff; // The buffer we're using
    ALuint buffHolder[16]; // An array to hold catch the unqueued buffers
    bool done = false;
    while (!done) { // Main loop
        // Poll for recoverable buffers
        alGetSourcei(helloSource[0],AL_BUFFERS_PROCESSED,&availBuffers);
        if (availBuffers>0) {
            alSourceUnqueueBuffers(helloSource[0],availBuffers,buffHolder);
            for (int ii=0;ii<availBuffers;++ii) {
                // Push the recovered buffers back on the queue
                bufferQueue.push_back(buffHolder[ii]);
            }
        }
        // Poll for captured audio
        alcGetIntegerv(inputDevice,ALC_CAPTURE_SAMPLES,1,&samplesIn);
        if (samplesIn>CAP_SIZE) {
            // Grab the sound
            alcCaptureSamples(inputDevice,buffer,CAP_SIZE);

            //***** Process/filter captured data here *****//
            //for (int ii=0;ii<CAP_SIZE;++ii) {
            //  buffer[ii]*=0.1; // Make it quieter
            //}

            // Stuff the captured data in a buffer-object
            if (!bufferQueue.empty()) { // We just drop the data if no buffers are available
                myBuff = bufferQueue.front(); bufferQueue.pop_front();
                alBufferData(myBuff,AL_FORMAT_MONO16,buffer,CAP_SIZE*sizeof(short),FREQ);

                // Queue the buffer
                alSourceQueueBuffers(helloSource[0],1,&myBuff);

                // Restart the source if needed
                // (if we take too long and the queue dries up,
                //  the source stops playing).
                ALint sState=0;
                alGetSourcei(helloSource[0],AL_SOURCE_STATE,&sState);
                if (sState!=AL_PLAYING) {
                    alSourcePlay(helloSource[0]);
                }
            }
        }
    }
    // Stop capture
    alcCaptureStop(inputDevice);
    alcCaptureCloseDevice(inputDevice);

    // Stop the sources
    alSourceStopv(1,&helloSource[0]);
    for (int ii=0;ii<1;++ii) {
        alSourcei(helloSource[ii],AL_BUFFER,0);
    }
    // Clean-up 
    alDeleteSources(1,&helloSource[0]); 
    alDeleteBuffers(16,&helloBuffer[0]);
    errorCode = alGetError();
    alcMakeContextCurrent(NULL);
    errorCode = alGetError();
    alcDestroyContext(audioContext);
    alcCloseDevice(audioDevice);

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文