npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bg-sound

v2.0.0

Published

Web Component to emulate the old-school <bgsound> HTML element

Downloads

33

Readme

bg-sound

travis npm downloads javascript style guide

Web Component to emulate the old-school <bgsound> HTML element

Play MIDI files in a browser with a simple Web Component, emulating <bgsound>, the Background Sound element.

Install

npm install bg-sound

If you do not use a bundler, you can use the standalone script directly in a <script> tag.

Usage

Add a custom HTML element called <bg-sound>:

<script src="bg-sound.min.js"></script>
<bg-sound src="sound.mid"></bg-sound>

Automatically make legacy <bgsound> and <embed> HTML tags work:

<!-- Must include *BEFORE* all <bgsound> and <embed> tags -->
<script src="../bg-sound.min.js"></script>
<script>BgSound.enableCompatMode()</script>

<!-- All of the following HTML tags now work! -->
<bgsound src="goldensun.mid">
<embed src="goldensun.mid">
<embed src="mario-death.mid" loop="2">
<embed src="sound.wav">
<embed src="sound.wav" loop=true>

Talk

I introduced this project in a talk at JSConf Colombia 2018.

The Lost Art of MIDI talk

  • Slides
  • Talk video (coming soon!)

FAQ

Why is the tag called <bg-sound> (with a dash)?

The name of a custom HTML element must contain a dash (-). This is what the spec says, presumably because otherwise browsers could not introduce new HTML tags without web compatibility risk.

Why is the script tag required?

The script tag is needed to define the behavior of the <bg-sound> HTML element. Without it, the browser would just treat the tag like a <div>.

Where do the WebAssembly code and instrument sound files come from?

By default, these files will load remotely from BitMidi. This is nice for simple demos and quick hacks. However, it is recommended to host these files yourself. (I reserve the right to remove the CORS headers which allow this to work at any time if too much bandwidth is used.)

What are the timidity and freepats packages?

npm install timidity freepats

The <bg-sound> custom element lazily loads a WebAssembly file and instrument sounds at runtime.

The timidity package provides the WebAssembly file (libtimidity.wasm). The freepats package provides the instrument sound files.

It's important to ensure that the timidity and freepats folders in node_modules are being served to the public. For example, here is how to mount the necessary files at / with the express server:

const timidityPath = path.dirname(require.resolve('timidity'))
app.use(express.static(timidityPath))

const freepatsPath = path.dirname(require.resolve('freepats'))
app.use(express.static(freepatsPath))

Optionally, provide a baseUrl attribute to customize where the player will look for the lazy-loaded WebAssembly file libtimidity.wasm and the FreePats General MIDI soundset files. The default baseUrl is https://bitmidi.com/timidity/.

<bg-sound src="sound.mid" baseUrl="/custom-path"></bg-sound>

How do I automatically make legacy <bgsound> and <embed> tags work?

Include this code before any <bgsound> or <embed> HTML tags:

<!-- Must include *BEFORE* all <bgsound> and <embed> tags -->
<script src="../bg-sound.min.js"></script>
<script>BgSound.enableCompatMode()</script>

<!-- All of the following HTML tags now work! -->
<bgsound src="goldensun.mid">
<embed src="goldensun.mid">
<embed src="mario-death.mid" loop="2">
<embed src="sound.wav">
<embed src="sound.wav" loop=true>

If you want to provide your own baseUrl, then simply pass that into the BgSound.enableCompatMode() function call as follows:

BgSound.enableCompatMode({ baseUrl: '/custom-path' })

Demo

If you like this, then check out BitMidi.com, the wayback machine for old-school MIDI files! Check out some examples MIDIs here:

License

MIT. Copyright (c) Feross Aboukhadijeh.