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

keyboard-midi-controller

v1.1.3

Published

An easy-to-use library made for emulating a MIDI device with your keyboard.

Downloads

28

Readme

An easy-to-use library made for emulating a MIDI device with your keyboard.
Typescript and Javascript compatible.

⬇️ Installation

npm i keyboard-midi-controller

✌️ Getting Started

First, you have to import the library:

import { KeyboardMidiController } from 'keyboard-midi-controller'

The library exposes the "KeyboardMidiController" class, which needs to be instantiated.

const controller = new KeyboardMidiController()

To make the controller usable, you need to connect it to your page

controller.link()

Now, if you open developer tools and press a key, you should be able to see logs such as these:

Attack Event > {key: 'z', frequency: 261.624, note: 'C4', velocity: 100}
Release Event > {key: 'z', frequency: 261.624, note: 'C4', velocity: 100}

🦾 Tailoring it to your needs

controllerOutputAttack: ControllerOutputType;

controllerOutputRelease: ControllerOutputType;

To access attack and release event objects, you have to pass a callback function to controllerOutputAttack and controllerOutputRelease

const myAttackCallback: ControllerOutputType = (e: ControllerEvent) {
    console.log(`my attack event note: ${e.note}`)
}
const myReleaseCallback: ControllerOutputType = (e: ControllerEvent) {
    console.log(`my release event note: ${e.note}`)
}

controller.controllerOutputAttack = myAttackCallback
controller.controllerOutputRelease = myReleaseCallback

Your custom callback will override the functions' default behaviour. To return the default behaviour back set a field to undefined.

controller.controllerOutputAttack = undefined

❇️ Basics

Base Frequency

baseFrequency: number = 440

A field that is used to calculate frequencies of all notes. By default it is set to 440hz.

baseFrequency can not be lower than 1 and higher than 20000

controller.baseFrequency = 1000

Octaves

firstOctave: number = 4
secondOctave: number = 5

Octave fields control the octave that you play in and take part in frequency calculation.

An octave can not have a value lower than 1 and higher than 7

controller.firstOctave = 3

Velocity

velocity: number = 100

A field that does not play a significant role behind a controller scenes, but can be used by the user as it is included in the output object

Velocity can not be lower than 0 and higher than 100

controller.velocity = 63

Constructor

constructor(
    controllerOutputAttack?: ControllerOutputType,
    controllerOutputRelease?: ControllerOutputType,
    baseFrequency: number = 440,
    firstOctave: number = 4,
    secondOctave: number = 5,
    velocity: number = 100
)

✴️ Additional options

Event Trigger

keydownTrigger: ControllerTriggerType
keyupTrigger: ControllerTriggerType

A trigger is a function that constructs an object of type ControllerEvent and passes it to the corresponding output function. Triggers are directly connected to event listeners.

If you want to change the default behaviour and use the controller in a custom way, you can set a trigger field to your function.

Be aware that a controller needs to be restarted in order for a new trigger to work.

const customTrigger: ControllerTriggerType = (e: KeyboardEvent) {
    console.log("this is my custom trigger")
}

controller.keydownTrigger = customTrigger

controller.restart()

Auto Restart

autoRestart: boolean = false

If is set to true, autoRestart makes the controller restart automatically whenever a trigger is changed. By default is set to false.

🧠 Methods

Link and Unlink

controller.link()

controller.unlink()

Link and unlink are responsible for connecting and disconencting controllers from a webpage.

In order to work, a controller must be linked.

Restart

controller.restart()

Restarts a controller and connects new triggers to event listeners.

Has to be called when you change a trigger.

Reset

controller.reset()

Returns a controller to its default state.

🌓 Types

ControllerEventType

type ControllerEventType = {
    key: string;
    frequency: number;
    note: string;
    velocity: number;
};

ControllerOutputType

type ControllerOutputType = (keyProps: ControllerEventType) => void

ControllerTriggerType

type ControllerTriggerType = (e: KeyboardEvent) => void

ControllerKeyPropsObjectType

type ControllerKeyPropsObjectType = {
    [key: string]: { keyboard: number, note: string, frequency: number, state: "pressed" | "released" }
}