Gapless sound loop

275 views
Skip to first unread message

Radek Bartoň

unread,
Jan 3, 2013, 10:36:18 AM1/3/13
to pl...@googlegroups.com
Greatings everyone.

I'm trying to play short sound which has definitely no gap in it the following way:

 Sound test = PlayN.assets().getSound("sounds/mysound");
 test.setLooping(true);
 test.prepare();
 test.play();


but the sound loop is repeaded with small (~100 ms) gap in it. Tested both on Android and iOS.

Any clue how to play gapless sound loops using PlayN, please?

Michael Bayne

unread,
Jan 3, 2013, 2:54:40 PM1/3/13
to pl...@googlegroups.com
On Thu, Jan 3, 2013 at 7:36 AM, Radek Bartoň <blac...@gmail.com> wrote:
Any clue how to play gapless sound loops using PlayN, please?

This is not a PlayN issue, it is a symptom of the underlying audio infrastructure on nearly any operating system. PlayN just tells the OS "loop this sound", it doesn't actually handle the looping.

The audio system will have a minimum buffer size, and if your sound is substantially shorter than that, you are not going to be able to loop without a gap. If you really need to loop a very short sound, duplicate it two or three (or a dozen depending on how short is short) times so that you're looping more audio data.

-- m...@samskivert.com

Radek Bartoň

unread,
Jan 4, 2013, 6:31:10 AM1/4/13
to pl...@googlegroups.com
Great idea but unfortunatelly length of the sound does not solve anything. I tried to play the sound natively on Android using

  SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
  pool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
      soundPool.play(sampleId, 1.0f, 1.0f, 1, -1, 1.0f);
    }
  });
  pool.load(this, R.raw.test, 1);


and the gap was threre too when format was MP3, playing WAV version of the sound is almost gapless.

The problem is that playing WAV or OGG from PlayN does not work at all :-(.

Michael Bayne

unread,
Jan 4, 2013, 1:54:47 PM1/4/13
to pl...@googlegroups.com
On Fri, Jan 4, 2013 at 3:31 AM, Radek Bartoň <blac...@gmail.com> wrote:
and the gap was threre too when format was MP3, playing WAV version of the sound is almost gapless.

Oh yeah, MP3 has even larger framing requirements than raw audio.
 
The problem is that playing WAV or OGG from PlayN does not work at all :-(.

We can add WAV support to the Android and Java backends pretty easily. I took them out because we were aiming for a uniform audio format across all platforms, but then I discovered that one really should to use CAFF on iOS, so I added support for CAFF with fallback to MP3 on iOS. I could easily add WAV with fallback to MP3 on Java and Android.

It's not possible to (efficiently) fallback from one audio format to another on the HTML backend, so that would remain MP3 only. We could add some out of band mechanism for providing hints to the format if we wanted to support WAV on the HTML backend. I'm not super familiar with the final SWF creation for our Flash backend, but normal SWF creation ends up baking the audio into the SWF in MP3 format, so I don't think there's any way to embed a WAV in a SWF. Maybe Flash works around the looping short MP3 problems anyway. 

-- m...@samskivert.com

Radek Bartoň

unread,
Jan 4, 2013, 2:04:28 PM1/4/13
to pl...@googlegroups.com
In this context, it would be great if assets would have different path prefixes for different resource types (images, sounds, etc.). For example in my game I'm using prefix path for loading different image resolution sets according to the target screen resolutions. Now I´d need also different sound resources prefixes for different platforms.

But it seems that for now I have to implement my own platform-specific looping sound playback.

Thank your for your explanation and help!

Michael Bayne

unread,
Jan 4, 2013, 2:13:13 PM1/4/13
to pl...@googlegroups.com
On Fri, Jan 4, 2013 at 11:04 AM, Radek Bartoň <blac...@post.cz> wrote:
Now I´d need also different sound resources prefixes for different platforms.

The point of the auto-fallback and not specifying the suffix when loading the sound is to avoid having to write special code to load different sounds. So you write:

assest().getSound("sounds/somesound");

On iOS it would load sounds/somesound.caf and if that does not exist, it loads sounds/somesound.mp3.
On Android it would load sounds/somesound.wav or if that does not exist, it loads sounds/somesound.mp3.
On Java it loads sounds/somesound.wav and falls back to sounds/somesound.mp3.
On Flash and HTML it just loads sounds/somesound.mp3.

Then you provide CAF, WAV or MP3 versions of your sound as appropriate, depending on your looping requirements and depending on which platform you're packaging your game for.

I think I already posted the scripts I use for Spellwood to generate MP3s for Android/Java from WAV files checked into source control, and CAF for iOS.


Reply all
Reply to author
Forward
0 new messages