Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
Running Tone.js in Ace Code Editor loop is duplicating

I am making a tutorial on how to learn JavaScript using Tone.js. I am using the Ace editor to run the code in. Everything is working but every time you press play it doubles that sound each time causing distortion.

I tried to use ace's editor.setValue(''); to clear the value on stop but that remove everything from the code editor. I need to clear the value from the script but leave it in the editor div. Not sure what I can do here.

const editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
editor.setOptions({fontSize:"14pt"});
const go = () => {

const userCode = editor.getSession().getValue();

try {
    new Function(userCode)();
    console.log(userCode);
} catch (err) {
    console.error(err);
}
    Tone.Transport.start();
}
const stop = () => {
    Tone.Transport.stop();
}
#editor {
    width: 100%;
    height: 91%;
    position: relative;
    min-height: 500px;
}
.run {
    display: inline-block;
    width: 100px;
    height: 30px;
    background-color: #f0f0f0;
    border-radius: 4px;
    text-align: center;
    line-height: 20pt;
    font-weight: 900;
    cursor: pointer;
    margin: 0 11px 11px 0;
}
#go {
margin-left: 0;
}
.run:hover {
background-color: #75e9e9;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/13.8.9/Tone.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.4/ace.js"></script>
<div>
<div id="go" class="run" onclick="go()">Play</div>
<div id="stop" class="run" onclick="stop()">Stop</div>
</div>
    
<div id="editor">
const synth = new Tone.Synth();
synth.toMaster();
// C major cord
const notes = [
"C4", "E4", "G4",
"C5", "E5", "G5"
];
var index = 0;

Tone.Transport.scheduleRepeat((time) => {
    repeat(time);
}, '8n');

function repeat(time) {
    let note = notes[index % notes.length];
    synth.triggerAttackRelease(note, '8n', time);
    index++;
}
</div>

</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try moving the stop function outside of the go function.

const go = () => {
  Tone.Transport.start();
};

const stop = () => {
  Tone.Transport.stop();
};

const synth = new Tone.Synth();
synth.toMaster();
// C major cord
const notes = ["C4", "E4", "G4", "C5", "E5", "G5"];
var index = 0;

Tone.Transport.scheduleRepeat((time) => {
  repeat(time);
}, "8n");

function repeat(time) {
  let note = notes[index % notes.length];
  synth.triggerAttackRelease(note, "8n", time);
  index++;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/13.8.9/Tone.js"></script>
<div>
  <div id="go" class="run" onclick="go()">Play</div>
  <div id="stop" class="run" onclick="stop()">Stop</div>
</div>

<div id="editor">

</div>

</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda