LMMS
Loading...
Searching...
No Matches
TimePos.h
Go to the documentation of this file.
1/*
2 * TimePos.h - declaration of class TimePos which provides data type for
3 * position- and length-variables
4 *
5 * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net
6 *
7 * This file is part of LMMS - https://lmms.io
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program (see COPYING); if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
23 *
24 */
25
26#ifndef LMMS_TIME_POS_H
27#define LMMS_TIME_POS_H
28
29#include <algorithm>
30#include <cassert>
31#include "lmms_export.h"
32#include "LmmsTypes.h"
33
34namespace lmms
35{
36
37// note: a bar was erroneously called "tact" in older versions of LMMS
38const int DefaultTicksPerBar = 192;
39const int DefaultStepsPerBar = 16;
41
42
43class MeterModel;
44
51class LMMS_EXPORT TimeSig
52{
53public:
54 TimeSig( int num, int denom );
55 TimeSig( const MeterModel &model );
56 int numerator() const { return m_num; }
57 int denominator() const { return m_denom; }
58private:
59 int m_num;
61};
62
63
67class LMMS_EXPORT TimePos
68{
69public:
70 TimePos( const bar_t bar, const tick_t ticks );
71 TimePos( const tick_t ticks = 0 );
72
73 TimePos quantize(float bars, bool forceRoundDown = false) const;
74 TimePos toAbsoluteBar() const { return getBar() * s_ticksPerBar; }
75
77 {
78 m_ticks += time.m_ticks;
79 return *this;
80 }
81
83 {
84 m_ticks -= time.m_ticks;
85 return *this;
86 }
87
88 // return the bar, rounded down and 0-based
89 bar_t getBar() const { return m_ticks / s_ticksPerBar; }
90
91 // return the bar, rounded up and 0-based
92 bar_t nextFullBar() const { return (m_ticks + (s_ticksPerBar - 1)) / s_ticksPerBar; }
93
94 void setTicks(tick_t ticks) { m_ticks = ticks; }
95 tick_t getTicks() const { return m_ticks; }
96
97 operator int() const { return m_ticks; }
98
99 tick_t ticksPerBeat(const TimeSig& sig) const { return ticksPerBar(sig) / sig.numerator(); }
100
101 // Remainder ticks after bar is removed
102 tick_t getTickWithinBar(const TimeSig& sig) const { return m_ticks % ticksPerBar(sig); }
103
104 // Returns the beat position inside the bar, 0-based
105 tick_t getBeatWithinBar(const TimeSig& sig) const { return getTickWithinBar(sig) / ticksPerBeat(sig); }
106
107 // Remainder ticks after bar and beat are removed
108 tick_t getTickWithinBeat(const TimeSig& sig) const { return getTickWithinBar(sig) % ticksPerBeat(sig); }
109
110 // calculate number of frame that are needed this time
111 f_cnt_t frames(const float framesPerTick) const
112 {
113 // Before, step notes used to have negative length. This
114 // assert is a safeguard against negative length being
115 // introduced again (now using Note Types instead #5902)
116 assert(m_ticks >= 0);
117 return static_cast<f_cnt_t>(m_ticks * framesPerTick);
118 }
119
120 double getTimeInMilliseconds(bpm_t beatsPerMinute) const { return ticksToMilliseconds(getTicks(), beatsPerMinute); }
121
122 static TimePos fromFrames(const f_cnt_t frames, const float framesPerTick)
123 {
124 return TimePos(static_cast<int>(frames / framesPerTick));
125 }
126
127 static tick_t ticksPerBar() { return s_ticksPerBar; }
128 static tick_t ticksPerBar(const TimeSig& sig) { return DefaultTicksPerBar * sig.numerator() / sig.denominator(); }
129
130 static int stepsPerBar() { return std::max(1, ticksPerBar() / DefaultBeatsPerBar); }
131 static void setTicksPerBar(tick_t ticks) { s_ticksPerBar = ticks; }
132 static TimePos stepPosition(int step) { return step * ticksPerBar() / stepsPerBar(); }
133
134 static double ticksToMilliseconds(tick_t ticks, bpm_t beatsPerMinute)
135 {
136 return ticksToMilliseconds(static_cast<double>(ticks), beatsPerMinute);
137 }
138
139 static double ticksToMilliseconds(double ticks, bpm_t beatsPerMinute) { return (ticks * 1250) / beatsPerMinute; }
140
141private:
143
145
146} ;
147
148
149} // namespace lmms
150
151#endif // LMMS_TIME_POS_H
assert(0)
Definition MeterModel.h:35
static void setTicksPerBar(tick_t ticks)
Definition TimePos.h:131
static TimePos stepPosition(int step)
Definition TimePos.h:132
void setTicks(tick_t ticks)
Definition TimePos.h:94
f_cnt_t frames(const float framesPerTick) const
Definition TimePos.h:111
tick_t ticksPerBeat(const TimeSig &sig) const
Definition TimePos.h:99
double getTimeInMilliseconds(bpm_t beatsPerMinute) const
Definition TimePos.h:120
tick_t getBeatWithinBar(const TimeSig &sig) const
Definition TimePos.h:105
bar_t getBar() const
Definition TimePos.h:89
static double ticksToMilliseconds(tick_t ticks, bpm_t beatsPerMinute)
Definition TimePos.h:134
static int stepsPerBar()
Definition TimePos.h:130
static TimePos fromFrames(const f_cnt_t frames, const float framesPerTick)
Definition TimePos.h:122
TimePos & operator+=(const TimePos &time)
Definition TimePos.h:76
tick_t m_ticks
Definition TimePos.h:142
tick_t getTicks() const
Definition TimePos.h:95
TimePos(const bar_t bar, const tick_t ticks)
Definition TimePos.cpp:45
tick_t getTickWithinBeat(const TimeSig &sig) const
Definition TimePos.h:108
TimePos toAbsoluteBar() const
Definition TimePos.h:74
TimePos & operator-=(const TimePos &time)
Definition TimePos.h:82
bar_t nextFullBar() const
Definition TimePos.h:92
static tick_t ticksPerBar()
Definition TimePos.h:127
static tick_t s_ticksPerBar
Definition TimePos.h:144
tick_t getTickWithinBar(const TimeSig &sig) const
Definition TimePos.h:102
static tick_t ticksPerBar(const TimeSig &sig)
Definition TimePos.h:128
TimePos quantize(float bars, bool forceRoundDown=false) const
Definition TimePos.cpp:55
static double ticksToMilliseconds(double ticks, bpm_t beatsPerMinute)
Definition TimePos.h:139
Definition TimePos.h:52
TimeSig(int num, int denom)
Definition TimePos.cpp:33
int numerator() const
Definition TimePos.h:56
int m_num
Definition TimePos.h:59
int denominator() const
Definition TimePos.h:57
int m_denom
Definition TimePos.h:60
Definition AudioAlsa.cpp:35
const int DefaultStepsPerBar
Definition TimePos.h:39
const int DefaultTicksPerBar
Definition TimePos.h:38
const int DefaultBeatsPerBar
Definition TimePos.h:40
std::int32_t bar_t
Definition LmmsTypes.h:34
std::int32_t tick_t
Definition LmmsTypes.h:35
std::uint64_t f_cnt_t
Definition LmmsTypes.h:43
std::uint16_t bpm_t
Definition LmmsTypes.h:45
typedef int(UZ_EXP MsgFn)()