如何在qt webassembly中使用OPENAL捕获语音
How to capture voice with OPENAL in qt webassembly
我想在 qt webassembly 中录制声音。我使用 openal 来捕捉声音。我在桌面版中成功使用了此代码,但在 webassembly 版中它无法正常工作。
ALenum errorCode=0;
ALuint helloBuffer[16], helloSource[1];
ALCdevice* audioDevice = alcOpenDevice(NULL); // Request default audio device
errorCode = alcGetError(audioDevice);
ALCcontext* audioContext = alcCreateContext(audioDevice,NULL); // Create the audio context
alcMakeContextCurrent(audioContext);
errorCode = alcGetError(audioDevice);
alGetError();
ALCdevice *device = alcCaptureOpenDevice(NULL, SRATE, AL_FORMAT_MONO16, SSIZE);
if (alGetError() != AL_NO_ERROR) {
return 0;
}
alcCaptureStart(device);
int i=0;
while (i<100) {
i++;
alcGetIntegerv(device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample);
alcCaptureSamples(device, (ALCvoid *)buffer, sample);
if(sample >0)
{
datarecord.push_back(buffer);
}
}
#include <QCoreApplication>
#include <stdio.h>
#include <emscripten.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <QFileDialog>
char buffer[48000*2];
QByteArray datarecord;
int cnt=0;
void mycallback(void *arg)
{
ALCint nframes;
ALCdevice *dev = (ALCdevice*)arg;
alcGetIntegerv(dev, ALC_CAPTURE_SAMPLES, 1, &nframes);
if(nframes>0)
{
alcCaptureSamples(dev,buffer, nframes);
datarecord.append(QByteArray(buffer, nframes*2));
cnt++;
if(cnt>1000)
{
QString fileName="save";
QFileDialog::saveFileContent(datarecord, fileName);
emscripten_cancel_main_loop();
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ALCdevice *dev = NULL;
dev = alcCaptureOpenDevice(NULL, 48000, AL_FORMAT_MONO16, 1024);
alcCaptureStart(dev);
emscripten_set_main_loop_arg(mycallback, dev, -1, 1);
return a.exec();
}
我想在 qt webassembly 中录制声音。我使用 openal 来捕捉声音。我在桌面版中成功使用了此代码,但在 webassembly 版中它无法正常工作。
ALenum errorCode=0;
ALuint helloBuffer[16], helloSource[1];
ALCdevice* audioDevice = alcOpenDevice(NULL); // Request default audio device
errorCode = alcGetError(audioDevice);
ALCcontext* audioContext = alcCreateContext(audioDevice,NULL); // Create the audio context
alcMakeContextCurrent(audioContext);
errorCode = alcGetError(audioDevice);
alGetError();
ALCdevice *device = alcCaptureOpenDevice(NULL, SRATE, AL_FORMAT_MONO16, SSIZE);
if (alGetError() != AL_NO_ERROR) {
return 0;
}
alcCaptureStart(device);
int i=0;
while (i<100) {
i++;
alcGetIntegerv(device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample);
alcCaptureSamples(device, (ALCvoid *)buffer, sample);
if(sample >0)
{
datarecord.push_back(buffer);
}
}
#include <QCoreApplication>
#include <stdio.h>
#include <emscripten.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <QFileDialog>
char buffer[48000*2];
QByteArray datarecord;
int cnt=0;
void mycallback(void *arg)
{
ALCint nframes;
ALCdevice *dev = (ALCdevice*)arg;
alcGetIntegerv(dev, ALC_CAPTURE_SAMPLES, 1, &nframes);
if(nframes>0)
{
alcCaptureSamples(dev,buffer, nframes);
datarecord.append(QByteArray(buffer, nframes*2));
cnt++;
if(cnt>1000)
{
QString fileName="save";
QFileDialog::saveFileContent(datarecord, fileName);
emscripten_cancel_main_loop();
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ALCdevice *dev = NULL;
dev = alcCaptureOpenDevice(NULL, 48000, AL_FORMAT_MONO16, 1024);
alcCaptureStart(dev);
emscripten_set_main_loop_arg(mycallback, dev, -1, 1);
return a.exec();
}