defuze.me  Client
mainplayer.cpp
00001 /**************************************************************************
00002 ** defuze.me Epitech Innovative Project
00003 **
00004 ** Copyright 2010-2011
00005 **   Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard
00006 **   Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier
00007 **
00008 ** All rights reserved.
00009 **************************************************************************/
00010 
00011 #include "mainplayer.hpp"
00012 #include "queueable.hpp"
00013 #include "queuetrack.hpp"
00014 #include "audiomixer.hpp"
00015 #include "mainplayerwidget.hpp"
00016 #include "audiomixerplugin.hpp"
00017 
00018 using namespace Player;
00019 
00020 MainPlayer::MainPlayer(QString playerName) : decoderA(0), decoderB(0), playQueue(0)
00021 {
00022     _playerName = playerName;
00023     widget = new MainPlayerWidget(this);
00024     uiModule = Gui::ModuleFactory::create(playerName, QPoint(1, 0), widget);
00025     uiModule->setSizePolicy(QSizePolicy::Maximum);
00026     uiModule->submitForDisplay();
00027     playingPlayer = 0;
00028     isCrossfading = false;
00029 }
00030 
00031 MainPlayer::~MainPlayer()
00032 {
00033     delete widget;
00034 }
00035 
00036 void    MainPlayer::init()
00037 {
00038     connect(widget, SIGNAL(nextButton_clicked()), SLOT(crossfadeNext()));
00039     connect(cores->audio(), SIGNAL(crossfadeFinished()), SLOT(crossfadeFinished()));
00040 
00041     decoderA = cores->audio()->newAudioDecoder(_playerName);
00042 //  outputDeviceA = cores->audio()->newAudioDevice(cores->audio()->availableDevices(QAudio::AudioOutput).last());
00043     outputDeviceA = cores->audio()->newAudioDevice();
00044     playerA = new AudioPlayer("Player A", playerWidgetA, decoderA, outputDeviceA);
00045 
00046     decoderB = cores->audio()->newAudioDecoder("Player B");
00047 //  outputDeviceB = cores->audio()->newAudioDevice(cores->audio()->availableDevices(QAudio::AudioOutput).last());
00048     outputDeviceB = cores->audio()->newAudioDevice();
00049     playerB = new AudioPlayer("Player B", playerWidgetB, decoderB, outputDeviceB);
00050 
00051 
00052     Audio::AudioMixer* masterMixer = cores->audio()->newAudioMixer("Master");
00053 
00054     mixerB = cores->audio()->newAudioMixer("Mixer B", playerB);
00055     mixerA = cores->audio()->newAudioMixer("Mixer A", playerA);
00056     mixerA->setOutput(masterMixer);
00057     mixerB->setOutput(masterMixer);
00058 
00059     outputDeviceA->setInput(masterMixer);
00060     outputDeviceB->setInput(masterMixer);
00061 
00062     masterMixer->addInput(mixerA);
00063     masterMixer->addInput(mixerB);
00064 
00065     Mixer::AudioMixerPlugin *m = plugins->cast<Mixer::AudioMixerPlugin>("mixer");
00066     m->refresh();
00067     playQueue = plugins->cast<Queue::PlayQueue>("queue");
00068 
00069 
00070     playerWidgetB->invertDirection();
00071 
00072     connect(playerA, SIGNAL(almostFinished()), this, SLOT(crossfadeNext()));
00073     connect(playerB, SIGNAL(almostFinished()), this, SLOT(crossfadeNext()));
00074     connect(playerA, SIGNAL(canIHasANewTrack()), this, SLOT(loadNextTrack()));
00075     connect(playerB, SIGNAL(canIHasANewTrack()), this, SLOT(loadNextTrack()));
00076     connect(playerWidgetA, SIGNAL(playButton_clicked()), this, SLOT(playA()));
00077     connect(playerWidgetB, SIGNAL(playButton_clicked()), this, SLOT(playB()));
00078 
00079     connect(playQueue, SIGNAL(addQueueElem(Queue::Queueable*)), this, SLOT(newElem(Queue::Queueable*)));
00080     connect(playQueue, SIGNAL(removeQueueElem(Queue::Queueable*)), this, SLOT(removeElem(Queue::Queueable*)));
00081 
00082     connect(playerA, SIGNAL(finished()), this, SLOT(finished()));
00083     connect(playerB, SIGNAL(finished()), this, SLOT(finished()));
00084 
00085     connect(playerA, SIGNAL(played()), SIGNAL(played()));
00086     connect(playerB, SIGNAL(played()), SIGNAL(played()));
00087     connect(playerA, SIGNAL(paused()), SIGNAL(paused()));
00088     connect(playerB, SIGNAL(paused()), SIGNAL(paused()));
00089     connect(playerA, SIGNAL(stopped()), SIGNAL(stopped()));
00090     connect(playerB, SIGNAL(stopped()), SIGNAL(stopped()));
00091     loadTracks();
00092 }
00093 
00094 bool    MainPlayer::isPlaying() const
00095 {
00096     return (playingPlayer && playingPlayer->isPlaying());
00097 }
00098 
00099 AudioPlayer*    MainPlayer::firstPlayer()
00100 {
00101     if (playingPlayer && playingPlayer == playerB)
00102         return playerB;
00103     else
00104         return playerA;
00105 }
00106 
00107 AudioPlayer*    MainPlayer::secondPlayer()
00108 {
00109     if (playingPlayer && playingPlayer == playerB)
00110         return playerA;
00111     else
00112         return playerB;
00113 }
00114 
00115 
00116 bool    MainPlayer::loadTracks()
00117 {
00118     playerA->loadTrack(playQueue->head());
00119     playerB->loadTrack(playQueue->next());
00120     playingPlayer = playerA;
00121     return true;
00122 }
00123 
00124 void    MainPlayer::removeElem(Queue::Queueable *elem)
00125 {
00126     if (elem->getPosition() == 0)
00127     {
00128         firstPlayer()->loadTrack(playQueue->head());
00129         secondPlayer()->unloadTrack();
00130         secondPlayer()->loadTrack(playQueue->next());
00131     }
00132     else if (elem->getPosition() == 1)
00133     {
00134         secondPlayer()->unloadTrack();
00135         secondPlayer()->loadTrack(playQueue->next());
00136     }
00137 }
00138 
00139 void    MainPlayer::newElem(Queue::Queueable *elem)
00140 {
00141     if (elem->getPosition() == 0)
00142     {
00143         firstPlayer()->loadTrack(playQueue->head());
00144         secondPlayer()->unloadTrack();
00145         secondPlayer()->loadTrack(playQueue->next());
00146     }
00147     else if (elem->getPosition() == 1)
00148     {
00149         secondPlayer()->loadTrack(playQueue->next());
00150         }
00151 }
00152 
00153 bool    MainPlayer::play()
00154 {
00155     if (playingPlayer->isPlaying())
00156         return false;
00157     else
00158     {
00159         if (playingPlayer == playerA)
00160             return playA();
00161         else
00162                         return playB();
00163         return true;
00164     }
00165 }
00166 
00167 void    MainPlayer::finished()
00168 {
00169     qDebug() << "finish : pop";
00170     if (!isCrossfading)
00171         playQueue->pop();
00172 }
00173 
00174 bool    MainPlayer::pause()
00175 {
00176     if (playingPlayer->isPlaying())
00177     {
00178         playingPlayer->play();
00179         return true;
00180     }
00181     else
00182         return false;
00183 }
00184 
00185 bool    MainPlayer::stop()
00186 {
00187     playingPlayer->stop();
00188     return true;
00189 }
00190 
00191 bool    MainPlayer::crossfadeNext()
00192 {
00193     bool r = false;
00194     if (playingPlayer == playerA)
00195         r = playB();
00196     else
00197         r = playA();
00198     if (r)
00199         isCrossfading = true;
00200     return r;
00201 }
00202 
00203 void    MainPlayer::crossfadeFinished()
00204 {
00205     playQueue->pop();
00206     widget->nextButton->setEnabled(true);
00207     cores->audio()->stopCrossfade();
00208     isCrossfading = false;
00209 
00210 }
00211 
00212 void    MainPlayer::setPlayers(AudioPlayerWidget *A, AudioPlayerWidget *B)
00213 {
00214     playerWidgetA = A;
00215     playerWidgetB = B;
00216 }
00217 
00218 bool    MainPlayer::playA()
00219 {
00220     if (decoderA->state() == Audio::AudioDecoder::NoFile)
00221         return false;
00222 
00223     if (playingPlayer != playerA && playerWidgetA->state() != Playing)
00224     {
00225         cores->audio()->startCrossfade(mixerB, mixerA);
00226         widget->setNextButtonIcon(widget->style()->standardIcon(QStyle::SP_MediaSkipForward));
00227         widget->nextButton->setEnabled(false);
00228         playerWidgetB->playButton->setEnabled(false);
00229         playerWidgetB->stopButton->setEnabled(false);
00230         playerWidgetB->setState(Disabled);
00231         widget->setBNext();
00232         widget->setACurrent();
00233         playingPlayer = playerA;
00234     }
00235     playerA->play();
00236     return true;
00237 }
00238 
00239 bool    MainPlayer::playB()
00240 {
00241     if (decoderB->state() == Audio::AudioDecoder::NoFile)
00242         return false;
00243 
00244     if (playingPlayer != playerB && playerWidgetB->state() != Playing)
00245     {
00246         cores->audio()->startCrossfade(mixerA, mixerB);
00247         widget->setNextButtonIcon(widget->style()->standardIcon(QStyle::SP_MediaSkipBackward));
00248         widget->nextButton->setEnabled(false);
00249         playerWidgetA->playButton->setEnabled(false);
00250         playerWidgetA->stopButton->setEnabled(false);
00251         playerWidgetA->setState(Disabled);
00252         widget->setANext();
00253         widget->setBCurrent();
00254         playingPlayer = playerB;
00255     }
00256     playerB->play();
00257     return true;
00258 }
00259