-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPlayer.h
118 lines (99 loc) · 4.04 KB
/
Player.h
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
#ifndef PLAYER_H
#define PLAYER_H
//#define DEBUG
#include "IStepMemory.h"
#include "RackInstrumentDefinitions.h"
#include "PlayerSettings.h"
#include "StepSynchronizer.h"
#ifdef DEBUG
#include <iostream>
#endif
/**
* @brief The Player class that plays defined pattern for soecific instruments
*/
class Player
{
public:
Player(IStepMemory * memory,
PlayerSettings * settings,
StepSynchronizer * synchronizer,
void (*instrumentEventCallback)(unsigned char instrumentID,
DrumStep::DrumVelocityType velocityType,
bool isOn)
);
void stepFourth();
unsigned char getCurrentInstrumentStep(unsigned char instrumentID);
void setCurrentInstrumentStep(unsigned char instrumentID, unsigned char step);
void changeActivesForCurrentStep(unsigned char instrumentID, unsigned char numberOfActiveSteps);
void changeActivesForCurrentStepInAllInstrunents(unsigned char numberOfActiveSteps);
unsigned char getCurrentInstrumentSubStep(unsigned char instrumentID);
void playNote(unsigned char instrumentID, DrumStep::DrumVelocityType velocityType, bool sendNoteOffBefore = true);
void resetAllInstruments();
void startLoop(unsigned char step);
void stopLoop();
bool isPlaying();
void update(unsigned int elapsedTimeUnits);
void setNoteOffDisalbedState(unsigned char instrument, bool disabled);
private:
IStepMemory * memory_;
PlayerSettings * settings_;
StepSynchronizer * synchronizer_;
unsigned char currentSteps_[INSTRUMENTS];
unsigned char playingInstruments[ALL_INSTRUMENTS_IN_BYTES];
bool isStopped_;
bool inLoop_;
unsigned char loopedStep_;
void (*instrumentEventCallback_)(unsigned char instrumentID, DrumStep::DrumVelocityType velocityType, bool isOn);
unsigned int lastElapsedTimeUnits_;
unsigned int lastDummyPlayInstrumentTimeUnits_;
unsigned char noteOffDisabledInstruments_;
void stepDrumInstruments();
bool isInstrumentPlaying(unsigned char instrumentID);
void setInstrumentPlaying(unsigned char instrumentID, bool isPlaying);
void sendNoteOffIfPlaying(unsigned char instrumentID);
};
inline void Player::startLoop(unsigned char step) {
inLoop_ = true;
loopedStep_ = step;
}
inline void Player::stopLoop() {
inLoop_ = false;
}
inline unsigned char Player::getCurrentInstrumentStep(unsigned char instrumentID) {
return currentSteps_[instrumentID] / 4;
}
inline void Player::setCurrentInstrumentStep(unsigned char instrumentID, unsigned char step) {
currentSteps_[instrumentID] = step * 4 + currentSteps_[instrumentID] % 4;
}
inline unsigned char Player::getCurrentInstrumentSubStep(unsigned char instrumentID) {
return currentSteps_[instrumentID] % 4;
}
inline bool Player::isInstrumentPlaying(unsigned char instrumentID)
{
#ifdef DEBUG
printf("Instrument %i, DataIndex %i,Data %i\n", instrumentID, instrumentID / 8, playingInstruments[instrumentID / 8]);
printf("dataAfterSwitch : %i \n", (playingInstruments[instrumentID / 8] & (1 << (instrumentID % 8))));
#endif
return (((playingInstruments[instrumentID / 8] & (1 << (instrumentID % 8))) >> (instrumentID % 8))) == 1;
}
inline void Player::setInstrumentPlaying(unsigned char instrumentID, bool isPlaying)
{
if (isPlaying) {
#ifdef DEBUG
printf("Set playing true Instrument %i, DataIndex %i,Data %i\n", instrumentID, instrumentID / 8, playingInstruments[instrumentID / 8]);
#endif
playingInstruments[instrumentID / 8] = playingInstruments[instrumentID / 8] | (1 << (instrumentID % 8));
} else {
#ifdef DEBUG
printf("Set playing false Instrument %i, DataIndex %i,Data %i\n", instrumentID, instrumentID / 8, playingInstruments[instrumentID / 8]);
#endif
playingInstruments[instrumentID / 8] = playingInstruments[instrumentID / 8] & ~(1 << (instrumentID % 8));
}
}
inline bool Player::isPlaying() {
return !isStopped_;
}
inline void Player::setNoteOffDisalbedState(unsigned char instrument, bool disabled) {
setBit(noteOffDisabledInstruments_, instrument, disabled);
}
#endif // PLAYER_H