File: Bar.cpp

package info (click to toggle)
pianobooster 1.0.0-4.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,740 kB
  • sloc: cpp: 13,718; python: 155; sh: 16; makefile: 14
file content (138 lines) | stat: -rw-r--r-- 3,844 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*********************************************************************************/
/*!
@file           Bar.cpp

@brief          xxx.

@author         L. J. Barman

    Copyright (c)   2008-2013, L. J. Barman, all rights reserved

    This file is part of the PianoBooster application

    PianoBooster is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PianoBooster is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PianoBooster.  If not, see <http://www.gnu.org/licenses/>.

*/
/*********************************************************************************/

#include "Bar.h"

#define OPTION_DEBUG_BAR     0
#if OPTION_DEBUG_BAR
#define ppDEBUG_BAR(args)     ppLogDebug args
#else
#define ppDEBUG_BAR(args)
#endif

void CBar::setTimeSig(int top, int bottom)
{
    m_currentTimeSigTop = top;
    m_currentTimeSigBottom = bottom;
    if (bottom == 0 || m_startTimeSigBottom == 0) {
        m_startTimeSigTop = top;
        m_startTimeSigBottom = bottom;
        if (bottom == 0 )
        {
            m_currentTimeSigTop = 4;
            m_currentTimeSigBottom = 4;
            m_deltaTime = 0;
            m_beatCounter = 0;
            m_barCounter = 0;
        }
    }
    m_beatLength = (CMidiFile::getPulsesPerQuarterNote() *4)/ m_currentTimeSigBottom;
    m_barLength = m_beatLength * getTimeSigTop();
}

qint64 CBar::addDeltaTime(qint64 ticks)
{
    if (m_flushTicks == true && ticks != 0) // ignore this set of ticks
    {
        ppDEBUG_BAR(("addDeltaTime m_flushTicks ticks %d", ticks));
        m_flushTicks = false;
        return 0;
    }
    m_deltaTime +=ticks;
    if (ticks && m_enablePlayFromBar)
        checkGotoBar();
    while (m_deltaTime > m_beatLength * SPEED_ADJUST_FACTOR)
    {
        m_deltaTime -= m_beatLength * SPEED_ADJUST_FACTOR;
        m_beatCounter++;
        if (m_beatCounter >= m_currentTimeSigTop)
        {
            m_barCounter++;
            m_beatCounter=0;
            ppLogDebug("Bar number %d", m_barCounter);
            m_eventBits |= EVENT_BITS_newBarNumber;
        }
    }

    return ticks;
}

qint64 CBar::goToBarNumer()
{
    const auto ticks = static_cast<qint64>(m_playFromBar - getCurrentBarPos()) * m_beatLength * SPEED_ADJUST_FACTOR + 1;
    addDeltaTime(ticks);
    return ticks;
}

void CBar::checkGotoBar()
{
    double currentBar = getCurrentBarPos();
    ppDEBUG_BAR(("checkGotoBar currentBarPos %.1f", currentBar));
    if (currentBar < m_playFromBar )
        m_seekingBarNumber = true;
    else
    {
        if (m_seekingBarNumber == true)
            m_flushTicks = true; // now throw away ticks before we start the music
        m_seekingBarNumber = false;

        if (m_enableLooping && currentBar > m_playUptoBar )
            m_eventBits |= EVENT_BITS_UptoBarReached;
    }
}

void CBar::setPlayFromBar(double bar)
{
    m_playFromBar = bar;
    m_playUptoBar = m_playFromBar + m_loopingBars;
    setupEnableFlags();
    checkGotoBar();
}

void CBar::setPlayFromBar(int bar, int beat, int ticks)
{
    Q_UNUSED(beat)
    Q_UNUSED(ticks)
    double playFromBar = bar;
    setPlayFromBar( playFromBar);
}

void CBar::setPlayUptoBar(double endBar)
{
    setLoopingBars(endBar - m_playUptoBar);
}

void CBar::setLoopingBars(double bars)
{
    if (bars < 0.0)
        bars = 0;
    m_loopingBars = bars;
    m_playUptoBar = m_playFromBar + m_loopingBars;
    setupEnableFlags();
    checkGotoBar();
}