how I can get current time when use playbackRate.value in Web Audio API

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

I need to know the current time of a source that is playing, but I can't use context.currentTime because when I change the source.playbackRate.value the speed rate of the context don't change too, so I can't determinate where is the current position of sound. There isn't another way?

Edit, some code:

I use this functions to load and play an mp3 from the network

function loadSoundFile(url) {     source = null;     var request = new XMLHttpRequest();     request.open('GET', url, true);     request.responseType = 'arraybuffer';     request.onload = function(e) {         context.decodeAudioData(request.response, initSound, function() {             alert("error");         });     };     request.send(); }  var source = null; var inittime = 0; function initSound(buffer)  {      source = context.createBufferSource();     source.buffer = buffer;      source.connect(context.destination);     source.start(0);     inittime = context.currentTime; //I save the initial time } 

Then to get the actual position of the audio track I should do:

var current_position = context.currentTime-inittime; 

This work fine while I don't change in the source the playbackRate:

source.playbackRate.value 

I need to change this value dynamically to synchronize the audio track to another audio track that is playing, so I need to speed up, if the actual position of the track is lower than the position received from a "server" or slow down if it is higher. But if I change the play back rate how I can know where is currently the position of the audio track?

In fact now if I use

var current_position = context.currentTime-inittime; 

current_position will be the time spent from the begin of the playback that is different from the current time position of the playback dude the changes playbackrate value.

回答1:

[This is in answer to the opening question in the first paragraph, and does not take in to account the edited in code that has been attempted to solve it, which I have not reviewed]

Well of course there is a way! There is always a way! You have to engineer it yourself!

There really is no need for Web Audio API to go any further. It gives you all you need. The currentTime will always tell you an accurate time. Using that, and your playbackRate, you can keep track of the current position yourself, by updating a position variable each time you adjust the playbackRate or need to know the current position.

Each time you update the playbackRate, or need to know the current position, update your tracking time by the time passed since your last change (store the previous currentTime), and modify the addition using the appropriate playbackRate. Hey presto! Now you have your own currentTime that takes in to account the playbackRate you are interested in. Reset your tracking variable any time you want to start your tracking again.



回答2:

Use the reciprocal of playbackRate, and multiply the time elapsed since the beginning of playback with that value. Assuming a non-offline rendering, at any given time you can use the following to calculate the actual position that takes playbackRate into consideration:

(context.currentTime - inittime) / source.playbackRate.value; 

From the documentation of AudioParam.value:

The value property of the AudioParam interface represents the parameter's current floating point value, which is initially set to the value of AudioParam.defaultValue.

Which means the value of the value property will take scheduled changes into consideration as well. Note, that the calculated value might go backwards momentarily if playback rate changes quickly.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!