Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline TimePos and TimeSig functions to improve performance #7549

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 58 additions & 26 deletions include/TimePos.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#ifndef LMMS_TIME_POS_H
#define LMMS_TIME_POS_H

#include <algorithm>
#include <cassert>
#include "lmms_export.h"
#include "lmms_basics.h"

Expand All @@ -51,8 +53,8 @@ class LMMS_EXPORT TimeSig
public:
TimeSig( int num, int denom );
TimeSig( const MeterModel &model );
int numerator() const;
int denominator() const;
int numerator() const { return m_num; }
int denominator() const { return m_denom; }
private:
int m_num;
int m_denom;
Expand All @@ -69,42 +71,72 @@ class LMMS_EXPORT TimePos
TimePos( const tick_t ticks = 0 );

TimePos quantize(float) const;
TimePos toAbsoluteBar() const;
TimePos toAbsoluteBar() const { return getBar() * s_ticksPerBar; }
sakertooth marked this conversation as resolved.
Show resolved Hide resolved

TimePos& operator+=( const TimePos& time );
TimePos& operator-=( const TimePos& time );
TimePos& operator+=(const TimePos& time)
{
m_ticks += time.m_ticks;
return *this;
}

TimePos& operator-=(const TimePos& time)
{
m_ticks -= time.m_ticks;
return *this;
}

// return the bar, rounded down and 0-based
bar_t getBar() const;
bar_t getBar() const { return m_ticks / s_ticksPerBar; }

// return the bar, rounded up and 0-based
bar_t nextFullBar() const;
bar_t nextFullBar() const { return (m_ticks + (s_ticksPerBar - 1)) / s_ticksPerBar; }

void setTicks(tick_t ticks) { m_ticks = ticks; }
tick_t getTicks() const { return m_ticks; }

void setTicks( tick_t ticks );
tick_t getTicks() const;
operator int() const { return m_ticks; }
sakertooth marked this conversation as resolved.
Show resolved Hide resolved

operator int() const;
tick_t ticksPerBeat(const TimeSig& sig) const { return ticksPerBar(sig) / sig.numerator(); }

tick_t ticksPerBeat( const TimeSig &sig ) const;
// Remainder ticks after bar is removed
tick_t getTickWithinBar( const TimeSig &sig ) const;
tick_t getTickWithinBar(const TimeSig& sig) const { return m_ticks % ticksPerBar(sig); }

// Returns the beat position inside the bar, 0-based
tick_t getBeatWithinBar( const TimeSig &sig ) const;
tick_t getBeatWithinBar(const TimeSig& sig) const { return getTickWithinBar(sig) / ticksPerBeat(sig); }

// Remainder ticks after bar and beat are removed
tick_t getTickWithinBeat( const TimeSig &sig ) const;
tick_t getTickWithinBeat(const TimeSig& sig) const { return getTickWithinBar(sig) % ticksPerBeat(sig); }

// calculate number of frame that are needed this time
f_cnt_t frames( const float framesPerTick ) const;

double getTimeInMilliseconds( bpm_t beatsPerMinute ) const;

static TimePos fromFrames( const f_cnt_t frames, const float framesPerTick );
static tick_t ticksPerBar();
static tick_t ticksPerBar( const TimeSig &sig );
static int stepsPerBar();
static void setTicksPerBar( tick_t tpt );
static TimePos stepPosition( int step );
static double ticksToMilliseconds( tick_t ticks, bpm_t beatsPerMinute );
static double ticksToMilliseconds( double ticks, bpm_t beatsPerMinute );
f_cnt_t frames(const float framesPerTick) const
{
// Before, step notes used to have negative length. This
// assert is a safeguard against negative length being
// introduced again (now using Note Types instead #5902)
assert(m_ticks >= 0);
return static_cast<f_cnt_t>(m_ticks * framesPerTick);
}

double getTimeInMilliseconds(bpm_t beatsPerMinute) const { return ticksToMilliseconds(getTicks(), beatsPerMinute); }

static TimePos fromFrames(const f_cnt_t frames, const float framesPerTick)
{
return TimePos(static_cast<int>(frames / framesPerTick));
}

static tick_t ticksPerBar() { return s_ticksPerBar; }
static tick_t ticksPerBar(const TimeSig& sig) { return DefaultTicksPerBar * sig.numerator() / sig.denominator(); }

static int stepsPerBar() { return std::max(1, ticksPerBar() / DefaultBeatsPerBar); }
static void setTicksPerBar(tick_t ticks) { s_ticksPerBar = ticks; }
static TimePos stepPosition(int step) { return step * ticksPerBar() / stepsPerBar(); }

static double ticksToMilliseconds(tick_t ticks, bpm_t beatsPerMinute)
{
return ticksToMilliseconds(static_cast<double>(ticks), beatsPerMinute);
}

static double ticksToMilliseconds(double ticks, bpm_t beatsPerMinute) { return (ticks * 1250) / beatsPerMinute; }

private:
tick_t m_ticks;
Expand Down
150 changes: 0 additions & 150 deletions src/core/TimePos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,6 @@ TimeSig::TimeSig( const MeterModel &model ) :
{
}


int TimeSig::numerator() const
{
return m_num;
}

int TimeSig::denominator() const
{
return m_denom;
}




TimePos::TimePos( const bar_t bar, const tick_t ticks ) :
m_ticks( bar * s_ticksPerBar + ticks )
{
Expand Down Expand Up @@ -86,140 +72,4 @@ TimePos TimePos::quantize(float bars) const
return (lowPos + snapUp) * interval;
}


TimePos TimePos::toAbsoluteBar() const
{
return getBar() * s_ticksPerBar;
}


TimePos& TimePos::operator+=( const TimePos& time )
{
m_ticks += time.m_ticks;
return *this;
}


TimePos& TimePos::operator-=( const TimePos& time )
{
m_ticks -= time.m_ticks;
return *this;
}


bar_t TimePos::getBar() const
{
return m_ticks / s_ticksPerBar;
}


bar_t TimePos::nextFullBar() const
{
return ( m_ticks + ( s_ticksPerBar - 1 ) ) / s_ticksPerBar;
}


void TimePos::setTicks( tick_t ticks )
{
m_ticks = ticks;
}


tick_t TimePos::getTicks() const
{
return m_ticks;
}


TimePos::operator int() const
{
return m_ticks;
}


tick_t TimePos::ticksPerBeat( const TimeSig &sig ) const
{
// (number of ticks per bar) divided by (number of beats per bar)
return ticksPerBar(sig) / sig.numerator();
}


tick_t TimePos::getTickWithinBar( const TimeSig &sig ) const
{
return m_ticks % ticksPerBar( sig );
}

tick_t TimePos::getBeatWithinBar( const TimeSig &sig ) const
{
return getTickWithinBar( sig ) / ticksPerBeat( sig );
}

tick_t TimePos::getTickWithinBeat( const TimeSig &sig ) const
{
return getTickWithinBar( sig ) % ticksPerBeat( sig );
}


f_cnt_t TimePos::frames( const float framesPerTick ) const
{
// Before, step notes used to have negative length. This
// assert is a safeguard against negative length being
// introduced again (now using Note Types instead #5902)
assert(m_ticks >= 0);
return static_cast<f_cnt_t>(m_ticks * framesPerTick);
}

double TimePos::getTimeInMilliseconds( bpm_t beatsPerMinute ) const
{
return ticksToMilliseconds( getTicks(), beatsPerMinute );
}

TimePos TimePos::fromFrames( const f_cnt_t frames, const float framesPerTick )
{
return TimePos( static_cast<int>( frames / framesPerTick ) );
}


tick_t TimePos::ticksPerBar()
{
return s_ticksPerBar;
}


tick_t TimePos::ticksPerBar( const TimeSig &sig )
{
return DefaultTicksPerBar * sig.numerator() / sig.denominator();
}


int TimePos::stepsPerBar()
{
int steps = ticksPerBar() / DefaultBeatsPerBar;
return std::max(1, steps);
}


void TimePos::setTicksPerBar( tick_t tpb )
{
s_ticksPerBar = tpb;
}


TimePos TimePos::stepPosition( int step )
{
return step * ticksPerBar() / stepsPerBar();
}

double TimePos::ticksToMilliseconds( tick_t ticks, bpm_t beatsPerMinute )
{
return TimePos::ticksToMilliseconds( static_cast<double>(ticks), beatsPerMinute );
}

double TimePos::ticksToMilliseconds(double ticks, bpm_t beatsPerMinute)
{
// 60 * 1000 / 48 = 1250
return ( ticks * 1250 ) / beatsPerMinute;
}


} // namespace lmms
Loading