defuze.me  Client
audioplayer.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 "audioplayer.hpp"
00012 #include "queueable.hpp"
00013 #include "queuetrack.hpp"
00014 #include "audiomixer.hpp"
00015 #include "audiooutputdevice.hpp"
00016 #include "status.hpp"
00017 #include <unistd.h>
00018 
00019 using namespace Player;
00020 
00021 const int CrossfadeWidth = 5;
00022 
00023 AudioPlayer::AudioPlayer(QString playerName) : decoder(0), currentTrack(0)
00024 {
00025     _playerName = playerName;
00026     _name = playerName;
00027     _type = Audio::AudioPlayerType;
00028     widget = new AudioPlayerWidget();
00029     _updatePosition = true;
00030     previousPosition = 0;
00031     timer = new QTimer(this);
00032     open(QIODevice::ReadOnly);
00033     connect(timer, SIGNAL(timeout()), this, SLOT(updatePosition()));
00034 }
00035 
00036 AudioPlayer::AudioPlayer(QString playerName, AudioPlayerWidget *widget, Audio::AudioDecoder *decoder, Audio::AudioOutputDevice *outputDevice)
00037     : widget(widget), decoder(decoder), currentTrack(0), outputDevice(outputDevice)
00038 {
00039     _playerName = playerName;
00040     _updatePosition = true;
00041     previousPosition = 0;
00042     timer = new QTimer(this);
00043     decoder->setPlayer(this);
00044     open(QIODevice::ReadOnly);
00045     connect(timer, SIGNAL(timeout()), this, SLOT(updatePosition()));
00046     connect(widget, SIGNAL(stopButton_clicked()), this, SLOT(stop()));
00047     connect(widget, SIGNAL(positionSlider_released(int)), SLOT(seek(int)));
00048 //  connect(decoder, SIGNAL(endOfTrack()), this, SLOT(unloadTrack()));
00049 }
00050 
00051 
00052 AudioPlayer::~AudioPlayer()
00053 {
00054     delete widget;
00055 }
00056 
00057 int     AudioPlayer::cueAt()
00058 {
00059     if (currentTrack)
00060         return currentTrack->getDuration() - CrossfadeWidth;
00061     else
00062         return -1;
00063 }
00064 
00065 void    AudioPlayer::init()
00066 {
00067 //  connect(widget, SIGNAL(playButton_clicked()), this, SLOT(play()));
00068 //  connect(widget, SIGNAL(stopButton_clicked()), this, SLOT(stop()));
00069 }
00070 
00071 bool AudioPlayer::loadTrack(Queue::Queueable *elem)
00072 {
00073     if (elem && elem->isTrack())
00074     {
00075         try
00076         {
00077             PlayerWidgetState   state = widget->state();
00078             if (currentTrack == elem->toQueueTrack()->getTrack())
00079                 return true;
00080             currentTrack = elem->toQueueTrack()->getTrack();
00081             widget->setState(Stopped);
00082             decoder->openFile(currentTrack->getAbsolutePath());
00083             outputDevice->setFormat(decoder->getFormat());
00084             decoder->start();
00085             widget->setTrackName(currentTrack->getTitle());
00086             widget->setTrackInfo(currentTrack);
00087             widget->setDuration(currentTrack->getDuration());
00088             widget->setState(state);
00089             return true;
00090         }
00091         catch (Exception &e)
00092         {
00093             Notification::Status::gMessage(e.msg(), Notification::ERR);
00094             currentTrack->remove();
00095             return false;
00096         }
00097     }
00098     else
00099     {
00100         unloadTrack();
00101         widget->setState(NoFile);
00102         return false;
00103     }
00104 }
00105 
00106 void    AudioPlayer::unloadTrack()
00107 {
00108     stop();
00109     widget->setState(Disabled);
00110     decoder->closeFile();
00111     widget->setTrackInfo();
00112     widget->setTimeLabel("--:--");
00113     currentTrack = 0;
00114 }
00115 
00116 void    AudioPlayer::play()
00117 {
00118     if (decoder->state() != Audio::AudioDecoder::NoFile)
00119     {
00120         _updatePosition = true;
00121         if (widget->state() == Playing)
00122         {
00123             timer->stop();
00124             widget->setState(Paused);
00125             outputDevice->pause();
00126             emit paused();
00127         }
00128         else
00129         {
00130             timer->start(100);
00131             widget->setState(Playing);
00132             outputDevice->start();
00133             emit played();
00134         }
00135     }
00136 }
00137 
00138 void    AudioPlayer::updatePosition()
00139 {
00140     int position = decoder->getPlayingPosition();
00141     widget->setPosition(position);
00142 
00143     if (previousPosition < cueAt() && position >= cueAt())
00144         emit almostFinished();
00145     previousPosition = position;
00146     if (position >= currentTrack->getDuration() - 1)
00147     {
00148         qDebug() << "emit: finish";
00149         emit finished();
00150         stop();
00151     }
00152 }
00153 
00154 void AudioPlayer::stop()
00155 {
00156     timer->stop();
00157     widget->setState(Stopped);
00158     decoder->setPlayingPosition(0);
00159     outputDevice->pause();
00160     _updatePosition = false;
00161     emit stopped();
00162 }
00163 
00164 void    AudioPlayer::seek(int position)
00165 {
00166     (void)position;
00167     PlayerWidgetState currentState = widget->state();
00168     widget->setState(Paused);
00169 //  decoder->seek(position);
00170     updatePosition();
00171     if (currentState == Playing)
00172         play();
00173 }
00174 
00175 qint64  AudioPlayer::writeData(const char *data, qint64 maxlen)
00176 {
00177     Q_UNUSED(data);
00178     Q_UNUSED(maxlen);
00179     return 0;
00180 }
00181 
00182 qint64  AudioPlayer::readData(char *data, qint64 maxlen)
00183 {
00184     if (decoder)
00185     {
00186 //      qDebug() << "ReadData Player";
00187             quint64 i = decoder->readDecodedFrame(data, maxlen);
00188             return i;
00189     }
00190     return 0;
00191 }
00192 
00193 bool    AudioPlayer::isPlaying() const
00194 {
00195     if (widget->state() == Playing)
00196         return true;
00197     return false;
00198 }
00199 
00200 Audio::AudioIO  *AudioPlayer::getAudioInput(const Audio::AudioIO*)
00201 {
00202     return 0;
00203 }
00204 
00205 const Audio::AudioIO    *AudioPlayer::getOutputDevice(const Audio::AudioIO*) const
00206 {
00207     return outputDevice;
00208 }
00209 
00210 qint64  AudioPlayer::readData(char *data, qint64 maxlen, Audio::AudioIO *from)
00211 {
00212     if (from == outputDevice)
00213         return readData(data, maxlen);
00214     return 0;
00215 }
00216