• Stars
    star
    228
  • Rank 175,267 (Top 4 %)
  • Language
    JavaScript
  • Created over 12 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A pure-JavaScript MIDI generator

jsmidgen, a pure-JavaScript MIDI file library

Introduction

jsmidgen (pronounced jay-smidgen or jer-smidgen) is a library that can be used to generate MIDI files in JavaScript. It currently provides output as a string, but there are plans to provide multiple output formats, including base64 and data URI.

Example Usage

The MIDI file structure is made up of one or more tracks, which contain one or more events. These events can be note on/off events, instrument changes, tempo changes, or more exotic things. A basic example is shown below:

var fs = require('fs');
var Midi = require('jsmidgen');

var file = new Midi.File();
var track = new Midi.Track();
file.addTrack(track);

track.addNote(0, 'c4', 64);
track.addNote(0, 'd4', 64);
track.addNote(0, 'e4', 64);
track.addNote(0, 'f4', 64);
track.addNote(0, 'g4', 64);
track.addNote(0, 'a4', 64);
track.addNote(0, 'b4', 64);
track.addNote(0, 'c5', 64);

fs.writeFileSync('test.mid', file.toBytes(), 'binary');

This example will create a MIDI file that will play an ascending C major scale, starting at middle C.

Fluid API

This library also has rudimentary support for a fluid (chained) style:

file = new Midi.File();
file
  .addTrack()

    .note(0, 'c4', 32)
    .note(0, 'd4', 32)
    .note(0, 'e4', 32)
    .note(0, 'f4', 32)
    .note(0, 'g4', 32)
    .note(0, 'a4', 32)
    .note(0, 'b4', 32)
    .note(0, 'c5', 32)

    // church organ
    .instrument(0, 0x13)

    // by skipping the third arguments, we create a chord (C major)
    .noteOn(0, 'c4', 64)
    .noteOn(0, 'e4')
    .noteOn(0, 'g4')

    // by skipping the third arguments again, we stop all notes at once
    .noteOff(0, 'c4', 47)
    .noteOff(0, 'e4')
    .noteOff(0, 'g4')

    //alternatively, a chord may be created with the addChord function
    .addChord(0, ['c4', 'e4', 'g4'], 64)

    .noteOn(0, 'c4', 1)
    .noteOn(0, 'e4')
    .noteOn(0, 'g4')
    .noteOff(0, 'c4', 384)
    .noteOff(0, 'e4')
    .noteOff(0, 'g4')
    ;

fs.writeFileSync('test2.mid', file.toBytes(), 'binary');

Note the use of setInstrument() to change to a church organ midway through, and the use of addNoteOn()/addNoteOff() to produce chords.

Reference

Midi.File

  • addTrack() - Add a new Track object to the file and return the new track
  • addTrack(track) - Add the given Track object to the file and return the file

Midi.Track

Time and duration are specified in "ticks", and there is a hardcoded value of 128 ticks per beat. This means that a quarter note has a duration of 128.

Pitch can be specified by note name with octave (a#4) or by note number (60). Middle C is represented as c4 or 60.

  • addNote(channel, pitch, duration[, time[, velocity]])

    Add a new note with the given channel, pitch, and duration

    • If time is given, delay that many ticks before starting the note
    • If velocity is given, strike the note with that velocity
  • addNoteOn(channel, pitch[, time[, velocity]])

    Start a new note with the given channel and pitch

    • If time is given, delay that many ticks before starting the note
    • If velocity is given, strike the note with that velocity
  • addNoteOff(channel, pitch[, time[, velocity]])

    End a note with the given channel and pitch

    • If time is given, delay that many ticks before ending the note
    • If velocity is given, strike the note with that velocity
  • addChord(channel, chord[, velocity])

    Add a chord with the given channel and pitches

    • Accepts an array of pitches to play as a chord
    • If velocity is given, strike the chord with that velocity
  • setInstrument(channel, instrument[, time])

    Change the given channel to the given instrument

    • If time is given, delay that many ticks before making the change
  • setTempo(bpm[, time])

    Set the tempo to bpm beats per minute

    • If time is given, delay that many ticks before making the change

More Repositories

1

putio-cli

Python command-line client for put.io
20
star
2

git-scripts

A set of useful git aliases and scripts
Shell
18
star
3

Nanode

Arduino examples for the Nanode
Java
14
star
4

glypher

Glyph training app for Ingress
8
star
5

barcode4android

A fork of barcode4j for Android
Java
6
star
6

mustache-php

A new PHP implementation of the Mustache templating system
PHP
5
star
7

mod_sqltemplate

A flexible Apache module that allows configuration to be stored in a database
4
star
8

sqldiff

A utility for transforming one database into another
Perl
3
star
9

pycloc

Simple commandline Python OAuth Client
Python
3
star
10

pl101

PL101 solutions from Nathan's University
PHP
3
star
11

emspectrum

The app for the Electromagnetic Field/Electromagnetic Wave events
3
star
12

destripper

PHP
3
star
13

TeamView

Work project to show highlighted subsets of teams in a tree structure
JavaScript
2
star
14

hypno101

SLEEP.
2
star
15

php-tmpl

PHP TextTemplate extension
C
2
star
16

dingram.github.com

Public site
2
star
17

binfiles

Scripts and other binaries that I find useful
2
star
18

excubitor-client

An Android client for the Ingress Excubitor service
2
star
19

libinsight

A library that provides semantic organisation for files.
2
star
20

insight

A FUSE-based semantic file system
C
2
star
21

mod_fakephp

This is a very simple Apache2 module that installs null handlers for the various PHP configuration directives
C
2
star
22

gentoo-overlay

My Gentoo overlay
2
star
23

advent-of-code-2022

Advent of Code 2022
Python
1
star
24

excubitor-server

The server-side component of the Ingress Excubitor service
1
star
25

dotfiles-pub

Public dotfiles
Vim Script
1
star
26

lonian-android

Android client for Lonian
Java
1
star
27

led-display

libusb-coded driver for Dream Link 21x7 LED display
C
1
star
28

tatamidoku

Generator and solver for tatamidoku puzzles
Java
1
star
29

ingress-mission-requests

Ingress mission requests
Python
1
star
30

hackspace-new-member-manual

1
star
31

syndiff

Syntax-aware diffing tool
1
star
32

uk-train-display

Python
1
star