⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oggtremor_motorola.cpp

📁 OggPlay for Symbian 是symbian上的一个媒体播放程序的源码。它支持ogg,wav等等多媒体格式。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 *  Copyright (c) 2003 L. H. Wilden.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

// Platform settings
#include <OggOs.h>

// This file is for non PLUGIN_SYSTEM and MOTOROLA only
#if !defined(PLUGIN_SYSTEM) && defined(MOTOROLA)

#ifdef __VC32__
#pragma warning( disable : 4244 ) // conversion from __int64 to unsigned int: Possible loss of data
#endif

#include "OggLog.h"
#include "OggTremor.h"
#include "TremorDecoder.h"
#ifdef MP3_SUPPORT
#include "MadDecoder.h"
#endif

#include <barsread.h>
#include <eikbtpan.h>
#include <eikcmbut.h>
#include <eiklabel.h>
#include <eikmover.h>
#include <eikbtgpc.h>
#include <eikon.hrh>
#include <eikon.rsg>
#include <charconv.h>
#include <OggPlay.rsg>

#include "ivorbiscodec.h"
#include "ivorbisfile.h"

////////////////////////////////////////////////////////////////
//
// COggPlayback
//
////////////////////////////////////////////////////////////////

// Construction/destruction methods
////////////////////////////////////////////////////////////////

// COggPlayback
////////////////////////////////////////////////////////////////

COggPlayback::COggPlayback( COggMsgEnv* anEnv, MPlaybackObserver* anObserver )
: CAbsPlayback(anObserver)
, iEnv(anEnv)
, iOggSampleRateConverter( NULL )
, iStream( NULL )
, iDelete( NULL )
, iMaxVolume( 0 )
, iSentIdx( 0 )
, iPlayWhenOpened( EFalse )
, iFile( NULL )
, iEof( EFalse )
, iDecoding( EFalse )
, iDecoder( NULL )
{
   for ( TInt j = 0; j < KMotoBuffers; j++ )
   {
      iBufferFlag[ j ] = EBufferFree;
   }

   iObserverAO = NULL;
}

// ~COggPlayback
////////////////////////////////////////////////////////////////

COggPlayback::~COggPlayback( void )
{
   __ASSERT_ALWAYS((iState == EClosed) || (iState == EStopped), User::Panic(_L("~COggPlayback"), 0));
	
   delete iDecoder; 
   delete iStream;
   delete iDelete;
 
   iBuffer.ResetAndDestroy();
   delete iObserverAO;
   delete iOggSampleRateConverter;

   iFs.Close();
}

// ConstructL
////////////////////////////////////////////////////////////////

void COggPlayback::ConstructL( void )
{
#ifdef _DEBUG
   RDebug::Print( _L("COggPlayback::ConstructL") );
#endif

   // Set up the session with the file server
   User::LeaveIfError(iFs.Connect());

   CreateStreamApi();

   iMaxVolume = 1; // This will be updated when stream is opened.

   TDes8 *buffer;
   for ( TInt i = 0; i < KMotoBuffers; i++ ) 
   {
      buffer = new (ELeave) TBuf8<KMotoBufferSize>;
      buffer->SetMax();
      CleanupStack::PushL( buffer );
      User::LeaveIfError( iBuffer.Append( buffer ) );
      CleanupStack::Pop( buffer );
   }

   iObserverAO = new (ELeave) CObserverCallback( this );
   iOggSampleRateConverter = new (ELeave) COggSampleRateConverter;
}

// Public interface methods
////////////////////////////////////////////////////////////////

// Info
////////////////////////////////////////////////////////////////

TInt COggPlayback::Info(const TDesC& aFileName, TBool silent)
{
   if (aFileName.Length()==0) return -100;

   RFile* f = new RFile;
   if ((f->Open(iFs, aFileName, EFileShareReadersOnly)) != KErrNone) 
   {
      if (!silent) 
      {
         TBuf<128> buf;
         CEikonEnv::Static()->ReadResource(buf, R_OGG_ERROR_14);
         iEnv->OggErrorMsgL(buf,aFileName);
      }

      return KErrOggFileNotFound;
   }

   MDecoder* decoder = GetDecoderL( aFileName );
   if ( decoder->OpenInfo(f, aFileName) < 0 )
   {
      decoder->Close();
	  delete decoder;

      f->Close();
      delete f;

      if (!silent)
      {
         iEnv->OggErrorMsgL(R_OGG_ERROR_20, R_OGG_ERROR_9);
      }
      return -102;
   }
   decoder->ParseTags(iTitle, iArtist, iAlbum, iGenre, iTrackNumber);

   iFileName = aFileName;
   iRate     = iDecoder->Rate();
   iChannels = iDecoder->Channels();
   iBitRate  = iDecoder->Bitrate();

   decoder->Close();
   delete decoder;

   f->Close();
   delete f;

   return KErrNone;
}

// Open
////////////////////////////////////////////////////////////////

TInt COggPlayback::Open( const TDesC& aFileName )
{
#ifdef _DEBUG
   RDebug::Print( _L("Open:%S"), &aFileName );
#endif

   TBool just_created_api = EFalse;

   if ( iStream && iDecoding )
   {
      ResetStreamApi();
   }

   if ( !iStream )
   {
      CreateStreamApi();
      just_created_api = ETrue;
   }
   
   if (iFile)
   {
      iDecoder->Close();
      iFile->Close();
	
	  delete iFile;
	  iFile = NULL;

	  delete iDecoder;
 	  iDecoder = NULL;
   }
   
   iTime = 0;
   
   if ( aFileName.Length() == 0 )
   {
      iEnv->OggErrorMsgL(R_OGG_ERROR_20,R_OGG_ERROR_8);
      return -100;
   }
   
   iFile = new(ELeave) RFile;
   if ((iFile->Open(iFs, aFileName, EFileShareReadersOnly)) != KErrNone)
   {
      delete iFile;
      iFile = NULL;

	  iEnv->OggErrorMsgL(R_OGG_ERROR_20, R_OGG_ERROR_14);
      return KErrOggFileNotFound;
   }
   
   iDecoder = GetDecoderL(aFileName);
   if(iDecoder->Open(iFile, aFileName) < 0)
   {
      iDecoder->Close();
	  iFile->Close();

      delete iFile;
      iFile = NULL;
    
      delete iDecoder;
      iDecoder = NULL;

      iEnv->OggErrorMsgL(R_OGG_ERROR_20,R_OGG_ERROR_9);
      return -102;
   }
   iDecoder->ParseTags(iTitle, iArtist, iAlbum, iGenre, iTrackNumber);

   iFileName = aFileName;
   iRate     = iDecoder->Rate();
   iChannels = iDecoder->Channels();
   iTime     = iDecoder->TimeTotal();
   iBitRate  = iDecoder->Bitrate();
   iFileSize = iDecoder->FileSize();
      
   TInt err = SetAudioCaps( iDecoder->Channels(), iDecoder->Rate() );
   if ( err == KErrNone ) 
   {
      if ( !just_created_api )
		  iState = EStopped;
   }
   else
   {
      iDecoder->Close();
      iFile->Close();
  
      delete iFile;
      iFile = NULL;

      delete iDecoder;
      iDecoder = NULL;
   }

   return err;
}

// Pause
////////////////////////////////////////////////////////////////

void COggPlayback::Pause()
{
#ifdef _DEBUG
   RDebug::Print( _L("Pause:%d"), iState );
#endif

   if (iState == EPlaying && !iPlayWhenOpened )
   {
      iState = EPaused;
   }
}

void COggPlayback::Resume()
{
  //Resume is equivalent of Play() 
  Play();
}

// Play
////////////////////////////////////////////////////////////////

void COggPlayback::Play() 
{
#ifdef _DEBUG
   RDebug::Print( _L("Play:%d"), iState );
#endif

#ifdef MDCT_FREQ_ANALYSER
  iLastFreqArrayIdx = 0;
  iLatestPlayTime = 0.0;
#endif

   TInt err;

   switch (iState)
   {
   case EStopped:  
      iEof = EFalse;
      // Intentionally fall-thru
      
   case EPaused:
      {
         iState = EPlaying;
         for ( TInt i = 0; i < KMotoBuffers; i++)
         {
            TRAP( err, SendBufferL( i ) );

            if ( iEof ) break;
         }
         if ( !iDecoding )
         {
         TRAP( err, iStream->DecodeL() );
            if ( err == KErrNone ) iDecoding = ETrue;
         }
      }
      break;
      
   case EStopped:
      if ( iStream )
      {
         iState = EPlaying;
         iPlayWhenOpened = ETrue;
         break;
      }
      // Else fall-thru

   default:
      iEnv->OggErrorMsgL(R_OGG_ERROR_20, R_OGG_ERROR_15);
#ifdef _DEBUG
      RDebug::Print(_L("Oggplay: Tremor - State not Open"));
#endif
      break;
   }
}

// Stop
////////////////////////////////////////////////////////////////

void COggPlayback::Stop()
{
#ifdef _DEBUG
   RDebug::Print( _L("Stop:%d"), iState );
#endif

   if ( (iState == EPlaying && !iPlayWhenOpened) || iState == EPaused)
   { 
      iStream->Stop();
      iState = EStopped;

      if (iFile)
      {
         iDecoder->Close();
         iFile->Close();

	     delete iFile;
	     iFile = NULL;

	     delete iDecoder;
	     iDecoder = NULL;
      }

      for (TInt i = 0; i < KMotoBuffers; i++)
      {
         iBufferFlag[i] = EBufferFree;
      }

      ClearComments();

      iTime = 0;
      iEof  = EFalse;
      
      if (iObserver)
      {
         iObserver->NotifyUpdate();
      }
   }
}

// SetPosition
////////////////////////////////////////////////////////////////

void COggPlayback::SetPosition(TInt64 aPos)
{
   if ( iStream )
   {
      if ( iDecoder ) iDecoder->Setposition(aPos);
   }
}

// Position
////////////////////////////////////////////////////////////////

TInt64 COggPlayback::Position()
{
   if ( !iStream ) return TInt64(0);
   if ( iDecoder ) return iDecoder->Position();
   return 0;
}

// Time
////////////////////////////////////////////////////////////////

TInt64 COggPlayback::Time()
{
   if ( iDecoder && iTime == 0 ) 
   {
      iTime=iDecoder->TimeTotal();
   }
   return ( iTime );
}

// Volume
////////////////////////////////////////////////////////////////

TInt COggPlayback::Volume()
{
   TInt vol = KMaxVolume;

   if (iStream)
   {
      TRAPD( err, vol = iStream->GetVolume() );
   }

   return ( vol );
}

// SetVolume
////////////////////////////////////////////////////////////////

void COggPlayback::SetVolume(TInt aVol)
{
   if (iStream)
   {
      if ( aVol > KMaxVolume )
      { 
         aVol = KMaxVolume;
      }
      else if( aVol < 0 )
      {
         aVol = 0;
      }
      
      TInt volume = (TInt) (((TReal) aVol)/KMaxVolume * iMaxVolume);
      if (volume > iMaxVolume)
      {
         volume = iMaxVolume;
      }

      TRAPD( err, iStream->SetVolume(volume) );
   }
}

// SetVolumeGain
////////////////////////////////////////////////////////////////

void COggPlayback::SetVolumeGain( TGainType aGain )
{
   iOggSampleRateConverter->SetVolumeGain(aGain);
}

// GetFrequencyBins
////////////////////////////////////////////////////////////////

#ifdef MDCT_FREQ_ANALYSER
const TInt32 * COggPlayback::GetFrequencyBins()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -