Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsmusicmanager.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #ifdef __HAVE_MMSMEDIA__
00034 #include "mmscore/mmsmusicmanager.h"
00035 #include <stdlib.h>
00036 
00037 MMSMusicManager::MMSMusicManager() :
00038     onNextSong(NULL),
00039     onPrevSong(NULL),
00040     cont(false),
00041     repeat(false),
00042     shuffle(false) {
00043     /* register myself to the music interface */
00044     MMSMusicInterface interface;
00045     interface.setManager(this);
00046     this->player.onPlaybackFinished.connect(sigc::mem_fun(this, &MMSMusicManager::next));
00047 }
00048 
00049 MMSMusicManager::~MMSMusicManager() {
00050     if(this->player.isPlaying())
00051         this->player.stop();
00052 }
00053 
00054 void MMSMusicManager::init(PLAYLIST list, int offset) {
00055     this->mutex.lock();
00056     this->playlist = list;
00057     this->offset = offset;
00058     this->alreadyPlayed.clear();
00059     for(unsigned int i = 0; i < playlist.size(); i++)
00060         this->alreadyPlayed.push_back(false);
00061     this->mutex.unlock();
00062 
00063     DEBUGMSG("MMSMusicManager", "got playlist size: %d offset: %d", list.size(), offset);
00064 }
00065 
00066 void MMSMusicManager::stopAll() {
00067     this->mutex.lock();
00068     if(player.isPlaying())
00069         player.stop();
00070     this->cont=false;
00071     this->mutex.unlock();
00072 }
00073 
00074 void MMSMusicManager::next() {
00075     this->mutex.lock();
00076     if(this->shuffle && (this->playlist.size() > 2)) {
00077         int newOffset;
00078         do {
00079             newOffset = int((double(rand())/RAND_MAX) * (this->playlist.size() - 1));
00080         }
00081         while(this->alreadyPlayed.at(newOffset));
00082         this->offset = newOffset;
00083     }
00084     else {
00085         this->offset++;
00086         if(this->offset>=(int)this->playlist.size()) {
00087             if(!this->repeat) return;
00088             this->offset=0;
00089         }
00090     }
00091     string file = this->playlist.at(this->offset);
00092 
00093     if(player.isPlaying())
00094         player.stop();
00095     player.startPlaying(file, false);
00096     this->alreadyPlayed.at(this->offset) = true;
00097     if(this->onNextSong)
00098         this->onNextSong->emit(this->offset);
00099 
00100     this->mutex.unlock();
00101 
00102 }
00103 
00104 void MMSMusicManager::prev() {
00105     this->mutex.lock();
00106     if(player.isPlaying())
00107         player.stop();
00108     if(this->shuffle && (this->playlist.size() > 2)) {
00109         int newOffset;
00110         do {
00111             newOffset = int((double(rand())/RAND_MAX) * (this->playlist.size() - 1));
00112         }
00113         while(this->alreadyPlayed.at(newOffset));
00114         this->offset = newOffset;
00115     }
00116     else {
00117         this->offset--;
00118         if(this->offset < 0)
00119           this->offset = this->playlist.size() - 1;
00120     }
00121     string file = this->playlist.at(this->offset);
00122     player.startPlaying(file,false);
00123     this->alreadyPlayed.at(this->offset) = true;
00124     if(this->onPrevSong)
00125         this->onPrevSong->emit(this->offset);
00126     this->mutex.unlock();
00127 }
00128 
00129 void MMSMusicManager::play() {
00130     if(this->cont) {
00131         player.play();
00132     }
00133     else {
00134         if(this->playlist.size() > (size_t)this->offset) {
00135             string file = this->playlist.at(this->offset);
00136             if(player.isPlaying()) player.stop();
00137             player.startPlaying(file, cont);
00138             this->alreadyPlayed.at(this->offset) = true;
00139         }
00140     }
00141 }
00142 
00143 void MMSMusicManager::pause() {
00144     this->mutex.lock();
00145     player.pause();
00146     cont=true;
00147     this->mutex.unlock();
00148 }
00149 
00150 bool MMSMusicManager::hasPlaylist() {
00151     this->mutex.lock();
00152     if(this->playlist.size()>0) {
00153         this->mutex.unlock();
00154         return true;
00155     }
00156     this->mutex.unlock();
00157     return false;
00158 }
00159 
00160 PLAYLIST MMSMusicManager::getPlaylist() {
00161     return this->playlist;
00162 }
00163 
00164 int MMSMusicManager::getPlaylistOffset() {
00165     return this->offset;
00166 
00167 }
00168 
00169 bool MMSMusicManager::isPlaying() {
00170     return player.isPlaying();
00171 }
00172 
00173 bool MMSMusicManager::isPaused() {
00174     return player.isPaused();
00175 }
00176 
00177 bool MMSMusicManager::getTimes(int *pos, int *length) {
00178     return player.getTimes(pos, length);
00179 }
00180 
00181 void MMSMusicManager::setOnNextSong(sigc::signal<void, int> *onNextSong) {
00182     this->onNextSong = onNextSong;
00183 }
00184 
00185 void MMSMusicManager::setOnPrevSong(sigc::signal<void, int> *onPrevSong) {
00186     this->onPrevSong = onPrevSong;
00187 }
00188 
00189 void MMSMusicManager::setRepeat(bool repeat) {
00190     this->repeat = repeat;
00191 }
00192 
00193 void MMSMusicManager::setShuffle(bool shuffle) {
00194     this->shuffle = shuffle;
00195 }
00196 #endif /* __HAVE_MMSMEDIA__ */

Generated by doxygen