0

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

1 に答える 1

0

あなたの質問はこれに非常に似ているようです。そこに書かれているアドバイスに従えば、うまくいくと思います。乾杯!

于 2013-01-17T02:59:29.610 に答える