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

minikeys

v0.1.2

Published

A javascript library that plays the piano

Downloads

6

Readme

MiniKeys

npm version release

A tiny javascript library that plays the piano.

Get Started

MiniKeys is hosted via CDN and can be linked with the following:

  • Latest verison:
    <script src="https://cdn.jsdelivr.net/npm/minikeys/dist/minikeys.min.js"></script>
  • Specific release (v0.1.1):
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/minikeys.min.js"></script>

Reference

First, instantiate a new instance of MiniKeys:

let miniKeys = new MiniKeys();

Then call init() to initialise the Web Audio Api:

miniKeys.init();

Load all samples with loadSamples(samples). Pass in an array of urls to audio files with the naming convention [p/f][midi-note], where [p/f] represents the notes dynamic (p for piano, f for forte) and [midi-note] is the corresponding midi value of the note (60 for C4):

miniKeys.loadSamples([
    '/samples/p45.ogg',
    '/samples/f45.ogg'
]);

MiniKeys works with any number of samples. When a note is played, MiniKeys finds the closest loaded sample and tunes it to the correct frequency. In the above example, only one sample has been loaded (with 2 dynamics), and so it will be used for all notes.

To play a note use playNote(note, velocity) where note is a midi note (0 to 127), and velocity is a midi velocity value (between 0 and 127):

miniKeys.playNote(60, 100);

To play a note via the keyboard, getKeyMap() returns a map of keyboard keys to midi notes in the format "key": midi-note. This is initialised as the following, where the highlighted section of the piano shows the notes playable on the keyboard:

keys

These mappings can be shifted up and down the keyboard with shift(direction, shiftMode), where direction is either 1 (up the piano) or -1 (down the piano) and shiftMode is either OCTAVE or TONE. OCTAVE shifts the keyboard by an octave (8 notes) TONE shifts the keyboard by one white key:

miniKeys.shift(-1, miniKeys.OCTAVE);

Several methods are provided to make rendering a representation of the piano and keyboard easy. getActiveKeys() returns an array of which keyboard keys currently map to midi notes in the format "key": midi-note (for example, in the above image, q currently is not an active key). getKeyIndex() returns the current position on the piano (in terms of white keys) of the furthest left note. This can be used with NUMKEYS to highlight a section of the piano corresponding to the playable keys. getWhiteMidiNotes() and getBlackMidiNotes() return an array of length 52 containing the midi notes along the piano from left to right for white keys and black keys respectively. This is useful for rendering black keys as gaps between keys are represented with a -1 in that location in the array (an example of this is included with the project).

Volume can be changed with volume(value), where value is between 0 and 1:

miniKeys.volume(0.5);

Build process

First, clone the project, then run the following commands:

npm install
npm run build

This will generate a /dist folder with the minimised version of the library minikeys.min.js.

To run tests, use the command:

npm test

TODO

  • [x] clean up interface with keyboard (move validation of key into library)
  • [x] compress samples (.ogg)
  • [x] trim start of samples
  • [ ] tidy/comment code
  • [x] add tests
  • [ ] cover all functions with tests
  • [x] write build guide for readme
  • [x] add method for adding loading bar
  • [x] add animations to page
  • [ ] fix on iOS
  • [ ] add midi parser and player
  • [ ] add tuning mode (perfect atm, out of tune with actual piano)
  • [ ] move keyboard with mouse in example