-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Midi support #110
Midi support #110
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,53 @@ | ||||
/* | ||||
* pedalboard | ||||
* Copyright 2021 Spotify AB | ||||
* | ||||
* Licensed under the GNU Public License, Version 3.0 (the "License"); | ||||
* you may not use this file except in compliance with the License. | ||||
* You may obtain a copy of the License at | ||||
* | ||||
* https://www.gnu.org/licenses/gpl-3.0.html | ||||
* | ||||
* Unless required by applicable law or agreed to in writing, software | ||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
* See the License for the specific language governing permissions and | ||||
* limitations under the License. | ||||
*/ | ||||
|
||||
#pragma once | ||||
#include "JuceHeader.h" | ||||
|
||||
#include <pybind11/numpy.h> | ||||
#include <pybind11/pybind11.h> | ||||
|
||||
namespace Pedalboard { | ||||
|
||||
juce::MidiMessageSequence | ||||
copyPyArrayIntoJuceMidiMessageSequence(const py::array_t<float, py::array::c_style> midiMessages) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This helper seems to re-interpret floating-point data into integer data, which seems a bit risky and confusing. Given that MIDI messages are probably going to be sparse (i.e.: not nearly as large as audio data) we could probably just use a |
||||
py::buffer_info inputInfo = midiMessages.request(); | ||||
|
||||
if(inputInfo.shape[1] != 4) { | ||||
throw std::runtime_error("Each element must have length 4 (got " + | ||||
std::to_string(inputInfo.shape[1]) + ")."); | ||||
} | ||||
|
||||
juce::MidiMessageSequence midiSequence; | ||||
|
||||
float *data = static_cast<float *>(inputInfo.ptr); | ||||
|
||||
for(int i = 0; i < inputInfo.shape[0]; i++) { | ||||
int byte1 = (int) data[i * 4 + 0]; | ||||
int byte2 = (int) data[i * 4 + 1]; | ||||
int byte3 = (int) data[i * 4 + 2]; | ||||
float timeSeconds = data[i * 4 + 3]; | ||||
|
||||
midiSequence.addEvent(juce::MidiMessage(byte1, byte2, byte3), timeSeconds); | ||||
|
||||
DBG( byte1 ); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
} | ||||
|
||||
return midiSequence; | ||||
} | ||||
|
||||
} // namespace Pedalboard |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .midi_messages import * |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import mido | ||
|
||
def note_on(time:float, note: int, velocity: int, channel: int = 1): | ||
return mido.Message('note_on', note=note, velocity=velocity, channel=channel).bytes() + [time] | ||
|
||
def note_off(time:float, note: int, velocity: int,channel: int = 1): | ||
return mido.Message('note_off', note=note, velocity=velocity, channel=channel).bytes() + [time] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.