int logg_update_stream(LOGG_Stream* s) { unsigned char* data = get_audio_stream_buffer(s->audio_stream); if (!data) { if (s->current_page != s->playing_page) { int read = read_ogg_data(s); if (read < logg_bufsize) { return 0; } else { return 1; } } else { return 1; } } s->playing_page++; s->playing_page %= OGG_PAGES_TO_BUFFER; memcpy(data, s->buf[s->playing_page], logg_bufsize); free_audio_stream_buffer(s->audio_stream); return 1; }
static int logg_play_stream(LOGG_Stream* s) { int len; int i; s->current_page = 0; s->playing_page = -1; len = logg_bufsize / (s->stereo ? 2 : 1) / (s->bits / (sizeof(char)*8)); s->audio_stream = play_audio_stream(len, s->bits, s->stereo, s->freq, s->volume, s->pan); if (!s->audio_stream) { return 1; } for (i = 0; i < OGG_PAGES_TO_BUFFER; i++) { s->buf[i] = malloc(logg_bufsize); if (!s->buf[i]) { logg_destroy_stream(s); return 1; } if (read_ogg_data(s) < 0) { return 1; } } return 0; }
static SAMPLE *load_ov(OggVorbis_File *vf) { int ret; vorbis_info *info; SAMPLE *sample=NULL; ogg_int64_t num_samples; size_t offset; AL_CONST size_t max_read=4096; size_t buffer_size; size_t buffer_inc=65536; int stereo; info=ov_info(vf,-1); if (!info) { TRACE("ov_info failed\n"); goto load_error_file; } stereo=(info->channels>1); num_samples=ov_pcm_total(vf,-1); TRACE("ov_pcm_total says %lld samples\n",num_samples); if (num_samples<0) { TRACE("ov_pcm_total failed: %lld - not fatal\n",(ogg_int64_t)num_samples); } TRACE( "ov_load: create sample: %d bits, %d channels (%s), %ld Hz, %lld samples\n", 16,info->channels,stereo?"stereo":"mono",info->rate,num_samples ); sample=create_sample(16,stereo,info->rate,num_samples>0?num_samples:1); if (!sample) { TRACE( "Failed creating a sample (%d channels, %ld Hz, %u samples", info->channels,info->rate,(size_t)num_samples ); goto load_error_file; } /* decode the encoded data */ for (offset=0,buffer_size=0;;) { if (num_samples<0) { /* If we could not determine the size of the sample, allocate as we go */ if (offset+max_read>buffer_size) { TRACE("Buffer too small: adding %u bytes\n",buffer_inc); buffer_size+=buffer_inc; buffer_inc+=buffer_inc/2; #ifdef FORTIFY { /* Fortify won't have seen the allocation which was done in Allegro */ void *tmpdata=malloc(buffer_size); memcpy(tmpdata,sample->data,offset); destroy_sample(sample); sample=create_sample(16,stereo,info->rate,buffer_size/(stereo?2:1)/2); memcpy(sample->data,tmpdata,offset); free(tmpdata); } #else sample->data=realloc(sample->data,buffer_size); #endif } if (!sample->data) { TRACE("Failed allocating sample data\n"); goto load_error_sample; } } ret=read_ogg_data(vf,((char*)sample->data)+offset,max_read,2,0,NULL); if (ret==0) break; if (ret<0) { TRACE("read_ogg_data failed: %d\n",ret); alogg_error_code=ret; goto load_error_sample; } offset+=ret; } TRACE("ov_load: clearing\n"); ov_clear(vf); /* ov_clear will have closed the PACKFILE, so no need to do it there */ /* If we couldn't allocate up front, update the size related fields */ if (num_samples<0) { ASSERT(offset%(stereo?2:1)/2==0); sample->len=offset/(stereo?2:1)/2; TRACE("Final size: %lu samples\n",sample->len); sample->loop_end=sample->len; lock_sample(sample); } return sample; load_error_sample: destroy_sample(sample); load_error_file: ov_clear(vf); return NULL; }