import { Readable } from 'stream' ; import * as strtok3 from 'strtok3/lib/core' ; import { ParserFactory } from './ParserFactory' ; import { IAudioMetadata , INativeTagDict , IOptions , IPicture , IPrivateOptions , IRandomReader , ITag } from './type' ; import { RandomUint8ArrayReader } from './common/RandomUint8ArrayReader' ; import { APEv2Parser } from './apev2/APEv2Parser' ; import { hasID3v1Header } from './id3v1/ID3v1Parser' ; import { getLyricsHeaderLength } from './lyrics3/Lyrics3' ; export { IFileInfo } from 'strtok3/lib/core' ; param stream param options param fileInfo returns export function parseStream ( stream , fileInfo ? , options = { } ) { return parseFromTokenizer ( strtok3 . fromStream ( stream , typeof fileInfo === 'string' ? { mimeType : fileInfo } : fileInfo ) , options ) ; } param uint8Array param fileInfo param options returns export async function parseBuffer ( uint8Array , fileInfo ? , options = { } ) { const bufferReader = new RandomUint8ArrayReader ( uint8Array ) ; await scanAppendingHeaders ( bufferReader , options ) ; const tokenizer = strtok3 . fromBuffer ( uint8Array , typeof fileInfo === 'string' ? { mimeType : fileInfo } : fileInfo ) ; return parseFromTokenizer ( tokenizer , options ) ; } param tokenizer param options returns export function parseFromTokenizer ( tokenizer , options ? ) { return ParserFactory . parseOnContentType ( tokenizer , options ) ; } param nativeTags returns export function orderTags ( nativeTags ) { const tags = { } ; for ( const tag of nativeTags ) { ( tags [ tag . id ] = ( tags [ tag . id ] || [ ] ) ) . push ( tag . value ) ; } return tags ; } param rating returns export function ratingToStars ( rating ) { return rating === undefined ? 0 : 1 + Math . round ( rating * 4 ) ; } param pictures return export function selectCover ( pictures ? ) { return pictures ? pictures . reduce ( ( acc , cur ) => { if ( cur . name && cur . name . toLowerCase ( ) in [ 'front' , 'cover' , 'cover (front)' ] ) return cur ; return acc ; } ) : null ; } export async function scanAppendingHeaders ( randomReader , options = { } ) { let apeOffset = randomReader . fileSize ; if ( await hasID3v1Header ( randomReader ) ) { apeOffset -= 128 ; const lyricsLen = await getLyricsHeaderLength ( randomReader ) ; apeOffset -= lyricsLen ; } options . apeHeader = await APEv2Parser . findApeFooterOffset ( randomReader , apeOffset ) ; }