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

241
Visualizações
Trying to detect when oscillator audio ends (javascript)

I am trying to detect when the oscillator audio has stopped playing. When I use onended,onend or event listener ended it only gets detected when I execute oscillator.stop()

Any one has any idea what I'm doing wrong or how this can be done? Thanks in advance.

The code of the oscillator is below:

let AudioContext = window.AudioContext || window.webkitAudioContext;
let ctx = new AudioContext();
let dot = 1.2 / 15;

function morsecode_audio_play() {
    morsecode_textToSpeech_stop()
    document.querySelector('#play_audio_button_morse').innerHTML = '<button id="stop_audio_morse">⏹️</button>';
    document.querySelector('#stop_audio_morse').addEventListener('click', morsecode_audio_stop);

    oscillator = ctx.createOscillator();
    let t = ctx.currentTime;

    oscillator.type = "sine";
    oscillator.frequency.value = 600;
  
    let gainNode = ctx.createGain();
    gainNode.gain.setValueAtTime(0, t);
    
    let morsecode = inputMorse.value.split("");

    morsecode.forEach(function(letter) {
        switch(letter) {
            case ".":
                gainNode.gain.setValueAtTime(1, t);
                t += dot;
                gainNode.gain.setValueAtTime(0, t);
                t += dot;
                break;
            case "-":
                gainNode.gain.setValueAtTime(1, t);
                t += 3 * dot;
                gainNode.gain.setValueAtTime(0, t);
                t += dot;
                break;
            case " ":
                t += 7 * dot; 
                break;
        }  
    });

    oscillator.connect(gainNode);
    gainNode.connect(ctx.destination);
    oscillator.start();
    oscillatorStatus = true;

    oscillator.addEventListener('ended', () => {
        console.log('oscillator ended');
        morsecode_audio_load();
    })

    return false;
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I managed to solve it by using the duration time of the oscillator sound.

The time is stored by the foreach in a variable t for time. I used that variable in a timer to detect the end.

   setTimeout(function(){
      morsecode_audio_stop();
      t = 0;
    }, t*1000);
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