MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
musicplayer.cpp
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _audiomusicplayer_cpp
41 #define _audiomusicplayer_cpp
42 
43 #include "Audio/musicplayer.h"
44 #include "Audio/playlist.h"
45 #include "Audio/sound.h"
46 
47 #include "exception.h"
48 
49 namespace Mezzanine
50 {
51  namespace Audio
52  {
54  : MusicPlaylist(NULL),
55  CurrSong(NULL),
56  ManualStop(false),
57  Playing(false),
58  EOPRepeat(false),
59  EOPShuffle(false)
60  {
61  MusicPlaylist = new Playlist();
62  }
63 
65  {
66  delete MusicPlaylist;
67  }
68 
69  std::list<Audio::iSound*>::iterator MusicPlayer::GetIteratorToSong(iSound* Song)
70  {
71  for( std::list< Audio::iSound* >::iterator it = MusicPlaylist->begin() ; it != MusicPlaylist->end() ; ++it )
72  {
73  if(Song == (*it))
74  {
75  CurrSong = (*it);
76  return it;
77  }
78  }
79  return MusicPlaylist->end();
80  }
81 
83  {
84  ManualStop = false;
85  Playing = true;
86  if(!CurrSong)
87  {
88  if(MusicPlaylist->empty()) { MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to play a song in MusicPlayer with an empty playlist"); }
89  else CurrSong = *(MusicPlaylist->begin());
90  }
91  CurrSong->Play();
92  }
93 
95  {
96  ManualStop = true;
97  Playing = false;
98  CurrSong->Stop();
99  }
100 
102  {
103  CurrSong->Pause();
104  }
105 
107  {
108  std::list<iSound*>::iterator SongIt = this->GetIteratorToSong(CurrSong);
109  SongIt++;
110  CurrSong = (*SongIt);
111  if(Playing && !ManualStop)
112  this->Play();
113  }
114 
116  {
117  std::list<iSound*>::iterator SongIt = GetIteratorToSong(CurrSong);
118  SongIt--;
119  CurrSong = (*SongIt);
120  if(Playing && !ManualStop)
121  this->Play();
122  }
123 
125  {
126  for( std::list< Audio::iSound* >::iterator it = MusicPlaylist->begin() ; it != MusicPlaylist->end() ; ++it )
127  {
128  if(Song == (*it))
129  {
130  CurrSong = (*it);
131  if(Playing && !ManualStop)
132  this->Play();
133  return;
134  }
135  }
136  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,"Attempting to switch to song not contained within the current Playlist.");
137  }
138 
140  {
141  return CurrSong->IsPlaying();
142  }
143 
145  {
146  return CurrSong->IsStopped();
147  }
148 
150  {
151  return CurrSong->IsPaused();
152  }
153 
155  {
156  return MusicPlaylist->ContainsSound(Song);
157  }
158 
160  {
161  EOPRepeat = Repeat;
162  }
163 
165  {
166  return EOPRepeat;
167  }
168 
170  {
171  EOPShuffle = Shuffle;
172  }
173 
175  {
176  return EOPShuffle;
177  }
178 
180  {
181  return MusicPlaylist;
182  }
183 
185  {
186  if(0!=CurrSong)
187  {
188  std::list<Audio::iSound*>::iterator SongIt = this->GetIteratorToSong(CurrSong);
189  std::list<Audio::iSound*>::iterator NextSong = SongIt;
190  NextSong++;
191  if(CurrSong->IsStopped() && Playing)
192  {
193  if(NextSong == MusicPlaylist->end())
194  {
195  if(EOPRepeat)
196  {
197  if(EOPShuffle) MusicPlaylist->Shuffle();
198  CurrSong = *(MusicPlaylist->begin());
199  Play();
200  }else{
201  Stop();
202  }
203  }else{
204  Next();
205  }
206  }
207  }
208  }
209  }//Audio
210 }//Mezzanine
211 
212 #endif