masengine.cpp
来自「Amarok是一款在LINUX或其他类UNIX操作系统中运行的音频播放器软件。 」· C++ 代码 · 共 592 行 · 第 1/2 页
CPP
592 行
/*************************************************************************** masengine.cpp - MAS audio interface -------------------begin : 2004-07-20copyright : (C) 2004-05 by Roland Gigleremail : rolandg@web.dewhat : interface to the Media Application Server (MAS)***************************************************************************//*************************************************************************** * * * 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. * * * ***************************************************************************/#define DEBUG_PREFIX "MAS-Engine"#include "debug.h"#include "enginebase.h"#include "engineobserver.h"#include <assert.h>#include <math.h> //setVolume(), timerEvent()#include <string>//#include <vector>#include <qtimer.h>#include <qfile.h>#include <kapplication.h>#include <kconfig.h>#include <kfileitem.h>#include <kgenericfactory.h>#include <klocale.h>#include <kmessagebox.h>#include <kmimetype.h>#include <kstandarddirs.h>#include <kurl.h>#include "masengine.h"//#define DB_CUTOFF -40.0#define BUFFER_TIME_MS 300#define POSTOUT_TIME_MS 100//#define QUERY_MIX_VOLUME 0//#define QUERY_MIX_EPSILON 5AMAROK_EXPORT_PLUGIN( MasEngine )MasEngine::MasEngine() : Engine::Base() , m_inited (false)// , m_scopeId( 0 )// , m_xfadeFadeout( false )// , m_xfadeValue( 0.0 )// , m_xfadeCurrent( "invalue2" ) , m_lastKnownPosition( 0 ) , m_state( Engine::Empty ) , m_pPlayingTimer( new QTimer( this ) ){ DEBUG_FUNC_INFO // NOT SUPPORTED //addPluginProperty( "HasConfigure", "true" ); //addPluginProperty( "HasCrossfading", "true" ); //addPluginProperty( "HasEqualizer", "true" );}MasEngine::~MasEngine(){ DEBUG_BLOCK if ( m_inited ) stop(); m_pPlayingTimer->stop(); killTimers();}bool MasEngine::init(){ DEBUG_BLOCK if (!masinit() ) { KMessageBox::error( 0, i18n("<h3>Amarok could not initialise MAS.</h3>" "<p>Check for a running mas daemon.</p>") ); error() << " connecting to MAS daemon failed. Aborting. " << endl; debug() << " Please restart amarok." << endl; debug() << k_funcinfo << " returns false !" << endl; return false; } m_inited=true; // we connected to MAS connect ( m_pPlayingTimer, SIGNAL( timeout() ), this, SLOT( playingTimeout() ) ); emit statusText( "MAS Engine inited :-)"); return true;}////////////////////////////////////////////////////////////////////////////////// PUBLIC METHODS////////////////////////////////////////////////////////////////////////////////bool MasEngine::canDecode( const KURL &url ) const{ DEBUG_BLOCK QStringList list; bool playable; debug() << " Param: url: " << url << endl; //debug() << " url.protocol() >" << url.protocol() <<"<"<< endl; if (url.protocol() == "http" ) return false; // TODO determine list of supported MimeTypes/Extensions from MAS list += QString("audio/x-mp3"); KFileItem fileItem( KFileItem::Unknown, KFileItem::Unknown, url, false ); //false = determineMimeType straight away KMimeType::Ptr mimetype = fileItem.determineMimeType(); debug() << "mimetype: " << mimetype->name().latin1() << endl; playable = list.contains( mimetype->name().latin1() ); if ( !playable ) warning() << "Mimetype is not playable by MAS (" << url << ")" << endl; return playable;} // canDecodebool MasEngine::load( const KURL& url, bool stream ){ DEBUG_BLOCK struct mas_package pkg; char pbuf[10240]; int pos = 0; m_isStream = stream; debug() << " m_url: " << m_url << endl; debug() << " Param: stream: " << stream << endl; debug() << " Param: url " << url << endl; if ( !url.isLocalFile() ) { // for now debug() << " only local files are supported (for now)" << endl; return false; } if ( !canDecode(url) ) { debug() << " cannot decode!" << endl; return false; } if ( m_url == url ) { return true; } else { stop(); } m_url = url; /* send fresh data to MAS */; masc_setup_package( &pkg, pbuf, sizeof pbuf, MASC_PACKAGE_STATIC ); masc_pushk_int16( &pkg, (char*)"pos", pos ); //masc_push_string( &pkg, (char *)m_url.path().latin1() ); QCString cs= QFile::encodeName( m_url.path()); const char *pcs = cs; masc_push_string( &pkg, (char *)pcs); masc_finalize_package( &pkg ); mas_set( m_mp1a_source_device, (char*)"playlist", &pkg ); masc_strike_package( &pkg ); mas_dev_show_state (m_mp1a_source_device); mas_source_flush( m_codec ); m_lastKnownPosition = 0; m_state = Engine::Idle; return true;} // loadbool MasEngine::play( unsigned int offset){ DEBUG_BLOCK struct mas_package pkg; char pbuf[10240]; debug() << " param: offset " << offset << endl; if ( m_state != Engine::Playing ) { /* change the track */ masc_setup_package( &pkg, pbuf, sizeof pbuf, MASC_PACKAGE_STATIC ); masc_pushk_int16( &pkg, (char*)"pos", 1 ); masc_finalize_package( &pkg ); mas_set( m_mp1a_source_device, (char*)"ctrack", &pkg ); masc_finalize_package( &pkg ); mas_source_flush( m_codec ); mas_source_play( m_mp1a_source_device ); mas_source_play_on_mark( m_sbuf ); debug() << "mas_source_play()" << endl; } m_pPlayingTimer->start(MAS_TIMER, false); m_state = Engine::Playing; emit stateChanged( Engine::Playing ); return true;} // playuintMasEngine::length() const{ DEBUG_BLOCK char pbuf[128]; struct mas_package pkg; struct mas_package nugget; float trklen; masc_setup_package( &pkg, pbuf, sizeof pbuf, MASC_PACKAGE_STATIC ); masc_pushk_int16( &pkg, (char *)"pos", 1 ); masc_finalize_package( &pkg ); mas_get( m_mp1a_source_device, (char *)"trklen", &pkg, &nugget ); masc_strike_package( &pkg ); masc_pullk_float( &nugget, (char *)"trklen", &trklen ); masc_strike_package( &nugget ); debug() << "trklen: " << trklen << endl; return uint(trklen*1000);} // position/* * return current position in milli seconds*/uint MasEngine::position() const{ return m_lastKnownPosition;} // position//////////////////////////////////////////////////////////////////////void MasEngine::playingTimeout() //SLOT{ //DEBUG_BLOCK m_lastKnownPosition += MAS_TIMER; // ask MAS if it's still playing struct mas_package nugget; int16 pos; mas_get( m_mp1a_source_device, (char *)"ctrack", NULL, &nugget ); masc_pullk_int16( &nugget, (char *)"pos", &pos ); masc_strike_package( &nugget ); if ( pos == 0 ) { m_pPlayingTimer->stop(); m_state = Engine::Idle; emit trackEnded(); }} // playingTimeout //SLOTvoid MasEngine::stop(){ DEBUG_BLOCK //switch xfade channels/* m_xfadeCurrent = ( m_xfadeCurrent == "invalue1" ) ? "invalue2" : "invalue1"; if ( m_xfadeValue == 0.0 ) m_xfadeValue = 1.0;*/ mas_source_stop( m_mp1a_source_device ); mas_source_stop( m_sbuf ); debug() << "performed: mas_source_stop()" << endl; m_pPlayingTimer->stop(); m_state = Engine::Empty; m_lastKnownPosition = 0; //emit stateChanged( m_state );}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?