Skip to content

Commit

Permalink
More MIDI fixes (ugh)
Browse files Browse the repository at this point in the history
  • Loading branch information
8bitbubsy committed Apr 12, 2024
1 parent d12b46e commit f4513ab
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 91 deletions.
46 changes: 12 additions & 34 deletions src/ft2_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static uint8_t configBuffer[CONFIG_FILE_SIZE];
static void xorConfigBuffer(uint8_t *ptr8)
{
for (int32_t i = 0; i < CONFIG_FILE_SIZE; i++)
ptr8[i] ^= i*7;
ptr8[i] ^= (uint8_t)(i*7);
}

static int32_t calcChecksum(const uint8_t *p, uint16_t len) // for Nibbles highscore data
Expand Down Expand Up @@ -290,7 +290,7 @@ bool loadConfig(bool showErrorFlag)
audio.currInputDevice = getAudioInputDeviceFromConfig();

#ifdef HAS_MIDI
if (midi.supported && midi.initThreadDone)
if (midi.initThreadDone)
{
setMidiInputDeviceFromConfig();
if (ui.configScreenShown && editor.currConfigScreen == CONFIG_SCREEN_MIDI_INPUT)
Expand Down Expand Up @@ -390,8 +390,7 @@ bool saveConfig(bool showErrorFlag)

saveAudioDevicesToConfig(audio.currOutputDevice, audio.currInputDevice);
#ifdef HAS_MIDI
if (midi.supported)
saveMidiInputDeviceToConfig();
saveMidiInputDeviceToConfig();
#endif

FILE *f = UNICHAR_FOPEN(editor.configFileLocationU, "wb");
Expand Down Expand Up @@ -470,6 +469,7 @@ static UNICHAR *getFullAudDevConfigPathU(void) // kinda hackish
return filePathU;
}

#ifdef HAS_MIDI
static UNICHAR *getFullMidiDevConfigPathU(void) // kinda hackish
{
int32_t mididevDotIniStrLen, ft2DotCfgStrLen;
Expand Down Expand Up @@ -501,6 +501,7 @@ static UNICHAR *getFullMidiDevConfigPathU(void) // kinda hackish

return filePathU;
}
#endif

static void setConfigFileLocation(void) // kinda hackish
{
Expand All @@ -523,8 +524,7 @@ static void setConfigFileLocation(void) // kinda hackish
return;
}

oldPathU[0] = 0;
tmpPathU[0] = 0;
oldPathU[0] = tmpPathU[0] = (UNICHAR)0;

if (GetCurrentDirectoryW(PATH_MAX - ft2DotCfgStrLen - 1, oldPathU) == 0)
{
Expand Down Expand Up @@ -674,9 +674,9 @@ static void setConfigFileLocation(void) // kinda hackish
strcat(editor.configFileLocationU, "/FT2.CFG");
#endif

if (midi.supported)
editor.midiConfigFileLocationU = getFullMidiDevConfigPathU();

#ifdef HAS_MIDI
editor.midiConfigFileLocationU = getFullMidiDevConfigPathU();
#endif
editor.audioDevConfigFileLocationU = getFullAudDevConfigPathU();
}

Expand Down Expand Up @@ -800,14 +800,6 @@ static void setConfigRadioButtonStates(void)
radioButtons[tmpID].state = RADIOBUTTON_CHECKED;

showRadioButtonGroup(RB_GROUP_CONFIG_SELECT);

// hide MIDI radio button if MIDI is not supported (hackish)
if (!midi.supported)
{
radioButton_t *t = &radioButtons[RB_CONFIG_MIDI_INPUT];
hideRadioButton(RB_CONFIG_MIDI_INPUT);
fillRect(t->x, t->y, RADIOBUTTON_W, RADIOBUTTON_H, PAL_DESKTOP);
}
}

void setConfigAudioRadioButtonStates(void) // accessed by other .c files
Expand Down Expand Up @@ -1031,10 +1023,7 @@ static void setConfigMiscCheckButtonStates(void)
checkBoxes[CB_CONF_CHANGE_PATTLEN_INS_DEL].checked = config.recTrueInsert;
checkBoxes[CB_CONF_MIDI_ALLOW_PC].checked = config.recMIDIAllowPC;
#ifdef HAS_MIDI
if (midi.supported)
checkBoxes[CB_CONF_MIDI_ENABLE].checked = midi.enable;
else
checkBoxes[CB_CONF_MIDI_ENABLE].checked = false;
checkBoxes[CB_CONF_MIDI_ENABLE].checked = midi.enable;
#else
checkBoxes[CB_CONF_MIDI_ENABLE].checked = false;
#endif
Expand Down Expand Up @@ -1125,8 +1114,7 @@ void showConfigScreen(void)
textOutShadow(21, 35, PAL_FORGRND, PAL_DSKTOP2, "Layout");
textOutShadow(21, 51, PAL_FORGRND, PAL_DSKTOP2, "Miscellaneous");
#ifdef HAS_MIDI
if (midi.supported)
textOutShadow(21, 67, PAL_FORGRND, PAL_DSKTOP2, "MIDI input");
textOutShadow(21, 67, PAL_FORGRND, PAL_DSKTOP2, "MIDI input");
#endif
textOutShadow(20, 93, PAL_FORGRND, PAL_DSKTOP2, "Auto save");

Expand Down Expand Up @@ -2089,17 +2077,7 @@ void cbMIDIAllowPC(void)
void cbMIDIEnable(void)
{
#ifdef HAS_MIDI
if (midi.supported)
{
midi.enable ^= 1;
}
else
{
checkBoxes[CB_CONF_MIDI_ENABLE].checked = false;
drawCheckBox(CB_CONF_MIDI_ENABLE);

okBox(0, "System message", "MIDI support is disabled for Windows XP as it is buggy!", NULL);
}
midi.enable ^= 1;
#else
checkBoxes[CB_CONF_MIDI_ENABLE].checked = false;
drawCheckBox(CB_CONF_MIDI_ENABLE);
Expand Down
2 changes: 1 addition & 1 deletion src/ft2_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#endif
#include "ft2_replayer.h"

#define PROG_VER_STR "1.81"
#define PROG_VER_STR "1.82"

// do NOT change these! It will only mess things up...

Expand Down
9 changes: 3 additions & 6 deletions src/ft2_keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,12 +1284,9 @@ static bool checkModifiedKeys(SDL_Keycode keycode)
else if (keyb.leftCtrlPressed)
{
#ifdef HAS_MIDI
if (midi.supported)
{
editor.currConfigScreen = 3;
showConfigScreen();
checkRadioButton(RB_CONFIG_MIDI_INPUT);
}
editor.currConfigScreen = 3;
showConfigScreen();
checkRadioButton(RB_CONFIG_MIDI_INPUT);
#endif
return true;
}
Expand Down
79 changes: 33 additions & 46 deletions src/ft2_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifdef _WIN32
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include <versionhelpers.h>
#include <SDL2/SDL_syswm.h>
#else
#include <unistd.h> // chdir()
Expand Down Expand Up @@ -96,12 +95,6 @@ int main(int argc, char *argv[])
#endif

#ifdef _WIN32
/* Disable MIDI support if using Windows XP,
** as it is unstable when initialized in an own thread.
*/
if (!IsWindowsVistaOrGreater())
midi.supported = false;

#ifndef _MSC_VER
SetProcessDPIAware();
#endif
Expand Down Expand Up @@ -153,19 +146,14 @@ int main(int argc, char *argv[])
#ifdef __APPLE__
osxSetDirToProgramDirFromArgs(argv);
#endif
if (!setupExecutablePath() || !loadBMPs())
if (!setupExecutablePath() || !loadBMPs() || !calcCubicSplineTable() || !calcWindowedSincTables())
{
cleanUpAndExit();
return 1;
}

if (!calcCubicSplineTable() || !calcWindowedSincTables()) // must be called before config is loaded
{
cleanUpAndExit();
return false;
}
loadConfigOrSetDefaults(); // config must be loaded at this exact point

loadConfigOrSetDefaults();
if (!setupWindow() || !setupRenderer())
{
// error message was shown in the functions above
Expand Down Expand Up @@ -233,17 +221,18 @@ int main(int argc, char *argv[])
}

#ifdef HAS_MIDI
// set up MIDI input (in a thread because it can take quite a while on f.ex. macOS)
if (midi.supported)
#ifdef __APPLE__
// MIDI init can take several seconds on Mac, use thread
midi.initMidiThread = SDL_CreateThread(initMidiFunc, NULL, NULL);
if (midi.initMidiThread == NULL)
{
midi.initMidiThread = SDL_CreateThread(initMidiFunc, NULL, NULL);
if (midi.initMidiThread == NULL)
{
showErrorMsgBox("Couldn't create MIDI initialization thread!");
cleanUpAndExit();
return 1;
}
showErrorMsgBox("Couldn't create MIDI initialization thread!");
cleanUpAndExit();
return 1;
}
#else
initMidiFunc(NULL);
#endif
#endif

hpc_ResetCounters(&video.vblankHpc); // quirk: this is needed for potential okBox() calls in handleModuleLoadFromArg()
Expand Down Expand Up @@ -278,7 +267,6 @@ static void initializeVars(void)
// clear common structs
#ifdef HAS_MIDI
memset(&midi, 0, sizeof (midi));
midi.supported = true;
#endif
memset(&video, 0, sizeof (video));
memset(&keyb, 0, sizeof (keyb));
Expand Down Expand Up @@ -340,26 +328,31 @@ static void initializeVars(void)
static void cleanUpAndExit(void) // never call this inside the main loop!
{
#ifdef HAS_MIDI
if (midi.supported)
#ifdef __APPLE__
// on Mac we used a thread to init MIDI (as it could take several seconds)
if (midi.initMidiThread != NULL)
{
if (midi.initMidiThread != NULL)
{
SDL_WaitThread(midi.initMidiThread, NULL);
midi.initMidiThread = NULL;
}
SDL_WaitThread(midi.initMidiThread, NULL);
midi.initMidiThread = NULL;
}
#endif
midi.enable = false; // stop MIDI callback from doing things
while (midi.callbackBusy) SDL_Delay(1); // wait for MIDI callback to finish

midi.enable = false; // stop MIDI callback from doing things
while (midi.callbackBusy) SDL_Delay(1); // wait for MIDI callback to finish
closeMidiInDevice();
freeMidiIn();
freeMidiInputDeviceList();

closeMidiInDevice();
freeMidiIn();
freeMidiInputDeviceList();
if (midi.inputDeviceName != NULL)
{
free(midi.inputDeviceName);
midi.inputDeviceName = NULL;
}

if (midi.inputDeviceName != NULL)
{
free(midi.inputDeviceName);
midi.inputDeviceName = NULL;
}
if (editor.midiConfigFileLocationU != NULL)
{
free(editor.midiConfigFileLocationU);
editor.midiConfigFileLocationU = NULL;
}
#endif

Expand Down Expand Up @@ -387,12 +380,6 @@ static void cleanUpAndExit(void) // never call this inside the main loop!
editor.configFileLocationU = NULL;
}

if (editor.midiConfigFileLocationU != NULL)
{
free(editor.midiConfigFileLocationU);
editor.midiConfigFileLocationU = NULL;
}

if (editor.binaryPathU != NULL)
{
free(editor.binaryPathU);
Expand Down
2 changes: 0 additions & 2 deletions src/ft2_midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ bool openMidiInDevice(uint32_t deviceID)
if (!midiInDev->ok)
return false;

/*
rtmidi_in_set_callback(midiInDev, midiInCallback, NULL);
if (!midiInDev->ok)
{
Expand All @@ -205,7 +204,6 @@ bool openMidiInDevice(uint32_t deviceID)
}

rtmidi_in_ignore_types(midiInDev, true, true, true);
*/

midiDeviceOpened = true;
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/ft2_midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
typedef struct midi_t
{
char *inputDeviceName, *inputDeviceNames[MAX_MIDI_DEVICES];
volatile bool supported, initThreadDone, callbackBusy, enable;
volatile bool initThreadDone, callbackBusy, enable;
bool rescanDevicesFlag;
uint32_t inputDevice, numInputDevices;
int16_t currMIDIVibDepth, currMIDIPitch;
#ifdef __APPLE__
SDL_Thread *initMidiThread;
#endif
} midi_t;

extern midi_t midi; // ft2_midi.c
Expand Down
5 changes: 4 additions & 1 deletion src/ft2_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ typedef struct cpu_t
typedef struct editor_t
{
UNICHAR *binaryPathU, *tmpFilenameU, *tmpInstrFilenameU; // used by saving/loading threads
UNICHAR *configFileLocationU, *audioDevConfigFileLocationU, *midiConfigFileLocationU;
UNICHAR *configFileLocationU, *audioDevConfigFileLocationU;
#ifdef HAS_MIDI
UNICHAR *midiConfigFileLocationU;
#endif

volatile bool mainLoopOngoing;
volatile bool busy, scopeThreadBusy, programRunning, wavIsRendering, wavReachedEndFlag;
Expand Down

0 comments on commit f4513ab

Please sign in to comment.