我现在正在做用openal的实时录音功能,并保存在本地。获取音频并播放功能已经实现,并且获取到的数据已经保存在buffer中。但是保存音频文件的相关接口或方法我并没有在openal中找到。初步设想是保存文件是alutLoadWAVFile()的逆过程,即加入WAV的文件头后再写入数据。请问大神们,用openal能不能实现呢?
1条回答
- 毛小线~ 2017-06-21 09:13关注
以下是照搬别人的代码。。具体链接找不到了。可以实现录音并回放
#include "stdafx.h"
#include
#include //添加openal的 header files(头文件)
#include
#includeusing std::list;
using namespace std;#define FREQ 22050 //定义采样率 Sample Rate
#define CAP_SIZE 2048 //一次捕获音频的长度int main(int argC,char* argV[])
{
//initial
list bufferQueue; // A quick and dirty queue of buffer objects
ALenum errorCode = 0;
ALuint helloBuffer[16], helloSource[1];ALCdevice* audioDevice = alcOpenDevice(NULL); //请求默认的音频设备 errorCode = alcGetError(audioDevice); ALCcontext* audioContext = alcCreateContext(audioDevice, NULL); //创建上下文 alcMakeContextCurrent(audioContext); errorCode = alcGetError(audioDevice); // Request the default capture device with a half-second buffer ALCdevice* inputDevice = alcCaptureOpenDevice(NULL, FREQ, AL_FORMAT_MONO16, FREQ / 2); errorCode = alcGetError(inputDevice); alcCaptureStart(inputDevice); // Begin capturing to where? errorCode = alcGetError(inputDevice); alGenBuffers(16, &helloBuffer[0]); // Create some buffer-objects errorCode = alGetError(); // Queue our buffers onto an STL list for (int ii = 0; ii<16; ++ii) { bufferQueue.push_back(helloBuffer[ii]); //将buffer放入队尾 } alGenSources(1, &helloSource[0]); // Create a sound source errorCode = alGetError(); short buffer[FREQ * 2]; // A buffer to hold captured audio ALCint samplesIn = 0; // How many samples are captured ALint availBuffers = 0; // Buffers to be recovered -- 这里保存 ALuint myBuff; // The buffer we're using ALuint buffHolder[16]; // An array to hold catch the unqueued buffers bool done = false; //play or cap? while (!done) { // Main loop // Poll for recoverable buffers alGetSourcei(helloSource[0], AL_BUFFERS_PROCESSED, &availBuffers); if (availBuffers>0) { alSourceUnqueueBuffers(helloSource[0], availBuffers, buffHolder); for (int ii = 0; ii<availBuffers; ++ii) { // Push the recovered buffers back on the queue bufferQueue.push_back(buffHolder[ii]); } } // Poll for captured audio alcGetIntegerv(inputDevice, ALC_CAPTURE_SAMPLES, 1, &samplesIn); //获取可用的样本数 if (samplesIn>CAP_SIZE) { // Grab the sound alcCaptureSamples(inputDevice, buffer, CAP_SIZE); //***** Process/filter captured data here *****// //for (int ii=0;ii<CAP_SIZE;++ii) { // buffer[ii]*=0.1; // Make it quieter //} // Stuff the captured data in a buffer-object if (!bufferQueue.empty()) { // We just drop the data if no buffers are available myBuff = bufferQueue.front(); bufferQueue.pop_front(); alBufferData(myBuff, AL_FORMAT_MONO16, buffer, CAP_SIZE * sizeof(short), FREQ); // Queue the buffer alSourceQueueBuffers(helloSource[0], 1, &myBuff); // Restart the source if needed // (if we take too long and the queue dries up, // the source stops playing). ALint sState = 0; alGetSourcei(helloSource[0], AL_SOURCE_STATE, &sState); if (sState != AL_PLAYING) { alSourcePlay(helloSource[0]); } } } } // Stop capture alcCaptureStop(inputDevice); alcCaptureCloseDevice(inputDevice); // Stop the sources alSourceStopv(1, &helloSource[0]); for (int ii = 0; ii<1; ++ii) { alSourcei(helloSource[ii], AL_BUFFER, 0); } // Clean-up alDeleteSources(1, &helloSource[0]); alDeleteBuffers(16, &helloBuffer[0]); errorCode = alGetError(); alcMakeContextCurrent(NULL); errorCode = alGetError(); alcDestroyContext(audioContext); alcCloseDevice(audioDevice); return 0;
}
解决 1无用
悬赏问题
- ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
- ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
- ¥20 腾讯企业邮箱邮件可以恢复么
- ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
- ¥15 错误 LNK2001 无法解析的外部符号
- ¥50 安装pyaudiokits失败
- ¥15 计组这些题应该咋做呀
- ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
- ¥15 让node服务器有自动加载文件的功能
- ¥15 jmeter脚本回放有的是对的有的是错的