Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

82
Views
Play loaded audio file with Tone.js web audio framework

Hi I'm creating a simple upload page where user load their local files and play it. For hour, i'm not uploading to the server, just want to create a file input and then play the audio file with Tone.js if it's possibel.

I'm using to this test P5.js library and Tone.js audio framework, the code below:

let inputFile, bttPlayFile;
let files;

window.AudioContext = window.AudioContext || window.webkitAudioContext;
let context = new Tone.Context();
let source;

function setup() {
  
  inputFile = createFileInput(uploadTrack);
  bttPlayFile = createButton('play');
  bttPlayFile.mousePressed(tooglePlay);
}

function playSound(arraybuffer) {
  context.decodeAudioData(arraybuffer, function(buf) {
    source = new Tone.BufferSource();
    source.connect(context).toDestinattion();
    source.Tone.Buffer = buf;
    source.start(0);
  });
}

function selectedFile(evt) {   
  files = evt.target.files;
  playFile(files[0]);
}

function playFile(file) {
  let reader = new FileReader();
  reader.onload = function(e) {
    console.log(e.target.result);
    playSound(e.target.result);
  }
  reader.readAsArrayBuffer(file);
}

function uploadTrack() {
  inputFile.changed(selectedFile, false);
}

function tooglePlay() {
  Tone.Transport.start();
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.32/Tone.js" integrity="sha512-USKCQh+O8BX/a2K06xPNTwduhmQvN/m9FhkR7PRysCRlPoqIItl7Qz3xVTZC/oIHe6g5XvnLHDUgGpRMZZTmFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>

At the moment, nothing happens and I want to understand why and how to proceed with this. Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you open the console of your browser you should see a warning.

The AudioContext is "suspended". Invoke Tone.start() from a user action to start the audio.

I think it will work if you call Tone.start() in the click handler of your "play" button before you load and decode the selected audio file.

It's unrelated to your question but it should be safe to remove window.AudioContext = window.AudioContext || window.webkitAudioContext from your code. Tone.js uses standardized-audio-context internally which should handle this for you.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!