weixin_33697898 2016-02-19 05:17 采纳率: 0%
浏览 113

在p5js中循环振荡器

I'm having trouble creating a sequence of sounds using an oscillator with a for loop in p5js. I'm getting my parameters from an ajax request and I verify the data. I can't find any examples of this online, so maybe there's a more library specific way of generating sequences of sounds. My approach that doesn't work goes something like this:

getParams is called upon pushing a button. Nothing goes wrong here...

function getParams(mode){
  console.log("trying to get tune...");
  var csrftoken = getCookie('csrftoken');
  var mode = mode;
  $.ajax({
    url: window.location.href,
    type: "POST",
    data : { mode : mode,
             csrfmiddlewaretoken : csrftoken,
   },
    success: function(json){
             genTune(json);
  },          
error : function(xhr, errmsg, err) {
              console.log(xhr.status + ": " + xhr.responseText);
    },
  })
}

genTune is fed data from getParams.

function genTune(json){
  playing = true;
  var level = json["level"] + 4;
  a = json["a"];
  b = json["b"];
  aFreq = json["a_freq"];
  bFreq = json["b_freq"];

  for (var i = 0; i < level; i++) {
    console.log("i = " + i);
    if (i===a){
      osc.freq(aFreq * 10);
      osc.amp(0.5, 0.05);
      osc.start();
      osc.stop(0.5);

    } else if (i===b){       
      osc.freq(bFreq * 10);
      osc.amp(0.5, 0.05);
      osc.start();
      osc.stop(0.5);

    } else {       
      osc.freq(NOTE_FREQS[random(NOTE_FREQS.length-1)]);
      osc.amp(0.5, 0.05);
      osc.start();
      osc.stop(0.5);
    }

  }

   }

EDIT: The loop works and the message is iterated now that I've fixed a stupid error, but sound is only played once. Can anyone tell me how to make a sequence of sounds with a loop? Thanks!

To clarify, there are no console errors in Firebug.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度