Skip to content

Commit

Permalink
Add Listen to Message feature, address Windows crash
Browse files Browse the repository at this point in the history
  • Loading branch information
nhielost committed Sep 3, 2022
1 parent bccae6d commit 7ed1ad2
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 37 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16...3.21)

# Change obs-plugintemplate to your plugin's name in a machine-readable format
# (e.g.: obs-myawesomeplugin) and set
project(obs-midi-mg VERSION 1.2.1)
project(obs-midi-mg VERSION 1.2.2)
add_library(obs-midi-mg MODULE)

# Replace `Your Name Here` with the name (yours or your organization's) you want
Expand Down Expand Up @@ -101,7 +101,7 @@ elseif(OS_MACOS)

set(MACOSX_PLUGIN_GUI_IDENTIFIER "${MACOS_BUNDLEID}")
set(MACOSX_PLUGIN_BUNDLE_VERSION "${CMAKE_PROJECT_VERSION}")
set(MACOSX_PLUGIN_SHORT_VERSION_STRING "1.1.1")
set(MACOSX_PLUGIN_SHORT_VERSION_STRING "1.2.2")

target_compile_options(obs-midi-mg PRIVATE -Wall)
# --- End of section ---
Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -17,6 +17,10 @@ Connect MIDI devices and seamlessly integrate with OBS Studio! This plugin offer

Go to the [Releases page](https://github.com/nhielost/obs-midi-mg/releases) and download and install the latest release for the proper operating system. That's it!

### macOS Users

It has been pointed out that the macOS installer will not always install correctly just by going through the installer normally. If this is the case, rerun the installer, and at the install page click the *Choose Install Location* button, then select the *Install for this user only* option. This will install the plugin in the proper location.

## Usage

To set up bindings, open OBS Studio and open the setup window under *Tools > obs-midi-mg Setup*. The setup window should appear.
Expand Down
6 changes: 3 additions & 3 deletions buildspec.json
@@ -1,10 +1,10 @@
{
"dependencies": {
"obs-studio": {
"version": "28.0.0-beta1",
"version": "28.0.1",
"repository": "https://github.com/obsproject/obs-studio.git",
"branch": "master",
"hash": "43a49dca47344a5170159ef99b86b97f90d4e4ad"
"hash": "e8dc70d0eef4503378d6ac300e680215eb5c9379"
},
"prebuilt": {
"version": "2022-08-02",
Expand Down Expand Up @@ -79,5 +79,5 @@
}
},
"name": "obs-midi-mg",
"version": "1.2.1"
"version": "1.2.2"
}
2 changes: 1 addition & 1 deletion cmake/ObsPluginHelpers.cmake
Expand Up @@ -543,7 +543,7 @@ else()

set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_DEPENDS
"obs-studio (>= 27.0.0), libqt5core5a (>= 5.9.0~beta), libqt5gui5 (>= 5.3.0), libqt5widgets5 (>= 5.7.0)"
"obs-studio (>= 28.0.0), libqt6core6 (>= 6.2.0), libqt6gui6 (>= 6.2.0), libqt6widgets6 (>= 6.2.0)"
)

set(CPACK_OUTPUT_FILE_PREFIX ${CMAKE_SOURCE_DIR}/release)
Expand Down
22 changes: 22 additions & 0 deletions src/forms/midimg-window.cpp
Expand Up @@ -107,6 +107,19 @@ MidiMGWindow::MidiMGWindow(QWidget *parent)
configure_lcd_widgets();

connect_ui_signals();

global()->set_listening_callback([this](MMGMessage *incoming) {
// Check the validity of the message type (whether it is one of the five
// supported types)
if (ui->editor_type->findText(incoming->get_type()) == -1)
return;
current_message->set_type(incoming->get_type());
current_message->set_channel(incoming->get_channel());
current_message->set_note(incoming->get_note());
current_message->set_value(incoming->get_value());
set_message_view();
ui->button_listen->setChecked(false);
});
}

bool MidiMGWindow::eventFilter(QObject *obj, QEvent *event)
Expand Down Expand Up @@ -191,6 +204,8 @@ void MidiMGWindow::connect_ui_signals()
// Message Display Connections
connect(ui->editor_type, &QComboBox::currentTextChanged, this,
&MidiMGWindow::on_message_type_change);
connect(ui->button_listen, &QAbstractButton::toggled, this,
&MidiMGWindow::on_message_listen);
// Action Display Connections
connect(ui->editor_cat, &QComboBox::currentTextChanged, this,
&MidiMGWindow::on_action_cat_change);
Expand Down Expand Up @@ -415,6 +430,13 @@ void MidiMGWindow::on_message_type_change(const QString &type)
}
}

void MidiMGWindow::on_message_listen(bool toggled)
{
global()->set_listening(toggled);
ui->button_listen->setText(toggled ? "Listening..."
: "Listen for Message...");
}

void MidiMGWindow::on_action_cat_change(const QString &cat)
{
current_action->set_category(MMGAction::categoryFromString(cat));
Expand Down
1 change: 1 addition & 0 deletions src/forms/midimg-window.h
Expand Up @@ -91,6 +91,7 @@ public slots:
void show_window();
private slots:
void on_message_type_change(const QString &type);
void on_message_listen(bool toggled);
void on_action_cat_change(const QString &cat);
void on_action_sub_change(int index);
void on_list_selection_change(const QListWidgetItem *current);
Expand Down
34 changes: 31 additions & 3 deletions src/forms/midimg-window.ui
Expand Up @@ -265,7 +265,7 @@
<number>0</number>
</property>
<property name="currentIndex">
<number>0</number>
<number>3</number>
</property>
<widget class="QWidget" name="empty_page">
<property name="palette">
Expand Down Expand Up @@ -889,7 +889,7 @@
<x>0</x>
<y>90</y>
<width>341</width>
<height>181</height>
<height>231</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -948,7 +948,7 @@
<item row="0" column="1">
<widget class="QComboBox" name="editor_type">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
Expand Down Expand Up @@ -2285,6 +2285,34 @@
</item>
</layout>
</item>
<item row="4" column="0" colspan="2">
<widget class="QToolButton" name="button_listen">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Tahoma</family>
<pointsize>9</pointsize>
</font>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="toolTip">
<string>Toggle this button to use the next message sent by the device as the message to display.</string>
</property>
<property name="text">
<string>Listen for Message...</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QLabel" name="text_message_name">
Expand Down
26 changes: 17 additions & 9 deletions src/mmg-action.cpp
Expand Up @@ -653,11 +653,17 @@ void MMGAction::do_action_audio_source(const MMGAction *params,
switch ((MMGAction::AudioSources)params->get_sub()) {
case MMGAction::AudioSources::SOURCE_AUDIO_VOLUME_CHANGETO:
// Value only has range 0-127, meaning full volume cannot be achieved (as it is divided by 128).
// Adding one allows the capability of full volume - at the cost of removing mute capability.
obs_source_set_volume(
obs_source, num_or_value(params->get_num(0),
midi->get_value() + 1, 100.0) /
100.0);
// Adding one allows the capability of full volume.
// ADDITION: Removing 1% from the value and changing it to 0% for muting is not as noticable.
if (params->get_num(0) == -1 && midi->get_value() == 0) {
obs_source_set_volume(obs_source, 0);
} else {
obs_source_set_volume(
obs_source,
num_or_value(params->get_num(0),
midi->get_value() + 1, 100.0) /
100.0);
}
break;
case MMGAction::AudioSources::SOURCE_AUDIO_VOLUME_CHANGEBY:
if (obs_source_get_volume(obs_source) * 100.0 +
Expand Down Expand Up @@ -960,7 +966,8 @@ void MMGAction::do_action_collections(const MMGAction *params,

// For new Scene Collection change (pre 28.0.0 method encounters threading errors)
auto set_collection = [](const char *name) {
char *current_collection = obs_frontend_get_current_scene_collection();
char *current_collection =
obs_frontend_get_current_scene_collection();
if (name == current_collection) {
bfree(current_collection);
return;
Expand All @@ -970,15 +977,16 @@ void MMGAction::do_action_collections(const MMGAction *params,
OBS_TASK_UI,
[](void *param) {
auto collection_name = (char **)param;
obs_frontend_set_current_scene_collection(*collection_name);
obs_frontend_set_current_scene_collection(
*collection_name);
},
&name, true);
};

if (params->get_sub() == 0) {
set_collection(params->get_str(0) == "Use Message Value"
? collection_names[midi->get_value()]
: params->get_str(0).qtocs());
? collection_names[midi->get_value()]
: params->get_str(0).qtocs());
}

bfree(collection_names);
Expand Down
7 changes: 3 additions & 4 deletions src/mmg-binding.cpp
Expand Up @@ -143,7 +143,7 @@ MMGMessage *MMGBinding::add_message(MMGMessage *const el)
return el;
}

void MMGBinding::insert_message(int index, MMGMessage *const el)
void MMGBinding::insert_message(size_t index, MMGMessage *const el)
{
messages.insert(index, el);
}
Expand All @@ -159,7 +159,7 @@ MMGAction *MMGBinding::add_action(MMGAction *const el)
return el;
}

void MMGBinding::insert_action(int index, MMGAction *const el)
void MMGBinding::insert_action(size_t index, MMGAction *const el)
{
actions.insert(index, el);
}
Expand Down Expand Up @@ -228,8 +228,7 @@ void MMGBinding::do_actions(const MMGSharedMessage &incoming)

// Check if the next message exists
if (!get_messages().value(current_message_index)) {
blog(LOG_DEBUG,
"Binding is no longer valid. Restarting...");
blog(LOG_DEBUG, "Binding is no longer valid. Restarting...");
goto reset_binding;
}

Expand Down
4 changes: 2 additions & 2 deletions src/mmg-binding.h
Expand Up @@ -48,10 +48,10 @@ class MMGBinding {
void set_mode(Mode val) { mode = (short)val; };

MMGMessage *add_message(MMGMessage *const el = new MMGMessage);
void insert_message(int index, MMGMessage *const el);
void insert_message(size_t index, MMGMessage *const el);
void remove(MMGMessage *const el);
MMGAction *add_action(MMGAction *const el = new MMGAction);
void insert_action(int index, MMGAction *const el);
void insert_action(size_t index, MMGAction *const el);
void remove(MMGAction *const el);

size_t index_of(MMGMessage *const el) const;
Expand Down
19 changes: 19 additions & 0 deletions src/mmg-config.cpp
Expand Up @@ -28,6 +28,9 @@ with this program. If not, see <https://www.gnu.org/licenses/>

using namespace MMGUtils;

bool MMGConfig::listening = false;
std::function<void(MMGMessage *)> MMGConfig::cb = 0;

void MMGConfig::blog(int log_status, const QString &message) const
{
QString temp_msg = "Config -> ";
Expand Down Expand Up @@ -193,6 +196,22 @@ MMGDevice *MMGConfig::find_device(const QString &name)
return nullptr;
}

bool MMGConfig::is_listening(MMGMessage *incoming)
{
if (listening) {
obs_queue_task(
OBS_TASK_UI,
[](void *param) {
auto incoming =
static_cast<MMGMessage *>(param);
cb(incoming);
},
incoming, true);
return true;
}
return false;
}

const QStringList MMGConfig::get_device_names() const
{
QStringList names;
Expand Down
10 changes: 10 additions & 0 deletions src/mmg-config.h
Expand Up @@ -51,11 +51,21 @@ class MMGConfig {
MMGSettings &preferences() { return settings; };

static QString get_filepath();
static void
set_listening_callback(std::function<void(MMGMessage *)> callback)
{
cb = callback;
};
static void set_listening(bool value) { listening = value; };
static bool is_listening(MMGMessage *incoming);

private:
MMGDevices devices;
MMGSettings settings;

void check_device_default_names();

static bool listening;
static std::function<void(MMGMessage *)> cb;
};
Q_DECLARE_METATYPE(MMGConfig);
16 changes: 12 additions & 4 deletions src/mmg-device.cpp
Expand Up @@ -119,7 +119,11 @@ MMGBinding *MMGDevice::find_binding(const QString &name)

void MMGDevice::open_input_port()
{
if (get_input_port_number(name) >= 0) {
if (input_port_open()) {
close_input_port();
}
if (get_input_port_number(name) != -1) {
blog(LOG_INFO, "Opening input port...");
input_device.set_callback(MMGUtils::call_midi_callback);
input_device.open_port(get_input_port_number(name));
blog(LOG_INFO, "Input port successfully opened.");
Expand All @@ -128,7 +132,11 @@ void MMGDevice::open_input_port()

void MMGDevice::open_output_port()
{
if (get_output_port_number(name) >= 0) {
if (output_port_open()) {
close_output_port();
}
if (get_output_port_number(name) != -1) {
blog(LOG_INFO, "Opening output port...");
output_device.open_port(get_output_port_number(name));
blog(LOG_INFO, "Output port successfully opened.");
}
Expand Down Expand Up @@ -206,12 +214,12 @@ QStringList MMGDevice::get_output_device_names()
return outputs;
}

int MMGDevice::get_input_port_number(const QString &device_name)
uint MMGDevice::get_input_port_number(const QString &device_name)
{
return get_input_device_names().indexOf(device_name);
}

int MMGDevice::get_output_port_number(const QString &device_name)
uint MMGDevice::get_output_port_number(const QString &device_name)
{
return get_output_device_names().indexOf(device_name);
}
Expand Down
4 changes: 2 additions & 2 deletions src/mmg-device.h
Expand Up @@ -52,9 +52,9 @@ class MMGDevice {
void do_all_actions(const MMGSharedMessage &message);

static QStringList get_input_device_names();
static int get_input_port_number(const QString &deviceName);
static uint get_input_port_number(const QString &deviceName);
static QStringList get_output_device_names();
static int get_output_port_number(const QString &deviceName);
static uint get_output_port_number(const QString &deviceName);

static qulonglong get_next_default() { return next_default; };
static void set_next_default(qulonglong num) { next_default = num; };
Expand Down
10 changes: 3 additions & 7 deletions src/mmg-message.cpp
Expand Up @@ -90,21 +90,17 @@ int MMGMessage::get_midi_note(const libremidi::message &mess)

int MMGMessage::get_midi_value(const libremidi::message &mess)
{
int part = -1;
switch (mess.get_message_type()) {
case libremidi::message_type::NOTE_ON:
case libremidi::message_type::NOTE_OFF:
case libremidi::message_type::CONTROL_CHANGE:
case libremidi::message_type::PITCH_BEND:
part = 2;
break;
return mess[2];
case libremidi::message_type::PROGRAM_CHANGE:
part = 1;
break;
return mess[1];
default:
break;
return -1;
}
return mess[part];
}

QString MMGMessage::get_midi_type(const libremidi::message &mess)
Expand Down

0 comments on commit 7ed1ad2

Please sign in to comment.