decoder-ogg.c
branchtrunk
changeset 33 65ed49cbc08b
parent 25 887faebaba0a
child 34 afc13760179b
     1.1 --- a/decoder-ogg.c	Sat Oct 24 11:31:53 2009 +0800
     1.2 +++ b/decoder-ogg.c	Fri Nov 13 19:27:36 2009 +0800
     1.3 @@ -212,24 +212,28 @@
     1.4  
     1.5  // --- cOggDecoder -------------------------------------------------------------
     1.6  
     1.7 -cOggDecoder::cOggDecoder(const char *Filename)
     1.8 +cOggDecoder::cOggDecoder(const char *Filename, bool preinit)
     1.9  :cDecoder(Filename)
    1.10 -,file(Filename)
    1.11 -,info(&file)
    1.12 -{
    1.13 -  pcm=0;
    1.14 +{
    1.15 +  file=0; info=0; pcm=0;
    1.16 +  if(preinit) {
    1.17 +    file=new cOggFile(Filename);
    1.18 +    info=new cOggInfo(file);
    1.19 +  }
    1.20  }
    1.21  
    1.22  cOggDecoder::~cOggDecoder()
    1.23  {
    1.24    Clean();
    1.25 +  delete info;
    1.26 +  delete file;
    1.27  }
    1.28  
    1.29  bool cOggDecoder::Valid(void)
    1.30  {
    1.31    bool res=false;
    1.32    if(TryLock()) {
    1.33 -    if(file.Open(false)) res=true;
    1.34 +    if(file->Open(false)) res=true;
    1.35      Unlock();
    1.36      }
    1.37    return res;
    1.38 @@ -238,9 +242,9 @@
    1.39  cFileInfo *cOggDecoder::FileInfo(void)
    1.40  {
    1.41    cFileInfo *fi=0;
    1.42 -  if(file.HasInfo()) fi=&file;
    1.43 +  if(file->HasInfo()) fi=file;
    1.44    else if(TryLock()){
    1.45 -    if(file.Open()) { fi=&file; file.Close(); }
    1.46 +    if(file->Open()) { fi=file; file->Close(); }
    1.47      Unlock();
    1.48      }
    1.49    return fi;
    1.50 @@ -249,9 +253,9 @@
    1.51  cSongInfo *cOggDecoder::SongInfo(bool get)
    1.52  {
    1.53    cSongInfo *si=0;
    1.54 -  if(info.HasInfo()) si=&info;
    1.55 +  if(info->HasInfo()) si=info;
    1.56    else if(get && TryLock()) {
    1.57 -    if(info.DoScan(false)) si=&info;
    1.58 +    if(info->DoScan(false)) si=info;
    1.59      Unlock();
    1.60      }
    1.61    return si;
    1.62 @@ -261,7 +265,7 @@
    1.63  {
    1.64    if(playing) {
    1.65      pi.Index=index/1000;
    1.66 -    pi.Total=info.Total;
    1.67 +    pi.Total=info->Total;
    1.68      return π
    1.69      }
    1.70    return 0;
    1.71 @@ -278,7 +282,7 @@
    1.72  {
    1.73    playing=false;
    1.74    delete pcm; pcm=0;
    1.75 -  file.Close();
    1.76 +  file->Close();
    1.77    return false;
    1.78  }
    1.79  
    1.80 @@ -288,10 +292,10 @@
    1.81  {
    1.82    Lock(true);
    1.83    Init(); playing=true;
    1.84 -  if(file.Open() && info.DoScan(true)) {
    1.85 +  if(file->Open() && info->DoScan(true)) {
    1.86      d(printf("ogg: open rate=%d channels=%d seek=%d\n",
    1.87 -             info.SampleFreq,info.Channels,file.CanSeek()))
    1.88 -    if(info.Channels<=2) {
    1.89 +             info->SampleFreq,info->Channels,file->CanSeek()))
    1.90 +    if(info->Channels<=2) {
    1.91        Unlock();
    1.92        return true;
    1.93        }
    1.94 @@ -324,15 +328,15 @@
    1.95    Lock(); // this is released in Done()
    1.96    if(playing) {
    1.97      short framebuff[2*SF_SAMPLES];
    1.98 -    int n=file.Stream(framebuff,SF_SAMPLES);
    1.99 +    int n=file->Stream(framebuff,SF_SAMPLES);
   1.100      if(n<0) return Done(dsError);
   1.101      if(n==0) return Done(dsEof);
   1.102  
   1.103 -    pcm->samplerate=info.SampleFreq;
   1.104 -    pcm->channels=info.Channels;
   1.105 +    pcm->samplerate=info->SampleFreq;
   1.106 +    pcm->channels=info->Channels;
   1.107      n/=pcm->channels;
   1.108      pcm->length=n;
   1.109 -    index=file.IndexMs();
   1.110 +    index=file->IndexMs();
   1.111  
   1.112      short *data=framebuff;
   1.113      mad_fixed_t *sam0=pcm->samples[0], *sam1=pcm->samples[1]; 
   1.114 @@ -347,6 +351,7 @@
   1.115        for(; n>0 ; n--)
   1.116          *sam0++=(*data++) << s;
   1.117        }
   1.118 +    info->InfoHook();
   1.119      return Done(dsPlay);
   1.120      }
   1.121    return Done(dsError);
   1.122 @@ -356,15 +361,15 @@
   1.123  {
   1.124    Lock();
   1.125    bool res=false;
   1.126 -  if(playing && file.CanSeek()) {
   1.127 +  if(playing && file->CanSeek()) {
   1.128      float fsecs=(float)Seconds - bsecs;
   1.129 -    long long newpos=file.IndexMs()+(long long)(fsecs*1000.0);
   1.130 +    long long newpos=file->IndexMs()+(long long)(fsecs*1000.0);
   1.131      if(newpos<0) newpos=0;
   1.132 -    d(printf("ogg: skip: secs=%d fsecs=%f current=%lld new=%lld\n",Seconds,fsecs,file.IndexMs(),newpos))
   1.133 -
   1.134 -    newpos=file.Seek(newpos,false);
   1.135 +    d(printf("ogg: skip: secs=%d fsecs=%f current=%lld new=%lld\n",Seconds,fsecs,file->IndexMs(),newpos))
   1.136 +
   1.137 +    newpos=file->Seek(newpos,false);
   1.138      if(newpos>=0) {
   1.139 -      index=file.IndexMs();
   1.140 +      index=file->IndexMs();
   1.141  #ifdef DEBUG
   1.142        int i=index/1000;
   1.143        printf("ogg: skipping to %02d:%02d\n",i/60,i%60);