Cabbage Logo
Back to Cabbage Site

Load audio file to ftgen tables when loading presets

Hello. I have made a simple granular instrument using the good old “Grain”. To save presets I have used 2 filebuttons to save and remove and a Combox. The samples loads into the i1 ftgen and i2 ftgen tables with 2 filebuttons and Sfile1, Sfile2. So far everything is going well, the only thing that doesn’t work as I would like is that when loading saved presets they remember the settings of the knobs but do not load the audio that had been loaded in the preset that was saved. It seems that the presets have information about the names of the .wav audio files that were used since the console shows me an error with those names but it does not load them, and every time I load a preset I have to reload the .wav audios.
Is there any way to make it so that when loading the presets the samples in the table are also loaded without having to load them manually? The console shows and error message for the first table, and then if the first sound is loaded it show the error message for the second table. Until both sounds are loaded and the instrument can be played. Greetings!

This is the instrument and the console warning. I attach the csd. to don´t make this post very long with code. This is what the console says:

“soundin cannot open G:/VOIDNESS/“C:/Users/ALE/Desktop/beats.wav”: No Error.
ftable 1: Failed to open file G:/VOIDNESS/“C:/Users/ALE/Desktop/beats.wav”
f 1 0.00 0.00 1.00 “G:/VOIDNESS/“C:/Users/ALE/Desktop/beats.wav”” …
INIT ERROR in instr 1 (opcode ftgen.iS) line 65: ftgen error
from file G:\VOIDNESS\Grain 1.csd (1)
i1 ftgen.iS 1 0 0 1 SFile1 0 0 0
T 95.823 - note deleted. instr 1 had 1 init errors”

Grain1.csd (6.9 KB)

I think you can get rid of the ftgen error with this but I’m not sure. As for the presets I’ve got no clue

if filevalid(SFile1) == 1 then
    i1 ftgen 1, 0,0 ,1,  SFile1, 0, 0, 0
if filevalid(SFile2) == 1 then
    i2 ftgen 2, 0,0 ,1,  SFile2, 0, 0, 0

Thank you @PrimroseO, the ftgen error only appears when trying to load the presets that make reference to the .wav files, when I load the .wav with the filebutton manually they load OK and they work. Anyway I will test your suggestion.
I found a workaround, I use Cubase and if I save the preset within Cubase and not within the Vst. It do load the .wav files. correctely.
it’s weird because I didn’t think that would work, I tried it just out of curiosity. Either way it would be better to be able to load presets that reference to audio files with Cabbage’s preset system.

The paths look a little odd? Btw, there is a newer, as of yet undocumented, system of presets. Check out the NewPreset.csd from the Misc menu. It might not fix this error, but moving forward I think it’s better system.

p/s/ by odd, I mean G:/VOIDNESS/“C:/Users/ALE/Desktop/beats.wav”: looks like two paths?

Thank you @rorywalsh
I tried with the new system of presets and I get the same error. The two paths in the console are because the first one is where the .csd is located and the second is where the .wav is. In this case the first is G:/VOIDNESS/ because it makes reference to a pendrive G: that it was where the .csd was and the second C:/Users/ALE/Desktop/beats.wav”:* is for the .wav
I also tried to put the .csd and the .wav in the same folder but it do not fix the error. I get this warning with the new system and the .csd and the .wav in the same folder. It seems that it is trying to open the .wav file but for some reason it is not possible.

soundin cannot open C:/Users/ALE/Desktop/Grain1/“C:/Users/ALE/Desktop/Grain1/beats.wav”: No Error.
ftable 1: Failed to open file C:/Users/ALE/Desktop/Grain1/“C:/Users/ALE/Desktop/Grain1/beats.wav”
f 1 0.00 0.00 1.00 “C:/Users/ALE/Desktop/Grain1/“C:/Users/ALE/Desktop/Grain1/beats.wav”” …
INIT ERROR in instr 1 (opcode ftgen.iS) line 69: ftgen error
from file C:\Users\ALE\Desktop\Grain1\Grain1.csd (1)
i1 ftgen.iS 1 0 0 1 SFile1 0 0 0
T 22.471 - note deleted. instr 1 had 1 init errors

This looks like a bug. I’ll take a look tomorrow. It shouldn’t be prepending the path…

Ok I didn`t know if it was a mistake in the way I was trying to upload the table to ftgen or if it was a bug. Good luck with that.

I just pushed a fix for this, but it will need further testing. Cabbage no longer sets its current working dir to that of the current .csd file as it was it was causing problems with multiple hosts. So everything now works with absolute file paths. It seems I missed one part of the code base when updating. Can you let me know if the new build works Ok? You can access it here once it’s done.

I’m glad you found a solution. I’m going to check it today with my instruments and let you know what happens. I have 2 instruments to test that use the same resource to upload audio tables to Ftgen, they use the Grain and Grain 3 opcodes

I have tried the fix with the 2 instruments and I can confirm that now it works much better, the .wav samples are recorded now. The two instruments that I,ve used to teste are instruments that use grain 1 and grain 3. I am attaching the instruments in case they can help to test on other systems, I have only tried it on Windows with the old preset system and not the new one.

I think the overall situation is really much better with this fix. However, I keep noticing some things that seem strange to me: in both instruments the preset is not saved correctly with the reference to the .wav if the daw is playing, it is saved correctly if the Daw has been paused.

Grain 1; a daw crash can be produced if the preset is saved or loaded when the instrument is playing. To Save and load the preset with reference to a .wav the instrument must be paused as it can produce Daw crashes. The problem with Grain 1 crashing when load presets while playing was also occurring before this fix although more frequently since it always happened, now it only happens sometimes. Also, I don’t know if this is a Grain 1, Csound or Cabbage problem as Grain 3 doesn’t crash at all.

Grain 3: Presets with references to .wav files can be loaded when the instrument is running, if the preset is saved when the instrument is running it is not saved correctly with the reference to the .wav files but no crash occurs. Overall the instrumkent with Grain 3 is much more stable and the only strange thing now is that the preset with .wav reference could not be saved with the instrument is playing.

Test (4.8 KB)

I hope it help, thanks.

Thanks for testing. I’ll try these out and see what the problem is. But I’m afraid it won’t be until early next week. Sorry about that, I hope it doesn’t hamper development too much!

Thank you for looking at it, now with these fix Grain 3 instrument is almost fully functional!. I suspect that the issue happening with Grain 1 could be that Grain 1 opcode is very old or maybe it has something to do that Grain 1 is working with non-deferred tables and Grain 3 works with deferred table size.

What DAW are you testing with? I just tried Grain1 with Reaper and it works fine for me. I’m on Windows 10 here…

Hello thanks for testing it and sorry for the delay, I couldn’t post these last days.

Grain 1’s crash issue when uploading new audio while running happens on different DAWS. I’ve tried it on Reaper, Cubase and Ableton and it happens on all of them. I’ve attached a short video to show what happens and how it happens.

Then I would like to comment that I have continued to develop the other instrument that I am working on which uses the Grain 3 opcode. In a recent update I have changed the way in which the .wav audio is uploaded to the table since ftgen or fgenonce need to deliver size values to Grain3 that are the next power of 2 with respect to the size value of the sample. I have created a code that detects the appropriate value of what would be the next power of 2 to inform ftgenonce so that the table can be automatically loaded without the need to manually inform the size. For that I have used the opcodes “filelen” “filesr” and “filenchnls" in combination with Sfile and that allowed to upload mono or stereo files of different sample rates without changing the pitch. I think that it was an improvement since before it I was using a slider to manually inform the number of samples required, which I need no more. Other benefit of this update is that now the instrument track in a daw could be frozen and it will recall the samples when is unfrozen. The same happens if a project is saved, when the project is opened again now it recall the files. I have no idea why but I think it is much better than before.
The downside is that when using this new way it fails again to load samples in a problem that seems to be related to diskin and the opcodes “filelen” “filesr”,“filenchnls” which are involved here. The soundin error that used to appear no longer appears and was fixed with Build 2.9.44 now the problem is with diskin. I attach a simple example of the code I have created to upload the tables, apart from the complexities that the instrument may have to isolate the problem in this case uploading the audio to an “oscili” (but the result is the same). I’ve seen that one of the examples provided with Cabbage called “Grain3FilePlayer” by Iain McCurdy uses a similar procedure to calculate the next power of 2 of the tables. Using that example for testing, I have added to that instrument the option to save presets and the result was the same as in my instrument since it seems that although in a slightly different way he also used “filelen” “filesr” and “filenchnls” to calculate the size of the table.

@rorywalsh If you could see what happens here to find some solution when possible, especially in the second issue with diskin I will be grateful. I think that beyond helping the performance of these particular instruments fixing this type of errors could help to improve Cabbage in regards to saving presets that involve the use of audio files.

This is the code for opening tables that need power of 2:

form caption(“Sample upload”) size(400, 300), guiMode(“queue”), pluginId(“1287”), colour (0,100,0)

keyboard bounds(8, 158, 381, 95)

filebutton bounds(14, 18, 80, 40) channel(“loadfile”)

combobox bounds(220, 20, 74, 25), populate("*.snaps"), channelType(“string”) automatable(0) channel(“combo38”) value(“0”)

filebutton bounds(164, 20, 60, 25), text(“Save”, “Save”), populate("*.snaps", “test”), mode(“named preset”) channel(“filebutton39”)

filebutton bounds(164, 50, 60, 25), text(“Remove”, “Remove”), populate("*.snaps", “test”), mode(“remove preset”) channel(“filebutton40”)

-n -d -+rtmidi=NULL -M0 --midi-key-cps=4 --midi-velocity-amp=5 -m0d

; Initialize the global variables.

ksmps = 32

nchnls = 2

0dbfs = 1

instr 1

Sfile chnget “loadfile”


ilen filelen Sfile

isr filesr Sfile

ich filenchnls Sfile

isamp = ilen*isr

;print ilen, isr, isamp

i_ipow2 = 1

while i_ipow2 < isamp do

i_ipow2 = i_ipow2 * 2


printf “File name %s\n”, 1, Sfile

printf “File sample rate %d\n”, 1, isr

printf “File duration %d\n”, 1, ilen

printf “File number of samples %d\n”, 1, isamp

printf “The next power of 2 after %d samples is %d\n”, 1, isamp, i_ipow2

printf “File channels %d\n”, 1, ich

ifno1 ftgen 0, 0, i_ipow2,1, Sfile, 0, 0, 0

ibasfrq = isr / ftlen(ifno1)

ibasfrqst= ibasfrq*ich

kEnv madsr .1, .2, .6, .4

a1 oscili 1, ibasfrqst, ifno1

outs a1kEnv, a1kEnv


f0 z ;--------------------------------------------------------------------------------------------------------- CONSOLE ERROR (when opening presets):

INIT ERROR in instr 1 (opcode filesr) line 29: diskinfo cannot open “C:/Users/ALE/Desktop/CSOUND FILES/TEST SAMPLES/fox 48.wav”

This is the crash issue with Grain 1:

DOes this path/file actually exist on disk? if so I am at a loss as to why it’s not found? :thinking:

Yes, the path exists and the file is there. Is it not similar at what was happening before? maybe it has some connection with that, which was fixed in the build 2.9.44. It also couldn´t find a file that was in the correct place but now is diskin.

soundin cannot open C:/Users/ALE/Desktop/Grain1/“C:/Users/ALE/Desktop/Grain1/beats.wav”: No Error.
ftable 1: Failed to open file C:/Users/ALE/Desktop/Grain1/“C:/Users/ALE/Desktop/Grain1/beats.wav”
f 1 0.00 0.00 1.00 “C:/Users/ALE/Desktop/Grain1/“C:/Users/ALE/Desktop/Grain1/beats.wav”” …

spaces in the path?

I tested with a file that do not have space in the path and the same happens after saving the preset.

INIT ERROR in instr 1 (opcode filelen) line 28: diskinfo cannot open “C:/Users/ALE/Desktop/WAVS/Crash-5.wav”
from file C:\Users\ALE\Desktop\CSOUND FILES\Power of 2.csd (1),ilen filelen Sfile 1
INIT ERROR in instr 1 (opcode filesr) line 29: diskinfo cannot open “C:/Users/ALE/Desktop/WAVS/Crash-5.wav”
from file C:\Users\ALE\Desktop\CSOUND FILES\Power of 2.csd (1),isr filesr Sfile 1
INIT ERROR in instr 1 (opcode filenchnls) line 30: diskinfo cannot open “C:/Users/ALE/Desktop/WAVS/Crash-5.wav”
from file C:\Users\ALE\Desktop\CSOUND FILES\Power of 2.csd (1),ich filenchnls Sfile 1

Before the file paths looked concatenated, but these looks perfectly fine. I’ll test this out when I get to the office on Monday as I’m not allowed to grow Cabbage at home any more :rofl:

Ok I leave here the .csd file of this code that I was talking about to calculate tables, maybe it will be easier to open it from here when you can see it. The next power of 2.csd (1.6 KB)
Thank you and good weekend (and good luck with the growing) :smile:

1 Like