Write a phrase and a chord
curl https://aisenseapi.com/services/v1/midi_encode/C4:4,E4:8,G4:8,R:4,C4+E4+G4:2 --output phrase.midThe answer is a MIDI file of 109 bytes: middle C as a quarter note, E and G as eighths, a quarter rest and the C major chord as a half note, at 120 beats a minute on the grand piano. Add the tempo and a General MIDI program as the next path segments, such as /midi_encode/C4:4,E4:8,G4:8,R:4,C4+E4+G4:2/90/24 for a nylon guitar at 90. With Accept: application/json the same call answers the fields, with the file in midi_base64:
curl -H "Accept: application/json" https://aisenseapi.com/services/v1/midi_encode/C4:4,E4:8,G4:8,R:4,C4+E4+G4:2
{"midi_base64":"TVRoZAAAAAYAAQACAeBNVHJrAAAAEwD/UQMHoSAA/1gE...","mime_type":"audio/midi","bytes":109,"tracks":1,"notes":6,"tempo":120,"program":0,"program_name":"Acoustic Grand Piano","beats":5,"duration_seconds":2.5}Try it
Write notes here and make a MIDI file of them, up to 4096 characters; the sample is the start of Beethoven's Ode to Joy over a bass line. Your browser posts the notes in a JSON field named data, with tempo and program beside them, and offers the file that comes back for saving.
How the notes are written
A token is R for a rest, or one or more notes joined by +, which sound together. A note is a letter A to G, then # or s for sharp or b for flat, then an octave from -1 to 9, so C4 is middle C, MIDI note 60, and F#3, Fs3 and Gb3 are the same key. Leave the octave out for octave 4, or give a MIDI note number from 0 to 127 instead.
After the notes, :n gives the length as 1/n of a whole note: :1 a whole note, :4 a quarter, :8 an eighth, :12 an eighth-note triplet, up to :64. A dot after it, :4., makes the note half as long again. A token without a length keeps the length of the token before it, a quarter note at the start. Tokens are separated by spaces or commas, and | starts a new track that plays at the same time: C4 E4 G4 | C3:2. is a melody over a held bass note.
In a URL the notes are one path segment. Commas, colons and + need no encoding; write a space as %20, | as %7C, and # as %23 or s for sharp. At most 4096 characters, 4000 notes and 16 tracks.
Exact timing with JSON tracks
For start times the notation cannot say, POST tracks: each track a list of notes with note (a name or a number), start and duration in quarter notes, and an optional velocity from 1 to 127 (96 by default). A track takes program, channel (9 is the drum channel, channel 10 to a musician) and name:
curl -X POST https://aisenseapi.com/services/v1/midi_encode \
-H "Content-Type: application/json" -H "Accept: application/json" \
-d '{"tracks": [{"program": 40, "notes": [{"note": "A4", "start": 0, "duration": 2}, {"note": "C5", "start": 2, "duration": 2}]}, {"channel": 9, "notes": [{"note": 36, "start": 0, "duration": 1}, {"note": 38, "start": 1, "duration": 1}]}], "tempo": 90}'
{"midi_base64":"TVRoZAAAAAYAAQADAeBNVHJrAAAAEwD/UQMKLCsA/1gE...","mime_type":"audio/midi","bytes":104,"tracks":2,"notes":4,"tempo":90,"program":0,"program_name":"Acoustic Grand Piano","beats":4,"duration_seconds":2.667}The notation goes in data the same way: {"data": "C4:4,E4:8,G4:8,R:4,C4+E4+G4:2", "tempo": 120, "program": 0}. Send one of data and tracks.
What the file holds
The file is format 1 at 480 ticks per quarter note. Its first track holds the tempo and a 4/4 time signature; each of your tracks follows on the channel it names, or else on the next one no track names, 0, 1, 2 and on, passing over the drum channel, with its name first when it has one, a program change at the start except on the drum channel, and note on and note off events. midi_decode reads it back, and audio_render plays the same notes as a WAV.
What the free MIDI encode API endpoint returns
| Field | Type | Description |
|---|---|---|
| midi_base64 | string | The MIDI file as base64. |
| mime_type | string | audio/midi. |
| bytes | integer | The size of the file. |
| tracks | integer | The note tracks, the tempo track not counted. |
| notes | integer | The notes in all tracks; a chord of three is three. |
| tempo | number | Beats a minute. |
| program, program_name | integer, string | The General MIDI program the tracks start with, unless a track names its own. |
| beats | number | Quarter notes from the start to where the last note ends, or to the end of the rests that close a line of notes. |
| duration_seconds | number | The same at the tempo. |
Without Accept: application/json the answer is the file itself, audio/midi, named aisense-notes.mid.
HTTP 400 when a note cannot be read
curl https://aisenseapi.com/services/v1/midi_encode/C4:4,H4:4
{"error":"Unknown note H4. A note is a letter A to G, then # or s for sharp or b for flat, then an octave -1 to 9, such as C4, F#3 or Bb2; or a number 0 to 127.","fix":"Send notes such as C4:4,E4:8,G4:8,R:4,C4+E4+G4:2 (R a rest, + a chord, :4 a quarter note, | a new track), or tracks of {\"note\", \"start\", \"duration\"} in quarter notes; tempo 20 to 300, program 0 to 127."}Also HTTP 400: a length outside :1 to :64, rests and nothing else, a tempo outside 20 to 300, a program outside 0 to 127, both data and tracks, more than 4096 characters, 4000 notes or 16 tracks, and a sixteenth program when the 15 channels for programs are taken; a track whose program is already on a channel shares it. An Accept header that allows neither audio/midi nor application/json is HTTP 406.
Where notes as text earn their keep
Agents that write music
A language model writes melodies well as text; one call turns them into a file a musician opens.
Ringtones and jingles
A short motif for an app or a game, written in one line and kept as MIDI.
Exercises and teaching
Scales, intervals and chords made from a template, one URL per exercise.
Sketches for a sequencer
A chord progression typed quickly, then opened in a DAW to be voiced and played.
Privacy and limits
The notes travel in the path or the body and are not stored; the file is made and answered in the same call.
The service-wide limit is 5000 requests per IP per day, with no key and no account behind it. Every endpoint is listed in the free public REST APIs reference.