Skip to content

Commit

Permalink
Fix WAV parser, sync issue with Broadcast Wave Format (BWF) Audio Ext…
Browse files Browse the repository at this point in the history
…ension chunk

Strips null characters from BWF extension strings.

Resolves: #1163
  • Loading branch information
Borewit committed Jul 4, 2022
1 parent 55344b5 commit b486eb6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/wav/BwfChunk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IGetToken } from 'strtok3/lib/core';
import * as Token from 'token-types';
import { stripNulls } from '../common/Util';

export interface IBroadcastAudioExtensionChunk {
description: string;
Expand All @@ -22,11 +23,11 @@ export const BroadcastAudioExtensionChunk: IGetToken<IBroadcastAudioExtensionChu

get: (uint8array, off) => {
return {
description: new Token.StringType(256, 'ascii').get(uint8array, off).trim(),
originator: new Token.StringType(32, 'ascii').get(uint8array, off + 256).trim(),
originatorReference: new Token.StringType(32, 'ascii').get(uint8array, off + 288).trim(),
originationDate: new Token.StringType(10, 'ascii').get(uint8array, off + 320).trim(),
originationTime: new Token.StringType(8, 'ascii').get(uint8array, off + 330).trim(),
description: stripNulls(new Token.StringType(256, 'ascii').get(uint8array, off)).trim(),
originator: stripNulls(new Token.StringType(32, 'ascii').get(uint8array, off + 256)).trim(),
originatorReference: stripNulls(new Token.StringType(32, 'ascii').get(uint8array, off + 288)).trim(),
originationDate: stripNulls(new Token.StringType(10, 'ascii').get(uint8array, off + 320)).trim(),
originationTime: stripNulls(new Token.StringType(8, 'ascii').get(uint8array, off + 330)).trim(),
timeReferenceLow: Token.UINT32_LE.get(uint8array, off + 338),
timeReferenceHigh: Token.UINT32_LE.get(uint8array, off + 342),
version: Token.UINT16_LE.get(uint8array, off + 346),
Expand Down
2 changes: 2 additions & 0 deletions lib/wav/WaveParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export class WaveParser extends BasicParser {
Object.keys(bext).forEach(key => {
this.metadata.addTag('exif', 'bext.' + key, bext[key]);
});
const bextRemaining = header.chunkSize - BroadcastAudioExtensionChunk.len;
await this.tokenizer.ignore(bextRemaining);
break;

case '\x00\x00\x00\x00': // padding ??
Expand Down
Binary file added test/samples/wav/issue-1163.bwf
Binary file not shown.
17 changes: 17 additions & 0 deletions test/test-file-wav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,22 @@ describe('Parse RIFF/WAVE audio format', () => {
assert.approximately(format.duration, 2478 / 16000, 5 / 1000, 'format.duration');
});

// https://github.com/Borewit/music-metadata/issues/1163
it('Support chunk size larger then BWF extension', async () => {
// const filePath = path.join(wavSamples, 'unreadable-tags.wav');
const filePath = path.join(wavSamples, 'issue-1163.bwf');
const {format, common, native} = await mm.parseFile(filePath);

assert.strictEqual(format.container, 'WAVE', 'format.container');
assert.strictEqual(format.codec, 'PCM', 'format.codec');

assert.strictEqual(common.artist, 'Some Composer', 'common.artists');
assert.strictEqual(common.title, 'Title Redacted', 'common.title');
assert.deepStrictEqual(common.track, {no: 1, of: 12}, 'common.track');

const exif = mm.orderTags(native.exif);
assert.deepStrictEqual(exif['bext.originator'], ['Pro Tools'], 'BWF: exif.bext.originator');
});

});

0 comments on commit b486eb6

Please sign in to comment.