本文整理汇总了C++中ov_pcm_total函数的典型用法代码示例。如果您正苦于以下问题:C++ ov_pcm_total函数的具体用法?C++ ov_pcm_total怎么用?C++ ov_pcm_total使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ov_pcm_total函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: QFile
f_cnt_t SampleBuffer::decodeSampleOGGVorbis( const char * _f,
int_sample_t * & _buf,
ch_cnt_t & _channels,
sample_rate_t & _samplerate )
{
static ov_callbacks callbacks =
{
qfileReadCallback,
qfileSeekCallback,
qfileCloseCallback,
qfileTellCallback
} ;
OggVorbis_File vf;
f_cnt_t frames = 0;
QFile * f = new QFile( _f );
if( f->open( QFile::ReadOnly ) == false )
{
delete f;
return 0;
}
int err = ov_open_callbacks( f, &vf, NULL, 0, callbacks );
if( err < 0 )
{
switch( err )
{
case OV_EREAD:
printf( "SampleBuffer::decodeSampleOGGVorbis():"
" media read error\n" );
break;
case OV_ENOTVORBIS:
/* printf( "SampleBuffer::decodeSampleOGGVorbis():"
" not an Ogg Vorbis file\n" );*/
break;
case OV_EVERSION:
printf( "SampleBuffer::decodeSampleOGGVorbis():"
" vorbis version mismatch\n" );
break;
case OV_EBADHEADER:
printf( "SampleBuffer::decodeSampleOGGVorbis():"
" invalid Vorbis bitstream header\n" );
break;
case OV_EFAULT:
printf( "SampleBuffer::decodeSampleOgg(): "
"internal logic fault\n" );
break;
}
delete f;
return 0;
}
ov_pcm_seek( &vf, 0 );
_channels = ov_info( &vf, -1 )->channels;
_samplerate = ov_info( &vf, -1 )->rate;
ogg_int64_t total = ov_pcm_total( &vf, -1 );
_buf = new int_sample_t[total * _channels];
int bitstream = 0;
long bytes_read = 0;
do
{
bytes_read = ov_read( &vf, (char *) &_buf[frames * _channels],
( total - frames ) * _channels *
BYTES_PER_INT_SAMPLE,
isLittleEndian() ? 0 : 1,
BYTES_PER_INT_SAMPLE, 1, &bitstream );
if( bytes_read < 0 )
{
break;
}
frames += bytes_read / ( _channels * BYTES_PER_INT_SAMPLE );
}
while( bytes_read != 0 && bitstream == 0 );
ov_clear( &vf );
// if buffer isn't empty, convert it to float and write it down
if ( frames > 0 && _buf != NULL )
{
convertIntToFloat ( _buf, frames, _channels);
}
return frames;
}
示例2: Unload
bool SoundBuffer::LoadOGG(const std::string & filename, const SoundInfo & sound_device_info, std::ostream & error_output)
{
if (loaded)
Unload();
name = filename;
FILE *fp;
unsigned int samples;
fp = fopen(filename.c_str(), "rb");
if (fp)
{
vorbis_info *pInfo;
OggVorbis_File oggFile;
ov_open_callbacks(fp, &oggFile, NULL, 0, OV_CALLBACKS_DEFAULT);
pInfo = ov_info(&oggFile, -1);
//I assume ogg is always 16-bit (2 bytes per sample) -Venzon
samples = ov_pcm_total(&oggFile,-1);
info = SoundInfo(samples*pInfo->channels, pInfo->rate, pInfo->channels, 2);
SoundInfo desired_info(info.samples, sound_device_info.frequency, info.channels, sound_device_info.bytespersample);
if (!(desired_info == info))
{
error_output << "SOUND FORMAT:" << std::endl;
info.DebugPrint(error_output);
error_output << "DESIRED FORMAT:" << std::endl;
desired_info.DebugPrint(error_output);
error_output << "Sound file isn't in desired format: "+filename << std::endl;
ov_clear(&oggFile);
return false;
}
//allocate space
unsigned int size = info.samples*info.channels*info.bytespersample;
sound_buffer = new char[size];
int bitstream;
int endian = 0; //0 for Little-Endian, 1 for Big-Endian
int wordsize = 2; //again, assuming ogg is always 16-bits
int issigned = 1; //use signed data
int bytes = 1;
unsigned int bufpos = 0;
while (bytes > 0)
{
bytes = ov_read(&oggFile, sound_buffer+bufpos, size-bufpos, endian, wordsize, issigned, &bitstream);
bufpos += bytes;
//cout << bytes << "...";
}
loaded = true;
//note: no need to call fclose(); ov_clear does it for us
ov_clear(&oggFile);
return true;
}
else
{
error_output << "Can't open sound file: "+filename << std::endl;
return false;
}
}
示例3: _AL_MALLOC
/* Function: al_load_ogg_vorbis_audio_stream_f
*/
ALLEGRO_AUDIO_STREAM *al_load_ogg_vorbis_audio_stream_f(ALLEGRO_FILE* file,
size_t buffer_count, unsigned int samples)
{
const int word_size = 2; /* 1 = 8bit, 2 = 16-bit. nothing else */
OggVorbis_File* vf;
vorbis_info* vi;
int channels;
long rate;
long total_samples;
long total_size;
AL_OV_DATA* extra;
ALLEGRO_AUDIO_STREAM* stream;
extra = _AL_MALLOC(sizeof(AL_OV_DATA));
if (extra == NULL) {
ALLEGRO_ERROR("Failed to allocate AL_OV_DATA struct.\n");
return NULL;
}
if (file == NULL) {
ALLEGRO_WARN("File failed to open\n");
fprintf(stderr, "File failed to open\n");
return NULL;
}
extra->file = file;
vf = _AL_MALLOC(sizeof(OggVorbis_File));
if (ov_open_callbacks(extra, vf, NULL, 0, callbacks) < 0) {
ALLEGRO_WARN("ogg: Input does not appear to be an Ogg bitstream.\n");
al_fclose(file);
return NULL;
}
extra->vf = vf;
vi = ov_info(vf, -1);
channels = vi->channels;
rate = vi->rate;
total_samples = ov_pcm_total(vf,-1);
total_size = total_samples * channels * word_size;
extra->vi = vi;
extra->bitstream = -1;
ALLEGRO_DEBUG("channels %d\n", channels);
ALLEGRO_DEBUG("word_size %d\n", word_size);
ALLEGRO_DEBUG("rate %ld\n", rate);
ALLEGRO_DEBUG("total_samples %ld\n", total_samples);
ALLEGRO_DEBUG("total_size %ld\n", total_size);
stream = al_create_audio_stream(buffer_count, samples, rate,
_al_word_size_to_depth_conf(word_size),
_al_count_to_channel_conf(channels));
if (!stream) {
free(vf);
return NULL;
}
stream->extra = extra;
extra->loop_start = 0.0;
extra->loop_end = ogg_stream_get_length(stream);
stream->feed_thread = al_create_thread(_al_kcm_feed_stream, stream);
stream->quit_feed_thread = false;
stream->feeder = ogg_stream_update;
stream->rewind_feeder = ogg_stream_rewind;
stream->seek_feeder = ogg_stream_seek;
stream->get_feeder_position = ogg_stream_get_position;
stream->get_feeder_length = ogg_stream_get_length;
stream->set_feeder_loop = ogg_stream_set_loop;
stream->unload_feeder = ogg_stream_close;
al_start_thread(stream->feed_thread);
return stream;
}
示例4: ov_test_open
void Sound::PlaySound(const boost::filesystem::path& path, bool is_ui_sound/* = false*/)
{
if (!GetOptionsDB().Get<bool>("UI.sound.enabled") || (is_ui_sound && UISoundsTemporarilyDisabled()))
return;
std::string filename = path.string();
ALuint current_buffer;
ALenum source_state;
ALsizei ogg_freq;
FILE *file = 0;
int m_i;
bool found_buffer = true;
bool found_source = false;
#ifdef FREEORION_WIN32
ov_callbacks callbacks = {
(size_t (*)(void *, size_t, size_t, void *)) fread,
(int (*)(void *, ogg_int64_t, int)) _fseek64_wrap,
(int (*)(void *)) fclose,
(long (*)(void *)) ftell
};
#endif
if (alcGetCurrentContext() != 0)
{
/* First check if the sound data of the file we want to play is already buffered somewhere */
std::map<std::string, ALuint>::iterator it = m_buffers.find(filename);
if (it != m_buffers.end())
current_buffer = it->second;
else {
if ((file = fopen(filename.c_str(), "rb")) != 0) // make sure we CAN open it
{
OggVorbis_File ogg_file;
vorbis_info *vorbis_info;
ALenum ogg_format;
#ifdef FREEORION_WIN32
if (!(ov_test_callbacks(file, &ogg_file, 0, 0, callbacks))) // check if it's a proper ogg
#else
if (!(ov_test(file, &ogg_file, 0, 0))) // check if it's a proper ogg
#endif
{
ov_test_open(&ogg_file); // it is, now fully open the file
/* now we need to take some info we will need later */
vorbis_info = ov_info(&ogg_file, -1);
if (vorbis_info->channels == 1)
ogg_format = AL_FORMAT_MONO16;
else
ogg_format = AL_FORMAT_STEREO16;
ogg_freq = vorbis_info->rate;
ogg_int64_t byte_size = ov_pcm_total(&ogg_file, -1) * vorbis_info->channels * 2;
if (byte_size <= 1024 * 1024 * 1024) {
/* fill up the buffers and queue them up for the first time */
ALuint sound_handle;
alGenBuffers(1, &sound_handle);
int loop = 0;
RefillBuffer(&ogg_file, ogg_format, ogg_freq, sound_handle, byte_size, loop);
current_buffer = sound_handle;
m_buffers.insert(std::make_pair(filename, sound_handle));
}
else
{
ErrorLogger() << "PlaySound: unable to open file " << filename.c_str() << " too big to buffer. Aborting\n";
}
ov_clear(&ogg_file);
}
else
{
ErrorLogger() << "PlaySound: unable to open file " << filename.c_str() << " possibly not a .ogg vorbis file. Aborting\n";
}
}
}
if (found_buffer) {
/* Now that we have the buffer, we need to find a source to send it to */
for (m_i = 1; m_i < NUM_SOURCES; ++m_i) { // as we're playing sounds we start at 1. 0 is reserved for music
alGetSourcei(m_sources[m_i],AL_SOURCE_STATE,&source_state);
if ((source_state != AL_PLAYING) && (source_state != AL_PAUSED)) {
found_source = true;
alSourcei(m_sources[m_i], AL_BUFFER, current_buffer);
alSourcePlay(m_sources[m_i]);
break; // so that the sound won't block all the sources
}
}
if (!found_source)
ErrorLogger() << "PlaySound: Could not find aviable source - playback aborted\n";
}
source_state = alGetError();
if (source_state != AL_NONE)
ErrorLogger() << "PlaySound: OpenAL ERROR: " << alGetString(source_state);
/* it's important to check for errors, as some functions won't work properly if
* they're called when there is a unchecked previous error. */
}
}
示例5: return
int64 OggVorbisReader::FrameCount(void)
{
return(ov_pcm_total(&ovfile, -1));
}
示例6: eprintf
// Just create the Quicktime objects since this routine is also called
// for reopening.
int FileVorbis::open_file(int rd, int wr)
{
int result = 0;
this->rd = rd;
this->wr = wr;
//printf("FileVorbis::open_file 1\n");
if(rd)
{
//printf("FileVorbis::open_file 1\n");
if(!(fd = fopen(asset->path, "rb")))
{
eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
result = 1;
}
else
{
//printf("FileVorbis::open_file 2 %p %p\n", fd, vf);
if(ov_open(fd, &vf, NULL, 0) < 0)
{
eprintf("Invalid bitstream in %s\n", asset->path);
result = 1;
}
else
{
//printf("FileVorbis::open_file 1\n");
vorbis_info *vi = ov_info(&vf, -1);
asset->channels = vi->channels;
if(!asset->sample_rate)
asset->sample_rate = vi->rate;
//printf("FileVorbis::open_file 1\n");
asset->audio_length = ov_pcm_total(&vf,-1);
//printf("FileVorbis::open_file 1\n");
asset->audio_data = 1;
// printf("FileVorbis::open_file 1 %d %d %d\n",
// asset->channels,
// asset->sample_rate,
// asset->audio_length);
}
}
}
if(wr)
{
if(!(fd = fopen(asset->path, "wb")))
{
eprintf("Error while opening \"%s\" for writing. \n%m\n", asset->path);
result = 1;
}
else
{
vorbis_info_init(&vi);
if(!asset->vorbis_vbr)
result = vorbis_encode_init(&vi,
asset->channels,
asset->sample_rate,
asset->vorbis_max_bitrate,
asset->vorbis_bitrate,
asset->vorbis_min_bitrate);
else
{
result = vorbis_encode_setup_managed(&vi,
asset->channels,
asset->sample_rate,
asset->vorbis_max_bitrate,
asset->vorbis_bitrate,
asset->vorbis_min_bitrate);
result |= vorbis_encode_ctl(&vi, OV_ECTL_RATEMANAGE_AVG, NULL);
result |= vorbis_encode_setup_init(&vi);
}
if(!result)
{
vorbis_analysis_init(&vd, &vi);
vorbis_block_init(&vd, &vb);
vorbis_comment_init(&vc);
srand(time(NULL));
ogg_stream_init(&os, rand());
ogg_packet header;
ogg_packet header_comm;
ogg_packet header_code;
vorbis_analysis_headerout(&vd,
&vc,
&header,
&header_comm,
&header_code);
ogg_stream_packetin(&os,
&header);
ogg_stream_packetin(&os,
&header_comm);
ogg_stream_packetin(&os,
&header_code);
while(1)
{
int result = ogg_stream_flush(&os, &og);
if(result == 0) break;
//.........这里部分代码省略.........
示例7: ov_open_callbacks
bool OggResourceLoader::ParseOgg(char* oggStream, size_t bufferLength, shared_ptr<ResHandle> handle)
{
shared_ptr<SoundResourceExtraData> extra = static_pointer_cast<SoundResourceExtraData>(handle->GetExtra());
OggVorbis_File vf;
ov_callbacks oggCallbacks;
// create the vorbis memory object
OggMemoryFile* vorbisMemoryFile = CB_NEW OggMemoryFile;
vorbisMemoryFile->dataSize = bufferLength;
vorbisMemoryFile->dataPtr = (unsigned char*)oggStream;
vorbisMemoryFile->dataRead = 0;
// set up ogg callbacks
oggCallbacks.read_func = VorbisRead;
oggCallbacks.close_func = VorbisClose;
oggCallbacks.seek_func = VorbisSeek;
oggCallbacks.tell_func = VorbisTell;
int ov_ret = ov_open_callbacks(vorbisMemoryFile, &vf, nullptr, 0, oggCallbacks);
CB_ASSERT(ov_ret >= 0);
vorbis_info* vi = ov_info(&vf, -1);
ZeroMemory(&(extra->m_WavFormatEx), sizeof(extra->m_WavFormatEx));
// set up the extra info
extra->m_WavFormatEx.cbSize = sizeof(extra->m_WavFormatEx);
extra->m_WavFormatEx.nChannels = vi->channels;
extra->m_WavFormatEx.wBitsPerSample = 16;
extra->m_WavFormatEx.nSamplesPerSec = vi->rate;
extra->m_WavFormatEx.nAvgBytesPerSec = extra->m_WavFormatEx.nSamplesPerSec * extra->m_WavFormatEx.nChannels * 2;
extra->m_WavFormatEx.nBlockAlign = extra->m_WavFormatEx.nChannels * 2;
extra->m_WavFormatEx.wFormatTag = 1;
DWORD size = 4096 * 16;
DWORD pos = 0;
int sec = 0;
int ret = 1;
DWORD bytes = (DWORD)ov_pcm_total(&vf, -1);
bytes *= 2 * vi->channels;
if (handle->Size() != bytes)
{
CB_ASSERT(0 && L"The Ogg size does not match the memory buffer size");
ov_clear(&vf);
CB_SAFE_DELETE(vorbisMemoryFile);
return false;
}
// read in the bytes
while (ret && pos < bytes)
{
ret = ov_read(&vf, handle->WritableBuffer() + pos, size, 0, 2, 1, &sec);
pos += ret;
if (bytes - pos < size)
{
size = bytes - pos;
}
}
extra->m_LengthMilliseconds = (int)(1000.0f * ov_time_total(&vf, -1));
ov_clear(&vf);
CB_SAFE_DELETE(vorbisMemoryFile);
return true;
}
示例8: main
int main(){
OggVorbis_File ov;
int i,ret;
ogg_int64_t pcmlength;
double timelength;
char *bigassbuffer;
int dummy;
int hs=0;
#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
_setmode( _fileno( stdin ), _O_BINARY );
#endif
/* open the file/pipe on stdin */
if(ov_open_callbacks(stdin,&ov,NULL,-1,OV_CALLBACKS_NOCLOSE)<0){
fprintf(stderr,"Could not open input as an OggVorbis file.\n\n");
exit(1);
}
#if 0 /*enable this code to test seeking with halfrate decode */
if(ov_halfrate(&ov,1)){
fprintf(stderr,"Sorry; unable to set half-rate decode.\n\n");
exit(1);
}else
hs=1;
#endif
if(ov_seekable(&ov)){
/* to simplify our own lives, we want to assume the whole file is
stereo. Verify this to avoid potentially mystifying users
(pissing them off is OK, just don't confuse them) */
for(i=0;i<ov.links;i++){
vorbis_info *vi=ov_info(&ov,i);
if(vi->channels!=2){
fprintf(stderr,"Sorry; right now seeking_test can only use Vorbis files\n"
"that are entirely stereo.\n\n");
exit(1);
}
}
/* because we want to do sample-level verification that the seek
does what it claimed, decode the entire file into memory */
pcmlength=ov_pcm_total(&ov,-1);
timelength=ov_time_total(&ov,-1);
bigassbuffer=malloc((pcmlength>>hs)*2); /* w00t */
i=0;
while(i<(pcmlength>>hs)*2){
int ret=ov_read(&ov,bigassbuffer+i,((pcmlength>>hs)*2)-i,1,1,1,&dummy);
if(ret<0){
fprintf(stderr,"Error reading file.\n");
exit(1);
}
if(ret){
i+=ret;
}else{
pcmlength=(i/2)<<hs;
}
fprintf(stderr,"\rloading.... [%ld left] ",
(long)((pcmlength>>hs)*2-i));
}
{
ogg_int64_t length=ov.end;
fprintf(stderr,"\rtesting raw seeking to random places in %ld bytes....\n",
(long)length);
for(i=0;i<1000;i++){
ogg_int64_t val=(double)rand()/RAND_MAX*length;
fprintf(stderr,"\r\t%d [raw position %ld]... ",i,(long)val);
ret=ov_raw_seek(&ov,val);
if(ret<0){
fprintf(stderr,"seek failed: %d\n",ret);
exit(1);
}
_verify(&ov,val,-1,-1.,pcmlength,bigassbuffer);
}
}
fprintf(stderr,"\r");
{
fprintf(stderr,"testing pcm page seeking to random places in %ld samples....\n",
(long)pcmlength);
for(i=0;i<1000;i++){
ogg_int64_t val= i==0?(ogg_int64_t)0:(double)rand()/RAND_MAX*pcmlength;
fprintf(stderr,"\r\t%d [pcm position %ld]... ",i,(long)val);
ret=ov_pcm_seek_page(&ov,val);
if(ret<0){
fprintf(stderr,"seek failed: %d\n",ret);
exit(1);
}
_verify(&ov,-1,val,-1.,pcmlength,bigassbuffer);
}
}
//.........这里部分代码省略.........
示例9: ogg_decoder
int ogg_decoder(const char *ogg_file, const char *pcm_file)
{
OggVorbis_File vf;
int eof = 0;
int current_section;
int tm1, tm2;
FILE *fin, *fout;
//open the ogg file
fin = fopen(ogg_file, "rb");
if (fin == NULL)
{
libc_printf("open the ogg file: %s failed!\n", ogg_file);
return -1;
}
//create the pcm file
#if 0
fout = fopen(pcm_file, "wb");
if (fout == NULL)
{
libc_printf("open the ogg file: %s failed!\n", pcm_file);
return -1;
}
#endif
if (ov_open(fin, &vf, NULL, 0) < 0)
{
libc_printf("Input does not appear to be an Ogg bitstream.\n");
return -1;
}
/* Throw the comments plus a few lines about the bitstream we're
decoding */
{
char **ptr = ov_comment(&vf, -1)->user_comments;
vorbis_info *vi = ov_info(&vf, -1);
while (*ptr)
{
libc_printf("%s\n", *ptr);
++ptr;
}
libc_printf("\nBitstream is %d channel, %ldHz\n", vi->channels, vi->rate);
libc_printf("\nDecoded length: %ld samples\n",
(long)ov_pcm_total(&vf, -1));
libc_printf("Encoded by: %s\n\n", ov_comment(&vf, -1)->vendor);
}
tm1 = osal_get_tick();
while (!eof)
{
long ret = ov_read(&vf, pcmout, sizeof(pcmout), ¤t_section);
if (ret == 0)
{
libc_printf("file end!\n");
/* EOF */
eof = 1;
}
else if (ret < 0)
{
libc_printf("error!!!\n");
/* error in the stream. Not a problem, just reporting it in
case we (the app) cares. In this case, we don't. */
}
else
{
/* we don't bother dealing with sample rate changes, etc, but
you'll have to*/
//fwrite(pcmout, ret, 1, fout);
}
}
/* cleanup */
ov_clear(&vf);
tm2 = osal_get_tick();
libc_printf("decoding time: %dms [%d ~ %d]\n", tm2 - tm1, tm1, tm2);
//fclose(fout);
libc_printf("Done.\n");
return(0);
}
示例10: getUrlString
SoundSource::OpenResult SoundSourceOggVorbis::tryOpen(
OpenMode /*mode*/,
const OpenParams& /*config*/) {
m_pFile = std::make_unique<QFile>(getLocalFileName());
if (!m_pFile->open(QFile::ReadOnly)) {
kLogger.warning()
<< "Failed to open file for"
<< getUrlString();
return OpenResult::Failed;
}
const int initDecoderResult = ov_open_callbacks(m_pFile.get(), &m_vf, nullptr, 0, s_callbacks);
switch (initDecoderResult) {
case 0:
// success -> continue
break;
case OV_ENOTVORBIS:
case OV_EVERSION:
kLogger.warning()
<< "Unsupported format in"
<< getUrlString();
return OpenResult::Aborted;
default:
kLogger.warning()
<< "Failed to initialize decoder for"
<< getUrlString();
return OpenResult::Failed;
}
if (!ov_seekable(&m_vf)) {
kLogger.warning()
<< "Stream in"
<< getUrlString()
<< "is not seekable";
return OpenResult::Aborted;
}
// lookup the ogg's channels and sample rate
const vorbis_info* vi = ov_info(&m_vf, kCurrentBitstreamLink);
if (!vi) {
kLogger.warning()
<< "Failed to read stream info from"
<< getUrlString();
return OpenResult::Failed;
}
setChannelCount(vi->channels);
setSampleRate(vi->rate);
if (0 < vi->bitrate_nominal) {
initBitrateOnce(vi->bitrate_nominal / 1000);
} else {
if ((0 < vi->bitrate_lower) && (vi->bitrate_lower == vi->bitrate_upper)) {
initBitrateOnce(vi->bitrate_lower / 1000);
}
}
ogg_int64_t pcmTotal = ov_pcm_total(&m_vf, kEntireBitstreamLink);
if (0 <= pcmTotal) {
initFrameIndexRangeOnce(IndexRange::forward(0, pcmTotal));
} else {
kLogger.warning()
<< "Failed to read read total length of"
<< getUrlString();
return OpenResult::Failed;
}
return OpenResult::Succeeded;
}
示例11: rewind
bool SoundManager::loadOgg(FILE* file, ALuint buffer)
{
OggVorbis_File ogg_file;
vorbis_info* info;
ALenum format;
int result;
int section;
unsigned int size = 0;
rewind(file);
if ((result = ov_open(file, &ogg_file, NULL, 0)) < 0) {
fclose(file);
qDebug() << "Failed to open ogg file.";
return false;
}
info = ov_info(&ogg_file, -1);
if (info->channels == 1)
format = AL_FORMAT_MONO16;
else
format = AL_FORMAT_STEREO16;
// size = #samples * #channels * 2 (for 16 bit).
unsigned int data_size = ov_pcm_total(&ogg_file, -1) * info->channels * 2;
char* data = new char[data_size];
while (size < data_size) {
result = ov_read(&ogg_file, data + size, data_size - size, 0, 2, 1, §ion);
if (result > 0) {
size += result;
}
else if (result < 0) {
delete data;
qDebug() << "Failed to read ogg file; file is missing data.";
return false;
}
else {
break;
}
}
if (size == 0) {
delete data;
qDebug() << "Filed to read ogg file; unable to read any data.";
return false;
}
alBufferData(buffer, format, data, data_size, info->rate);
ALenum error = alGetError();
if (error != AL_NO_ERROR) {
reportOpenALError(error);
}
delete data;
ov_clear(&ogg_file);
// ov_clear actually closes the file pointer as well.
file = 0;
return true;
}
示例12: init_vgmstream_ogg_vorbis_callbacks
//.........这里部分代码省略.........
strstr(comment->user_comments[i],"um3.stream.looppoint.start=")==
comment->user_comments[i] ||
strstr(comment->user_comments[i],"LOOP_BEGIN=")==
comment->user_comments[i] ||
strstr(comment->user_comments[i],"LoopStart=")==
comment->user_comments[i]
) {
loop_start=atol(strrchr(comment->user_comments[i],'=')+1);
if (loop_start >= 0)
loop_flag=1;
}
else if (strstr(comment->user_comments[i],"LOOPLENGTH=")==
comment->user_comments[i]) {
loop_length=atol(strrchr(comment->user_comments[i],'=')+1);
loop_length_found=1;
}
else if (strstr(comment->user_comments[i],"title=-lps")==
comment->user_comments[i]) {
loop_start=atol(comment->user_comments[i]+10);
if (loop_start >= 0)
loop_flag=1;
}
else if (strstr(comment->user_comments[i],"album=-lpe")==
comment->user_comments[i]) {
loop_end=atol(comment->user_comments[i]+10);
loop_flag=1;
loop_end_found=1;
}
else if (strstr(comment->user_comments[i],"LoopEnd=")==
comment->user_comments[i]) {
if(loop_flag) {
loop_length=atol(strrchr(comment->user_comments[i],'=')+1)-loop_start;
loop_length_found=1;
}
}
else if (strstr(comment->user_comments[i],"LOOP_END=")==
comment->user_comments[i]) {
if(loop_flag) {
loop_length=atol(strrchr(comment->user_comments[i],'=')+1)-loop_start;
loop_length_found=1;
}
}
else if (strstr(comment->user_comments[i],"lp=")==
comment->user_comments[i]) {
sscanf(strrchr(comment->user_comments[i],'=')+1,"%d,%d",
&loop_start,&loop_end);
loop_flag=1;
loop_end_found=1;
}
}
}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(info->channels,loop_flag);
if (!vgmstream) goto fail;
/* store our fun extra datas */
vgmstream->codec_data = data;
/* fill in the vital statistics */
vgmstream->channels = info->channels;
vgmstream->sample_rate = info->rate;
/* let's play the whole file */
vgmstream->num_samples = ov_pcm_total(ovf,-1);
if (loop_flag) {
vgmstream->loop_start_sample = loop_start;
if (loop_length_found)
vgmstream->loop_end_sample = loop_start+loop_length;
else if (loop_end_found)
vgmstream->loop_end_sample = loop_end;
else
vgmstream->loop_end_sample = vgmstream->num_samples;
vgmstream->loop_flag = loop_flag;
if (vgmstream->loop_end_sample > vgmstream->num_samples)
vgmstream->loop_end_sample = vgmstream->num_samples;
}
vgmstream->coding_type = coding_ogg_vorbis;
vgmstream->layout_type = vgm_inf->layout_type;
vgmstream->meta_type = vgm_inf->meta_type;
return vgmstream;
/* clean up anything we may have opened */
fail:
if (data) {
if (inited_ovf)
ov_clear(&data->ogg_vorbis_file);
if (data->ov_streamfile.streamfile)
close_streamfile(data->ov_streamfile.streamfile);
free(data);
}
if (vgmstream) {
vgmstream->codec_data = NULL;
close_vgmstream(vgmstream);
}
return NULL;
}
示例13: ov_raw_seek
int ov_raw_seek(OggVorbis_File *vf,long pos){
ogg_stream_state work_os;
if(vf->ready_state<OPENED)return(OV_EINVAL);
if(!vf->seekable)
return(OV_ENOSEEK); /* don't dump machine if we can't seek */
if(pos<0 || pos>vf->offsets[vf->links])return(OV_EINVAL);
/* clear out decoding machine state */
vf->pcm_offset=-1;
_decode_clear(vf);
_seek_helper(vf,pos);
/* we need to make sure the pcm_offset is set, but we don't want to
advance the raw cursor past good packets just to get to the first
with a granulepos. That's not equivalent behavior to beginning
decoding as immediately after the seek position as possible.
So, a hack. We use two stream states; a local scratch state and
a the shared vf->os stream state. We use the local state to
scan, and the shared state as a buffer for later decode.
Unfortuantely, on the last page we still advance to last packet
because the granulepos on the last page is not necessarily on a
packet boundary, and we need to make sure the granpos is
correct.
*/
{
ogg_page og;
ogg_packet op;
int lastblock=0;
int accblock=0;
int thisblock=-1;
int eosflag=0;
memset(&work_os,0,sizeof(work_os));/* so that it's safe to clear
it later even if we don't
init it */
while(1){
if(vf->ready_state==STREAMSET){
/* snarf/scan a packet if we can */
int result=ogg_stream_packetout(&work_os,&op);
if(result>0){
if(vf->vi[vf->current_link].codec_setup)
thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
if(eosflag)
ogg_stream_packetout(&vf->os,NULL);
else
if(lastblock)accblock+=(lastblock+thisblock)>>2;
if(op.granulepos!=-1){
int i,link=vf->current_link;
ogg_int64_t granulepos=op.granulepos;
for(i=0;i<link;i++)
granulepos+=vf->pcmlengths[i];
vf->pcm_offset=granulepos-accblock;
break;
}
lastblock=thisblock;
continue;
}
}
if(!lastblock){
if(_get_next_page(vf,&og,-1)<0){
vf->pcm_offset=ov_pcm_total(vf,-1);
break;
}
}else{
/* huh? Bogus stream with packets but no granulepos */
vf->pcm_offset=-1;
break;
}
/* has our decoding just traversed a bitstream boundary? */
if(vf->ready_state==STREAMSET)
if(vf->current_serialno!=ogg_page_serialno(&og)){
_decode_clear(vf); /* clear out stream state */
ogg_stream_clear(&work_os);
}
if(vf->ready_state<STREAMSET){
int link;
vf->current_serialno=ogg_page_serialno(&og);
for(link=0;link<vf->links;link++)
if(vf->serialnos[link]==vf->current_serialno)break;
if(link==vf->links)goto seek_error; /* sign of a bogus stream.
error out, leave
machine uninitialized */
vf->current_link=link;
ogg_stream_init(&vf->os,vf->current_serialno);
//.........这里部分代码省略.........
示例14: ogg_load_vorbis
static void ogg_load_vorbis(const char *filename, WAVEFORMATEX *pwf, std::vector<char> *data)
{
SafePtr<FS::Stream> s = g_fs->Open(filename)->QueryStream();
ov_callbacks cb;
cb.read_func = read_func;
cb.seek_func = seek_func;
cb.close_func = NULL;
cb.tell_func = tell_func;
OggVorbis_File vf;
if( int result = ov_open_callbacks(GetRawPtr(s), &vf, NULL, 0, cb) )
{
switch( result )
{
case OV_EREAD: throw std::runtime_error("A read from media returned an error");
case OV_ENOTVORBIS: throw std::runtime_error("Bitstream does not contain any Vorbis data");
case OV_EVERSION: throw std::runtime_error("Vorbis version mismatch");
case OV_EBADHEADER: throw std::runtime_error("Invalid Vorbis bitstream header");
case OV_EFAULT: throw std::runtime_error("Internal logic fault; indicates a bug or heap/stack corruption");
}
throw std::runtime_error("unknown error opening ov stream");
}
try
{
vorbis_info *pinfo = ov_info(&vf, -1);
if( NULL == pinfo )
{
throw std::runtime_error("could not get info from ov stream");
}
pwf->wFormatTag = WAVE_FORMAT_PCM;
pwf->nChannels = pinfo->channels;
pwf->nSamplesPerSec = pinfo->rate;
pwf->nAvgBytesPerSec = pinfo->rate * pinfo->channels * 2;
pwf->nBlockAlign = pinfo->channels * 2;
pwf->wBitsPerSample = 16;
pwf->cbSize = 0;
size_t size = ov_pcm_total(&vf, -1) * pwf->nBlockAlign;
data->resize(size);
int bitstream = 0;
size_t total = 0;
while( total < size )
{
long ret = ov_read(&vf, &data->at(total), size - total, 0, 2, 1, &bitstream);
if( 0 == ret )
{
break; // eof
}
if( ret < 0 )
{
// error in stream
switch( ret )
{
case OV_HOLE:
throw std::runtime_error("garbage between pages, loss of sync followed by recapture, or a corrupt page");
case OV_EBADLINK:
throw std::runtime_error("invalid stream section or the requested link is corrupt");
case OV_EINVAL:
throw std::runtime_error("initial file headers couldn't be read or are corrupt");
}
throw std::runtime_error("unknown error in ov stream");
}
else
{
total += ret;
}
}
}
catch(...)
{
ov_clear(&vf);
throw;
}
ov_clear(&vf);
}
示例15: return
uint64 CDAFReader_Vorbis::FrameCount(void)
{
return(ov_pcm_total(&ovfile, -1));
}