Browse Source

Extract MIDI utils

Separate MIDI utils into its own package.
master
TheoryOfNekomata 1 year ago
parent
commit
45391950dc
7 changed files with 15 additions and 89 deletions
  1. +3
    -0
      .gitmodules
  2. +5
    -4
      CMakeLists.txt
  3. +4
    -4
      src/packages/game/input/IZ_midi.c
  4. +2
    -2
      src/packages/game/input/IZ_midi.h
  5. +0
    -61
      src/packages/midi/IZ_midi.c
  6. +0
    -18
      src/packages/midi/IZ_midi.h
  7. +1
    -0
      subprojects/midi-utils

+ 3
- 0
.gitmodules View File

@@ -37,3 +37,6 @@
[submodule "subprojects/alsa-lib"]
path = subprojects/alsa-lib
url = git://git.alsa-project.org/alsa-lib.git
[submodule "subprojects/midi-utils"]
path = subprojects/midi-utils
url = https://code.modal.sh/TheoryOfNekomata/midi-utils.git

+ 5
- 4
CMakeLists.txt View File

@@ -70,6 +70,7 @@ include_directories(
"${CMAKE_HOME_DIRECTORY}/subprojects/minIni/dev"
"${CMAKE_HOME_DIRECTORY}/subprojects/bdd-for-c"
"${CMAKE_HOME_DIRECTORY}/subprojects/bdd-for-c-mocks"
"${CMAKE_HOME_DIRECTORY}/subprojects/midi-utils"
"${CMAKE_HOME_DIRECTORY}/subprojects/portmidi/pm_common"
"${CMAKE_HOME_DIRECTORY}/dependencies/spine-runtimes/spine-c/spine-c/include"
"${CMAKE_HOME_DIRECTORY}/dependencies/getopt-for-windows"
@@ -138,6 +139,8 @@ add_executable(
${IZ_EXECUTABLE_TYPE}
subprojects/minIni/dev/minIni.h
subprojects/minIni/dev/minIni.c
subprojects/midi-utils/midi-utils.h
subprojects/midi-utils/midi-utils.c
${IZ_GETOPT_DEPENDENCIES}
src/packages/game/output/video/IZ_video.h
src/packages/game/output/video/IZ_video.c
@@ -174,8 +177,6 @@ add_executable(
src/packages/net/svc/IZ_wsclient.h
src/packages/log/IZ_intercept.c
src/packages/log/IZ_intercept.h
src/packages/midi/IZ_midi.c
src/packages/midi/IZ_midi.h
src/packages/net/core/IZ_websocket.h
src/packages/net/core/IZ_websocket.c
src/packages/net/IZ_net_client.c
@@ -250,8 +251,8 @@ add_executable(
src/packages/game/input/IZ_midi.h
src/packages/game/input/IZ_midi.c

src/packages/midi/IZ_midi.c
src/packages/midi/IZ_midi.h
subprojects/midi-utils/midi-utils.h
subprojects/midi-utils/midi-utils.c

src/packages/stdinc/IZ_string.h
__mocks__/src/packages/stdinc/IZ_string.mock.h


+ 4
- 4
src/packages/game/input/IZ_midi.c View File

@@ -7,12 +7,12 @@ bool IZ_MIDIInputIsValidChannel(u8 value) {
}

void IZ_MIDIInputSerializeControl(i32 value, char* control[128]) {
const char* serialized = IZ_MIDIGetNoteName(value);
const char* serialized = MIDI_GetNoteName(value);
IZ_memcpy(control, 128, serialized, 128);
}

i32 IZ_MIDIInputDeserializeControl(const char* control) {
return IZ_MIDIGetNoteFromName(control);
return MIDI_GetNoteFromName(control);
}

void IZ_MIDIInputHandleNoteOnOffEvents(IZ_MIDIInputState* state, IZ_Action* action, PmEvent e) {
@@ -32,11 +32,11 @@ void IZ_MIDIInputHandleNoteOnOffEvents(IZ_MIDIInputState* state, IZ_Action* acti
)
) {
const u16 bitflag = (0x1 << control_index);
if (status == IZ_MIDI_NOTE_ON) {
if (status == MIDI_MESSAGE_NOTEON) {
*action |= bitflag;
return;
}
if (status == IZ_MIDI_NOTE_OFF) {
if (status == MIDI_MESSAGE_NOTEOFF) {
*action &= ~bitflag;
return;
}


+ 2
- 2
src/packages/game/input/IZ_midi.h View File

@@ -5,8 +5,8 @@
#include <portmidi.h>

#include <minIni.h>
#include <midi-utils.h>
#include "../../config/IZ_config.h"
#include "../../midi/IZ_midi.h"
#include "../../stdinc/IZ_string.h"
#include "../../stdinc/IZ_stdlib.h"
#include "IZ_action.h"
@@ -16,7 +16,7 @@
typedef struct {
PmDeviceID device_id;
u8 channel;
IZ_MIDINote control_mapping[IZ_CONTROLS];
u8 control_mapping[IZ_CONTROLS];
} IZ_MIDIInputConfig;

typedef struct {


+ 0
- 61
src/packages/midi/IZ_midi.c View File

@@ -1,61 +0,0 @@
#include "IZ_midi.h"

char* IZ_MIDIGetNoteName(unsigned char midi_note) {
static const char* pitch_names[] = {
"C",
"C#",
"D",
"D#",
"E",
"F",
"F#",
"G",
"G#",
"A",
"A#",
"B"
};

const unsigned char pitch_class = midi_note % 12;
const unsigned char octave = midi_note / 12;
static char note_name[8];
sprintf(note_name, "%s%u", pitch_names[pitch_class], octave);
return note_name;
}

unsigned char IZ_MIDIGetNoteFromName(const char* name) {
char name_copy[8];
IZ_strlwr(name_copy, name, 8);

unsigned char octave;
const char base_pitch_name[] = "c d ef g a b";

if (strlen(name_copy) == 2) {
octave = name_copy[1] - '0';
unsigned char pitch_index;
for (pitch_index = 0; pitch_index < 12; pitch_index += 1) {
if (base_pitch_name[pitch_index] == name_copy[0]) {
return (octave * 12) + pitch_index;
}
}
return 255u; // invalid note value
}

unsigned char pitch_class;
octave = name_copy[2] - '0';
if (strstr(name_copy, "c#") || strstr(name_copy, "db")) {
pitch_class = 1;
} else if (strstr(name_copy, "d#") || strstr(name_copy, "eb")) {
pitch_class = 3;
} else if (strstr(name_copy, "f#") || strstr(name_copy, "gb")) {
pitch_class = 6;
} else if (strstr(name_copy, "g#") || strstr(name_copy, "ab")) {
pitch_class = 8;
} else if (strstr(name_copy, "a#") || strstr(name_copy, "bb")) {
pitch_class = 10;
} else {
return 255u;
}

return (octave * 12) + pitch_class;
}

+ 0
- 18
src/packages/midi/IZ_midi.h View File

@@ -1,18 +0,0 @@
#ifndef IZ_MIDI_MIDI_H
#define IZ_MIDI_MIDI_H

#include <stdio.h>
#include <string.h>
#include "../stdinc/IZ_string.h"

typedef unsigned char IZ_MIDINote;

static const unsigned char IZ_MIDI_NOTE_ON = 0x90u;

static const unsigned char IZ_MIDI_NOTE_OFF = 0x80u;

char* IZ_MIDIGetNoteName(unsigned char);

unsigned char IZ_MIDIGetNoteFromName(const char*);

#endif

+ 1
- 0
subprojects/midi-utils

@@ -0,0 +1 @@
Subproject commit dc3396c806d500802cb8a8dad15d3e46a9621954

Loading…
Cancel
Save