How do I linear ramp to a position with the Web Audio API's pannernode? - web-audio-api

I can use the linearRampToValueAtTime() method of the Web Audio API's AudioParam Interface to schedule a gradual linear change in the param. For instance, for gain,
var gainNode = audioContext.createGain();
gainNode.gain.linearRampToValueAtTime(1.0, audioContext.currentTime + 2);
I want to linearly ramp the position of a PannerNode. A Panner has a setPosition method, but I don't see an associated AudioParam:
var pannerNode = audioContext.createPanner();
pannerNode.setPosition(xPosition, yPosition, zPosition);
Can I linearly ramp the position of a panner node? I know that I could manually create a timer, and directly call setPosition over time, but can the Web Audio API handle that for me?

You can't. It's one of the many things wrong with the initial AudioPanner design, and why it's being refactored into two different nodes. https://github.com/WebAudio/web-audio-api/issues/372.
For the time being, you'll have to animate this via setInterval or the like.

Related

Detuned oscillators seem to bounce against each other

Note: If you click on the fiddle, ensure your sound is turned down to a minimum.
I'm using the Web Audio API and I want to detune one oscillator against another by a few cents to create a thicker sound. The problem is when I do detune one oscillator it creates a pulsating sound. If i play with the detune amount it gets better or worse depending on the values I set for each oscillator. If I detune one oscillator by -1 (as shown in the example below) the sound coming back from the oscillators cuts out periodically.
Why is this? Software synths I've used before allow you to detune by single cents so assuming that isn't the issue.
// create web audio api context
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// create Oscillator node
var oscillator = audioCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.setValueAtTime(440, audioCtx.currentTime); // value in hertz
oscillator.connect(audioCtx.destination);
// create Oscillator node
var oscillator1 = audioCtx.createOscillator();
oscillator1.type = 'square';
oscillator1.frequency.setValueAtTime(440, audioCtx.currentTime); // value in hertz
oscillator1.detune.setValueAtTime(-1, audioCtx.currentTime);
oscillator1.connect(audioCtx.destination);
oscillator.start();
oscillator1.start();
https://jsfiddle.net/c1xdnLtm/

Improve animation rendering in Matlab

I have written a code to create an animation (satellite movement around the Earth). When I run it, it works fine. However, when it is modified to be part of a code much more complex present in a Matlab GUI, the results produced changes (mainly because of the bigger number of points to plot). I also have noticed that if I use the OpenGL renderer the movement of the satellite is quicker than when the other renderers (Painters and Zbuffer) are used. I do not know if there are further possibilities to achieve an improvement in the rendering of the satellite movement. I think the key is, perhaps, changing the code that creates the actual position of the satellite (handles.psat) and its trajectory along the time (handles.tray)
handles.tray = zeros(1,Fin);
handles.psat = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),...
'ZData',Y(1,3),'Marker','o', 'MarkerSize',10,'MarkerFaceColor','b');
...
while (k<Fin)
az = az + 0.01745329252;
set(hgrot,'Matrix',makehgtform('zrotate',az));
handles.tray(k) = line([Y(k-1,1) Y(k,1)],[Y(k-1,2) Y(k,2)],...
[Y(k-1,3) Y(k,3)],...
'Color','red','LineWidth',3);
set(handles.psat,'XData',Y(k,1),'YData',Y(k,2),'ZData',Y(k,3));
pause(0.02);
k = k + 1;
if (state == 1)
state = 0;
break;
end
end
...
Did you consider to apply a rotation transform matrix on your data instead of the axis?
I think <Though I haven't checked it> that it can speedup your code.
You've used the typical tricks that I use to speed things up, like precomputing the frames, setting XData and YData rather than replotting, and selecting a renderer. Here are a couple more tips though:
1) One thing I noticed in your description is that different renderers and different complexities changed how fast your animation appeared to be running. This is often undesirable. Consider using the actual interval between frames (i.e. use tic; dt = toc) to calculate how far to advance the animation, rather than relying on pause(0.2) to generate a steady frame rate.
2) If the complexity is such that your frame rate is undesirably low, consider replacing pause(0.02) with drawnow, or at least calculate how long to pause on each frame.
3) Try to narrow down the source of your bottleneck a bit further by measuring how long the various steps take. That will let you optimize the right stage of the operation.

Setting up a power meter in cocos2d

I am a straight noob. Everyone else says it, but I'm dead serious.
My question is, what is the best way to make a power meter to move a object? Meaning, how to set it up so that the longer the player holds the more power they get. Also how, would I incorporate physics?
What I'd like to accomplish is to have a player holding onto something so that when he taps on the screen and hold he powers up, and when he lets go he throws the object a certain distance.
just checking if the there is any thouch sequence or not is rather an easy thing, you just have to overload two functions for your scene class, one to inform you whenever a touch sequence begins and one to tell you touch is ended. the source code example is describe in this link. after than i think you need a gauge to show how much power is gathered so far, the easiest way is to use a texture with full power shown in it and the set it as texture and then show it little by little as the power goes up just as the code below:
// to create the gauge with zero power
CCSprite *s=[CCSprite spriteWithTexture:[CCTextureCache addImage:#"gauge.png"] rect:CGRectMake(0,0,0,10)];
// and then whenever the power changes you call this method
[s setTextureRect:CGRectmake(0,0,power,10)]
note that in my code i am using a 100x10 texture (power is somthing between 0..100 and texture height is 10 as the last parameter in both CGRectMake functions)

Implementing a saved ghost for each level in a game

I'm developing a video game for the iPhone. I want each level to save a "ghost" of the best run, just like in Mario Kart.
I've already implemented it, but I used a brute force approach that stores (x, y, rotation) values each frame (60 per second). Needless to say this is very expensive, especially for a mobile device.
I guess I could store the data less often and interpolate when rendering the ghost, but I'm not 100% sure how to do this and if it will look good.
Has anyone done this before? How would you implement it? Is there a standard approach?
Linear interpolation is very easy, if that's what you want. But if the objects you want to interpolate can have non-linear trajectories (from ballistic or lateral accelleration effects) the thing gets more complicated.
The simplest approach would be for you to record the initial position for an object and its movement vector (x,y,z speed, as offsets per time unit). You won't have to record it again for that object until it changes its speed and/or its direction. Then you just record the elapsed time, the new position and the new movement vector (theoretically you don't have to record the position again, just the vectors, but I'd recommend doing so to have a check value - after the program is debugged you can discard it).
Then to playback you place the object at the original position and, for each time frame, add the movement offset to it until it reaches the time for the next recorded position. And so on.

iOS Question. Is There a Framework for Build Time Based Apps

I have the need for some time based effects in the iPad app I am building. The UIView class animation capability beginAnimations/commitAnimations is exactly the sort of thing I am looking for but it is restricted to specific properties of UIView deemed animatable.
Ideally, I am looking for a solution that lets me drive a time-based function that can perhaps send messages to a class of my own choosing at the rate I specify in the animation.
Specifically, I have a function - my implementation of the RenderMan function "smoothstep" which is essentially an ease-in ease-out curve common in animation. It takes [0 - 1] as input and outputs [0 - 1] as the curve is evaluated. I want to drive this function for a duration of my own choosing at rate of my own choosing.
Thanks in advance.
-Doug
Have you looked at NSTimer ? You can define a repeating timer that will call a method on your class repeatedly.