Skip to content

Commit

Permalink
Add midi-file for MIDI manipulations
Browse files Browse the repository at this point in the history
  • Loading branch information
infojunkie committed Oct 13, 2024
1 parent d73700b commit a9f57f7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
34 changes: 24 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "musicxml-midi",
"version": "2.8.5",
"version": "2.9.0",
"description": "MusicXML to MIDI converter",
"type": "module",
"directories": {
Expand All @@ -9,6 +9,7 @@
"bin": {
"musicxml-midi": "src/js/server.js",
"midi-timemap": "src/js/midi-timemap.js",
"midi-file": "src/js/midi-file.js",
"musicxml-examples": "src/js/musicxml-examples.js",
"musicxml-grooves": "src/js/musicxml-grooves.js"
},
Expand All @@ -30,7 +31,7 @@
"develop": "nodemon -e js,json src/js/server.js",
"start": "node src/js/server.js",
"test:xsl": "test/libs/bats/bin/bats test/*.bats",
"test:js": "cross-env PORT=1331 NODE_OPTIONS=--experimental-vm-modules NODE_ENV=test jest",
"test:js": "cross-env PORT=1331 NODE_OPTIONS=--experimental-vm-modules NODE_ENV=test jest -t ${TEST:-''}",
"test": "npm run test:xsl && npm run test:js",
"server": "PORT=8085 npm run start",
"postinstall": "./postinstall.sh"
Expand All @@ -51,6 +52,7 @@
"cors": "^2.8.5",
"express": "^4.21.1",
"express-fileupload": "^1.5.1",
"get-stdin": "^9.0.0",
"morgan": "^1.10.0",
"node-fetch": "^3.3.2",
"node-jq": "^6.0.1",
Expand Down
24 changes: 24 additions & 0 deletions src/js/midi-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

/**
* Parse a MIDI file into JSON and back.
*
* Usage: midi-file.js < source.mid | jq [..] | midi-file.js > target.mid
*/

import { parseMidi, writeMidi } from 'midi-file'
import fs from 'fs'
import process from 'process'
import { buffer } from 'stream/consumers'

const input = await buffer(process.stdin)
try {
const midi = JSON.parse(input)
const buffer = Buffer.from(writeMidi(midi))
fs.writeFileSync(1, buffer)
}
catch {
const midi = parseMidi(input)
const buffer = Buffer.from(JSON.stringify(midi))
process.stdout.write(buffer)
}
18 changes: 18 additions & 0 deletions test/midi-file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import util from 'util'
import { createRequire } from 'module'
import fs from 'fs'
const require = createRequire(import.meta.url)
const exec = util.promisify(require('child_process').exec)

describe('midi-file', () => {
test('should convert incoming MIDI files to JSON', async () => {
const execResult = await exec('node src/js/midi-file.js < test/data/midi-timemap.test.mid')
const json = JSON.parse(execResult.stdout)
expect(json.header.numTracks).toEqual(3)
})
test('should convert incoming JSON to MIDI files', async () => {
const execResult = await exec('cat test/data/midi-timemap.test.mid | node src/js/midi-file.js | node src/js/midi-file.js', { encoding: 'buffer' })
const original = fs.readFileSync('test/data/midi-timemap.test.mid')
expect(execResult.stdout).toEqual(original)
})
})
2 changes: 1 addition & 1 deletion test/midi-timemap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const exec = util.promisify(require('child_process').exec)

describe('MIDI to timemap conversion cli', () => {
describe('midi-timemap', () => {
test('should run successfully', async () => {
const execResult = await exec('node src/js/midi-timemap.js test/data/midi-timemap.test.mid')
const timemap = JSON.parse(execResult.stdout);
Expand Down

0 comments on commit a9f57f7

Please sign in to comment.