File: player.cpp

package info (click to toggle)
libdrumstick 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,876 kB
  • sloc: cpp: 25,685; xml: 122; sh: 14; makefile: 5
file content (189 lines) | stat: -rw-r--r-- 4,999 bytes parent folder | download
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
/*
    SMF GUI Player test using the MIDI Sequencer C++ library 
    Copyright (C) 2006-2024, Pedro Lopez-Cabanillas <plcl@users.sf.net>

    This program 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.

    This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "player.h"
#include "song.h"
#include <cmath>
#include <drumstick/alsaclient.h>
#include <drumstick/alsaqueue.h>

using namespace drumstick::ALSA;

Player::Player(MidiClient *seq, int portId) 
    : SequencerOutputThread(seq, portId),
    m_song(nullptr),
    m_songIterator(nullptr),
    m_lastEvent(nullptr),
    m_songPosition(0),
    m_echoResolution(0),
    m_pitchShift(0),
    m_volumeFactor(100)
{
    for (int chan = 0; chan < MIDI_CHANNELS; ++chan)
        m_volume[chan] = 100;
}

Player::~Player()
{
    if (isRunning()) {
        stop();
    }
    delete m_songIterator;
    delete m_lastEvent;
}

void Player::setSong(Song* s)
{
    m_song = s;
    if (m_song != nullptr) {
        delete m_songIterator;
        m_songIterator = new SongIterator(*m_song);
        m_echoResolution = m_song->getDivision() / 12;
        m_songPosition = 0;
    }
}

void Player::resetPosition()
{
    if ((m_song != nullptr) && (m_songIterator != nullptr)) {
        m_songIterator->toFront();
        m_songPosition = 0;
    }
}

void Player::setPosition(unsigned int pos)
{
    m_songPosition = pos;
    m_songIterator->toFront();
    while (m_songIterator->hasNext() &&
          (m_songIterator->next()->getTick() < pos)) { };
    if (m_songIterator->hasPrevious())
        m_songIterator->previous();
}

bool Player::hasNext()
{
    auto res = m_songIterator->hasNext();
    return res;
}

SequencerEvent* Player::nextEvent()
{
    delete m_lastEvent;
    m_lastEvent = m_songIterator->next()->clone();
    switch (m_lastEvent->getSequencerType()) {
        case SND_SEQ_EVENT_NOTE:
        case SND_SEQ_EVENT_NOTEON:
        case SND_SEQ_EVENT_NOTEOFF:
        case SND_SEQ_EVENT_KEYPRESS: {
            KeyEvent* kev = static_cast<KeyEvent*>(m_lastEvent);
            if (kev->getChannel() != MIDI_GM_DRUM_CHANNEL)
                kev->setKey(kev->getKey() + m_pitchShift);
        }
        break;
        case SND_SEQ_EVENT_CONTROLLER: {
            ControllerEvent *cev = static_cast<ControllerEvent*>(m_lastEvent);
            if (cev->getParam() == MIDI_CTL_MSB_MAIN_VOLUME) {
                int chan = cev->getChannel();
                int value = cev->getValue();
                m_volume[chan] = value;
                value = floor(value * m_volumeFactor / 100.0);
                if (value < 0) value = 0;
                if (value > 127) value = 127;
                cev->setValue(value);
            }
        }
        break;
    }
    return m_lastEvent;
}

unsigned int Player::getInitialPosition()
{
    return m_songPosition;
}

unsigned int Player::getEchoResolution()
{
    return m_echoResolution;
}

unsigned int Player::getPitchShift()
{
    return m_pitchShift;
}

unsigned int Player::getVolumeFactor()
{
    return m_volumeFactor;
}

void Player::setPitchShift(unsigned int pitch)
{
    bool playing = isRunning();
    if (playing) {
        stop();
        unsigned int pos = m_Queue->getStatus().getTickTime();
        m_Queue->clear();
        allNotesOff();
        setPosition(pos);
    }
    m_pitchShift = pitch;
    if (playing)
        start();
}

void Player::setVolumeFactor(unsigned int vol)
{
    m_volumeFactor = vol;
    for(int chan = 0; chan < MIDI_CHANNELS; ++chan) {
        int value = m_volume[chan];
        value = floor(value * m_volumeFactor / 100.0);
        if (value < 0) value = 0;
        if (value > 127) value = 127;
        sendController(chan, MIDI_CTL_MSB_MAIN_VOLUME, value);
    }
}

void Player::sendController(int chan, int control, int value)
{
    ControllerEvent ev(chan, control, value);
    ev.setSource(m_PortId);
    ev.setSubscribers();
    ev.setDirect();
    sendSongEvent(&ev);
}

void Player::allNotesOff()
{
    for(int chan = 0; chan < MIDI_CHANNELS; ++chan) {
        sendController(chan, MIDI_CTL_ALL_NOTES_OFF, 0);
        sendController(chan, MIDI_CTL_ALL_SOUNDS_OFF, 0);
    }
}

void Player::sendVolumeEvents()
{
    for(int chan = 0; chan < MIDI_CHANNELS; ++chan) {
        int value = m_volume[chan] = 100;
        value = floor(value * m_volumeFactor / 100.0);
        if (value < 0) value = 0;
        if (value > 127) value = 127;
        sendController(chan, MIDI_CTL_MSB_MAIN_VOLUME, value);
    }
}