Skip to content
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 display #96

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions modules/foleys_gui_magic/General/foleys_MagicJUCEFactories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "../Widgets/foleys_MagicLevelMeter.h"
#include "../Widgets/foleys_MagicPlotComponent.h"
#include "../Widgets/foleys_MidiLearnComponent.h"
#include "../Widgets/foleys_MidiDisplayComponent.h"
#include "../Widgets/foleys_MidiDrumpadComponent.h"
#include "../Helpers/foleys_PopupMenuHelper.h"

Expand Down Expand Up @@ -891,6 +892,34 @@ class MidiLearnItem : public GuiItem

//==============================================================================

class MidiDisplayItem : public GuiItem
{
public:
FOLEYS_DECLARE_GUI_FACTORY (MidiDisplayItem)

MidiDisplayItem (MagicGUIBuilder& builder, const juce::ValueTree& node) : GuiItem (builder, node)
{
if (auto* state = dynamic_cast<MagicProcessorState*>(&builder.getMagicState()))
midiDisplay.setMagicProcessorState (state);

addAndMakeVisible (midiDisplay);
}

void update() override {}

juce::Component* getWrappedComponent() override
{
return &midiDisplay;
}

private:
MidiDisplayComponent midiDisplay;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiDisplayItem)
};

//==============================================================================

class ListBoxItem : public GuiItem,
public juce::ChangeListener
{
Expand Down Expand Up @@ -995,6 +1024,7 @@ void MagicGUIBuilder::registerJUCEFactories()
registerFactory (IDs::drumpadComponent, &DrumpadItem::factory);
registerFactory (IDs::meter, &LevelMeterItem::factory);
registerFactory ("MidiLearn", &MidiLearnItem::factory);
registerFactory ("MidiDisplay", &MidiDisplayItem::factory);
registerFactory (IDs::listBox, &ListBoxItem::factory);

#if JUCE_MODULE_AVAILABLE_juce_gui_extra && JUCE_WEB_BROWSER
Expand Down
15 changes: 15 additions & 0 deletions modules/foleys_gui_magic/State/foleys_MagicProcessorState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ int MagicProcessorState::getLastController() const
return midiMapper.getLastController();
}

int MagicProcessorState::getLastMidiChannel() const
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about exposing the midiMapper instead of cluttering the MagicProcessorState API?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fine to me - I was just following the pattern I found in front of me, really shooting for "minimum perturbation".

{
return midiMapper.getLastMidiChannel();
}

int MagicProcessorState::getLastMidiNote() const
{
return midiMapper.getLastMidiNote();
}

int MagicProcessorState::getLastMidiVelocity() const
{
return midiMapper.getLastMidiVelocity();
}

void MagicProcessorState::timerCallback()
{
getPropertyAsValue ("playhead:bpm").setValue (bpm.load());
Expand Down
15 changes: 15 additions & 0 deletions modules/foleys_gui_magic/State/foleys_MagicProcessorState.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ class MagicProcessorState : public MagicGUIState,
*/
int getLastController() const;

/**
Returns the last MIDI Channel on which MIDI was received by MIDI Mapper
*/
int getLastMidiChannel() const;

/**
Returns the last MIDI Note received by MIDI Mapper
*/
int getLastMidiNote() const;

/**
Returns the last MIDI Velocity received by MIDI Mapper
*/
int getLastMidiVelocity() const;

private:

void addParametersToMenu (const juce::AudioProcessorParameterGroup& group, juce::PopupMenu& menu, int& index) const;
Expand Down
41 changes: 38 additions & 3 deletions modules/foleys_gui_magic/State/foleys_MidiParameterMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ void MidiParameterMapper::processMidiBuffer (juce::MidiBuffer& buffer)

for (auto m : buffer)
{
if (m.getMessage().isController())
juce::MidiMessage mm = m.getMessage();
if (mm.isController())
{
auto number = m.getMessage().getControllerNumber();
auto value = m.getMessage().getControllerValue() / 127.0f;
auto number = mm.getControllerNumber();
auto value = mm.getControllerValue() / 127.0f;
lastController.store (number);

if (isLocked)
Expand All @@ -78,6 +79,11 @@ void MidiParameterMapper::processMidiBuffer (juce::MidiBuffer& buffer)
p->endChangeGesture();
}
}
} else if (mm.isNoteOn())
{
lastMidiChannel.store(mm.getChannel());
lastMidiNote.store(mm.getNoteNumber());
lastMidiVelocity.store(mm.getVelocity());
}
}

Expand All @@ -90,6 +96,20 @@ void MidiParameterMapper::mapMidiController (int cc, const juce::String& paramet
auto mappings = getMappingSettings();

juce::ValueTree node { IDs::mapping, {{IDs::cc, cc}, {IDs::parameter, parameterID}} };

// If mapping already there, remove it:
if (mappings.isValid()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my OCD, but please let's stay with 4 spaces ;-)
I intend to ad clang-format further down, so it is no longer a manual task.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do try to keep to four spaces in your code, but sometimes I slip, because both Emacs and Xcode are set to two spaces for me. Yes, clang-format is a good answer!

int index = 0;
while (index < mappings.getNumChildren()) {
const auto& child = mappings.getChild (index);
if (int (child.getProperty (IDs::cc, -1)) == cc && child.getProperty (IDs::parameter, juce::String()).toString() == parameterID) {
mappings.removeChild (child, nullptr);
return;
} else
++index;
}
}
// Otherwise add it:
mappings.appendChild (node, nullptr);
}

Expand Down Expand Up @@ -132,6 +152,21 @@ int MidiParameterMapper::getLastController() const
return lastController.load();
}

int MidiParameterMapper::getLastMidiChannel() const
{
return lastMidiChannel.load();
}

int MidiParameterMapper::getLastMidiNote() const
{
return lastMidiNote.load();
}

int MidiParameterMapper::getLastMidiVelocity() const
{
return lastMidiVelocity.load();
}

juce::ValueTree MidiParameterMapper::getMappingSettings()
{
return settings->settings.getOrCreateChildWithName (IDs::mappings, nullptr);
Expand Down
18 changes: 18 additions & 0 deletions modules/foleys_gui_magic/State/foleys_MidiParameterMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ class MidiParameterMapper : private juce::ValueTree::Listener
*/
int getLastController() const;

/*!
* @return the last MIDI Channel on which MIDI was received by MIDI Mapper
*/
int getLastMidiChannel() const;

/*!
* @return the last MIDI Note received by MIDI Mapper
*/
int getLastMidiNote() const;

/*!
* @return the last MIDI Velocity received by MIDI Mapper
*/
int getLastMidiVelocity() const;

/*!
* Grant access to the ValueTree to save or restore the mappings manually
* @return the ValueTree containing the mappings
Expand All @@ -98,6 +113,9 @@ class MidiParameterMapper : private juce::ValueTree::Listener

MagicProcessorState& state;
std::atomic<int> lastController { -1 };
std::atomic<int> lastMidiChannel { -1 };
std::atomic<int> lastMidiNote { -1 };
std::atomic<int> lastMidiVelocity { -1 };
MidiMapping midiMapper;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiParameterMapper)
Expand Down
86 changes: 86 additions & 0 deletions modules/foleys_gui_magic/Widgets/foleys_MidiDisplayComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
==============================================================================
Copyright (c) 2023 Julius Smith and Foleys Finest Audio - Daniel Walz
All rights reserved.

**BSD 3-Clause License**

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

==============================================================================

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
==============================================================================
*/

#include "foleys_MidiDisplayComponent.h"

namespace foleys
{

void MidiDisplayComponent::setMagicProcessorState (MagicProcessorState* state)
{
processorState = state;
startTimerHz (4);
}

void MidiDisplayComponent::paint (juce::Graphics& g)
{
if (processorState)
{
auto channel = processorState->getLastMidiChannel();
auto text = juce::String("Channel: " + (channel > 0 ? juce::String (channel) : "unknown"));

auto note = processorState->getLastMidiNote();
text += ("\nNote: " + (note > 0 ? juce::String (note) : "unknown"));

auto velocity = processorState->getLastMidiVelocity();
text += ("\nVelocity: " + (velocity > 0 ? juce::String (velocity) : "unknown"));

auto cc = processorState->getLastController();
text += ("\nCC: " + (cc > 0 ? juce::String (cc) : "unknown"));

g.setColour (juce::Colours::silver);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have those configurable...

Copy link
Author

@josmithiii josmithiii Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the GuiItem should take care of that. Colo[u]r is something I've never spent any time on, but I'm sure it's easy to learn.

Also, font size and spacing et al. should be configurable in the GuiItem, maybe even the layout as well. It's hard to know where to stop on this stuff. What would be really cool would be a pop-up text-editor, analogous to the color inspector, that allows super general control like any good rich text editor. Then properties would have magic names like "$lastControllerColour" that would do the right thing, analogous to spreadsheet cell references.

For the short term, I'll take a look at what MIDI Learn offers and at least get it to that level.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea: The MidiDisplayComponent should only display one item, and a pop-up list can support selecting which one. That way, each item can be dragged into a View and arranged via flexbox or in "contents mode" in the normal way.
Given that, why not simply use the LabelItem for this? It already has a value field, so it just becomes a matter of adding MIDI channel, noteNumber, velocity, etc., and lastController to its popup list.

g.drawFittedText (text, getLocalBounds(), juce::Justification::centred, 1);
}
}

void MidiDisplayComponent::mouseDrag (const juce::MouseEvent& event)
{
if (processorState && event.mouseWasDraggedSinceMouseDown())
{
auto cc = processorState->getLastController();
if (cc < 1)
return;

if (auto* container = juce::DragAndDropContainer::findParentDragContainerFor (this))
{
container->startDragging (IDs::dragCC + juce::String (cc), this);
}
}
}

void MidiDisplayComponent::timerCallback()
{
repaint();
}

}
68 changes: 68 additions & 0 deletions modules/foleys_gui_magic/Widgets/foleys_MidiDisplayComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
==============================================================================
Copyright (c) 2023 Julius Smith and Foleys Finest Audio - Daniel Walz
All rights reserved.

**BSD 3-Clause License**

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

==============================================================================

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
==============================================================================
*/

#pragma once

#include <juce_gui_basics/juce_gui_basics.h>

namespace foleys
{

class MagicProcessorState;

/**
The MidiDisplayComponent displays the last moved CC controller and allows via dragging
onto a knob to connect to its parameter
*/
class MidiDisplayComponent : public juce::Component,
public juce::SettableTooltipClient,
private juce::Timer
{
public:
MidiDisplayComponent() = default;

void setMagicProcessorState (MagicProcessorState* state);

void paint (juce::Graphics& g) override;
void mouseDrag (const juce::MouseEvent& event) override;

private:

void timerCallback() override;

MagicProcessorState* processorState = nullptr;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiDisplayComponent)
};

}
1 change: 1 addition & 0 deletions modules/foleys_gui_magic/foleys_gui_magic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#include "Widgets/foleys_XYDragComponent.cpp"
#include "Widgets/foleys_FileBrowserDialog.cpp"
#include "Widgets/foleys_MidiLearnComponent.cpp"
#include "Widgets/foleys_MidiDisplayComponent.cpp"
#include "Widgets/foleys_MidiDrumpadComponent.cpp"

#include "LookAndFeels/foleys_JuceLookAndFeels.cpp"
Expand Down
1 change: 1 addition & 0 deletions modules/foleys_gui_magic/foleys_gui_magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
#include "Widgets/foleys_XYDragComponent.h"
#include "Widgets/foleys_FileBrowserDialog.h"
#include "Widgets/foleys_MidiLearnComponent.h"
#include "Widgets/foleys_MidiDisplayComponent.h"
#include "Widgets/foleys_MidiDrumpadComponent.h"

#include "State/foleys_RadioButtonManager.h"
Expand Down
Loading