【问题标题】:How I can recording sound with OpenAL如何使用 OpenAL 录制声音
【发布时间】:2013-01-15 08:23:28
【问题描述】:

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

#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 
    }

【问题讨论】:

标签: c++ audio openal


【解决方案1】:

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

【讨论】:

  • 好的,如果我从那个答案中获取代码,并且在我将缓冲区发送到文件(样本大小的缓冲区)时的每一步,我的文件中有噪音。我需要做什么?
  • 在没有深入研究的情况下离开我的头顶,我会看看硬件产生的噪音。您可以尝试从其他频道录制吗?或者,如果您使用其他录音应用程序,您也会收到噪音吗?
  • 是的,当然。而且我知道我的麦克风工作正常,因为如果我在 Windows 录音机中使用它,它会给我声音。
  • 好的,如果你确定你的硬件工作正常,那么我认为这里有另一个地方可以为你更详细地解释事情,并向你展示如何在制作过程中进行测试确保您都设置正确:blitzbasic.com/Community/posts.php?topic=90830
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多