2013-01-15 7 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