我现在正在做用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 数学的三元一次方程求解
- ¥20 iqoo11 如何下载安装工程模式
- ¥15 本题的答案是不是有问题
- ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
- ¥15 C++使用Gunplot
- ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
- ¥15 matlab数字图像处理频率域滤波
- ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
- ¥15 ELGamal和paillier计算效率谁快?
- ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题