-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add midi-file for MIDI manipulations
- Loading branch information
1 parent
d73700b
commit a9f57f7
Showing
5 changed files
with
71 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters