Skip to content

Commit 5cc7a64

Browse files
committed
Midi note shift node, cleanup
1 parent fd4117b commit 5cc7a64

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

runtime/elem/BlockEvents.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,6 @@ struct MidiEvent {
2727
{}
2828
};
2929

30-
struct AssignedMidiEvent {
31-
choc::midi::ShortMessage message;
32-
size_t voiceIndex;
33-
34-
AssignedMidiEvent(uint8_t byte0, uint8_t byte1, uint8_t byte2)
35-
: message(byte0, byte1, byte2)
36-
, voiceIndex(0)
37-
{}
38-
39-
AssignedMidiEvent(choc::midi::ShortMessage const& msg)
40-
: message(msg)
41-
, voiceIndex(0)
42-
{}
43-
};
44-
4530
// Type-erased event structure that can hold any event type within a certain
4631
// size. The event struct holds its data strictly in stack-allocated space.
4732
struct BlockEvent {

runtime/elem/DefaultNodeTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ namespace elem
147147
callback("midinotein", GenericNodeFactory<MidiNoteInNode<FloatType>>());
148148
callback("midinoteallocate", GenericNodeFactory<MidiNoteAllocateNode<FloatType>>());
149149
callback("midinoteunpack", GenericNodeFactory<MidiNoteUnpackNode<FloatType>>());
150+
callback("midinoteshift", GenericNodeFactory<MidiNoteShiftNode<FloatType>>());
150151
}
151152
};
152153

runtime/elem/Runtime.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ namespace elem
284284
void Runtime<FloatType>::process(const FloatType** inputChannelData, size_t numInputChannels, FloatType** outputChannelData, size_t numOutputChannels, size_t numSamples, void* userData)
285285
{
286286
BlockEvents emptyInputEvents;
287-
// SmallVec!
288287
BlockEvents emptyOutputEvents;
289288

290289
process(BlockContext<FloatType> {

runtime/elem/builtins/MIDI.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ namespace elem
122122
int64_t steadyClock = 0;
123123
};
124124

125+
// Maps incoming midi note events into audio signal data.
126+
//
127+
// This will constantly emit a pair of audio signals representing
128+
// the note frequency (in Hz) and note velocity (0-1) of the most
129+
// recent midi note that it has processed.
125130
template <typename FloatType>
126131
struct MidiNoteUnpackNode : public GraphNode<FloatType> {
127132
using GraphNode<FloatType>::GraphNode;
@@ -181,4 +186,39 @@ namespace elem
181186
FloatType noteVelocity = 0;
182187
};
183188

189+
// A simple pitch utility for midi events.
190+
//
191+
// For every incoming midi note event, this will nudge the associated
192+
// note value according to the steps property.
193+
template <typename FloatType>
194+
struct MidiNoteShiftNode : public GraphNode<FloatType> {
195+
using GraphNode<FloatType>::GraphNode;
196+
197+
int setProperty(std::string const& key, js::Value const& val) override
198+
{
199+
if (key == "steps") {
200+
if (!val.isNumber())
201+
return ReturnCode::InvalidPropertyType();
202+
203+
auto v = static_cast<int32_t>((js::Number) val);
204+
steps.store(v);
205+
}
206+
207+
return GraphNode<FloatType>::setProperty(key, val);
208+
}
209+
210+
void process (BlockContext<FloatType> const& ctx) override {
211+
auto const s = steps.load();
212+
213+
ctx.inputEvents.template processEventsOfType<MidiEvent>([&](size_t time, MidiEvent const& event) {
214+
auto* bytes = event.message.data();
215+
auto newNoteValue = std::max(0, std::min(127, static_cast<int32_t>(bytes[1]) + s));
216+
auto shiftedEvent = MidiEvent(bytes[0], static_cast<uint8_t>(newNoteValue), bytes[2]);
217+
ctx.outputEvents.addEvent(time, std::move(shiftedEvent));
218+
});
219+
}
220+
221+
std::atomic<int32_t> steps = 0;
222+
};
223+
184224
} // namespace elem

0 commit comments

Comments
 (0)