【问题标题】:How do I get only a unique value (note) to play - no repeats Javascript我如何只获得一个唯一值(注释)来播放 - 不重复 Javascript
【发布时间】:2021-10-20 11:15:18
【问题描述】:

我有一个完美的工作脚本。基本上,我每 5 秒轮询一次 API 以获取 myJson.Temperature,并根据返回的值播放一个 MIDI 音符。值在 0 到 16 之间。这是脚本:

let easymidi = require("easymidi")
let output = new easymidi.Output("mindiPort", true)
let interval;
const fetch = require("node-fetch");

let sendNote = (noteValue, duration) => {
    output.send("noteon", noteValue)

    setTimeout(()=> {
        output.send("noteoff", noteValue)
    }, duration);
}

const api_url = 'https://www.placeholder_url.com?timeout=1000'
setInterval(() =>
fetch(api_url)
   .then((response) => {
      return response.json();
   })

.then((myJson) => {
      let note;
      if (myJson.temperature == 0) {
        note = 24;
      } else if (myJson.temperature == 2) {
        note = 25;
      } else if (myJson.temperature == 4) {
        note = 26;
      } else if (myJson.temperature == 6) {
        note = 27;
      } else if (myJson.temperature == 8) {
        note = 28;
      } else if (myJson.temperature == 10) {
        note = 29;
      } else if (myJson.temparature == 12) {
        note = 30;
      } else if (myJson.temperature == 14) {
        note = 31;
      } else {
        note = 32;
      }
      let noteValue = {
         note: note,
         velocity: 100,
         channel: 1
       }
       sendNote(noteValue, 500)
   
 }), 5000); // API call every 5 seconds **This will also play a (same) note every five seconds
//  if conditions are met**

我怎样才能让音符只播放一次,即; - 如果后续轮询完成并返回相同的值,则可以忽略该注释,直到返回唯一值?我查看了filteronlyUnique,但我不确定如何将它们合并到代码中。提前谢谢你。

【问题讨论】:

  • 您是否尝试过简单地将您的笔记声明移到范围内,以便在新呼叫中重新分配它时,您可以简单地执行if(myNewNote === note) return
  • 您可以尝试存储区间外的最后一个温度值。每次轮询时,比较最后一个值,如果它们相等,则不做任何事情。如果它们不同,则更新最后一个值
  • 附注:您可以将温度->音符映射存储在对象中。然后你可以像note = myObj[temperature] 那样做,而不是所有这些else if 语句

标签: javascript unique repeat midi


【解决方案1】:

我会采用一种简单的方法并跟踪最后发出的音符:

编辑:我的代码是错误的,因为它正在检查复杂对象的相等性,已修复它

let easymidi = require("easymidi")
let output = new easymidi.Output("mindiPort", true)
let interval;
const fetch = require("node-fetch");

let lastNote = null;
let sendNote = (noteValue, duration) => {
    output.send("noteon", noteValue)

    setTimeout(()=> {
        output.send("noteoff", noteValue)
    }, duration);
}

const api_url = 'https://www.placeholder_url.com?timeout=1000'
setInterval(() =>
fetch(api_url)
   .then((response) => {
      return response.json();
   })

.then((myJson) => {
      let note;
      if (myJson.temperature == 0) {
        note = 24;
      } else if (myJson.temperature == 2) {
        note = 25;
      } else if (myJson.temperature == 4) {
        note = 26;
      } else if (myJson.temperature == 6) {
        note = 27;
      } else if (myJson.temperature == 8) {
        note = 28;
      } else if (myJson.temperature == 10) {
        note = 29;
      } else if (myJson.temparature == 12) {
        note = 30;
      } else if (myJson.temperature == 14) {
        note = 31;
      } else {
        note = 32;
      }
      if (lastNote !== note) {
          let noteValue = {
             note: note,
             velocity: 100,
             channel: 1
           }
           sendNote(noteValue, 500)
      }
      lastNote = note;
   
 }), 5000); // API call every 5 seconds **This will also play a (same) note every five seconds
//  if conditions are met**

【讨论】:

  • 好主意 - def 让我朝着正确的方向前进!由于某种原因,注释仍然重复 - 不知道为什么,但我应该能够从这里进行故障排除:) 编辑它实际上每 3 秒左右重复一次,所以它可能与代码有其他关系。
  • 该死的 - 没有理由这不应该工作。我的代码肯定有其他根本性的问题。
  • 我的错,我没有看到 noteValue 是一个复杂的对象,我会更新我的代码
  • 谢谢谢谢!!!不要道歉,你的小排放教会了我很多试图解决这个问题。它现在可以工作了——100% 我很高兴——我对 Python 更舒服了,所以修复 Javascript 错误对我来说简直是头疼。我欠你!! :) :) :)
猜你喜欢
  • 2021-10-05
  • 2018-12-13
  • 1970-01-01
  • 2017-07-08
  • 2015-07-27
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多