Just Intonation  Version 1.3.1 (19)
Explore key-independent dynamically adapting tuning in just intonation
midiplayereventlist.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2016-2017 Karolin Stange, Christoph Wick, and Haye Hinrichsen
3  *
4  * This file is part of JustIntonation.
5  *
6  * JustIntonation is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * JustIntonation is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with JustIntonation. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Midi Player Events and Event List
22 //=============================================================================
23 
24 #include "midiplayereventlist.h"
25 
26 #include <QFile>
27 #include <QTextStream>
28 
29 //=============================================================================
30 // Midi Player Event constructor
31 //=============================================================================
32 
41 
42 MidiPlayerEvent::MidiPlayerEvent(quint8 track, double deltaticks,
43  quint8 command, quint8 byte1, quint8 byte2)
44 {
45  this->track = track;
46  this->deltaticks = deltaticks;
47  this->command = command;
48  this->byte1 = byte1;
49  this->byte2 = byte2;
50 }
51 
52 
53 //*****************************************************************************
54 
55 //------------------------------------------------------------------------------
56 // Constructor
57 //------------------------------------------------------------------------------
58 
62 
64  : mEventList()
65  , mMaxTime(0)
66  , pEventIterator(mEventList.begin())
67  , mAccessMutex()
68 {}
69 
70 
71 //------------------------------------------------------------------------------
72 // Clear
73 //------------------------------------------------------------------------------
74 
78 
80 {
81  mEventList.clear();
82  pEventIterator = mEventList.begin();
83  mMaxTime = 0;
84 }
85 
86 //------------------------------------------------------------------------------
87 // Insert a new event in the time-ordered list of events
88 //------------------------------------------------------------------------------
89 
95 
96 void MidiPlayerEventList::insert(double time, const MidiPlayerEvent &event)
97 {
98  QMutexLocker locker (&mAccessMutex);
99  mEventList.insert(time,event);
100  if (time > mMaxTime) mMaxTime = time;
101 }
102 
103 
104 //------------------------------------------------------------------------------
105 // Rewind
106 //------------------------------------------------------------------------------
107 
111 
113 {
114  QMutexLocker locker (&mAccessMutex);
115  pEventIterator = mEventList.begin();
116 }
117 
118 
119 //------------------------------------------------------------------------------
120 // Advance iterator
121 //------------------------------------------------------------------------------
122 
128 
130 {
131  QMutexLocker locker (&mAccessMutex);
132  pEventIterator++;
133 }
134 
135 
136 //------------------------------------------------------------------------------
137 // Are we at the end of the list?
138 //------------------------------------------------------------------------------
139 
144 
146 {
147  QMutexLocker locker (&mAccessMutex);
148  return pEventIterator == mEventList.end();
149 }
150 
151 
152 //------------------------------------------------------------------------------
153 // Get actual event
154 //------------------------------------------------------------------------------
155 
161 
163 {
164  QMutexLocker locker (&mAccessMutex);
165  if (pEventIterator == mEventList.end()) return MidiPlayerEvent();
166  else return pEventIterator.value();
167 }
168 
169 
170 //------------------------------------------------------------------------------
171 // Get actual time
172 //------------------------------------------------------------------------------
173 
179 
181 {
182  QMutexLocker locker (&mAccessMutex);
183  if (pEventIterator == mEventList.end()) return mMaxTime;
184  else return pEventIterator.key();
185 }
186 
187 
188 //------------------------------------------------------------------------------
189 // Get actual list size
190 //------------------------------------------------------------------------------
191 
196 
198 {
199  QMutexLocker locker (&mAccessMutex);
200  return mEventList.size();
201 }
202 
203 
204 //-----------------------------------------------------------------------------
205 // Get the playing progress in percent
206 //-----------------------------------------------------------------------------
207 
212 
214 {
215  QMutexLocker locker (&mAccessMutex);
216  if (pEventIterator == mEventList.begin()) return 0;
217  else if (pEventIterator == mEventList.end()) return 100;
218  else if (mMaxTime<=0) return 0;
219  else return 100.0*pEventIterator.key()/mMaxTime;
220 }
221 
222 
223 //-----------------------------------------------------------------------------
224 // Set the EventList iterator according to a given progress in percent
225 //-----------------------------------------------------------------------------
226 
231 
232 void MidiPlayerEventList::setProgress (double percent)
233 {
234  QMutexLocker locker (&mAccessMutex);
235  if (mEventList.empty() or mMaxTime <= 0) return;
236  pEventIterator = mEventList.begin();
237  quint32 threshold = mMaxTime * percent / 100.0;
238  while (pEventIterator != mEventList.end()
239  and pEventIterator.key() < threshold) pEventIterator++;
240 }
241 
242 
243 //-----------------------------------------------------------------------------
244 // Debugging: Write content of the list to file in a readable form
245 //-----------------------------------------------------------------------------
246 
251 
253 {
254  QFile file(filename);
255  if (not file.open(QIODevice::WriteOnly)) return;
256  QTextStream out(&file);
257  for (MidiPlayerEvent &e: mEventList)
258  {
259  out << "t=" << e.deltaticks << "\t" << "tr=" << e.track << "\tcmd=" << e.command
260  << "\tb1="<< e.byte1 << "\tb2="<< e.byte2 << "\n";
261  }
262  if (file.isOpen()) file.close();
263 }
quint32 getCumulativeTime() const
Return time of actual event (zero if iterator is at the end of the list).
MidiPlayerEventList()
Constructor, resetting member variables.
double deltaticks
Time ticks elapsed since the last event.
quint8 byte2
Midi second argument byte (zero if none)
quint8 command
Midi command byte.
quint8 byte1
Midi first argument byte.
void advance()
Advance iterator in the list of events.
void clear()
Clear the event list (clear all Midi data).
MidiEventList mEventList
Instance of the event list.
bool atEnd() const
Predicate, telling us whether we are already at the end of the list.
Structure used internally in the MidiPlayer to hold a Midi event.
const MidiPlayerEvent getEvent() const
Return a copy of the actual event (zero event if iterator is at the end of the list).
quint8 track
Midi track number.
void rewind()
Rewind: Move the iterator back to the beginning of the Midi song.
double getProgressInPercent() const
Get the playing progress in percent.
QMutex mAccessMutex
Mutex for access.
MidiEventList::Iterator pEventIterator
Iterator playing the notes.
quint32 mMaxTime
Maximum time at the end of the list.
int getSize() const
Get actual list size.
void setProgress(double percent)
Set the EventList iterator according to a given progress in percent.
void writeListInReadableForm(QString filename)
Write content of the list to file in a readable form (for debugging).
void insert(double time, const MidiPlayerEvent &event)
Insert a new event in the time-ordered list of events.
MidiPlayerEvent(quint8 track=0, double deltaticks=0, quint8 command=0, quint8 byte1=0, quint8 byte2=0)
Constructor for a MidiPlayerEvent, copying the arguments.