MIDISongPlayer.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <vector>
00025 #include "MIDISongPlayer.hxx"
00026 #include "MIDITrack.hxx"
00027 #include "MIDISong.hxx"
00028
00029
00030 namespace MIDI
00031 {
00032
00033 class TrackPlayer
00034
00035 {
00036 friend class SongPlayerImpl;
00037 private:
00038 Track* mTrack;
00039 std::list<Event*>::const_iterator mIterator;
00040 public:
00041 TrackPlayer(Track* track);
00042
00043 };
00044
00045 TrackPlayer::TrackPlayer(Track* track)
00046 {
00047 mTrack = track;
00048 mIterator = mTrack->Begin();
00049 }
00050
00051 class SongPlayerImpl
00052 {
00053
00054 friend class SongPlayer;
00055 private:
00056 Song* mSong;
00057 std::vector<TrackPlayer*> mTrackPlayerList;
00058 SongPlayerImpl(Song* song)
00059 {
00060 Init(song);
00061 }
00062
00063 void Init(Song* song)
00064 {
00065 mTrackPlayerList.clear();
00066 mSong = song;
00067 if (mSong)
00068 {
00069 for (int i=0;i<mSong->Tracks();i++)
00070 {
00071 mTrackPlayerList.push_back(
00072 new TrackPlayer(mSong->GetTrack(i)));
00073 }
00074 }
00075 }
00076
00077 bool GetEvent(Event& event,int& trackId)
00078 {
00079 Ticks smallest = 0;
00080 int smallestId = -1;
00081
00082 for (unsigned int i=0;i<mTrackPlayerList.size();i++)
00083
00084 {
00085 if (mTrackPlayerList[i]->mIterator !=
00086 mTrackPlayerList[i]->mTrack->End())
00087 {
00088 Event* ev = *mTrackPlayerList[i]->mIterator;
00089 if (smallestId==-1 || ev->GetTicks()<smallest)
00090 {
00091 smallest = ev->GetTicks();
00092 smallestId = i;
00093 }
00094 }
00095 }
00096 if (smallestId != -1)
00097 {
00098
00099 event = *(*(mTrackPlayerList[smallestId]->mIterator));
00100 trackId = smallestId;
00101 mTrackPlayerList[smallestId]->mIterator++;
00102 return true;
00103 }
00104 return false;
00105 }
00106
00107 };
00108
00109 SongPlayer::SongPlayer(Song* song)
00110 {
00111 mImpl = new SongPlayerImpl(song);
00112 }
00113
00114 void SongPlayer::Init(Song* song)
00115 {
00116 mImpl->Init(song);
00117 }
00118
00119 SongPlayer::~SongPlayer()
00120 {
00121 delete mImpl;
00122 }
00123
00124 bool SongPlayer::GetEvent(Event& event,int& trackId)
00125 {
00126 return mImpl->GetEvent(event,trackId);
00127 }
00128
00129 }
00130
00131
00132