FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NeuralNote/NeuralNote/Source/SynthController.cpp at master · DamRsn/NeuralNote · GitHub
DamRsn
/
NeuralNote
Public
Notifications
You must be signed in to change notification settings
Fork
190
Star
2.9k
Code
Issues
22
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NeuralNote
/
NeuralNote
/
Source
/
SynthController.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
163 lines (131 loc) · 4.92 KB
Breadcrumbs
NeuralNote
/
NeuralNote
/
Source
/
SynthController.cpp
Copy path
File metadata and controls
163 lines (131 loc) · 4.92 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
//
Created by Damien Ronssin on 10.06.23.
//
#
include
"
SynthController.h
"
#
include
"
PluginProcessor.h
"
SynthController::SynthController
(NeuralNoteAudioProcessor* inProcessor, MPESynthesiser* inMPESynth)
: mProcessor(inProcessor)
, mSynth(inMPESynth)
{
//
Set midi buffer size to 200 elements to avoid allocating memory on audio thread.
mMidiBuffer
.
ensureSize
(
3
*
200
);
}
std::vector<MidiMessage>
SynthController::buildMidiEventsVector
(
const
std::vector<Notes::Event>& inNoteEvents)
{
//
TODO: Deal with pitch bends
bool
INCLUDE_PITCH_BENDS
=
false
;
//
Compute size of single event vector
size_t
num_midi_messages =
0
;
for
(
const
auto
& note_event: inNoteEvents) {
num_midi_messages +=
2
;
if
(
INCLUDE_PITCH_BENDS
) {
if
(!note_event.
bends
.
empty
())
num_midi_messages += note_event.
bends
.
size
();
}
}
std::vector<MidiMessage>
out
(num_midi_messages);
size_t
i =
0
;
for
(
const
auto
& note_event: inNoteEvents) {
bool
include_bends =
INCLUDE_PITCH_BENDS
&& !note_event.
bends
.
empty
();
float
first_bend = include_bends ?
float
(note_event.
bends
[
0
]) /
3
.
0f
:
0
.
0f
;
//
TODO: Use different channels if there's a pitch bend
out[i++] =
MidiMessage::noteOn
(
1
, note_event.
pitch
, (
float
) note_event.
amplitude
).
withTimeStamp
(note_event.
startTime
);
if
(include_bends) {
for
(
size_t
j =
0
; j < note_event.
bends
.
size
(); j++) {
out[i++] =
MidiMessage::pitchWheel
(
1
,
MidiMessage::pitchbendToPitchwheelPos
(
static_cast
<
float
>(note_event.
bends
[j]) /
3
.
0f
,
2
))
.
withTimeStamp
(note_event.
startTime
+
double
(j) *
0.011
);
}
}
out[i++] =
MidiMessage::noteOff
(
1
, note_event.
pitch
).
withTimeStamp
(note_event.
endTime
);
jassert
(i <= num_midi_messages);
}
std::sort
(out.
begin
(), out.
end
(), [](
const
MidiMessage& a,
const
MidiMessage& b) {
return
a.
getTimeStamp
() < b.
getTimeStamp
();
});
return
out;
}
void
SynthController::setNewMidiEventsVectorToUse
(std::vector<MidiMessage>& inEvents)
{
const
ScopedLock
sl
(
mProcessor
->
getCallbackLock
());
std::swap
(inEvents,
mEvents
);
_updateCurrentEventIndex
();
_sanitizeVoices
();
}
void
SynthController::setSampleRate
(
double
inSampleRate)
{
mSampleRate
= inSampleRate;
}
const
MidiBuffer&
SynthController::generateNextMidiBuffer
(
int
inNumSamples)
{
mMidiBuffer
.
clear
();
double
end_time =
mCurrentTime
+ inNumSamples /
mSampleRate
;
while
(
mCurrentEventIndex
<
mEvents
.
size
() &&
mEvents
[
mCurrentEventIndex
].
getTimeStamp
() < end_time) {
int
index =
std::clamp
(
static_cast
<
int
>(
std::round
(
mEvents
[
mCurrentEventIndex
].
getTimeStamp
() -
mCurrentTime
)),
0
,
inNumSamples -
1
);
mMidiBuffer
.
addEvent
(
mEvents
[
mCurrentEventIndex
], index);
mCurrentEventIndex
+=
1
;
}
mCurrentTime
= end_time;
mCurrentSampleIndex
+= inNumSamples;
if
(
mCurrentTime
>=
mProcessor
->
getSourceAudioManager
()->
getAudioSampleDuration
()) {
//
Stop playing and reset to start
mProcessor
->
getPlayer
()->
setPlayingState
(
false
);
setNewTimeSeconds
(
0
);
}
return
mMidiBuffer
;
}
void
SynthController::reset
()
{
mCurrentEventIndex
=
0
;
mCurrentSampleIndex
=
0
;
mCurrentTime
=
0.0
;
mMidiBuffer
.
clear
();
}
void
SynthController::setNewTimeSeconds
(
double
inNewTime)
{
jassert
(inNewTime >=
0
);
mCurrentTime
= inNewTime;
mCurrentSampleIndex
=
static_cast
<
int
>(
std::round
(inNewTime *
mSampleRate
));
_updateCurrentEventIndex
();
_sanitizeVoices
();
}
double
SynthController::getCurrentTimeSeconds
()
const
{
return
mCurrentTime
;
}
void
SynthController::_sanitizeVoices
()
{
//
Insert note off event to avoid hanging notes
for
(
int
i =
0
; i <
mSynth
->
getNumVoices
(); i++) {
auto
* voice =
dynamic_cast
<SynthVoice*>(
mSynth
->
getVoice
(i));
if
(voice->
isActive
()) {
auto
midi_note = voice->
getCurrentMidiNote
();
if
(!
_isNextOnOffEventNoteOff
(midi_note)) {
voice->
noteStopped
(
true
);
}
}
}
}
void
SynthController::_updateCurrentEventIndex
()
{
mCurrentEventIndex
=
std::lower_bound
(
mEvents
.
begin
(),
mEvents
.
end
(),
mCurrentTime
,
[](
const
MidiMessage& a,
double
b) {
return
(a.
getTimeStamp
() < b); })
-
mEvents
.
begin
();
}
bool
SynthController::_isNextOnOffEventNoteOff
(
int
inMidiNote)
{
auto
iter =
std::find_if
(
mEvents
.
begin
() +
static_cast
<
long
>(
mCurrentEventIndex
),
mEvents
.
end
(), [inMidiNote](
const
MidiMessage& a) {
return
!a.
isPitchWheel
() && a.
getNoteNumber
() == inMidiNote;
});
if
(iter ==
mEvents
.
end
()) {
return
false
;
}
return
iter->
isNoteOff
();
}
Back
|
FazBrowse Home
|
New Git URL