Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • So the idea is that i have my source code in src folder, and my audio files in another folder. Would this work if i were to use fileReader.readAsArrayBuffer("path/to/file.mp3")? Or maybe by reading the file to a Buffer and passing the buffer to the file reader? Commented Jul 3 at 8:30
  • Ah, I misinterpreted the original question. I assumed you were trying to have users select a file and play it back. Works roughly like that: 1. Read the data with fs.readFile or fs.readFileSync etc. with encoding set to buffer 2. Turn the buffer into an ArrayBuffer 3. Pass ArrayBuffer into Tone.Player constructor
    – geekonaut
    Commented Jul 3 at 10:22
  • I tried following those steps with the following code: let buffer = fs.readFileSync(rscPath + "/samples/piano/A4.mp3") const view = new Uint8Array(buffer).buffer; console.log(view) let player = new Tone.Player(view).toDestination(); Tone.loaded().then(() => { player.start(); }) but i just get the following error: Debug.js:17 Uncaught (in promise) RangeError: Value must be within [0, Infinity], got: NaN Commented Jul 3 at 15:36
  • That doesn't work, because readFileSync defaults to reading the data as a string. Use fs.readFileSync(rscPath + '/samples/piano/A4.mp3', {encoding: 'buffer') instead.
    – geekonaut
    Commented Jul 3 at 16:26