Skip to content

Commit

Permalink
Merge pull request #98 from ccrma/fluidsynth_api_v10
Browse files Browse the repository at this point in the history
update FluidSynth.cpp to APIv10.1, fixed segfault in FluidSynth-play.ck example
  • Loading branch information
kellycochran authored Apr 15, 2024
2 parents 007c87f + 0421f33 commit f152568
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 32 deletions.
10 changes: 6 additions & 4 deletions FluidSynth/FluidSynth-play.ck
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand All @@ -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))
{
Expand Down
69 changes: 46 additions & 23 deletions FluidSynth/FluidSynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -104,14 +104,22 @@ 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);

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++) {
fluid_synth_activate_tuning(m_synth, chan, 0, 0, false);
Expand All @@ -121,14 +129,22 @@ 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);

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++) {
fluid_synth_activate_tuning(m_synth, chan, 0, 0, false);
Expand All @@ -154,20 +170,27 @@ class FluidSynth
fluid_synth_tune_notes(m_synth, 0, chan, 1, &noteNum , &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
contain 4-byte ints (at least on my 64-bit linux system). So we
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);
}

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, pitches->size(),
noteNumArr, &pitches->m_vector[0], false);

fluid_synth_tune_notes(m_synth, 0, chan, api->object->array_float_size(pitches),
noteNumArr, pitchesArr, false);

delete [] noteNumArr;
}
Expand Down Expand Up @@ -449,12 +472,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)
Expand All @@ -464,12 +487,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)
Expand All @@ -478,12 +501,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)
Expand All @@ -493,12 +516,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)
Expand Down Expand Up @@ -543,12 +566,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)
Expand All @@ -559,12 +582,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)
Expand Down
31 changes: 26 additions & 5 deletions FluidSynth/makefile.mac
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
#-----------------------------------
# makefile.mac
# macOS-specific build configuration
#-----------------------------------

ARCHS?=x86_64 arm64
ARCHOPTS=$(addprefix -arch ,$(ARCHS))
FLUIDSYNTH_PREFIX=/usr/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?=

FLUIDSYNTH_PREFIX=/opt/local
# construct compiler option string
ARCHOPTS=$(addprefix -arch ,$(ARCHS))

# compiler flags
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
# linker flags
LDFLAGS+=-mmacosx-version-min=10.9 -shared -lc++ $(ARCHOPTS) -L$(FLUIDSYNTH_PREFIX)/lib -lfluidsynth

LD=clang++
# which C++ compiler
CXX=clang++
# which linker to user
LD=clang++

0 comments on commit f152568

Please sign in to comment.