我需要使用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
}