|
| 1 | +import { describe, it, before } from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import { loadScore, setSoundFont, hasMagic, parseWav, rms } from './helpers.js' |
| 4 | + |
| 5 | +// a short score: rendering is about 2.5x real time |
| 6 | +const SCORE = 'repeat01.mscx' |
| 7 | + |
| 8 | +const MAGIC = { |
| 9 | + wav: (d) => hasMagic(d, 'RIFF') && hasMagic(d, 'WAVE', 8), |
| 10 | + ogg: (d) => hasMagic(d, 'OggS'), |
| 11 | + flac: (d) => hasMagic(d, 'fLaC'), |
| 12 | + mp3: (d) => hasMagic(d, 'ID3') || (d[0] === 0xff && (d[1] & 0xe0) === 0xe0), |
| 13 | +} |
| 14 | + |
| 15 | +describe('saveAudio', () => { |
| 16 | + let score, duration |
| 17 | + before(async () => { |
| 18 | + score = await loadScore(SCORE) |
| 19 | + duration = (await score.metadata()).duration |
| 20 | + }) |
| 21 | + |
| 22 | + it('throws without a soundfont', async () => { |
| 23 | + await assert.rejects(score.saveAudio('ogg'), /soundfont/) |
| 24 | + }) |
| 25 | + |
| 26 | + it('wav: the whole score, not silent', async () => { |
| 27 | + await setSoundFont() |
| 28 | + const wav = parseWav(await score.saveAudio('wav')) |
| 29 | + assert.equal(wav.channels, 2) |
| 30 | + assert.equal(wav.sampleRate, 44100) |
| 31 | + assert.ok(wav.duration >= duration && wav.duration < duration + 10, `${wav.duration}s vs ${duration}s`) |
| 32 | + assert.ok(rms(wav.samples) > 0.01, 'not silent') |
| 33 | + }) |
| 34 | + |
| 35 | + // each format is a new export of the same score (used to work only once per instance) |
| 36 | + for (const format of ['ogg', 'flac', 'mp3']) { |
| 37 | + it(`${format}: valid file of a plausible size`, async () => { |
| 38 | + const data = await score.saveAudio(format) |
| 39 | + assert.ok(MAGIC[format](data), 'magic bytes') |
| 40 | + // > 8 kbit/s: a silent or truncated file is a few KB at most |
| 41 | + assert.ok(data.length > duration * 1000, `${data.length} bytes`) |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + it('rejects an unknown format', async () => { |
| 46 | + await assert.rejects(score.saveAudio('aiff')) |
| 47 | + }) |
| 48 | + |
| 49 | + it('exports the same audio twice', async () => { |
| 50 | + const first = parseWav(await score.saveAudio('wav')) |
| 51 | + const second = parseWav(await score.saveAudio('wav')) |
| 52 | + assert.equal(second.samples.length, first.samples.length) |
| 53 | + assert.ok(Math.abs(rms(second.samples) - rms(first.samples)) < rms(first.samples) * 0.05) |
| 54 | + }) |
| 55 | + |
| 56 | + it('exports after synthAudio', async () => { |
| 57 | + const fn = await score.synthAudio(0) |
| 58 | + await fn() |
| 59 | + await fn(true) |
| 60 | + const wav = parseWav(await score.saveAudio('wav')) |
| 61 | + assert.ok(wav.duration >= duration, `${wav.duration}s vs ${duration}s`) |
| 62 | + }) |
| 63 | + |
| 64 | + it('exports its own score, not the last loaded one', async () => { |
| 65 | + const other = await loadScore('Aequale_No_1.mscz') |
| 66 | + const wav = parseWav(await score.saveAudio('wav')) |
| 67 | + assert.ok(wav.duration >= duration && wav.duration < duration + 10, `${wav.duration}s vs ${duration}s`) |
| 68 | + const { duration: otherDuration } = await other.metadata() |
| 69 | + assert.ok(otherDuration > duration + 10) |
| 70 | + }) |
| 71 | +}) |
0 commit comments