From 1f37c9ba7c23e5ec1f8741b3889965def9baf33a Mon Sep 17 00:00:00 2001 From: saker Date: Sat, 26 Oct 2024 12:41:46 -0400 Subject: [PATCH] Inline `TimePos` and `TimeSig` functions to improve performance (#7549) * Inline TimeSig functions * Inline TimePos functions --- include/TimePos.h | 84 ++++++++++++++++-------- src/core/TimePos.cpp | 150 ------------------------------------------- 2 files changed, 58 insertions(+), 176 deletions(-) diff --git a/include/TimePos.h b/include/TimePos.h index b86d8eb0fa6..68f3bd01b05 100644 --- a/include/TimePos.h +++ b/include/TimePos.h @@ -26,6 +26,8 @@ #ifndef LMMS_TIME_POS_H #define LMMS_TIME_POS_H +#include +#include #include "lmms_export.h" #include "lmms_basics.h" @@ -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; @@ -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; } - 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; } - 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(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(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(ticks), beatsPerMinute); + } + + static double ticksToMilliseconds(double ticks, bpm_t beatsPerMinute) { return (ticks * 1250) / beatsPerMinute; } private: tick_t m_ticks; diff --git a/src/core/TimePos.cpp b/src/core/TimePos.cpp index 09c1019bcef..6e5e034fd23 100644 --- a/src/core/TimePos.cpp +++ b/src/core/TimePos.cpp @@ -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 ) { @@ -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(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( 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(ticks), beatsPerMinute ); -} - -double TimePos::ticksToMilliseconds(double ticks, bpm_t beatsPerMinute) -{ - // 60 * 1000 / 48 = 1250 - return ( ticks * 1250 ) / beatsPerMinute; -} - - } // namespace lmms