From d1677325ebc2a13d748673f3f106cce83ee55063 Mon Sep 17 00:00:00 2001 From: kellycochran Date: Sun, 14 Apr 2024 15:28:45 -0700 Subject: [PATCH 1/2] update FluidSynth.cpp to APIv10.1, fixed segfault in FluidSynth-play.ck example --- FluidSynth/FluidSynth-play.ck | 10 +++++--- FluidSynth/FluidSynth.cpp | 48 ++++++++++++++++++----------------- FluidSynth/makefile.mac | 33 +++++++++++++++++++----- 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/FluidSynth/FluidSynth-play.ck b/FluidSynth/FluidSynth-play.ck index 5d47034d..15a8672b 100644 --- a/FluidSynth/FluidSynth-play.ck +++ b/FluidSynth/FluidSynth-play.ck @@ -18,6 +18,11 @@ if(!min.open(filename)) chout <= filename <= ": " <= min.numTracks() <= " tracks\n"; +FluidSynth m => rev; +m => dac; +0.91 => m.gain; +m.open(sfont); + int done; for(int t; t < min.numTracks(); t++) @@ -32,10 +37,7 @@ minute => now; fun void track(int t) { - FluidSynth m => rev; - m => dac; - 0.91 => m.gain; - m.open(sfont); + while(min.read(msg, t)) { diff --git a/FluidSynth/FluidSynth.cpp b/FluidSynth/FluidSynth.cpp index a6ee4081..49b3dee5 100644 --- a/FluidSynth/FluidSynth.cpp +++ b/FluidSynth/FluidSynth.cpp @@ -60,7 +60,7 @@ class FluidSynth m_settings = new_fluid_settings(); m_synth = new_fluid_synth(m_settings); - fluid_synth_set_sample_rate(m_synth, m_srate); + fluid_settings_setnum(m_settings, "synth.sample-rate", m_srate); } ~FluidSynth() @@ -104,14 +104,15 @@ class FluidSynth fluid_synth_bank_select(m_synth, chan, bankNum); } - void setTuning(int chan, Chuck_ArrayFloat * tuning) + void setTuning(int chan, Chuck_ArrayFloat * tuning, CK_DL_API api) { bool allChans = false; if (chan < 0) { allChans = true; chan = 0; } - fluid_synth_activate_key_tuning(m_synth, 0, chan, "", &tuning->m_vector[0], false); + fluid_synth_activate_key_tuning(m_synth, 0, chan, "", (double *) tuning, false); + if (allChans) { for (chan = 0 ; chan<16 ; chan++) { fluid_synth_activate_tuning(m_synth, chan, 0, 0, false); @@ -121,14 +122,15 @@ class FluidSynth } } - void setOctaveTuning(int chan, Chuck_ArrayFloat * tuning) + void setOctaveTuning(int chan, Chuck_ArrayFloat * tuning, CK_DL_API api) { bool allChans = false; if (chan < 0) { allChans = true; chan = 0; } - fluid_synth_activate_octave_tuning(m_synth, 0, chan, "", &tuning->m_vector[0], false); + fluid_synth_activate_octave_tuning(m_synth, 0, chan, "", (double *) tuning, false); + if (allChans) { for (chan = 0 ; chan<16 ; chan++) { fluid_synth_activate_tuning(m_synth, chan, 0, 0, false); @@ -154,7 +156,7 @@ class FluidSynth fluid_synth_tune_notes(m_synth, 0, chan, 1, ¬eNum , &pitch, false); } - void tuneNotes(Chuck_ArrayInt * noteNums, Chuck_ArrayFloat * pitches, int chan) + void tuneNotes(Chuck_ArrayInt * noteNums, Chuck_ArrayFloat * pitches, int chan, CK_DL_API api) { /* This ugly hack is required because Chuck_ArrayInt doesn't actually @@ -162,12 +164,12 @@ class FluidSynth need to copy the elements into an int array. */ int * noteNumArr; - noteNumArr = new int[noteNums->size()]; - for (int i = 0; i < noteNums->size(); i++) { - noteNumArr[i] = (int) noteNums->m_vector[i]; + noteNumArr = new int[api->object->array_int_size(noteNums)]; + for (int i = 0; i < api->object->array_int_size(noteNums); i++) { + noteNumArr[i] = (int) api->object->array_int_get_idx(noteNums,i); } - fluid_synth_tune_notes(m_synth, 0, chan, pitches->size(), - noteNumArr, &pitches->m_vector[0], false); + fluid_synth_tune_notes(m_synth, 0, chan, api->object->array_float_size(pitches), + noteNumArr, (double *) pitches, false); delete [] noteNumArr; } @@ -449,12 +451,12 @@ CK_DLL_MFUN(fluidsynth_setTuning) Chuck_ArrayFloat * tuning = (Chuck_ArrayFloat *) GET_NEXT_OBJECT(ARGS); - if (tuning->size() != 128 ) { + if (API->object->array_float_size(tuning) != 128 ) { printf("FluidSynth ERROR: setTuning() requires a tuning array of exactly 128 values\n"); return; } - f_data->setTuning(-1, tuning); + f_data->setTuning(-1, tuning, API); } CK_DLL_MFUN(fluidsynth_setTuningChan) @@ -464,12 +466,12 @@ CK_DLL_MFUN(fluidsynth_setTuningChan) Chuck_ArrayFloat * tuning = (Chuck_ArrayFloat *) GET_NEXT_OBJECT(ARGS); t_CKINT chan = GET_NEXT_INT(ARGS); - if (tuning->size() != 128 ) { + if (API->object->array_float_size(tuning) != 128 ) { printf("FluidSynth ERROR: setOctaveTuning() requires a tuning array of exactly 12 values\n"); return; } - f_data->setTuning(chan, tuning); + f_data->setTuning(chan, tuning, API); } CK_DLL_MFUN(fluidsynth_setOctaveTuning) @@ -478,12 +480,12 @@ CK_DLL_MFUN(fluidsynth_setOctaveTuning) Chuck_ArrayFloat * tuning = (Chuck_ArrayFloat *) GET_NEXT_OBJECT(ARGS); - if (tuning->size() != 12 ) { + if (API->object->array_float_size(tuning) != 12 ) { printf("FluidSynth ERROR: setOctaveTuning() requires a tuning array of exactly 12 values\n"); return; } - f_data->setOctaveTuning(-1, tuning); + f_data->setOctaveTuning(-1, tuning, API); } CK_DLL_MFUN(fluidsynth_setOctaveTuningChan) @@ -493,12 +495,12 @@ CK_DLL_MFUN(fluidsynth_setOctaveTuningChan) Chuck_ArrayFloat * tuning = (Chuck_ArrayFloat *) GET_NEXT_OBJECT(ARGS); t_CKINT chan = GET_NEXT_INT(ARGS); - if (tuning->size() != 12 ) { + if (API->object->array_float_size(tuning) != 12 ) { printf("FluidSynth ERROR: setOctaveTuning() requires a tuning array of exactly 12 values\n"); return; } - f_data->setOctaveTuning(chan, tuning); + f_data->setOctaveTuning(chan, tuning, API); } CK_DLL_MFUN(fluidsynth_resetTuning) @@ -543,12 +545,12 @@ CK_DLL_MFUN(fluidsynth_tuneNotes) Chuck_ArrayInt * noteNums = (Chuck_ArrayInt *) GET_NEXT_OBJECT(ARGS); Chuck_ArrayFloat * pitches = (Chuck_ArrayFloat *) GET_NEXT_OBJECT(ARGS); - if (pitches->size() != noteNums->size()) { + if (API->object->array_float_size(pitches) != API->object->array_int_size(noteNums)) { printf("FluidSynth ERROR: tuneNotes requires pitches and noteNums arrays to be the same length\n"); return; } - f_data->tuneNotes(noteNums, pitches, 0); + f_data->tuneNotes(noteNums, pitches, 0, API); } CK_DLL_MFUN(fluidsynth_tuneNotesChan) @@ -559,12 +561,12 @@ CK_DLL_MFUN(fluidsynth_tuneNotesChan) Chuck_ArrayFloat * pitches = (Chuck_ArrayFloat *) GET_NEXT_OBJECT(ARGS); t_CKINT chan = GET_NEXT_INT(ARGS); - if (pitches->size() != noteNums->size()) { + if (API->object->array_float_size(pitches) != API->object->array_int_size(noteNums)) { printf("FluidSynth ERROR: tuneNotes requires pitches and noteNums arrays to be the same length\n"); return; } - f_data->tuneNotes(noteNums, pitches, chan); + f_data->tuneNotes(noteNums, pitches, chan, API); } CK_DLL_MFUN(fluidsynth_setPitchBend) diff --git a/FluidSynth/makefile.mac b/FluidSynth/makefile.mac index d7180ac5..dcb57fd2 100644 --- a/FluidSynth/makefile.mac +++ b/FluidSynth/makefile.mac @@ -1,11 +1,32 @@ +#----------------------------------- +# makefile.mac +# macOS-specific build configuration +#----------------------------------- -ARCHS?=x86_64 arm64 -ARCHOPTS=$(addprefix -arch ,$(ARCHS)) +FLUIDSYNTH_PREFIX=/usr/local -FLUIDSYNTH_PREFIX=/opt/local +# to build for the native architecture: (leave blank) +# ARCHS?= +# +# to build for intel: +# ARCHS?=x86_64 +# +# to build for apple silicon: +# ARCHS?=arm64 +# +# to build a universal=binary chugin: +# ARCHS?=x86_64 arm64 +ARCHS?= -FLAGS+=-mmacosx-version-min=10.9 -I$(CK_SRC_PATH) $(ARCHOPTS) -I$(FLUIDSYNTH_PREFIX)/include -LDFLAGS+=-mmacosx-version-min=10.9 -shared -lc++ $(ARCHOPTS) -L$(FLUIDSYNTH_PREFIX)/lib +# construct compiler option string +ARCHOPTS=$(addprefix -arch ,$(ARCHS)) -LD=clang++ +# compiler flags +FLAGS+=-mmacosx-version-min=10.9 -I$(CK_SRC_PATH) $(ARCHOPTS) -fPIC -I$(FLUIDSYNTH_PREFIX)/include +# linker flags +LDFLAGS+=-mmacosx-version-min=10.9 -shared -lc++ $(ARCHOPTS) -L$(FLUIDSYNTH_PREFIX)/lib -lfluidsynth + +# which C++ compiler CXX=clang++ +# which linker to user +LD=clang++ From 0421f33864bd9e825adb40a9f9b72b560c8fced1 Mon Sep 17 00:00:00 2001 From: kellycochran Date: Sun, 14 Apr 2024 17:24:54 -0700 Subject: [PATCH 2/2] fixed FluidSynth.cpp so not-chuck arrays are correctly passed into FS API, and removed tag in makefile not needed --- FluidSynth/FluidSynth.cpp | 27 ++++++++++++++++++++++++--- FluidSynth/makefile.mac | 2 +- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/FluidSynth/FluidSynth.cpp b/FluidSynth/FluidSynth.cpp index 49b3dee5..964b652b 100644 --- a/FluidSynth/FluidSynth.cpp +++ b/FluidSynth/FluidSynth.cpp @@ -111,7 +111,14 @@ class FluidSynth allChans = true; chan = 0; } - fluid_synth_activate_key_tuning(m_synth, 0, chan, "", (double *) tuning, false); + + double * tuningArr; + tuningArr = new double[api->object->array_float_size(tuning)]; + for (int i = 0; i < api->object->array_float_size(tuning); i++) { + tuningArr[i] = (double) api->object->array_float_get_idx(tuning,i); + } + + fluid_synth_activate_key_tuning(m_synth, 0, chan, "", tuningArr, false); if (allChans) { for (chan = 0 ; chan<16 ; chan++) { @@ -129,7 +136,14 @@ class FluidSynth allChans = true; chan = 0; } - fluid_synth_activate_octave_tuning(m_synth, 0, chan, "", (double *) tuning, false); + + double * tuningArr; + tuningArr = new double[api->object->array_float_size(tuning)]; + for (int i = 0; i < api->object->array_float_size(tuning); i++) { + tuningArr[i] = (double) api->object->array_float_get_idx(tuning,i); + } + + fluid_synth_activate_octave_tuning(m_synth, 0, chan, "", tuningArr, false); if (allChans) { for (chan = 0 ; chan<16 ; chan++) { @@ -168,8 +182,15 @@ class FluidSynth for (int i = 0; i < api->object->array_int_size(noteNums); i++) { noteNumArr[i] = (int) api->object->array_int_get_idx(noteNums,i); } + + double * pitchesArr; + pitchesArr = new double[api->object->array_float_size(pitches)]; + for (int i = 0; i < api->object->array_float_size(pitches); i++) { + pitchesArr[i] = (double) api->object->array_float_get_idx(pitches,i); + } + fluid_synth_tune_notes(m_synth, 0, chan, api->object->array_float_size(pitches), - noteNumArr, (double *) pitches, false); + noteNumArr, pitchesArr, false); delete [] noteNumArr; } diff --git a/FluidSynth/makefile.mac b/FluidSynth/makefile.mac index dcb57fd2..6f46e569 100644 --- a/FluidSynth/makefile.mac +++ b/FluidSynth/makefile.mac @@ -22,7 +22,7 @@ ARCHS?= ARCHOPTS=$(addprefix -arch ,$(ARCHS)) # compiler flags -FLAGS+=-mmacosx-version-min=10.9 -I$(CK_SRC_PATH) $(ARCHOPTS) -fPIC -I$(FLUIDSYNTH_PREFIX)/include +FLAGS+=-mmacosx-version-min=10.9 -I$(CK_SRC_PATH) $(ARCHOPTS) -I$(FLUIDSYNTH_PREFIX)/include # linker flags LDFLAGS+=-mmacosx-version-min=10.9 -shared -lc++ $(ARCHOPTS) -L$(FLUIDSYNTH_PREFIX)/lib -lfluidsynth