使用 jspsyche 插件 (jspsych-libet) 在 Javascript 中播放声音时出现延迟

问题描述 投票:0回答:1

在我的实验中,使用名为 jspsych-libet 的 jspsyche 插件播放蜂鸣音时遇到延迟。声音的时机至关重要,但我观察到延迟高达 400 毫秒。这些延迟是随机发生的并且相对罕见。

检查代码后,它看起来很简单。单击按钮后,将设置一个固定的 250ms 定时器来播放声音。然而,延迟似乎超过了这个值。这是发出蜂鸣声的代码片段:

setTimeout(function() {
    // play the tone
    if (context !== null) {
        startTime = context.currentTime;
        source.start(startTime);
    } else {
        audio.play();
    }
    // record cock hand angle of audio
    trial_data.theta_tone = clock.theta;
    trial_data.tone_ms = performance.now();
    // trigger estimation?
    ctrl_fcn('estimate');
}, trial.tone_delay_ms);

此代码同时使用 Web 音频 API 和 HTML5 音频。我尝试过使用这两种方法,但问题仍然存在。

这是加载音频的部分:

// load audio
var context = jsPsych.pluginAPI.audioContext();
var audio;
// load audio file
jsPsych.pluginAPI.getAudioBuffer(trial.tone_file)
  .then(function (buffer) {
    if (context !== null) {
      audio = context.createBufferSource();
      audio.buffer = buffer;
      audio.connect(context.destination);
    } else {
      audio = buffer;
      audio.currentTime = 0;
    }
    // start the trial
    ctrl_fcn('start');
  })
  .catch(function (err) {
    console.error(`Failed to load audio file "${trial.tone_file}". Please check the file path. We recommend using the preload plugin to load audio files.`);
    console.error(err);
  });

我已经研究并遇到了有关预加载的建议,但预加载(使用网络音频 api 或 jspsyche)似乎并不能解决问题。我也尝试过像 Howler.js 这样的替代音频库,但问题仍然存在。

我还注意到,当 RAM 使用率较高时,发生率会明显升高。

如果您有任何可能有帮助的想法,我将不胜感激。

javascript audio html5-audio web-audio-api jspsych
1个回答
0
投票

使用

setTimeout()
不太准确。它仅保证回调将在时间过去后被调用,但不能保证恰好在该时间被调用。

但是您可以直接使用 Web Audio API 来非常精确地安排某些事情。

类似下面的东西应该可以做到。

source.start(context.currentTime + trial.tone_delay_ms / 1000);
© www.soinside.com 2019 - 2024. All rights reserved.