Author Topic: Sound source partial rewinding and looping to a point  (Read 3943 times)

Offline XspeedPL

  • bit
  • Posts: 6
    • View Profile
Sound source partial rewinding and looping to a point
« on: May 24, 2013, 03:43:25 PM »
Hi,

As the title says, is it possible to rewind a source to a specific point in time?
Ideally, I would want my sound to play from 0:00 and loop back to 0:05 automatically.
I've seen a function to get the time already played, but how about another to set the time?

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3495
  • Developer
    • View Profile
    • PaulsCode.Com
Re: Sound source partial rewinding and looping to a point
« Reply #1 on: May 24, 2013, 04:33:59 PM »
I think this could be done with normal sources (I'll look at adding a function for it).  It obviously would not work for streaming sources, since they are by nature not linked to a static block of audio data.
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted

Offline XspeedPL

  • bit
  • Posts: 6
    • View Profile
Re: Sound source partial rewinding and looping to a point
« Reply #2 on: May 24, 2013, 04:57:28 PM »
So, it isn't possible using the current version? Too bad :\

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3495
  • Developer
    • View Profile
    • PaulsCode.Com
Re: Sound source partial rewinding and looping to a point
« Reply #3 on: May 24, 2013, 05:03:03 PM »
Technically it is possible by exposing the underlying interface (different for each library plug-in)  Would be preferable to have a common interface for all library plug-ins, though.  I'll try and make some time to look at it this weekend.  It should be a pretty small fix (I know both OpenAL and Java Sound have interfaces for doing this, so just need to wrap those up into a common one)
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted

Offline XspeedPL

  • bit
  • Posts: 6
    • View Profile
Re: Sound source partial rewinding and looping to a point
« Reply #4 on: May 24, 2013, 05:52:49 PM »
Would this be correct?

Code: [Select] [nofollow]
    public static final void rewindTo(final String src, final int msec)
    {
        try
        {
            final Field f = SoundManager.sndSystem.getClass().getDeclaredField("soundLibrary");
            f.setAccessible(true);
            final ChannelLWJGLOpenAL c = (ChannelLWJGLOpenAL)((Source)((Library)f.get(SoundManager.sndSystem)).getSource(src)).channel;
            AL10.alSourcei(c.ALSource.get(0), AL11.AL_SEC_OFFSET, msec / 1000);
            if(AL10.alGetError() == AL10.AL_NO_ERROR)
                c.millisPreviouslyPlayed = msec;
        }
        catch (final Exception ex) { ex.printStackTrace(); }
    }

EDIT: I just found out about AL_SEC_OFFSET

EDIT2: That code works, but not always?
« Last Edit: May 25, 2013, 11:10:07 AM by XspeedPL »

Offline XspeedPL

  • bit
  • Posts: 6
    • View Profile
Re: Sound source partial rewinding and looping to a point
« Reply #5 on: May 26, 2013, 07:43:46 AM »
I have a problem.
Any idea why this code below gives me
"Error in class 'ChannelLWJGL OpenAL', Invalid enumerated parameter value"
Code: [Select] [nofollow]
    public static final void rewindTo(final String src, final float msec)
    {
        try
        {
            final Source s = getSndLib().getSource(src);
            final ChannelLWJGLOpenAL c = (ChannelLWJGLOpenAL)s.channel;
            final boolean play = s.playing();
            AL10.alSourceRewind(c.ALSource.get(0));
            AL10.alSourcef(c.ALSource.get(0), AL11.AL_SEC_OFFSET, msec / 1000);
            if (AL10.alGetError() == AL10.AL_NO_ERROR)
                c.millisPreviouslyPlayed = msec;
            if (play && s.toStream)
            {
                s.stop();
                s.play(c);
            }
        }
        catch (final Exception ex) { ex.printStackTrace(); }
    }
This is weird, since the code is pretty much copied from original "rewind()" with the inserted line that contains AL_SEC_OFFSET.

EDIT: Solved it. It was a matter of SoundSystem asynchronous command processing. I needed to put "ComandQueue(null);" a the beginning of the function.
« Last Edit: May 26, 2013, 08:02:07 AM by XspeedPL »

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3495
  • Developer
    • View Profile
    • PaulsCode.Com
Re: Sound source partial rewinding and looping to a point
« Reply #6 on: May 26, 2013, 08:56:55 AM »
A better solution would be to create a new command type to put in the queue, so it is done synchronously on the same thread.  Thanks for the code, will make the feature easier for me to add.
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted

Offline XspeedPL

  • bit
  • Posts: 6
    • View Profile
Re: Sound source partial rewinding and looping to a point
« Reply #7 on: May 26, 2013, 12:25:09 PM »
Glad I could be of help. Hope to see the new feature soon!

Offline XspeedPL

  • bit
  • Posts: 6
    • View Profile
Re: Sound source partial rewinding and looping to a point
« Reply #8 on: July 22, 2013, 07:19:58 PM »
Hello, I haven't been around for a while, so I wasn't quite following the updates.
Has this been implemented yet?