-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#782 Support Adaptive Multi-Rate (AMR) audio codec
- Loading branch information
Showing
10 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
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
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,12 @@ | ||
import type { IParserLoader, ITokenParser } from '../ParserFactory.js'; | ||
import type { INativeMetadataCollector } from '../common/MetadataCollector.js'; | ||
import type { ITokenizer } from 'strtok3'; | ||
import type { IOptions } from '../type.js'; | ||
|
||
export const amrParserLoader: IParserLoader = { | ||
parserType: 'amr', | ||
extensions: ['.amr'], | ||
async load(metadata: INativeMetadataCollector, tokenizer: ITokenizer, options: IOptions): Promise<ITokenParser> { | ||
return new (await import('./AmrParser.js')).AmrParser(metadata, tokenizer, options); | ||
} | ||
}; |
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,57 @@ | ||
import { BasicParser } from '../common/BasicParser.js'; | ||
import { AnsiStringType } from 'token-types'; | ||
import initDebug from 'debug'; | ||
import { FrameHeader } from './AmrToken.js'; | ||
|
||
const debug = initDebug('music-metadata:parser:AMR'); | ||
|
||
/** | ||
* There are 8 varying levels of compression. First byte of the frame specifies CMR | ||
* (codec mode request), values 0-7 are valid for AMR. Each mode have different frame size. | ||
* This table reflects that fact. | ||
*/ | ||
const m_block_size = [12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0]; | ||
|
||
/** | ||
* Adaptive Multi-Rate audio codec | ||
*/ | ||
export class AmrParser extends BasicParser { | ||
|
||
public async parse(): Promise<void> { | ||
const magicNr = await this.tokenizer.readToken(new AnsiStringType(5)); | ||
if (magicNr !== '#!AMR') { | ||
throw new Error('Invalid AMR file: invalid MAGIC number'); | ||
} | ||
this.metadata.setFormat('container', 'AMR'); | ||
this.metadata.setFormat('codec', 'AMR'); | ||
this.metadata.setFormat('sampleRate', 8000); | ||
this.metadata.setFormat('bitrate', 64000); | ||
this.metadata.setFormat('numberOfChannels', 1); | ||
|
||
let total_size = 0; | ||
let frames = 0; | ||
|
||
const assumedFileLength = this.tokenizer.fileInfo?.size ?? Number.MAX_SAFE_INTEGER; | ||
|
||
if (this.options.duration) { | ||
while (this.tokenizer.position < assumedFileLength) { | ||
|
||
const header = await this.tokenizer.readToken(FrameHeader); | ||
|
||
/* first byte is rate mode. each rate mode has frame of given length. look it up. */ | ||
const size = m_block_size[header.frameType]; | ||
|
||
if(size>0) { | ||
total_size += size + 1; | ||
if (total_size > assumedFileLength) break; | ||
await this.tokenizer.ignore(size); | ||
++frames; | ||
} else { | ||
debug(`Found no-data frame, frame-type: ${header.frameType}. Skipping`); | ||
} | ||
|
||
} | ||
this.metadata.setFormat('duration', frames * 0.02); | ||
} | ||
} | ||
} |
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,21 @@ | ||
import type { IGetToken } from 'strtok3'; | ||
import { getBitAllignedNumber } from '../common/Util.js'; | ||
|
||
interface IFrameHeader { | ||
frameType: number; | ||
} | ||
|
||
/** | ||
* ID3v2 header | ||
* Ref: http://id3.org/id3v2.3.0#ID3v2_header | ||
* ToDo | ||
*/ | ||
export const FrameHeader: IGetToken<IFrameHeader > = { | ||
len: 1, | ||
|
||
get: (buf: Uint8Array, off: number): IFrameHeader => { | ||
return { | ||
frameType: getBitAllignedNumber(buf, off, 1, 4) | ||
}; | ||
} | ||
}; |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,42 @@ | ||
import { assert } from 'chai'; | ||
import path from 'node:path'; | ||
import { Parsers } from './metadata-parsers.js'; | ||
import { samplePath } from './util.js'; | ||
|
||
const amrPath = path.join(samplePath, 'amr'); | ||
|
||
describe('Adaptive Multi-Rate (AMR) audio file', () => { | ||
|
||
Parsers.forEach(parser => { | ||
|
||
describe('parser.description', () => { | ||
|
||
it('parse: sample.amr', async function () { | ||
const {format} = await parser.initParser(() => this.skip(), path.join(amrPath, 'sample.amr'), 'audio/amr', {duration: true}); | ||
assert.strictEqual(format.sampleRate, 8000, 'format.sampleRate'); | ||
assert.strictEqual(format.numberOfChannels, 1, 'format.numberOfChannels'); | ||
assert.strictEqual(format.bitrate, 64000, 'format.bitrate'); | ||
assert.approximately(format.duration, 35.36, 0.0005, 'format.duration'); | ||
}); | ||
|
||
it('parse: gs-16b-1c-8000hz.amr', async function () { | ||
const {format} = await parser.initParser(() => this.skip(), path.join(amrPath, 'gs-16b-1c-8000hz.amr'), 'audio/amr', {duration: true}); | ||
assert.strictEqual(format.sampleRate, 8000, 'format.sampleRate'); | ||
assert.strictEqual(format.numberOfChannels, 1, 'format.numberOfChannels'); | ||
assert.strictEqual(format.bitrate, 64000, 'format.bitrate'); | ||
assert.approximately(format.duration, 16.42, 0.0005, 'format.duration'); | ||
}); | ||
|
||
|
||
it('parse: ff-16b-1c-8000hz.amr', async function () { | ||
const {format} = await parser.initParser(() => this.skip(), path.join(amrPath, 'ff-16b-1c-8000hz.amr'), 'audio/amr', {duration: true}); | ||
assert.strictEqual(format.sampleRate, 8000, 'format.sampleRate'); | ||
assert.strictEqual(format.numberOfChannels, 1, 'format.numberOfChannels'); | ||
assert.strictEqual(format.bitrate, 64000, 'format.bitrate'); | ||
assert.approximately(format.duration, 187.56, 0.0005, 'format.duration'); | ||
}); | ||
|
||
}); | ||
}); | ||
|
||
}); |