c++ - 如何使用OpenAL录制声音

标签 c++ audio openal

我需要使用MMSYSTEM录制声音,为此,我正在使用OpenAL库。录制后,我会将声音缓冲区发送到网络(VoIP)。首先,我编写了一些代码来记录声音并将其写入文件。但是我的代码无法正常工作。如果使用例如Wavosaur打开FILE,则只会看到白噪声。

#include <OpenAL/al.h>    // OpenAL header files
#include <OpenAL/alc.h>
#include <boost\thread.hpp>
#pragma comment (lib, "OpenAl32.lib")
#include <fstream>
#include <iostream>
#include <stdio.h>
bool key = true;
using namespace std;
void ThreadFunc()
{
    int a =1;
    cin>>a;
    if (a==0)
    {
        key = false;
    }
}


int main()
{
    ofstream of;
    of.open("FILE1");
    boost::thread MyThread(&ThreadFunc);
    ALCdevice *dev[2];
    ALCcontext *ctx;
    ALuint source, buffers[3];
    char data[5000];
    ALuint buf;
    ALint val;

    float ttotal;
    unsigned int ccount;
    long int c1ount;
    c1ount =0;

    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); //22050 mean 22.050 samples per     second. or 44100 for 44.1 per second.

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

    while(key) 
    {
        /* 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);


          //***** Process/filter captured data here *****//



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

      /* 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);
        of.write(data, val*2);
        /* Make sure the source is still playing */
        alGetSourcei(source, AL_SOURCE_STATE, &val);

        if(val != AL_PLAYING)
        {

            alSourcePlay(source);
        }
    }

    cout<< "stop\n";

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

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

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

    return 0;
}

请帮助我,或者如果您有工作的例子,请给我。
编辑后
  while (key) {
        alcGetIntegerv(device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample);

        alcCaptureSamples(device, (ALCvoid *)buffer, sample);
       if (sample!=0)
        {
            cout<<samplenotzero++<<endl;
            f1.write(buffer, sample);
        }

        // ... do something with the buffer 
    }

最佳答案

您的问题似乎与this非常相似。我认为,如果您遵循那里给出的建议,那么您应该会顺利进行。干杯!

关于c++ - 如何使用OpenAL录制声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14333645/

相关文章:

c++ - 抛出错误的分配

c++ - C++如何从数组中获取最长的素数序列

java - Java 中的合成器和音序器(javax.sound.midi) 是什么?

audio - 在音频编程环境中音高和速度是一回事吗?

iphone - openAL 获取/设置偏移量(以秒为单位)

c++ - Android Studio 调试器控制台显示 "Failed to connect port"

c++ - 显示已安装的应用程序列表

android - 来自文件的 SoundPool.load() 和 FileDescriptor

java - 是否热衷于使用mp3spi获得mp3文件的持续时间?

java - 在哪里可以找到 LWJGL 的示例使用?