Pause for a looped audio in Three.js - audio

I extensively use the Audio and PositionalAudio part of ThreeJS and it seems to me that the play/pause function can not work correctly for a looped audio.
In the ThreeJS Audio source we can read:
this._pausedAt += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;
If I am understand correctly, _pausedAt is thus somehow the elapsed duration of play in the audio file which is fine and totally usable (in particular for a straightforward playing with no loop). But when executing the play() function I see:
source.start( this._startedAt, this._pausedAt + this.offset, this.duration );
And in the MDN documentation of AudioBufferSourceNode.start():
offsets past the end of the audio which will be played (based on the audio
buffer's duration and/or the loopEnd property) are silently clamped to the
maximum value allowed.
That means that once the file has been read at least once until the end (with or without pauses), the next play() (typically after a pause()) will start at the end of the file (duration and/or loopEnd as mentioned) instead of restarting from the correct position where it was paused – somewhere in between loopStart and loopEnd.
Am I correct on this understanding?
If so, I see no better option than doing something like the code below to correct the position of the playhead (or _pausedAt):
if (sound.getLoop() === true) {
var looping = (sound.loopEnd === 0) ? sound.source.buffer.duration : sound.loopEnd
var loopDur = looping - sound.loopStart
_pausedAt = (_pausedAt - sound.loopStart) % loopDur + sound.loopStart;
}
Does any of this make sense?
Thank you very much,
Benjamin

Answered on ThreeJS discourse: https://discourse.threejs.org/t/pause-for-looped-audio/16894
Fixed here: https://github.com/mrdoob/three.js/pull/19079

What is your other way of contacting Benjamin? I can not post on ThreeJS forum.

Related

How to display an image saved as "/storage/emulated/0/Download/IMG_1582623402006.jpg" in emulator

I capture a image by camera in emulator and it was saved as:
"/storage/emulated/0/Download/IMG_1582623402006.jpg"
I am trying to display this image in < ImageView > as the following:
Bitmap finalBitmap = BitmapFactory.decodeFile("/storage/emulated/0/Download/IMG_1582623402006.jpg");
holder.image.setImageBitmap(scaledBitmap);
but it shows that "finalBitmap" is empty. What have I missed?
Thanks in advance!
You posted this over a year ago, so it may be too old at this point.
It appears that you are using two variables for your bitmaps: finalBitmap and scaledBitmap and not referencing the proper one in the setImageBitmap command.
Instead, have you tried this?
holder.image.setImageBitmap(finalBitmap);
Not totally sure, but adding some attributes in "options" seems make it work in emulator:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inSampleSize = 5;
Bitmap scaledBitmap = BitmapFactory.decodeFile(/storage/emulated/0/Download/IMG_1582623402006.jpg, options);
holder.image.setImageBitmap(scaledBitmap);

PixiJS - Not all touchend events triggering (outside of displayObject)

Pixijs (3.0.8) supports multi-touch as shown in their demos, and I've set up start, move and end listeners for touches on my mobile device.
The touches are registered on a square within the canvas which I'll call the interactiveArea, but the touchend events trigger when let go outside of the area as well. This is behavior that works fine with a single mouse cursor.
However, when using more fingers, having touches with the identifiers 0,1 and 2, only the first touchEnd is triggered outside of the area.
So I press and hold 3 fingers inside the interactiveArea and move them all outside of it. Then I let go of 1, and then the others. I won't be notified of touchEnds for event 0 and 2, and I'd have to re-register 3 touches and let go properly just to get a touchend for 2 triggered!
Any tips on how I can detect all touchends, rather than have it stop on the first touchend? I've tried working with a setTimeout hack as well, but that really doesn't suit my use case.
Edit I've made a basic codepen to demonstrate how touchendoutside is only triggered once. https://codepen.io/Thomaswithaar/pen/EygRjM Do visit the pen on mobile, as it is about touches rather than mouse interactivity.
Holding two fingers on the red square and then moving them out and letting go will only trigger one touchendoutside event.
Looking at the PIXI source code, there is indeed a bug in the Interaction Manager. Here is the method that processes touch end events:
InteractionManager.prototype.processTouchEnd = function ( displayObject, hit )
{
if(hit)
{
this.dispatchEvent( displayObject, 'touchend', this.eventData );
if( displayObject._touchDown )
{
displayObject._touchDown = false;
this.dispatchEvent( displayObject, 'tap', this.eventData );
}
}
else
{
if( displayObject._touchDown )
{
displayObject._touchDown = false;
this.dispatchEvent( displayObject, 'touchendoutside', this.eventData );
}
}
};
You can see in the else statement that a touchendoutside event gets dispatched when displayObject._touchDown is true. But after you release your first finger, it sets the flag to false. That is why you only receive that event once.
I've opened an issue here:
https://github.com/pixijs/pixi.js/issues/2662
And provided a fix here:
https://github.com/karmacon/pixi.js/blob/master/src/interaction/InteractionManager.js
This solution removes the flag and uses a counter instead. I haven't tested it yet, so please let me know if it works.

Bumpy jigsaw type data into curve data

I have a problem I solved in Excel but I am completely stuck in Matlab.
A weight measuring how much liquid I pump is refilled when its almost empty. Here is a picture
Now I want to see it in one motion, not like a jigsaw. Here is my solution, doing it manually in Excel:
Now to Matlab where this should be done automatically: I know how to index the row before and after I have the bumps, but I'm kind of stuck now. I do d=diff(x) ok and now I can replace the high peaks when the bumps occur (i=d(:,1)>0) with 0 so it never happened. And how do I translate it back? Somehow "undiff(x)"? im completely lost.
Here's an extract of my x:
2533,30
3540,00
3484,90
3430,00
3375,00
3320,20
3265,60
3210,60
3155,80
3101,20
3046,50
2991,70
2937,00
2882,50
2828,10
2773,80
2719,30
2664,90
2610,50
2556,10
2501,60
3508,00
3454,00
3399,70
3352,10
Like this?
temp = [0; diff(x)];
temp(temp < 0) = 0;
y = x - cumsum(temp);
y(temp > 0) = interp1(y, find(temp > 0) + 0.5);
plot(y);
hen using the default plot() function, matlab will automatically draw a line plot and connect each point in the data you are plotting.
Instead it seems like you just need to plot the points individually and unconnected. So if you are currently doing something like plot(x) or area(x) instead try plot(x,'o'). This will cause Matlab to plot the points individually without connecting them as lines.
If you'd like to change the marker type used to plot each point, use doc linespec to get more info.

Flash shared object always gets reset

So in my game, you want to make the potato as big as possible. The first time you play it, the highscores work fine,giving you a highscore and saving it. The problem is that whenever you play the game, the highscore is always reset to your current score, which means that even if you get a lower score then your previous highscore, it saves your current score as the highscore. my code looks like this:
var savedstuff:SharedObject = SharedObject.getLocal("myStuff");
if (savedstuff.data.bestScore = 0) {
savedstuff.data.bestScore = 1
}
bigness.text = finish.toString();
if (finish > savedstuff.data.bestScore){
savedstuff.data.bestScore = finish;
}
best.text = savedstuff.data.bestScore.toString();
savedstuff.flush();
where bigness is a textbox displaying your current score, best is a textbox displaying your highscore and bestScore is where the best score is stored. I have the line "if (finish > savedstuff.dada.bestScore)" which should make the overwrite only occur if you get a higher score, but it seems to just ignore that line. my game is here
http://www.kongregate.com/games/pwnedcat/grow-a-potato
any help is appreciated. I just started flash so I really don't know anything. :(
Check the condition, it should be
savedstuff.data.bestScore == 0
Instead of
savedstuff.data.bestScore = 0

XNA SoundEffects not playing

Followed a few tutorials and I'm having no luck, I'm trying to add a simple .WAV sound effect into XNA using free samples, I have named the sound as:
SoundEffect hit1;
SoundEffect hit2;
And then loaded the content with:
hit1= content.load<SoundEffect>("hit1")
But when it comes to adding the play to a button press and I go to test it there's no sound at all even no errors or nothing the game loads and is playable but any sound effects are not working.
** // Sounds
SoundEffect hit1;
SoundEffect hit2;
This is my variable names:
// Sounds
hit1 = Content.Load<SoundEffect>("hit1");
hit2 = Content.Load<SoundEffect>("hit2");
This is how I'm loading them in the loadcontent method:
//If Keyboard Key W is Pressed or Buttong Y
if (keys1.IsKeyDown(Keys.W) && oldKeys1.IsKeyUp(Keys.W)
|| (oldpad1.Buttons.Y == ButtonState.Released) && (pad1.Buttons.Y == ButtonState.Pressed))
{
secondsPassed = 0;
// IF The Target Image is a Gnome
if (targets[0] == sprites[0])
{
//They whacked a gnome
GnomeHits = GnomeHits + 1;
runNum = 0;
secondsPassed = 0;
hit1.Play();
}
else
{
//They whacked a troll
scoreNum = scoreNum + 1;
runNum = runNum + 1;
GnomeHits = 0;
secondsPassed = 0;
hit2.Play();
}
SetUpSpriteLoop();
And this is one of the control buttons I'm trying to assign sound too
when I hit F5 and run the game when I hit the key or button no sound at all.
I had a similar problem with Monogame (built from XNA's ashes) on a Windows 10 machine. I fixed it by reinstalling DirectX, and everything just worked.
My first recommendation would be to make sure you are updating the keyboard presses. I only say that as I can't see you doing that in your code. If you do not do this then the key states will not update and therefore you will never enter the if statement.
Once you have ensured you are entering the if statement, I would play around with the volume of the sound effect to make sure it is actually audible. I may be a bit off base there however if I am I suggest following this tutorial rbwhitaker is a great resource for XNA.

Resources