Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members  

dec_ogg.cpp

00001 /* MuSE - Multiple Streaming Engine
00002  * Copyright (C) 2000-2002 Denis Roio aka jaromil <jaromil@dyne.org>
00003  *
00004  * This source code is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Public License as published 
00006  * by the Free Software Foundation; either version 2 of the License,
00007  * or (at your option) any later version.
00008  *
00009  * This source code is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00012  * Please refer to the GNU Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Public License along with
00015  * this source code; if not, write to:
00016  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  *
00018  * "$Id: dec_ogg.cpp,v 1.1 2004/02/13 16:58:16 jaromil Exp $"
00019  *
00020  */
00021 
00022 
00023 #include <dec_ogg.h>
00024 #include <jutils.h>
00025 #include <config.h>
00026 
00027 #ifdef HAVE_VORBIS
00028 
00029 /* ----- OggVorbis input channel ----- */
00030 
00031 MuseDecOgg::MuseDecOgg() : MuseDec() {
00032   func("MuseDecOgg::MuseDecOgg()");
00033 
00034   old_section = current_section = -1;
00035 
00036   oggfile = NULL;
00037 
00038   strncpy(name,"Ogg",4);
00039 }
00040 
00041 MuseDecOgg::~MuseDecOgg() {
00042   func("MuseDecOgg::~MuseDecOgg()");
00043   ov_clear(&vf);
00044 }
00045 
00046 IN_DATATYPE *MuseDecOgg::get_audio() {
00047   int res;
00048 
00049   old_section = current_section;  
00050   
00051   if(seekable) {
00052     /* we check the position here 'cause ov_pcm_tell returns the NEXT frame */
00053     framepos = ov_pcm_tell(&vf);
00054     fps = samplerate;
00055   }
00056 
00057   res = 
00058     ov_read(&vf, _inbuf, IN_CHUNK, 0, 2, 1, &current_section);
00059   
00060   if(res<0) {
00061     warning("MuseDecOgg:_get_audio() : bitstream error");
00062     err = true;
00063     return(NULL);
00064   }
00065 
00066   if((res==0)||(old_section != current_section && old_section != -1)) {
00067     // with this we check when entering into a new logical bitstream
00068     eos = true;
00069     return(NULL);
00070   }
00071 
00072   frames = res>>1;
00073 
00074   return((IN_DATATYPE *)_inbuf);
00075 }
00076 
00077 int MuseDecOgg::load(char *file) {
00078   int res = 0;
00079 
00080   oggfile = fopen(file,"rb");
00081   if(oggfile==NULL) {
00082     error("MuseDecOgg::open(%s) : can't open file",file);
00083     return(res);
00084   }
00085 
00086   if(ov_open(oggfile,&vf,NULL,0) < 0) {
00087     error("MuseDecOgg::open(%s): not a valid OggVorbis audio stream",file);
00088     fclose(oggfile);
00089     return(res);
00090   }
00091 
00092   vc = ov_comment(&vf, -1);
00093   vi = ov_info(&vf, -1);
00094 
00095   samplerate = vi->rate;
00096   channels = vi->channels;
00097   seekable = ov_seekable(&vf);
00098 
00099   /* pcm position */
00100   framepos = 0;
00101 
00102   /* check if seekable */
00103   res = (ov_seekable(&vf)) ? 1 : 2;
00104   seekable = (res>1) ? false : true;
00105 
00106   if(seekable)
00107     frametot = ov_pcm_total(&vf,-1);
00108 
00109   return(res);
00110 }
00111 
00112 bool MuseDecOgg::seek(float pos) {
00113 
00114   if(pos==0.0) {
00115     if(ov_pcm_seek(&vf,1)!=0) {
00116       error("MuseDecOgg::pos : error in ov_pcm_seek(%p,1)",&vf);
00117       return(false);
00118     }
00119 
00120   } else {
00121 
00122     if(ov_pcm_seek_page(&vf,(ogg_int64_t)((double)frametot * pos))!=0) {
00123       error("MuseDecOgg::play : error in ov_pcm_seek_page(%p,%u)",
00124             (ogg_int64_t)((double)frametot * pos));
00125       return(false);
00126     }
00127   }
00128 
00129   return(true);
00130 }
00131 
00132 #endif /* HAVE VORBIS */

Generated on Sat Apr 17 17:38:48 2004 for MuSE by doxygen1.3