Spinning Topp Logo BlackTopp Studios
inc
musicplayer.cpp
1 // © Copyright 2010 - 2016 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  CurrTrack(NULL),
56  ManualStop(false),
57  Playing(false),
58  EOPRepeat(false),
59  EOPShuffle(false)
60  { this->MusicPlaylist = new Playlist(); }
61 
63  { delete this->MusicPlaylist; }
64 
66  {
67  for( TrackIterator TrackIt = this->MusicPlaylist->begin() ; TrackIt != this->MusicPlaylist->end() ; ++TrackIt )
68  {
69  if(Track == (*TrackIt)) {
70  this->CurrTrack = (*TrackIt);
71  return TrackIt;
72  }
73  }
74  return this->MusicPlaylist->end();
75  }
76 
78  {
79  this->ManualStop = false;
80  this->Playing = true;
81  if( !this->CurrTrack ) {
82  if( this->MusicPlaylist->empty() ) {
83  MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Attempting to play a song in MusicPlayer with an empty playlist");
84  }else{
85  this->CurrTrack = *(this->MusicPlaylist->begin());
86  }
87  }
88  this->CurrTrack->Play();
89  }
90 
92  {
93  this->ManualStop = true;
94  this->Playing = false;
95  this->CurrTrack->Stop();
96  }
97 
99  {
100  this->CurrTrack->Pause();
101  }
102 
104  {
105  TrackIterator TrackIt = this->GetIteratorToTrack(this->CurrTrack);
106  TrackIt++;
107  this->CurrTrack = (*TrackIt);
108  if( this->Playing && !this->ManualStop )
109  this->Play();
110  }
111 
113  {
114  TrackIterator TrackIt = this->GetIteratorToTrack(this->CurrTrack);
115  TrackIt--;
116  this->CurrTrack = (*TrackIt);
117  if( this->Playing && !this->ManualStop )
118  this->Play();
119  }
120 
122  {
123  for( TrackIterator it = this->MusicPlaylist->begin() ; it != this->MusicPlaylist->end() ; ++it )
124  {
125  if(Track == (*it)) {
126  this->CurrTrack = (*it);
127  if( this->Playing && !this->ManualStop )
128  this->Play();
129  return;
130  }
131  }
132  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,"Attempting to switch to song not contained within the current Playlist.");
133  }
134 
136  { return this->CurrTrack->IsPlaying(); }
137 
139  { return this->CurrTrack->IsStopped(); }
140 
142  { return this->CurrTrack->IsPaused(); }
143 
145  { return this->MusicPlaylist->ContainsSound(Track); }
146 
148  { this->EOPRepeat = Repeat; }
149 
151  { return this->EOPRepeat; }
152 
154  { this->EOPShuffle = Shuffle; }
155 
157  { return this->EOPShuffle; }
158 
160  { return this->MusicPlaylist; }
161 
163  {
164  if( 0 != this->CurrTrack ) {
165  TrackIterator TrackIt = this->GetIteratorToTrack(this->CurrTrack);
166  TrackIterator NextTrack = TrackIt;
167  NextTrack++;
168  if( this->CurrTrack->IsStopped() && this->Playing) {
169  if(NextTrack == this->MusicPlaylist->end()) {
170  if( this->EOPRepeat ) {
171  if( this->EOPShuffle ) {
172  this->MusicPlaylist->Shuffle();
173  }
174  this->CurrTrack = *(this->MusicPlaylist->begin());
175  this->Play();
176  }else{
177  this->Stop();
178  }
179  }else{
180  this->Next();
181  }
182  }
183  }
184  }
185  }//Audio
186 }//Mezzanine
187 
188 #endif
virtual void Pause()=0
Pauses playback of the sound at it's current position in the stream.
TrackIterator GetIteratorToTrack(iSound *Track)
Gets an iterator to the iSound instance in the current playlist.
Definition: musicplayer.cpp:65
virtual Boole IsPlaying() const =0
Gets whether or not the sound is currently playing.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Boole IsStopped() const
Gets whether or not the current selection is stopped.
virtual void Stop()=0
Stops playback of the sound and resets it's position back to the start.
Thrown when the requested identity could not be found.
Definition: exception.h:94
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
~MusicPlayer()
Class destructor.
Definition: musicplayer.cpp:62
Playlist * GetPlaylist() const
Gets the playlist in use by this music player.
void Next()
Advances to the next selection on the playlist.
virtual Boole Play()=0
Plays the sound with it's current configuration.
Boole EOPRepeat
Stores whether or not the player will loop back to the start when it finishes playing all tracks in t...
Definition: musicplayer.h:78
Audio::iSound * CurrTrack
A pointer to the currently selected track.
Definition: musicplayer.h:69
This implements the exception hiearchy for Mezzanine.
This is an interface class for a non-spacialized sound.
Definition: sound.h:60
Boole ContainsSound(iSound *TheSound)
Checks the playlist to see if it contains a sound.
Definition: playlist.cpp:72
void SetPlaylistRepeat(Boole Repeat)
Sets whether the playlist should return to the start after it reaches the end of the list...
void Play()
Plays the current selection.
Definition: musicplayer.cpp:77
This class is a list of sounds with common playlist features.
Definition: playlist.h:58
void SetPlaylistShuffle(Boole Shuffle)
Sets whether the playlist should shuffle it's contents after it reaches the end of the list...
Audio::Playlist * MusicPlaylist
A pointer to the active playlist used by this player.
Definition: musicplayer.h:66
The declaration of the Playlist class.
MusicPlayer()
Class constructor.
Definition: musicplayer.cpp:53
Thrown when the available information should have worked but failed for unknown reasons.
Definition: exception.h:113
void Pause()
Pauses the current selection.
Definition: musicplayer.cpp:98
void Stop()
Stops the current selection.
Definition: musicplayer.cpp:91
Boole GetPlaylistRepeat() const
Gets wether playlist repeat is enabled.
Boole IsPaused() const
Gets whether or not the current selection is paused.
void Shuffle()
Randomly shuffles the content in the Playlist.
Definition: playlist.cpp:65
Boole GetPlaylistShuffle() const
Gets wether playlist shuffle is enabled.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void Update()
Called on by the AudioManager to perform all music player responsibilities.
virtual Boole IsPaused() const =0
Gets whether or not the sound is currently paused.
void SwitchToTrack(iSound *Track)
Sets the specified track as the current track.
virtual Boole IsStopped() const =0
Gets whether or not the sound is currently stopped.
Boole Playing
Stores whether or not this player is currently playing.
Definition: musicplayer.h:75
std::list< Audio::iSound * >::iterator TrackIterator
Convenience type for track iterators from playlists.
Definition: musicplayer.h:62
Boole ContainsSong(iSound *Track) const
Checks the set playlist to see if it contains a track.
The interface for the Musicplayer class.
void Previous()
Moves back to the previous selection on the playlist.
A base type that provides container features for different tracks.
Definition: track.h:65
Boole EOPShuffle
Stores whether or not the player will shuffle the playlist when it finishes playing all the tracks in...
Definition: musicplayer.h:81
Boole IsPlaying() const
Gets whether or not the current selection is playing.
Boole ManualStop
Stores whether or not the current track has been manually stopped.
Definition: musicplayer.h:72