Bug#740710: problem with SF3 playback

2015-11-30 Thread Fabian Greffrath
Hi again,

Am Samstag, den 28.11.2015, 11:14 +1100 schrieb Hamish Moffatt:
> loopstart = loopend = 0 fixes it.
> 
> Yes, I do see the problem in MuseScore (2.0.2 release). Interesting.

Maybe it would help if you could report the issues you encountered over
at MuseScore. They are somehow "upstream" for the SF3 sound font format
and it would be interesting to know their opinion e.g. about the stray
notes.

Please tell them how you composed your compressed sound fonts and also
that forcing loopstart = loopend = 0 apparently fixes the issues.

Would you do that, please?

Thank you!

 - Fabian


signature.asc
Description: This is a digitally signed message part


Bug#740710: problem with SF3 playback

2015-11-30 Thread Hamish Moffatt

On 01/12/15 07:42, Fabian Greffrath wrote:

Hi again,

Am Samstag, den 28.11.2015, 11:14 +1100 schrieb Hamish Moffatt:

loopstart = loopend = 0 fixes it.

Yes, I do see the problem in MuseScore (2.0.2 release). Interesting.

Maybe it would help if you could report the issues you encountered over
at MuseScore. They are somehow "upstream" for the SF3 sound font format
and it would be interesting to know their opinion e.g. about the stray
notes.

Please tell them how you composed your compressed sound fonts and also
that forcing loopstart = loopend = 0 apparently fixes the issues.



Hi Fabian,

I've reported it to MuseScore:
https://musescore.org/en/node/89216

and also to sftools, since the issue could be there:
https://github.com/wschweer/sftools/issues/14


regards,
Hamish



Bug#740710: problem with SF3 playback

2015-11-30 Thread Fabian Greffrath
Hey Hamish,

Am Dienstag, den 01.12.2015, 15:30 +1100 schrieb Hamish Moffatt:
> I've reported it to MuseScore:
> https://musescore.org/en/node/89216
> 
> and also to sftools, since the issue could be there:
> https://github.com/wschweer/sftools/issues/14
> 

this is great, thank you very much!

 - Fabian


signature.asc
Description: This is a digitally signed message part


Bug#740710: problem with SF3 playback

2015-11-28 Thread Fabian Greffrath
Am Samstag, den 28.11.2015, 11:14 +1100 schrieb Hamish Moffatt:
> loopstart = loopend = 0 fixes it.
> 
> Yes, I do see the problem in MuseScore (2.0.2 release). Interesting.

Hah, I think this is good news. Since MuseScore is the reference
implementation for sf3 sound fonts support I consider it a success that
FluidSynth with my patch sounds the same. Even if this means that there
are the same artifacts.

Honestly, I think now the sound font or the software that created it is
to blame for setting wrong loopstart and loopend values. Resetting them
both to 0 is really just a hack that should be avoided.

Thank you very much for testing my patch!

Cheers,

Fabian


signature.asc
Description: This is a digitally signed message part


Bug#740710: problem with SF3 playback

2015-11-27 Thread Hamish Moffatt

Hi,

On 27/11/15 19:05, Fabian Greffrath wrote:

Am Freitag, den 27.11.2015, 12:39 +1100 schrieb Hamish Moffatt:

I'm afraid that while this plays correctly with FluidR3Mono_GM.sf3, I
  

have stray notes again with other sound fonts.

Do you also have these stray notes if you play back the MIDI in
Musescore using these other sound fonts?

Do you also have these stray notes if you forcefully reset loopstart
and loopback, like in my previous workaround?


loopstart = loopend = 0 fixes it.

Yes, I do see the problem in MuseScore (2.0.2 release). Interesting.


thanks,
Hamish



Bug#740710: problem with SF3 playback

2015-11-27 Thread Fabian Greffrath
Am Freitag, den 27.11.2015, 12:39 +1100 schrieb Hamish Moffatt:
> I'm afraid that while this plays correctly with FluidR3Mono_GM.sf3, I
 
> have stray notes again with other sound fonts.

Do you also have these stray notes if you play back the MIDI in
Musescore using these other sound fonts?

Do you also have these stray notes if you forcefully reset loopstart
and loopback, like in my previous workaround?

 - Fabian


signature.asc
Description: This is a digitally signed message part


Bug#740710: problem with SF3 playback

2015-11-26 Thread Hamish Moffatt

On 26/11/15 21:39, Fabian Greffrath wrote:

Hi Hamish,

Am Dienstag, den 24.11.2015, 22:40 +1100 schrieb ham...@cloud.net.au:

Thanks Fabian. I will try tomorrow.

I think I found the real culprit: I had an off-by-one error in the
calculation of the sample sizes. In FluidSynth, sample->end points to
the last valid point in a sample, not to the first point after. This
means that all samples were calculated one byte too short.

Now, all samples are correctly decoded, i.e. there are no samples with
sfinfo.frames == 0 anymore. This also means that the loopstart and
loopend variables are now correctly set; the workaround of setting them
both to 0 that I previously suggested is not necessary anymore. Also,
most of the quality loss that I originally blamed on the compression
seems to be gone now.

Please find a new patch attached.



Hi Fabian,

I'm afraid that while this plays correctly with FluidR3Mono_GM.sf3, I 
have stray notes again with other sound fonts.


I'm not sure of the license of those fonts so I won't send them to the 
BTS, but I can share them with you directly. I converted them from sf2 
with sfconvert.



Hamish



Bug#740710: problem with SF3 playback

2015-11-26 Thread Fabian Greffrath
Hi Hamish,

Am Dienstag, den 24.11.2015, 22:40 +1100 schrieb ham...@cloud.net.au:
> Thanks Fabian. I will try tomorrow.

I think I found the real culprit: I had an off-by-one error in the
calculation of the sample sizes. In FluidSynth, sample->end points to
the last valid point in a sample, not to the first point after. This
means that all samples were calculated one byte too short.

Now, all samples are correctly decoded, i.e. there are no samples with
sfinfo.frames == 0 anymore. This also means that the loopstart and
loopend variables are now correctly set; the workaround of setting them
both to 0 that I previously suggested is not necessary anymore. Also,
most of the quality loss that I originally blamed on the compression
seems to be gone now.

Please find a new patch attached.

Thanks,

Fabian
Description: Add support for sound fonts in SF3 format that contain Ogg Vorbis compressed samples
Author: Fabian Greffrath 
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=740710
Forwarded: https://sourceforge.net/p/fluidsynth/tickets/142/
Last-Update: 2015-11-26

--- a/include/fluidsynth/sfont.h
+++ b/include/fluidsynth/sfont.h
@@ -270,6 +270,7 @@ struct _fluid_sample_t
 #define FLUID_SAMPLETYPE_RIGHT	2   /**< Flag for #fluid_sample_t \a sampletype field for right samples of a stereo pair */
 #define FLUID_SAMPLETYPE_LEFT	4   /**< Flag for #fluid_sample_t \a sampletype field for left samples of a stereo pair */
 #define FLUID_SAMPLETYPE_LINKED	8   /**< Flag for #fluid_sample_t \a sampletype field, not used currently */
+#define FLUID_SAMPLETYPE_OGG_VORBIS	0x10 /**< Flag for #fluid_sample_t \a sampletype field for Ogg Vorbis compressed samples */
 #define FLUID_SAMPLETYPE_ROM	0x8000  /**< Flag for #fluid_sample_t \a sampletype field, ROM sample, causes sample to be ignored */
 
 
--- a/src/sfloader/fluid_defsfont.c
+++ b/src/sfloader/fluid_defsfont.c
@@ -26,6 +26,10 @@
 /* Todo: Get rid of that 'include' */
 #include "fluid_sys.h"
 
+#if LIBSNDFILE_SUPPORT
+#include 
+#endif
+
 /***
  *
  *   SFONT LOADER
@@ -1582,6 +1586,14 @@ new_fluid_sample()
 int
 delete_fluid_sample(fluid_sample_t* sample)
 {
+  if (sample->sampletype & FLUID_SAMPLETYPE_OGG_VORBIS)
+  {
+#if LIBSNDFILE_SUPPORT
+if (sample->data)
+  FLUID_FREE(sample->data);
+#endif
+  }
+
   FLUID_FREE(sample);
   return FLUID_OK;
 }
@@ -1598,6 +1610,61 @@ fluid_sample_in_rom(fluid_sample_t* samp
 /*
  * fluid_sample_import_sfont
  */
+#if LIBSNDFILE_SUPPORT
+// virtual file access rountines to allow for handling
+// samples as virtual files in memory
+static sf_count_t sfvio_pos;
+
+static sf_count_t
+sfvio_get_filelen(void* user_data)
+{
+  fluid_sample_t *sample = (fluid_sample_t *)user_data;
+
+  return (sf_count_t)(sample->end + 1 - sample->start);
+};
+
+static sf_count_t
+sfvio_seek(sf_count_t offset, int whence, void* user_data)
+{
+  fluid_sample_t *sample = (fluid_sample_t *)user_data;
+
+  switch (whence)
+  {
+case SEEK_SET:
+  sfvio_pos = offset;
+  break;
+case SEEK_CUR:
+  sfvio_pos += offset;
+  break;
+case SEEK_END:
+  sfvio_pos = sfvio_get_filelen(user_data) + offset;
+  break;
+  }
+
+  return sfvio_pos;
+};
+
+static sf_count_t
+sfvio_read(void* ptr, sf_count_t count, void* user_data)
+{
+  fluid_sample_t *sample = (fluid_sample_t *)user_data;
+
+  if (count > sfvio_get_filelen(user_data) - sfvio_pos)
+  count = sfvio_get_filelen(user_data) - sfvio_pos;
+
+  memcpy(ptr, (char *)sample->data + sample->start + sfvio_pos, count);
+  sfvio_pos += count;
+
+  return count;
+};
+
+static sf_count_t
+sfvio_tell (void* user_data)
+{
+  return sfvio_pos;
+};
+#endif
+
 int
 fluid_sample_import_sfont(fluid_sample_t* sample, SFSample* sfsample, fluid_defsfont_t* sfont)
 {
@@ -1612,6 +1679,87 @@ fluid_sample_import_sfont(fluid_sample_t
   sample->pitchadj = sfsample->pitchadj;
   sample->sampletype = sfsample->sampletype;
 
+  if (sample->sampletype & FLUID_SAMPLETYPE_OGG_VORBIS)
+  {
+#if LIBSNDFILE_SUPPORT
+SNDFILE *sndfile;
+SF_INFO sfinfo;
+SF_VIRTUAL_IO sfvio = {
+  sfvio_get_filelen,
+  sfvio_seek,
+  sfvio_read,
+  NULL,
+  sfvio_tell
+};
+short *sampledata_ogg;
+
+// initialize file position indicator and SF_INFO structure
+sfvio_pos = 0;
+memset(, 0, sizeof(sfinfo));
+
+// open sample as a virtual file in memory
+sndfile = sf_open_virtual(, SFM_READ, , sample);
+if (!sndfile)
+{
+  FLUID_LOG(FLUID_ERR, sf_strerror(sndfile));
+  return FLUID_FAILED;
+}
+
+// empty sample
+if (!sfinfo.frames || !sfinfo.channels)
+{
+  sample->start = sample->end =
+  sample->loopstart = sample->loopend =
+  sample->valid = 0;
+  sample->data = NULL;
+  sf_close(sndfile);
+  return FLUID_OK;
+}
+
+// allocate memory for uncompressed sample data stream
+

Bug#740710: problem with SF3 playback

2015-11-24 Thread Fabian Greffrath
Dear Hamish,

thank you very much for tewsting my patch.

Am Dienstag, den 24.11.2015, 16:26 +1100 schrieb Hamish Moffatt:
> If you play the attached MIDI file with the FluidR3Mono_GM.sf3 file
> (from MuseScore) loaded, you'll hear some extra garbage notes played at 
> the end.

I can confirm this issue, but currently it is a complete mystery to me.
I fail to see how the choice of a sound font (format) should affect the
notes being played at the end of the MIDI?

This sure needs further investigation.

Thanks,

Fabian


signature.asc
Description: This is a digitally signed message part


Bug#740710: problem with SF3 playback

2015-11-24 Thread hamish
Thanks Fabian. I will try tomorrow.

Hamish

Sent from my android device.

-Original Message-
From: Fabian Greffrath <fab...@debian.org>
To: 740...@bugs.debian.org, Hamish Moffatt <hamish-deb...@cloud.net.au>
Sent: Tue, 24 Nov 2015 22:32
Subject: Re: Bug#740710: problem with SF3 playback

Am Dienstag, den 24.11.2015, 11:09 +0100 schrieb Fabian Greffrath:
> This sure needs further investigation.

Got a bit further:

I forgot to adjust the sample->loopstart and sample->loopend values and
I am not even sure if they make sense at all for compressed samples. If
you wipe away this whole block in src/sfloader/fluid_defsfont.c that my
patch adds to fluid_sample_import_sfont()

/* loop is fowled?? (cluck cluck :) */
if (sample->loopend > sample->end ||
sample->loopstart >= sample->loopend ||
sample->loopstart <= sample->start)
{
  /* can pad loop by 8 samples and ensure at least 4 for loop (2*8+4) */
  if ((sample->end - sample->start) >= 20)
  {
sample->loopstart = sample->start + 8;
sample->loopend = sample->end - 8;
  }
  else /* loop is fowled, sample is tiny (can't pad 8 samples) */
  {
sample->loopstart = sample->start + 1;
sample->loopend = sample->end - 1;
  }
}

and replace it with a simple

sample->loopstart = sample->loopend = 0;

the MIDI plays back as expected.

 - Fabian


Bug#740710: problem with SF3 playback

2015-11-24 Thread Fabian Greffrath
Am Dienstag, den 24.11.2015, 11:09 +0100 schrieb Fabian Greffrath:
> This sure needs further investigation.

Got a bit further:

I forgot to adjust the sample->loopstart and sample->loopend values and
I am not even sure if they make sense at all for compressed samples. If
you wipe away this whole block in src/sfloader/fluid_defsfont.c that my
patch adds to fluid_sample_import_sfont()

    /* loop is fowled?? (cluck cluck :) */
    if (sample->loopend > sample->end ||
        sample->loopstart >= sample->loopend ||
        sample->loopstart <= sample->start)
    {
      /* can pad loop by 8 samples and ensure at least 4 for loop (2*8+4) */
      if ((sample->end - sample->start) >= 20)
      {
        sample->loopstart = sample->start + 8;
        sample->loopend = sample->end - 8;
      }
      else /* loop is fowled, sample is tiny (can't pad 8 samples) */
      {
        sample->loopstart = sample->start + 1;
        sample->loopend = sample->end - 1;
      }
    }

and replace it with a simple

    sample->loopstart = sample->loopend = 0;

the MIDI plays back as expected.

 - Fabian


signature.asc
Description: This is a digitally signed message part


Bug#740710: problem with SF3 playback

2015-11-23 Thread Hamish Moffatt

Hi Fabian,

I tried your SF3 playback patch for Fluidsynth, but I discovered a problem.

If you play the attached MIDI file with the FluidR3Mono_GM.sf3 file 
(from MuseScore) loaded, you'll hear some extra garbage notes played at 
the end.


This doesn't happen when playing the same file with FluidR3_GM.sf2. I 
don't see it in MuseScore either.




Hamish





bach.mid
Description: MIDI audio