Music Maker: Using NodeJS to Create Songs

In my graduate school career, I had the opportunity with our evolutionary complexity lab to study creating music using neural networks and interactive genetic algorithms. It’s fun to study these two topics together since I enjoy crafting code and music. If you enjoy this too, you might enjoy Music maker, a library I created to explore generating music with code. Sonic Pi by Sam Aaron, a popular tool to teach music theory and Ruby code, inspired me to build this tool. My team from InspiredToEducate.NET enjoyed teaching a coding workshop on music using Sonic Pi. We, however, encountered a challenge of installing Sonic-Pi on a lab of computers. The workshop targets 5th-grade to 8th-grade students who have good typing skills. It would be cool if something like Sonic-Pi supported features like Blockly coding too.

In terms of musical motivations, I wanted to provide features like the following:

  • Like Sonic-Pi, the tool should make it easy to generate chords and scales.
  • I want it to feel simple like Sonic-Pi. I, however, don’t think I’ve achieved this yet.
  • I wanted the tool to have a concept of players who can generate music over a chord progression. I believe it would be cool to grow an ecosystem of players for various time signatures and musical types.
    I wanted to support the MIDI file format for output making it possible to blend music from this tool in sequencers broadly available on the market. This also enables us to print out sheet music using the MIDI files.
  • Building drum patterns can be tedious at times, I wanted to create a way to express rhythm simply.
  • We desired to have a browser-based interface that a teacher could install on a Raspberry Pi or some other computer. This was a key idea from one of my teachers.  I’m focusing on building a tool that works on a local area network.  (not the broad internet)
  • From a coding perspective, we need to build a tool that could interface with Blockly coding someday. JavaScript became a logical choice. I’ve wanted to explore a project the used TypeScript, NodeJS and Express too. I especially enjoyed using TypeScript for enums, classes, abstract classes, etc.

Here’s a sample MIDI file for your enjoyment: Music maker test.

I do want to give a shout out to David Ingram of Google for putting together jsmidgen. David’s library handled all the low-level concerns for generating MIDI files, adding tracks, and notes. Please keep in mind that MIDI is a music protocol and file format that focuses on the idea of turning notes and off like switches over time. Make sure to check out his work. It’s great NodeJS library.

Here’s a quick tour of the API. It’s a work in progress.

Where do I get the code?

https://github.com/michaelprosario/music_maker

  • run app.js to load the browser application.   Once the application is running, you should find it running on http://localhost:3000 .
  • Make sure to check out the demo type scripts and music_maker.ts for sample code.

JSMIDGen reference

To learn more JSMIDGEN,
please visit https://github.com/dingram/jsmidgen

addNote
var track = new Midi.Track();
track.addNote(0, ‘c4’, 64);

addChord

var track = new Midi.Track();
var c = mm.MakeChord(“c5”, mm.ChordType.Major);
track.addChord(0, c, beat*4);

setTempo
track.setTempo(bpm[, time])

setInstrument
track.setInstrument(channel, instrument[, time])

Music Maker Reference

You can find many of these functions in the ‘MusicMaker’ module.

GetNoteNumber(aNote)

  • Converts a note string to a number. Returns an integer.
  • aNote: note to convert

MakeChord(root,type: ChordType)

  • Creates a chord based on note and chord type.
  • root: This can be a note like the string “c4” or an integer.
  • type: Type of chord. Please refer to the ‘ChordType’ enumeration.

MakeScale(note, type: ScaleType, octaves: number)

  • Creates a scale based on note and type.
  • root: This can be a note like the string “c4” or an integer.
  • type: Type of scale. Please refer to the ‘ScaleType’ enumeration.

SelectRandom(myArray)

  • Selects a random element from ‘myArray’ and returns it.
  • myArray: is an array of stuff.

AddRhythmPattern(track,strPattern: string, note: number)

  • Provides an easy way to express patterns of notes
  • track: represents a track objects from jsmidgen
  • strPattern: Provide a string to represent a pattern of sound. Each character represents a 16th note. x plays the note. – advances by a 16th note. All other characters are ignored.
  • note: Note to play.

ChordChange(chord,length:number)

  • Class represents a chord played over a number of beats.
  • chord: an array of notes or chord
  • length: number of beats to play chord.

Chord Players

  • You can use chord player classes to generate music patterns based on a sequence of chords or chord progression.
  • Some of the chord player classes include the following: Arpeggio1, BassPLayer1, BassPLayer2, BassPLayer3,
    OffBeatPlayer, RandomPlayer, and SimplePlayer
  • To use a chord player class, create an instance of the class and call the following: PlayFromChordChanges(track,chordList,channel).   You need to pass a track, a list of chord changes, and a MIDI channel.

If you write music using Music Maker, got ideas for features, or you’d like to contribute to the project, drop me a line in the comments or contact me through GitHub!  Hope you have a great one!

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*