2013-01-15 45 views
0

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

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

你有没有试过Google https://www.google.com/search?q=recording+sound+with+OpenAL&ie=utf-8&oe=utf-8&aq=t? –

+1

是的,当然... – EXTRAM

回答

0

你的问题似乎是非常相似的this。我认为,如果你遵循那里给出的建议,你应该顺利地前进。干杯!

+0

好的,如果我从那个答案中取出代码,并且每当我发送缓冲区到文件(样本大小的缓冲区)时,我的文件中都有噪音。我需要做什么? – EXTRAM

+0

如果没有深入研究它,我会看到硬件产生的噪音。你可以尝试从不同的频道录制吗?或者如果您使用其他录制应用程序,您是否也听到噪音? –

+0

当然可以。而且我知道我的麦克风工作正常,因为如果我在Windows录音机中使用它,它会给我提供声音。 – EXTRAM