/* Guess */ long ov_serialnumber(OggVorbis_File *vf,int i){ if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1)); if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1)); if(i<0){ return(vf->current_serialno); }else{ return(vf->serialnos[i]); } }
int main(){ OggVorbis_File ov; int i; #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); #endif /* open the file/pipe on stdin */ if(ov_open(stdin,&ov,NULL,-1)<0){ printf("Could not open input as an OggVorbis file.\n\n"); exit(1); } /* print details about each logical bitstream in the input */ if(ov_seekable(&ov)){ printf("Input bitstream contained %ld logical bitstream section(s).\n", ov_streams(&ov)); printf("Total bitstream samples: %ld\n\n", (long)ov_pcm_total(&ov,-1)); printf("Total bitstream playing time: %ld seconds\n\n", (long)ov_time_total(&ov,-1)); }else{ printf("Standard input was not seekable.\n" "First logical bitstream information:\n\n"); } for(i=0;i<ov_streams(&ov);i++){ vorbis_info *vi=ov_info(&ov,i); printf("\tlogical bitstream section %d information:\n",i+1); printf("\t\t%ldHz %d channels bitrate %ldkbps serial number=%ld\n", vi->rate,vi->channels,ov_bitrate(&ov,i)/1000, ov_serialnumber(&ov,i)); printf("\t\theader length: %ld bytes\n",(long) (ov.dataoffsets[i]-ov.offsets[i])); printf("\t\tcompressed length: %ld bytes\n",(long)(ov_raw_total(&ov,i))); printf("\t\tplay time: %lds\n",(long)ov_time_total(&ov,i)); } ov_clear(&ov); return 0; }
/* open ogg/vorbis file */ static void oggread_open(t_oggread *x, t_symbol *filename) { int i; x->x_stream = 0; /* first close previous file */ if(x->x_fd > 0) { ov_clear(&x->x_ov); post("oggread~: previous file closed"); } /* open file for reading */ if((x->x_file = sys_fopen(filename->s_name, "rb")) <= 0) { post("oggread~: could not open file \"%s\"", filename->s_name); x->x_eos = 1; x->x_fd = -1; } else { x->x_stream = 0; x->x_eos = 0; x->x_fd = 1; x->x_outreadposition = 0; x->x_outwriteposition = 0; x->x_outunread = 0; post("oggread~: file \"%s\" opened", filename->s_name); outlet_float( x->x_out_position, 0); /* try to open as ogg vorbis file */ if(ov_open(x->x_file, &x->x_ov, NULL, -1) < 0) { /* an error occured (no ogg vorbis file ?) */ post("oggread~: error: could not open \"%s\" as an OggVorbis file", filename->s_name); ov_clear(&x->x_ov); post("oggread~: file closed due to error"); x->x_fd=-1; x->x_eos=1; return; } /* print details about each logical bitstream in the input */ if(ov_seekable(&x->x_ov)) { post("oggread~: input bitstream contained %ld logical bitstream section(s)", ov_streams(&x->x_ov)); post("oggread~: total bitstream playing time: %ld seconds", (long)ov_time_total(&x->x_ov,-1)); post("oggread~: encoded by: %s\n",ov_comment(&x->x_ov,-1)->vendor); } else { post("oggread~: file \"%s\" was not seekable\n" "oggread~: first logical bitstream information:", filename->s_name); } for(i = 0; i < ov_streams(&x->x_ov); i++) { x->x_vi = ov_info(&x->x_ov,i); post("\tlogical bitstream section %d information:",i+1); post("\t\t%ldHz %d channels bitrate %ldkbps serial number=%ld", x->x_vi->rate,x->x_vi->channels,ov_bitrate(&x->x_ov,i)/1000, ov_serialnumber(&x->x_ov,i)); post("\t\theader length: %ld bytes",(long) (x->x_ov.dataoffsets[i] - x->x_ov.offsets[i])); post("\t\tcompressed length: %ld bytes",(long)(ov_raw_total(&x->x_ov,i))); post("\t\tplay time: %ld seconds\n",(long)ov_time_total(&x->x_ov,i)); } } }
static BseErrorType dh_vorbis_open (GslDataHandle *dhandle, GslDataHandleSetup *setup) { VorbisHandle *vhandle = (VorbisHandle*) dhandle; VFile *vfile; vorbis_info *vi; GslLong n, i; gint err; vfile = g_new0 (VFile, 1); vfile->rfile = gsl_rfile_open (vhandle->dhandle.name); if (!vfile->rfile) { g_free (vfile); return gsl_error_from_errno (errno, BSE_ERROR_FILE_OPEN_FAILED); } vfile->byte_length = gsl_rfile_length (vfile->rfile); if (vhandle->rfile_add_zoffset) { vfile->byte_offset = gsl_hfile_zoffset (vfile->rfile->hfile) + 1; vfile->byte_offset += vhandle->rfile_byte_offset; vfile->byte_offset = MIN (vfile->byte_offset, vfile->byte_length); vfile->byte_length -= vfile->byte_offset; } else { vfile->byte_offset = MIN (vhandle->rfile_byte_offset, vfile->byte_length); vfile->byte_length -= vfile->byte_offset; } if (vhandle->rfile_byte_length > 0) vfile->byte_length = MIN (vfile->byte_length, vhandle->rfile_byte_length); err = ov_open_callbacks (vfile, &vhandle->ofile, NULL, 0, vfile_ov_callbacks); if (err < 0) { if (0) g_printerr ("failed to open ogg at offset %d (real offset=%lld) (add-zoffset=%d): %s\n", vhandle->rfile_byte_offset, vfile->byte_offset, vhandle->rfile_add_zoffset, bse_error_blurb (ov_errno_to_error (err, BSE_ERROR_FILE_OPEN_FAILED))); vfile_close (vfile); return ov_errno_to_error (err, BSE_ERROR_FILE_OPEN_FAILED); } n = ov_streams (&vhandle->ofile); if (n > vhandle->bitstream) { vhandle->bitstream_serialno = ov_serialnumber (&vhandle->ofile, vhandle->bitstream); vhandle->n_bitstreams = n; if (0) g_printerr ("Ogg/Vorbis: opening ogg: bitstream=%d/%d serialno=%d\n", vhandle->bitstream, vhandle->n_bitstreams, vhandle->bitstream_serialno); } else { ov_clear (&vhandle->ofile); /* closes file */ return BSE_ERROR_NO_DATA; /* requested bitstream not available */ } vhandle->soffset = 0; for (i = 0; i < vhandle->bitstream; i++) vhandle->soffset += ov_pcm_total (&vhandle->ofile, i); n = ov_pcm_total (&vhandle->ofile, vhandle->bitstream); vi = ov_info (&vhandle->ofile, vhandle->bitstream); if (n > 0 && vi && vi->channels && ov_pcm_seek (&vhandle->ofile, vhandle->soffset) >= 0) { setup->n_channels = vi->channels; setup->n_values = n * setup->n_channels; } else { ov_clear (&vhandle->ofile); /* closes file */ return BSE_ERROR_NO_DATA; } vhandle->max_block_size = vorbis_info_blocksize (vi, 0); n = vorbis_info_blocksize (vi, 1); vhandle->max_block_size = MAX (vhandle->max_block_size, n); vhandle->pcm_pos = 0; vhandle->pcm_length = 0; setup->bit_depth = 24; setup->mix_freq = vi->rate; setup->needs_cache = TRUE; setup->xinfos = bse_xinfos_add_float (setup->xinfos, "osc-freq", vhandle->osc_freq); return BSE_ERROR_NONE; }
static int OGG_open(Sound_Sample *sample, const char *ext) { int rc; double total_time; OggVorbis_File *vf; vorbis_info *info; Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; vf = (OggVorbis_File *) malloc(sizeof (OggVorbis_File)); BAIL_IF_MACRO(vf == NULL, ERR_OUT_OF_MEMORY, 0); rc = ov_open_callbacks(internal->rw, vf, NULL, 0, RWops_ogg_callbacks); if (rc != 0) { #if (defined DEBUG_CHATTER) SNDDBG(("OGG: can't grok data. reason: [%s].\n", ogg_error(rc))); #endif free(vf); BAIL_MACRO("OGG: Not valid Ogg Vorbis data.", 0); } /* if */ info = ov_info(vf, -1); if (info == NULL) { ov_clear(vf); free(vf); BAIL_MACRO("OGG: failed to retrieve bitstream info", 0); } /* if */ output_ogg_comments(vf); SNDDBG(("OGG: bitstream version == (%d).\n", info->version)); SNDDBG(("OGG: bitstream channels == (%d).\n", info->channels)); SNDDBG(("OGG: bitstream sampling rate == (%ld).\n", info->rate)); SNDDBG(("OGG: seekable == {%s}.\n", ov_seekable(vf) ? "TRUE" : "FALSE")); SNDDBG(("OGG: number of logical bitstreams == (%ld).\n", ov_streams(vf))); SNDDBG(("OGG: serial number == (%ld).\n", ov_serialnumber(vf, -1))); SNDDBG(("OGG: total seconds of sample == (%f).\n", ov_time_total(vf, -1))); internal->decoder_private = vf; sample->flags = SOUND_SAMPLEFLAG_CANSEEK; sample->actual.rate = (uint32_t) info->rate; sample->actual.channels = (uint8_t) info->channels; total_time = ov_time_total(vf, -1); if (OV_EINVAL == total_time) internal->total_time = -1; else internal->total_time = (int32_t)(total_time); /* * Since we might have more than one logical bitstream in the OGG file, * and these bitstreams may be in different formats, we might be * converting two or three times: once in vorbisfile, once again in * SDL_sound, and perhaps a third time to get it to the sound device's * format. That's wickedly inefficient. * * To combat this a little, if the user specified a desired format, we * claim that to be the "actual" format of the collection of logical * bitstreams. This means that VorbisFile will do a conversion as * necessary, and SDL_sound will not. If the user didn't specify a * desired format, then we pretend the "actual" format is something that * OGG files are apparently commonly encoded in. */ sample->actual.format = (sample->desired.format == 0) ? AUDIO_S16SYS : sample->desired.format; return(1); } /* OGG_open */