FluidSynth / fluidsynth

Software synthesizer based on the SoundFont 2 specifications
https://www.fluidsynth.org
GNU Lesser General Public License v2.1
1.89k stars 259 forks source link

Fluidsynth ignores a 0-1 loop but correctly does a 0-2 loop #819

Closed midimusic closed 3 years ago

midimusic commented 3 years ago

I am making really large sf2/3 organ soundfonts which include long stereo samples (3 to 5 seconds) and also include release samples which play when a key is released. These release samples should work with a 0-1 loop which produces silence until the key is released and then plays it release sound.

This fault applies to ALL FS versions including the latest.

Unfortunately fluidsynth does not recognise a 0-1 loop (its ignored) but does work correctly with a 0-2 loop. However a 0-2 loop is sub-optimal because it cannot be guaranteed silent. I suspect it would be very easy to change the FS 2 code so that it works correctly on 0-1 loops. I have attached a sf2 file containing one complete organ rank. All notes below middle C have a 0-1 loop and do not play these loop correctly. All notes above middle C have a 0-2 loop and do play correctly. Please correct your code so that it correctly plays 0-1 loops. Thank you. csw900 (This site will not accept my file because it is >10MB (its 77MB)) If you really need a sample file I can chop a large chunk out - and then send it - please let me know.

mawe42 commented 3 years ago

I'm not quite sure where the terms "0-1 loop" and "0-2 loop" come from, can you explain that in more detail? I first thought you simply mean the two possible SoundFont loop methods ("loop to end" or "loop until release then play rest"). But that doesn't seem to fit with the rest of your report.

And yes: a sample SoundFont would be great. :)

mawe42 commented 3 years ago

I just tried to create an instrument that only plays a sound on release. Attached is a small SoundFont that contains a single sample. The sample has some silence at the beginning, which is used for the loop. Then the actual sound after the loop. So when you press the key, the silence loop is played. As soon as you release it, you hear the rest of the sample. Is that what you were trying to achieve?

test_release.zip

jjceresa commented 3 years ago

I'm not quite sure where the terms "0-1 loop" and "0-2 loop" come from, can you explain that in more detail?

To play a sample when a key is released (and not when a key is pressed), there is a trick that consists to have a tiny part of the sample at the beginning (say 10 samples at index [0..9]) filled with 0. Then we choose "loop until release" as soundfont loop method on this tiny part. When the musician press a key, the synth is looping while rendering silence. Then when the musician release the key we heard the rest of the audible sample.

@midimusic is not happy because fluidsynth doesn't loop on index range [loopstart =0, loopend=1]. But it loops on range [0..2].

We just need to recall that the SF specs says that loopend "contains the index, in sample data points, from the beginning of the sample data field to the first data point following the loop of this sample". That means that choosing [loopstart =0, loopend=1] will never loop.

@midimusic, to ensure that a silent loop exists, you need to add at least 2 samples set to 0 at the beginning of your audible sample. But you must follow those general rules:

General sample looping rule for any samples: required leading/trailing samples for any synthesizers (Emu - Creatives labs - software - whatever): Eight data points (four on each side) surrounding the two equivalent loop points should be forced to be identical.

By forcing the data to be identical, all interpolation algorithms are guaranteed to properly reproduce an artifact-free loop. In the case of our "release sample", this leads to set these eight data points to value zero.

mawe42 commented 3 years ago

And I just noticed that Polyphone has some good documentation on how to create custom release sounds in SoundFonts: https://www.polyphone-soundfonts.com/documentation/en/tutorials/using-custom-releases-in-an-instrument

This is basically the same method I used in the above test SoundFont.

mawe42 commented 3 years ago

Ah, thanks for the clarification @jjceresa, that makes perfect sense!

FWIW: FluidSynth seems to cope very well with only 3 zero samples at the beginning and a 0-2 loop. I tried all possible interpolation methods and could not hear unwanted sounds during the loop.

jjceresa commented 3 years ago

This is basically the same method I used in the above test SoundFont

Yes, and to control the volume shape of this "release-sample" we can use a long release in the appropriate ADSR generator. if you want the release sample to play back quieter after a note has been held down for a while, you can control this using the volume envelope decay.

jjceresa commented 3 years ago

FWIW: FluidSynth seems to cope very well with only 3 zero samples at the beginning

It works also with 2 zero samples, but 2 is the required minimum.

midimusic commented 3 years ago

Thanks for your quick response.

Yes the test.sf2 is what I want - and already am achieving only rather better. I use polyphone and am fully familiar with its capabilities and can assure you that there is no need for any formal silence at the beginning of the sample, provided the loop starts at 0 and ends at 1 (i.e. it loops around only one sample and is thus automatically silent). Polyphone can do this and it also plays the resulting loop correctly. The problem is that FS cannot properly play a 0-1 loop but does correctly play a 0-2 loop.

What the sf2 spec says is not relevant to me - it is very old and I am doing something that is ahead of the state of the art. There is no reason to restrict FS to an old spec!!

I will reduce the size of my sample sound font to below 10MB and attach a copy but it will not be until late tomorrow. csw900

mawe42 commented 3 years ago

It works also with 2 zero samples, but 2 is the required minimum.

I don't think it does. At least my test with 2 zero samples with a 0 - 2 loop results in awful noises during the "silent" loop. But maybe I set it up incorrectly?

mawe42 commented 3 years ago

I will reduce the size of my sample sound font to below 10MB and attach a copy

I don't think that is necessary anymore. We understand what you request in this issue.

midimusic commented 3 years ago

OK thanks - I will not send my sf2 font for the time being - but it is available if you want to do any tests on it. csw900

jjceresa commented 3 years ago

It works also with 2 zero samples, but 2 is the required minimum.

I just wanted to say that fluidsynth ignores loop with only 1 sample. 2 samples minimum are required to get fluidsynth doing a loop. But it is possible that for free artifact loop more zero samples are required (8 zeros samples is the SF specs recommendation which make sense if we use the sinc interpolator) .

jjceresa commented 3 years ago

What the sf2 spec says is not relevant to me - it is very old and I am doing something that is ahead of the state of the art.

In this case you need a plain sfz synthesizer and not a sf2 synthesizer.

midimusic commented 3 years ago

There are no sfz synthesizers at present which can even begin to play a 3 manual + pedals virtual pipe organ. I already use FS is my organ sfz is not a valid option. csw900

derselbst commented 3 years ago

What the sf2 spec says is not relevant to me - it is very old and I am doing something that is ahead of the state of the art. There is no reason to restrict FS to an old spec!!

David, FS is a SoundFont2 synth. So what the spec says matters very much to us! We had enough trouble in the past with sample loops causing audible glitches, esp. when FS started to mess around with those sample loops. The spec is absolutely clear that loopend specifies the first sample after the loop. I will not risk breaking any soundfonts out there just because of your ignorance to set a valid sample loop.

FS is opensource, feel free to introduce this change on your own and at your own risk.

mawe42 commented 3 years ago

What the sf2 spec says is not relevant to me - it is very old and I am doing something that is ahead of the state of the art.

That is not true, I'm afraid. You use Polyphone to edit SoundFonts files which you would like to play via FluidSynth. All of this is possible because Polyphone and FluidSynth try to adhere to the SoundFont specs.