【问题标题】:Can anyone help me pass this data? ;)谁能帮我传递这些数据? ;)
【发布时间】:2021-09-15 04:35:49
【问题描述】:

我是开发新手。当然,我正在努力解决一些基本问题。如果有人可以帮助我并给出一点解释,我将不胜感激。

我正在使用tone.js 库创建一个合成器。我了解如何通过一个“乐器”来创建合成器,但是我在动态执行它时遇到了问题,我正在给自己挖一个洞。

代码如下:

//instruments 
const synth = new Tone.Synth().toDestination();
const amSynth = new Tone.AMSynth().toDestination();
const duoSynth = new Tone.DuoSynth().toDestination();
const fmSynth = new Tone.FMSynth().toDestination();
const membraneSynth = new Tone.MembraneSynth().toDestination();
const metalSynth = new Tone.MetalSynth().toDestination();
const monoSynth = new Tone.MonoSynth({
  oscillator: {
    type: "square"
  },
  envelope: {
    attack: 0.1
  }
}).toDestination();
const noiseSynth = new Tone.NoiseSynth().toDestination();
const pluckSynth = new Tone.PluckSynth().toDestination();
const polySynth = new Tone.PolySynth().toDestination();
const sampler = new Tone.Sampler({
  urls: {
    A1: "A1.mp3",
    A2: "A2.mp3",
  },
  baseUrl: "https://tonejs.github.io/audio/casio/",
  onload: () => {
    sampler.triggerAttackRelease(["C1", "E1", "G1", "B1"], 0.5);
  }
}).toDestination();

//effects 
const distortion = new Tone.Distortion(0.4).toDestination();
const vibrato = new Tone.Vibrato(0.4).toDestination();
const pingPong = new Tone.PingPongDelay("4n", 0.2).toDestination()
const autoWah = new Tone.AutoWah(50, 6, -30).toDestination();
const cheby = new Tone.Chebyshev(50).toDestination();
const autoFilter = new Tone.AutoFilter("4n").toDestination()
const autoPanner = new Tone.AutoPanner("4n").toDestination();
const crusher = new Tone.BitCrusher(4).toDestination();
const chorus = new Tone.Chorus(4, 2.5, 0.5).toDestination();
const feedbackDelay = new Tone.FeedbackDelay("8n", 0.5).toDestination();
const freeverb = new Tone.Freeverb().toDestination();
freeverb.dampening = 1000;
const shift = new Tone.FrequencyShifter(42).toDestination();
const reverb = new Tone.JCReverb(0.4).toDestination();
const phaser = new Tone.Phaser({
  frequency: 15,
  octaves: 5,
  baseFrequency: 1000
}).toDestination();
const tremolo = new Tone.Tremolo(9, 0.75).toDestination();


//array of notes --- we add the sharps in the function 
var notes = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
var html = '';

for (var octave = 0; octave < 2; octave++) {


  for (var i = 0; i < notes.length; i++) {
    var hasSharp = true;
    var note = notes[i]

    if (note == 'E' || note == 'B')
      hasSharp = false;

    //white keys with one octive
    html += `<div class='whitenote play' onmousedown='noteDown(this)' data-note='${note + (octave + 4)}'>`;

    //black keys with one octive
    if (hasSharp) {
      html += `<div class='blacknote play' onmousedown='noteDown(this)' data-note='${note + '#' + (octave + 4)}'></div>`;
    }

    html += '</div>'
  }

}
$('container').innerHTML = html;

$(".play").click(function(elem) {
  var note = elem.dataset.note;
  var synth = getSynth(elem.data("synth"));
  synth.connect(phaser);
  synth.connect(autoWah);
  synth.triggerAttackRelease(note, "16n");
  event.stopPropagation();
});


function getSynth(name) {
  switch (name) {
    case "Synth":
      return synth;
    case "AM":
      return amSynth;
    case "Duo":
      return duoSynth;
    case "FM":
      return fmSynth;
    case "Membrane":
      return membraneSynth;
    case "Metal":
      return metalSynth;
    case "Mono":
      return monoSynth;
    case "Noise":
      return noiseSynth;
    case "Pluck":
      return pluckSynth;
    case "Poly":
      return polySynth;
  }
}
#container {
  position: absolute;
  height: 200px;
  border: 2px solid black;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  white-space: nowrap;
  display: block;
  white-space: inherit;
  overflow: hidden;
}

.whitenote {
  height: 100%;
  width: 50px;
  background: white;
  float: left;
  border-right: 1px solid black;
  position: relative;
}

.blacknote {
  position: absolute;
  height: 65%;
  width: 55%;
  z-index: 1;
  background: #777;
  left: 68%;
}

.instrument-button {
  background: orangered;
  border: none;
  color: white;
  padding: 8px 16px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 12px;
}

.effect-button {
  background: blue;
  border: none;
  color: white;
  padding: 8px 16px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 12px;
}

.effect-button-padding {
  padding-top: 16px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.26/Tone.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.26/Tone.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <script src='node_modules\tone\build\Tone.js'></script>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.26/Tone.js.map'></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>

</head>

<body style="align-items: center;">
  <div>
    <h1>Synth AF</h1>
  </div>
  <div>
    <h4>Intruments</h4>
    <button class="btn instrument-button" data-synth="Synth" id="synth-button">Synth</button>
    <button class="btn instrument-button" data-synth="AM" id="amSynth-button">AM</button>
    <button class="btn instrument-button" data-synth="Duo" id="duoSynth-button">Duo</button>
    <button class="btn instrument-button" data-synth="FM" id="fmSynth-button">FM</button>
    <button class="btn instrument-button" data-synth="Membrane" id="membraneSynth-button">Drums</button>
    <button class="btn instrument-button" data-synth="Metal" id="metalSynth-button">Metal</button>
    <button class="btn instrument-button" data-synth="Mono" id="monoSynth-button">Mono</button>
    <button class="btn instrument-button" data-synth="Noise" id="noiseSynth-button">Noise</button>
    <button class="btn instrument-button" data-synth="Pluck" id="pluckSynth-button">Plucky</button>
    <button class="btn instrument-button" data-synth="Poly" id="polySynth-button">Poly</button>
  </div>
  <div class="effect-button-padding">
    <h4>Effects</h4>
    <button class='btn effect-button' id="distortion-button">Distortion</button>
    <button class='btn effect-button' id="vibrato-button">Vibrato</button>
    <button class='btn effect-button' id="pingPong-button">PingPong</button>
    <button class='btn effect-button' id="autoWah-button">Wah</button>
    <button class='btn effect-button' id="crusher-button">BitCrusher</button>
    <button class='btn effect-button' id="phaser-button">Phaser</button>
    <button class='btn effect-button' id="reverb-button">Reverb</button>
    <button class='btn effect-button' id="autoFilter-button">Auto Filter</button>
    <button class='btn effect-button' id="feedbackDelay-button">Feedback</button>
    <button class='btn effect-button' id="cheby-button">Chebyshev</button>
  </div>
  <div class="" id="container">

  </div>
  
  </body> 
 </html>

这是用户界面。 (还没设计...不要评判) UI Photo

我们的想法是使用在 one 常量中创建的新工具之一,方法是通过单击按钮将其传递给 switch 语句,然后传递给输出。如果我没有清楚地解释任何内容或使用了错误的术语,我深表歉意。

非常感谢您的帮助。

感谢您的阅读, FandopTheNoob

【问题讨论】:

  • 欢迎使用 Stack Overflow,以后在添加代码片段时,请确保将所需的库正确添加到代码中,以便您的片段正确解析代码。添加代码段的原因是您可以重新创建您在问题中概述的问题。我添加了tone js CDN,因为您的本地链接到您的机器。我还添加了 JQuery 并将正确的代码放置在 snippit 的正确部分中,以便它可以为您正确解析。祝你的问题好运,再次欢迎来到 Stack Overflow ;)
  • 非常感谢!感谢@dalelandry 的帮助。我无法将密钥显示在单独的文件中。我相信我会从中学到更多。

标签: javascript jquery user-interface audio dynamic


【解决方案1】:

// this will be the "state" of the synthesiser
const synthSetup = {
  instrument: "",
  effects: [],
}

// this is the list of instruments
const instruments = [{
    synth: "Synth",
    id: "synth-button",
    text: "Synth",
    tone: new Tone.Synth().toDestination(),
  },
  {
    synth: "AM",
    id: "amSynth-button",
    text: "AM",
    tone: new Tone.AMSynth().toDestination(),
  },
]

// this is the list of effects
const effects = [{
    id: "distortion-button",
    text: "Distortion",
    tone: new Tone.Distortion(0.4).toDestination(),
  },
  {
    id: "vibrator-button",
    text: "Vibrato",
    tone: new Tone.Vibrato(0.4).toDestination(),
  },
]

// setting up the instruments & effects buttons
const instrumentsHtml = (instruments) => {
  return instruments.map(({
    synth,
    id,
    text
  }) => {
    return `
      <button class="btn instrument-button" data-synth=${synth} id="${id}">${text}</button>
    `
  }).join('')
}

const effectsHtml = (effects) => {
  return effects.map(({
    id,
    text
  }) => {
    return `
      <button class="btn effect-button" id="${id}">${text}</button>
    `
  }).join('')
}

const instrumentsContainer = document.getElementById("btn-group-instruments")
instrumentsContainer.innerHTML = instrumentsHtml(instruments)

const effectsContainer = document.getElementById("btn-group-effects")
effectsContainer.innerHTML = effectsHtml(effects)

// setting up click handlers on the instruments & effects buttons
$("body").on("click", ".instrument-button", function() {
  // setting up synthSetup.instrument:
  synthSetup.instrument = $(this).data("synth")
})

$("body").on("click", ".effect-button", function() {
  // setting up synthSetup.effects:
  synthSetup.effects = [...new Set([...synthSetup.effects, $(this).attr("id")])]
})

//array of notes --- we add the sharps in the function
var notes = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
var html = '';

for (var octave = 0; octave < 2; octave++) {


  for (var i = 0; i < notes.length; i++) {
    var hasSharp = true;
    var note = notes[i]

    if (note == 'E' || note == 'B')
      hasSharp = false;

    //white keys with one octave
    html += `<div class='whitenote play' data-note='${note + (octave + 4)}'>`;

    //black keys with one octave
    if (hasSharp) {
      html += `<div class='blacknote play' data-note='${note + '#' + (octave + 4)}'></div>`;
    }

    html += '</div>'
  }

}
const container = document.getElementById("container")
container.innerHTML = html;

$("body").on("click", ".play", function() {
  if (!synthSetup.instrument) {
    alert("Choose an instrument first!")
  } else {
    const {
      tone: synth
    } = instruments.find(({
      synth
    }) => synth === synthSetup.instrument)
    const note = $(this).data("note")

    // connecting the effects
    synthSetup.effects.forEach(e => {
      const effect = effects.find(({
        id
      }) => e)
      synth.connect(effect.tone)
    })

    const now = Tone.now()
    synth.triggerAttackRelease(note, "2n", now)
  }
})
html,
body {
  height: 100%;
  position: relative;
}

#container {
  position: absolute;
  height: 200px;
  border: 2px solid black;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  white-space: nowrap;
  display: block;
  white-space: inherit;
  overflow: hidden;
}

.whitenote {
  height: 100%;
  width: 50px;
  background: white;
  float: left;
  border-right: 1px solid black;
  position: relative;
}

.blacknote {
  position: absolute;
  height: 65%;
  width: 55%;
  z-index: 1;
  background: #777;
  left: 68%;
}

.instrument-button {
  background: orangered;
  border: none;
  color: white;
  padding: 8px 16px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 12px;
}

.effect-button {
  background: blue;
  border: none;
  color: white;
  padding: 8px 16px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 12px;
}

.effect-button-padding {
  padding-top: 16px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.26/Tone.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.26/Tone.js.map'></script>

<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div>
  <h1>Synth AF</h1>
</div>
<div>
  <h4>Intruments</h4>
  <div id="btn-group-instruments"></div>
</div>
<div class="effect-button-padding">
  <h4>Effects</h4>
  <div id="btn-group-effects"></div>
</div>
<div class="" id="container">

</div>

所以,这是合成器的下一步:

  • 合成器的当前状态保存在synthSetup
  • 仪器存储为对象数组
  • 效果存储为对象数组
  • 乐器和效果动态放入 DOM
  • 点击任何白色或黑色按钮时,synthSetup(状态)会被读取并播放音符(所选乐器和效果列表)

问题:

  • 您不能从效果列表中删除一个效果(可以在效果按钮的点击处理程序中处理)

【讨论】:

  • 非常感谢。我会回复你这是如何工作的
  • 非常感谢您,这非常有帮助。我想我需要为这个节点项目创建服务器,然后我可以测试。我会更新
  • @FandopTheNoob 我很高兴能帮上忙。以易于迭代的格式(数组、对象等)存储数据总是有帮助的 - 计算机非常擅长处理一系列事物:)
  • Uncaught TypeError: Cannot set property 'innerHTML' of null at synth.js:60 在这条线上const instrumentsContainer = document.getElementById("btn-group-instruments") instrumentsContainer.innerHTML = instrumentsHtml(instruments)
  • 此外,黑色音符不再演奏升号。现在是以前的白音符音调。
猜你喜欢
  • 2015-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-11
  • 1970-01-01
  • 2011-05-03
  • 2019-08-28
  • 1970-01-01
相关资源
最近更新 更多