File: Chord.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 (205 lines) | stat: -rw-r--r-- 5,718 bytes parent folder | download | duplicates (4)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*********************************************************************************/
/*!
@file           Chord.cpp

@brief          Reads ahead from the songdata and collects chords to be matched.

@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 "Chord.h"
#include "Cfg.h"

int CNote::m_leftHandChannel = -2;
int CNote::m_rightHandChannel = -2;
int CNote::m_rightHandTrack[MAX_MIDI_CHANNELS];

whichPart_t CNote::m_activeHand = PB_PART_both;

int CChord::m_cfg_highestPianoNote = 127; // The highest note on the users piano keyboard;
int CChord::m_cfg_lowestPianoNote = 0;


void CNote::reset()
{
    CNote::setChannelHands(-2, -2);  // -2 for not set -1 for none

    for (int chan = 0; chan < MAX_MIDI_CHANNELS; chan++) {
        m_rightHandTrack[chan]=-1;
    }
}

void CNote::setChannelHands(int left, int right)
{
    m_leftHandChannel = left;
    m_rightHandChannel = right;
}


whichPart_t CNote::findHand(CMidiEvent midi, int whichChannel, whichPart_t whichPart)
{
    int midiNote = midi.note();
    int midiChannel = midi.channel();
    whichPart_t hand = PB_PART_none;
    // exit if it is not for this channel
    if (midiChannel != whichChannel)
    {
        // return none if this is not being used with the other hand.
        if (CNote::hasPianoPart(whichChannel) == false || CNote::hasPianoPart(midiChannel) == false)
            return PB_PART_none;
    }
    int rightHandTrack = CNote::rightHandTrack(midiChannel);

    if (midiChannel == whichChannel && rightHandTrack >= 0 ) {
        hand = (midi.track() == rightHandTrack) ? PB_PART_right : PB_PART_left ;
    } else if (midiChannel == CNote::rightHandChan()) {
        hand  = PB_PART_right;
    } else if (midiChannel == CNote::leftHandChan()) {
        hand  = PB_PART_left;
    } else  if (midiChannel == whichChannel) {
        hand = (midiNote >= MIDDLE_C) ? PB_PART_right : PB_PART_left ;
    }

    if (whichPart == PB_PART_left && hand == PB_PART_right)
        hand = PB_PART_none;
    if (whichPart == PB_PART_right && hand == PB_PART_left)
        hand = PB_PART_none;
    return hand;
}

void CChord::addNote(whichPart_t part, int note, int duration)
{
    if (m_length >= MAX_CHORD_NOTES)
    {
        ppDEBUG(("Over the chord note limit"));
        return;
    }

    if (searchChord(note)) // don't add duplicates
        return;
    m_notes[m_length] = CNote(part, note, duration);
    m_length++;
}


//////////////// CChord /////////////////////

bool CChord::removeNote(int note)
{
    int i;
    bool noteFound = false;

    for (i = 0; i < MAX_CHORD_NOTES; i++)
    {
        if (i >= m_length)
            break;
        if (noteFound == false)
        {
            if (getNote(i).pitch() == note)
                noteFound = true;
        }
        else
        {
            // shove everything else up to remove the note
            m_notes[i-1] = getNote(i);
        }
    }
    if (noteFound)
        m_length--;
    return noteFound;
}

bool CChord::searchChord(int note, int transpose)
{
    int i;

    for (i = 0; i < m_length; i++)
    {
        if (getNote(i).pitch() + transpose == note)
            return true;
    }
    return false;
}

int CChord::trimOutOfRangeNotes(int transpose)
{
    int i;
    int removedNotes = 0;

    for (i = 0; i < MAX_CHORD_NOTES; i++)
    {
        if (i >= m_length)
            break;

        if (!isNotePlayable(getNote(i).pitch(), transpose) || !isHandPlayable(getNote(i).part()))
            removedNotes++; // remove the note
        else if (removedNotes)
        {
            // shove everything else up to remove the note
            m_notes[i-removedNotes] = getNote(i);
        }
    }
    m_length -= removedNotes;
    return m_length;
}

bool CFindChord::findChord(CMidiEvent midi, int channel, whichPart_t part)
{
    bool foundChord = false;

    if (midi.type() == MIDI_PB_EOF)
    {
        if (m_currentChord.length() > 0)
        {
            m_completeChord = m_currentChord;
            foundChord = true;
        }
        return foundChord;
    }

    m_noteGapTime += midi.deltaTime();

    if ((m_noteGapTime >= m_cfg_ChordNoteGap || m_cordSpanGapTime > m_cfg_ChordMaxLength)
            && m_currentChord.length() > 0 )
    {
        foundChord = true;
        m_completeChord = m_currentChord;
        m_currentChord.clear();
    }

    if (midi.type() == MIDI_NOTE_ON)
    {
        whichPart_t hand = CNote::findHand( midi, channel, part );
        if ( hand != PB_PART_none)
        {
            m_currentChord.addNote(hand, midi.note());
            m_currentChord.setDeltaTime(m_noteGapTime + m_currentChord.getDeltaTime());
            if (m_currentChord.length() <= 1)
                m_cordSpanGapTime = 0;
            else
                m_cordSpanGapTime += m_noteGapTime; // measure the span of the cord
            m_noteGapTime = 0;
        }
    }
    return foundChord;
}