Skip to content

Commit cce7db3

Browse files
Remove explicit time calculations by moving them into Song and MidiTime
Add a method to convert a MidiTime instance to milliseconds. Also add a static method to MidiTime that computes the time in milliseconds for a given number of ticks and a tempo. Remove the method that sets the milliseconds explicitly from Song. Replace it by a method that takes a MidiTime instance and one method that takes an amount of ticks. Remove several explicit time calculations from the implementation of Song and TimeLineWidget. Instead use the new methods.
1 parent b7b2204 commit cce7db3

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

include/MidiTime.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,15 @@ class EXPORT MidiTime
9090
// calculate number of frame that are needed this time
9191
f_cnt_t frames( const float framesPerTick ) const;
9292

93+
double getTimeInMilliseconds(bpm_t beatsPerMinute) const;
94+
9395
static MidiTime fromFrames( const f_cnt_t frames, const float framesPerTick );
9496
static tick_t ticksPerTact();
9597
static tick_t ticksPerTact( const TimeSig &sig );
9698
static int stepsPerTact();
9799
static void setTicksPerTact( tick_t tpt );
98100
static MidiTime stepPosition( int step );
101+
static double ticksToMilliseconds(tick_t ticks, bpm_t beatsPerMinute);
99102

100103
private:
101104
tick_t m_ticks;

include/Song.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,22 @@ class EXPORT Song : public TrackContainer
101101
{
102102
return m_nLoadingTrack;
103103
}
104+
104105
inline int getMilliseconds() const
105106
{
106107
return m_elapsedMilliSeconds;
107108
}
108-
inline void setMilliSeconds( float ellapsedMilliSeconds )
109+
110+
inline void setToTime( MidiTime const & midiTime )
111+
{
112+
m_elapsedMilliSeconds = midiTime.getTimeInMilliseconds(getTempo());
113+
}
114+
115+
inline void setToTimeByTicks(tick_t ticks)
109116
{
110-
m_elapsedMilliSeconds = ellapsedMilliSeconds;
117+
m_elapsedMilliSeconds = MidiTime::ticksToMilliseconds(ticks, getTempo());
111118
}
119+
112120
inline int getTacts() const
113121
{
114122
return currentTact();

src/core/Song.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ void Song::processNextBuffer()
258258
if( m_playPos[m_playMode] < tl->loopBegin() ||
259259
m_playPos[m_playMode] >= tl->loopEnd() )
260260
{
261-
m_elapsedMilliSeconds =
262-
( tl->loopBegin().getTicks() * 60 * 1000 / 48 ) / getTempo();
261+
setToTime(tl->loopBegin());
263262
m_playPos[m_playMode].setTicks(
264263
tl->loopBegin().getTicks() );
265264
emit updateSampleTracks();
@@ -316,8 +315,7 @@ void Song::processNextBuffer()
316315
ticks %= ( maxTact * MidiTime::ticksPerTact() );
317316

318317
// wrap milli second counter
319-
m_elapsedMilliSeconds =
320-
( ticks * 60 * 1000 / 48 ) / getTempo();
318+
setToTimeByTicks(ticks);
321319

322320
m_vstSyncController.setAbsolutePosition( ticks );
323321
}
@@ -335,10 +333,7 @@ void Song::processNextBuffer()
335333
if( m_playPos[m_playMode] >= tl->loopEnd() )
336334
{
337335
m_playPos[m_playMode].setTicks( tl->loopBegin().getTicks() );
338-
339-
m_elapsedMilliSeconds =
340-
( ( tl->loopBegin().getTicks() ) * 60 * 1000 / 48 ) /
341-
getTempo();
336+
setToTime(tl->loopBegin());
342337
}
343338
else if( m_playPos[m_playMode] == tl->loopEnd() - 1 )
344339
{
@@ -673,9 +668,8 @@ void Song::stop()
673668
if( tl->savedPos() >= 0 )
674669
{
675670
m_playPos[m_playMode].setTicks( tl->savedPos().getTicks() );
676-
m_elapsedMilliSeconds =
677-
( ( ( tl->savedPos().getTicks() ) * 60 * 1000 / 48 ) /
678-
getTempo() );
671+
setToTime(tl->savedPos());
672+
679673
if( gui && gui->songEditor() &&
680674
( tl->autoScroll() == TimeLineWidget::AutoScrollEnabled ) )
681675
{

src/core/midi/MidiTime.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ f_cnt_t MidiTime::frames( const float framesPerTick ) const
155155
return 0;
156156
}
157157

158+
double MidiTime::getTimeInMilliseconds(bpm_t beatsPerMinute) const
159+
{
160+
return ticksToMilliseconds(getTicks(), beatsPerMinute);
161+
}
158162

159163
MidiTime MidiTime::fromFrames( const f_cnt_t frames, const float framesPerTick )
160164
{
@@ -191,3 +195,9 @@ MidiTime MidiTime::stepPosition( int step )
191195
{
192196
return step * ticksPerTact() / stepsPerTact();
193197
}
198+
199+
double MidiTime::ticksToMilliseconds(tick_t ticks, bpm_t beatsPerMinute)
200+
{
201+
// 60 * 1000 / 48 = 1250
202+
return ( ticks * 1250 ) / beatsPerMinute;
203+
}

src/gui/TimeLineWidget.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,7 @@ void TimeLineWidget::mouseMoveEvent( QMouseEvent* event )
371371
{
372372
case MovePositionMarker:
373373
m_pos.setTicks( t.getTicks() );
374-
Engine::getSong()->setMilliSeconds( ( t.getTicks() *
375-
( 60 * 1000 / 48 ) ) /
376-
Engine::getSong()->getTempo() );
374+
Engine::getSong()->setToTime(t);
377375
m_pos.setCurrentFrame( 0 );
378376
updatePosition();
379377
positionMarkerMoved();

0 commit comments

Comments
 (0)