Cabbage Logo
Back to Cabbage Site

Fluidsynth or multi sample player example for Cabbage?

hello folks - as this is a subtopic for games, i can pretty much tell you that a soundfont player would be a pretty common request to make. it does appear that there is fluidsynth support in Csound so it doesn’t seem to be too difficult to get it going, but i was curious if there was a soundfont player example either in the Cabbage environment or elsewhere that could be looked at. most of the rest of the examples i’ve seen are single sample based so i was curious if a multisample based one was possible.

It’s possible and I already know of one other person using Fluidsynth in a game. The example here uses a GM sound font that might help get you started. That sf ships with Csound. You may need to search your install dir to find it. If you can’t find it I can send it on to you.

@rorywalsh - sorry for the incredibly late reply here. life and barely learning C++ got in the way over the spring semester.

so, i did download the example you linked and tried to run it in Cabbage and immediate got errors:
“No Csound channels found in orchestra. If you wish to have real time control of parameters please declare channels using the chn or chnexport opcodes.”

technically the demo script should run just fine but i’m suspecting there might be a need to “Cabbage-ify” the code a bit? the output channels definitions are different but they seem to functionally similar. i think basically what i’m asking is can you run this code as is on your computer, assuming you got the GM soundbank loaded correctly? i would also like to see what it would take to run if i used this in your sequencer script you made for me. this example used a score or possibly a separate MIDI file (it’s a bit unclear, but it looks like the configuration is set for using the advancedMIDI.mid file), but you used an array of kNoteValues so wondering how to translate one to the other. any help appreciated!

scott

Good to hear from you. I’ll take a look early next week. I’m away at the minute…

This version will run in Cabbage. Note that it just plays the same note over and over, but perhaps you can tweak it to your needs. Instead of reading from a midi file it just triggers the playback instrument using events. Just make sure that you have the .sf2 file in the same folder as the .csd, the .sf2 file can be found in the Csound manual folder under examples.
fluidEngine.csd (1.1 KB)

thanks very much, Rory! - worked fine once i downloaded the sf2 file as you instructed.

next i tried putting the fluidsynth oriented definitions in your existing Sequencer.csd file and running it in Unity 2017. having problems with it. using my adapted script, sequencer is frozen and does not play anything. with the original sequencer everything plays fine with the simple synth. can you let me know what i’m not doing right here? interesting to note that there’s a unix executable icon for the sequencer.csd file but not for my edited script. also, i’m putting the sf2 in the StreamingAssets folder, but i originally had it in the CsoundScripts folder. either way it didn’t work. wondering if it might be a Csound compilation error?

EDIT:turned the logging on and got these errors to the console:

could not open library
‘/Users/scottlooney/testSequencer/Assets/StreamingAssets/CsoundLib64.framework/Resources/Opcodes64/libfluidOpcodes.dylib’ (-1)
WARNING: could not open library ‘/Users/scottlooney/testSequencer/Assets/StreamingAssets/CsoundLib64.framework/Resources/Opcodes64/libimage.dylib’ (-1)
WARNING: could not open library ‘/Users/scottlooney/testSequencer/Assets/StreamingAssets/CsoundLib64.framework/Resources/Opcodes64/libosc.dylib’ (-1)
WARNING: could not open library ‘/Users/scottlooney/testSequencer/Assets/StreamingAssets/CsoundLib64.framework/Resources/Opcodes64/libpmidi.dylib’ (-1)
WARNING: could not open library ‘/Users/scottlooney/testSequencer/Assets/StreamingAssets/CsoundLib64.framework/Resources/Opcodes64/librtpa.dylib’ (-1)
WARNING: could not open library ‘/Users/scottlooney/testSequencer/Assets/StreamingAssets/CsoundLib64.framework/Resources/Opcodes64/libstdutil.dylib’ (-1)
WARNING: could not open library ‘/Users/scottlooney/testSequencer/Assets/StreamingAssets/CsoundLib64.framework/Resources/Opcodes64/libvirtual.dylib’ (-1)
WARNING: could not open library ‘/Users/scottlooney/testSequencer/Assets/StreamingAssets/CsoundLib64.framework/Resources/Opcodes64/libwidgets.dylib’ (-1)
WARNING: could not open library ‘/Users/scottlooney/testSequencer/Assets/StreamingAssets/CsoundLib64.framework/Resources/Opcodes64/libwiimote.dylib’ (-1)
UnityEngine.MonoBehaviour:print(Object)
CsoundUnity:logCsoundMessages() (at Assets/Scripts/CsoundUnityScripts/CsoundUnity.cs:301)

Sequencer-fluid.csd (2.2 KB)

The Csound files contained in the CsoundScript folder are copied to the streaming assets folder each time you update or modify them. There is a script which takes cares of that. You may well have to place the SF file into the streaming assets folder. It will have a different icon because CsoundUnity gives it a somewhat unique file extension when it copies it over. Nothing to worry about there. Also nothing to worry about regarding those warnings. They are annoying, but non fatal.

I will take a look at your file, but I don’t have a PC that can run Unity with me on my travels. But hopefully we can sort it out somehow!

Your score sems to be a little messed up:

</CsInstruments>
<CsScore>
f100 0 8 2 0 10 0 0 8 0 0 0
i"ROW_SEQUENCER" 0 [3600*12] 1
i"ROW_SEQUENCER" 0 [3600*12] 2
i"ROW_SEQUENCER" 0 [3600*12] 3
i"ROW_SEQUENCER" 0 [3600*12] 4
i"ROW_SEQUENCER" 0 [3600*12] 5
i"ROW_SEQUENCER" 0 [3600*12] 6
i"ROW_SEQUENCER" 0 [3600*12] 7
i"ROW_SEQUENCER" 0 [3600*12] 8

</CsScore>

<CsScore>
i1 0 z
i"FluidVol" 0 z
e
</CsScore>
</CsoundSynthesizer>

You have nested a second score section into the first. That won’t do. Try this:

</CsInstruments>
<CsScore>
f100 0 8 2 0 10 0 0 8 0 0 0
i"ROW_SEQUENCER" 0 [3600*12] 1
i"ROW_SEQUENCER" 0 [3600*12] 2
i"ROW_SEQUENCER" 0 [3600*12] 3
i"ROW_SEQUENCER" 0 [3600*12] 4
i"ROW_SEQUENCER" 0 [3600*12] 5
i"ROW_SEQUENCER" 0 [3600*12] 6
i"ROW_SEQUENCER" 0 [3600*12] 7
i"ROW_SEQUENCER" 0 [3600*12] 8
i"FluidVol" 0 z
</CsScore>
</CsoundSynthesizer>

hmm. well i changed the script but it’s still freezing and not doing playback. BTW this is on a Mac not a PC so if it runs for you it should run for me. i’ll send you the Unity project. and the icon change i mentioned is actually on the meta files. probably meaningless but the sequencer.csd has a meta file that looks like a Unix icon, but my altered file does not.

an additional weird error - Cabbage wants to hang if i save the edited sequencer file, but your sequencer file edits just fine. does it not like the hyphen in my filename perchance? or maybe it’s something not compiling? this is from a version of Cabbage 1.0 in late May of this year (May 31).

I get a crash each time I save my .csd file in Cabbage, but it runs fine the first time. I’m not sure why. It may have something to do with the fluidengine, but I can’t be sure.

As I mentioned earlier, I don’t have Unity here and can’t test it with Unity until later in August when I return to Ireland. Can you check the Unity logs and see what they say? Perhaps the clue lies there somewhere?

i looked at the Unity editor logs, they are no help as there are no Unity errors but i did find a lot more messages from the Csound log. it looks like a possible parsing error on line 14. it doesn’t seem to understand the reference to gienginenum1 , but then it iterates every character of line 14 before giving up. including an edited version of the log with the relevant info. the whole thing starts with:

error: syntax error, unexpected T_IDENT (token “fluidEngine”)

and then after the iteration of line characters ends with:

Unexpected untyped word gienginenum1 when expecting a variable

after that the parser fails and it can’t compile the orchestra.log.txt (3.9 KB)

anyway, will keep working on this. just curious could you put me in contact with those folks using the fluidsynth opcode in a game, especially if it’s Unity? i’m getting an ominous sense that ‘later in August’ means like end of August, and i have a sort of deadline of mid to late Sept to meet. i’ll try to keep moving forward on this, though.

This looks very much like Csound is not finding the relevant fluid opcodes? Which is strange considering that it runs Ok in Cabbage. You can try to remove the Csound framework from your streaming assets folder. CsoundUnity might present you with a little dialogue box stating that it can’t find Csound, but you can ignore that. It should pick up the system installed Csound Ok. I will ask Will if he wouldn’t mind popping in here to offer some advice. He just finished creating some adaptive work using Csound and Unity. And he works mostly on OSX too.

Btw, to double check it is an issue with the fluid stuff, can you simply add the following line to a .csd file that you know works fine in Unity?

gienginenum1 fluidEngine

Just add it after the header section before any instr definitions. If you still get the same error then it’s clear the error is with the fluidengine.

One other thing, you could try using the old-style soundfont opcodes? In fact, they may work just fine for you. TBH, I only just now realized they exist at all! Definitely worth a shot.

hi Rory - quick reply as it it’s quite late here. i added the gienginenum1 fluidEngine line to the working sequencer example you sent me and it freezes and gives exactly the same parsing error as before. so it does appear to be an issue with the fluidEngine opcode not being found. i’ll check out the sound font player example later, and see if perhaps they work better. thanks for all your help!

okay - well i tried to use the example there to test it in Cabbage, and did my best to edit the csound doc example file and make the code appear somewhat similar to your testable fluidsynth version and i’m getting nowhere. the error mentions the lack of STK opcodes, but that may just be spurious. i think the first relevant error is when it says ‘isf is used before defined’. i did play around a bit and nothing really changed, so once again, i’m lost and need some help testing this to run in Cabbage. there just seem to be so many ways to define behavior it gets really confusing. anyway check it out when you have time. thanks! sfinstr-edit.csd (1.2 KB)

EDIT - looks like parser error at the moment. Cabbage sees the definition of the metronome, but gets totally confused and never acknowledges the next definition as being valid.

it never mentions the sf2inst i defined at all, but instead skips it and regards at least some of the following lines as instr definitions. weird.

isf is causing an error as it ought to be gisf.
The STK error does seem to be spurious and I am not getting it.
Here is a version of your csd that works for me. I made a few other tweaks as well.sfinstr-edit.csd (1.0 KB)

Incidentally, the fluidsynth opcodes produce nasty quantisation artefacts on Cabbage2. They sound fine on Cabbage Classic.

thanks very much @iainmccurdy! i’m eventually using this inside Unity and Unity for sure did not want to load the fluid opcode. not sure about this one in Unity for the sequencer project, but will give it a try. basically i realized a sample player would be desirable and sf2 files are pretty well known multisample formats.

also just a small reminder for the sequencer thread that i am also needing a simple, event driven version of your GEN02 when you have a moment. thanks!

bang on! that worked perfectly @iainmccurdy! i tweaked the script to get it to work with the sequencer example and it works just fine. now i’ll need to figure out how to send a bit more data so that i switch between instruments, and other things, but i’ll go back to the sequencer thread for that if i have further questions. thanks again!

For what it’s worth I fixed that parsing issue you reported above. Newer versions shouldn’t suffer from such instrumental confusion :+1: