libvorbis从C ++中的内存音频解码 [英] libvorbis audio decode from memory in C++
问题描述
给定C ++中的编码缓冲区,使用oggvorbis结构解码已经在内存中的数据的步骤是什么?
OggVorbis_File不能使用,
我试图研究必要的结构和方法,但我对音频编码和解码相当新。
任何可以帮助我进一步阅读的资源都会受到赞赏。
我应该澄清一下,我打算使用解码的数据流OpenAL。
谢谢。
回答自己的问题。 / p>
这可以通过为vorbis提供自定义回调来实现。
struct ogg_file
{
char * curPtr;
char * filePtr;
size_t fileSize;
};
size_t AR_readOgg(void * dst,size_t size1,size_t size2,void * fh)
{
ogg_file * of = reinterpret_cast< ogg_file *>(fh);
size_t len = size1 * size2;
if(of-> curPtr + len> of-> filePtr + of-> fileSize)
{
len = of-> filePtr + of-> fileSize- of> curPtr;
}
memcpy(dst,of-> curPtr,len);
of-> curPtr + = len;
return len;
}
int AR_seekOgg(void * fh,ogg_int64_t to,int type){
ogg_file * of = reinterpret_cast< ogg_file *>(fh);
switch(type){
case SEEK_CUR:
of-> curPtr + = to;
break;
case SEEK_END:
of-> curPtr = of-> filePtr + of-> fileSize - to;
break;
case SEEK_SET:
of-> curPtr = of-> filePtr + to;
break;
默认值:
return -1;
}
if(of-> curPtr< of-> filePtr){
of-> curPtr = of-> filePtr;
return -1;
}
if(of-> curPtr> of-> filePtr + of-> fileSize){
of-> curPtr = of-> filePtr + of-> ;文件大小;
return -1;
}
return 0;
}
int AR_closeOgg(void * fh)
{
return 0;
}
long AR_tellOgg(void * fh)
{
ogg_file * of = reinterpret_cast< ogg_file *>(fh);
return(of-> curPtr-of-> filePtr);
}
使用
ov_callbacks callbacks;
ogg_file t;
t.curPtr = t.filePtr = compressedData;
t.fileSize = compressedDataSize;
OggVorbis_File * ov = new OggVorbis_File;
mOggFile = ov;
memset(ov,0,sizeof(OggVorbis_File));
callbacks.read_func = AR_readOgg;
callbacks.seek_func = AR_seekOgg;
callbacks.close_func = AR_closeOgg;
callbacks.tell_func = AR_tellOgg;
int ret = ov_open_callbacks((void *)& t,ov,NULL,-1,callbacks);
vorbis_info * vi = ov_info(ov,-1);
assert(vi);
/ *可以使用压缩数据,解压缩查看ov_read * /
A特别感谢Doom3 GPL来源,大部分的帮助,它可以
查看:这里
Given an encoded buffer in C++, what would be the steps using oggvorbis structs to decode the already in-memory data?
OggVorbis_File cannot be used, because assets are within compressed archives.
I'm trying to research the necessary structs and methods, but I'm fairly new to audio encoding and decoding.
Any resources that can help further my reading are appreciated as well!
I should clarify, I intend to use the decoded data to stream into OpenAL.
Thanks.
Answering my own question.
This can be done by providing custom callbacks to vorbis.
struct ogg_file
{
char* curPtr;
char* filePtr;
size_t fileSize;
};
size_t AR_readOgg(void* dst, size_t size1, size_t size2, void* fh)
{
ogg_file* of = reinterpret_cast<ogg_file*>(fh);
size_t len = size1 * size2;
if ( of->curPtr + len > of->filePtr + of->fileSize )
{
len = of->filePtr + of->fileSize - of->curPtr;
}
memcpy( dst, of->curPtr, len );
of->curPtr += len;
return len;
}
int AR_seekOgg( void *fh, ogg_int64_t to, int type ) {
ogg_file* of = reinterpret_cast<ogg_file*>(fh);
switch( type ) {
case SEEK_CUR:
of->curPtr += to;
break;
case SEEK_END:
of->curPtr = of->filePtr + of->fileSize - to;
break;
case SEEK_SET:
of->curPtr = of->filePtr + to;
break;
default:
return -1;
}
if ( of->curPtr < of->filePtr ) {
of->curPtr = of->filePtr;
return -1;
}
if ( of->curPtr > of->filePtr + of->fileSize ) {
of->curPtr = of->filePtr + of->fileSize;
return -1;
}
return 0;
}
int AR_closeOgg(void* fh)
{
return 0;
}
long AR_tellOgg( void *fh )
{
ogg_file* of = reinterpret_cast<ogg_file*>(fh);
return (of->curPtr - of->filePtr);
}
Usage
ov_callbacks callbacks;
ogg_file t;
t.curPtr = t.filePtr = compressedData;
t.fileSize = compressedDataSize;
OggVorbis_File* ov = new OggVorbis_File;
mOggFile = ov;
memset( ov, 0, sizeof( OggVorbis_File ) );
callbacks.read_func = AR_readOgg;
callbacks.seek_func = AR_seekOgg;
callbacks.close_func = AR_closeOgg;
callbacks.tell_func = AR_tellOgg;
int ret = ov_open_callbacks((void *)&t, ov, NULL, -1, callbacks);
vorbis_info* vi = ov_info(ov, -1);
assert(vi);
/* compressed data is available to use, to uncompress look into ov_read */
A Special thanks to the Doom3 GPL source for most of the help with this, it can be viewed at : here
这篇关于libvorbis从C ++中的内存音频解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!