danigb / smplr

A web audio sampler instrument
https://danigb.github.io/smplr/
186 stars 19 forks source link

Error: "Relative module specifiers must start with “./”, “../” or “/”.`" #35

Closed msh75 closed 1 year ago

msh75 commented 1 year ago

Hi, I'm getting this error when trying to make a simple sound : Uncaught TypeError: The specifier “smplr” was a bare specifier, but was not remapped to anything. Relative module specifiers must start with “./”, “../” or “/”. The code I run -after installing using 'npm i smplr' is like this:

<script type="module">
import { Soundfont } from "smplr";
const context = new AudioContext();
const marimba = new Soundfont(context, { instrument: "marimba" });
marimba.start({ note: 60, velocity: 80 });
</script>
danigb commented 1 year ago

There are two issues with your code:

  1. npm packages needs a url to be retrieved
  2. you need to play audio after a user interaction

This code will work (tested on codepen). In this example I used unpkg.com, but any other service could work:

<html>
  <body>
    <button id="btn">play</button>
  </body>
  <script type="module">
    import { Soundfont } from "https://unpkg.com/smplr@0.10.0/dist/index.mjs"; // needs to be a url
    const context = new AudioContext(); // create the audio context
    const marimba = new Soundfont(context, { instrument: "marimba" }); // create the player
    document.getElementById("btn").onclick = () => {
      context.resume(); // enable audio context after a user interaction
      marimba.start({ note: 60, velocity: 80 }); // play the note    
    }
  </script>
</html>

Feel free to reopen if you still have problems.