-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load a preset using a midi button (#104)
* Update frontend to support midi button * Create new class for handling mapped MIDI messages * Implement notifications on MIDI preset change * Implement preset dropdown for midi mapping page * Allow switching between parameter and toggle presets using dropdown * Implement midi mapping edit dialog
- Loading branch information
1 parent
1ae7b44
commit ba1a7a0
Showing
33 changed files
with
1,156 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
add_library(shrapnel_midi_handling INTERFACE) | ||
add_library(shrapnel::midi_handling ALIAS shrapnel_midi_handling) | ||
|
||
target_include_directories(shrapnel_midi_handling INTERFACE include) | ||
|
||
target_link_libraries(shrapnel_midi_handling INTERFACE | ||
shrapnel::etl | ||
shrapnel::messages | ||
shrapnel::presets | ||
) | ||
|
||
if(DEFINED TESTING) | ||
add_executable(midi_handling_test | ||
test/test_midi_handling.cpp) | ||
|
||
target_link_libraries(midi_handling_test | ||
PRIVATE | ||
GTest::gmock | ||
GTest::gtest_main | ||
shrapnel::audio_param | ||
shrapnel::compiler_warning_flags | ||
shrapnel::midi_handling) | ||
|
||
gtest_discover_tests(midi_handling_test) | ||
endif() |
108 changes: 108 additions & 0 deletions
108
firmware/components/midi_handling/include/midi_handling.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2022 Barabas Raffai | ||
* | ||
* This file is part of ShrapnelDSP. | ||
* | ||
* ShrapnelDSP 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. | ||
* | ||
* ShrapnelDSP 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 | ||
* ShrapnelDSP. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "etl/delegate.h" | ||
#include "messages.h" | ||
#include "midi_mapping.h" | ||
#include "midi_protocol.h" | ||
#include "presets_manager.h" | ||
#include "selected_preset_manager.h" | ||
|
||
namespace shrapnel { | ||
|
||
template <typename AudioParametersT, | ||
typename MappingManagerT, | ||
typename PresetLoaderT> | ||
class MidiMessageHandler | ||
{ | ||
public: | ||
MidiMessageHandler(std::shared_ptr<AudioParametersT> a_parameters, | ||
std::shared_ptr<MappingManagerT> a_mapping_manager, | ||
std::shared_ptr<PresetLoaderT> a_preset_loader) | ||
: parameters{std::move(a_parameters)}, | ||
mapping_manager{std::move(a_mapping_manager)}, | ||
preset_loader{std::move(a_preset_loader)} {}; | ||
|
||
/** React to a MIDI message by updating an audio parameter if there is a | ||
* mapping registered | ||
*/ | ||
void process_message(midi::Message message) const | ||
{ | ||
auto cc_params = | ||
get_if<midi::Message::ControlChange>(&message.parameters); | ||
if(!cc_params) | ||
return; | ||
|
||
for(const auto &[_, mapping] : *mapping_manager) | ||
{ | ||
if(mapping.midi_channel != message.channel) | ||
{ | ||
continue; | ||
} | ||
|
||
if(mapping.cc_number != cc_params->control) | ||
{ | ||
continue; | ||
} | ||
|
||
switch(mapping.mode) | ||
{ | ||
case midi::Mapping::Mode::PARAMETER: | ||
parameters->update(mapping.parameter_name, | ||
cc_params->value / | ||
float(midi::CC_VALUE_MAX)); | ||
break; | ||
case midi::Mapping::Mode::TOGGLE: | ||
{ | ||
if(cc_params->value == 0) | ||
{ | ||
continue; | ||
} | ||
|
||
auto old_value = parameters->get(mapping.parameter_name); | ||
|
||
parameters->update(mapping.parameter_name, | ||
old_value < 0.5f ? 1.f : 0.f); | ||
break; | ||
} | ||
case midi::Mapping::Mode::BUTTON: | ||
{ | ||
// A button sends the maximum value when it becomes pressed. | ||
if(cc_params->value != midi::CC_VALUE_MAX) | ||
{ | ||
continue; | ||
} | ||
|
||
auto id = mapping.preset_id; | ||
preset_loader->load_preset(id); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private: | ||
std::shared_ptr<AudioParametersT> parameters; | ||
std::shared_ptr<MappingManagerT> mapping_manager; | ||
std::shared_ptr<PresetLoaderT> preset_loader; | ||
}; | ||
|
||
} // namespace shrapnel |
Oops, something went wrong.