2021-11-25 03:15:50 +01:00
|
|
|
const { isNullOrNaN } = require('../utils/index')
|
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
const Logger = require('../Logger')
|
2021-09-30 03:43:36 +02:00
|
|
|
const AudioFileMetadata = require('./AudioFileMetadata')
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
class AudioFile {
|
|
|
|
constructor(data) {
|
|
|
|
this.index = null
|
|
|
|
this.ino = null
|
|
|
|
this.filename = null
|
|
|
|
this.ext = null
|
|
|
|
this.path = null
|
|
|
|
this.fullPath = null
|
|
|
|
this.addedAt = null
|
|
|
|
|
2021-08-26 14:09:23 +02:00
|
|
|
this.trackNumFromMeta = null
|
|
|
|
this.trackNumFromFilename = null
|
2021-10-24 03:31:48 +02:00
|
|
|
this.cdNumFromFilename = null
|
2021-08-26 14:09:23 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
this.format = null
|
|
|
|
this.duration = null
|
|
|
|
this.size = null
|
|
|
|
this.bitRate = null
|
|
|
|
this.language = null
|
|
|
|
this.codec = null
|
|
|
|
this.timeBase = null
|
|
|
|
this.channels = null
|
|
|
|
this.channelLayout = null
|
2021-09-08 16:15:54 +02:00
|
|
|
this.chapters = []
|
2021-09-30 03:43:36 +02:00
|
|
|
this.embeddedCoverArt = null
|
2021-08-26 00:36:54 +02:00
|
|
|
|
2021-09-30 03:43:36 +02:00
|
|
|
// Tags scraped from the audio file
|
|
|
|
this.metadata = null
|
2021-08-26 00:36:54 +02:00
|
|
|
|
|
|
|
this.manuallyVerified = false
|
|
|
|
this.invalid = false
|
2021-09-05 01:02:42 +02:00
|
|
|
this.exclude = false
|
2021-08-26 00:36:54 +02:00
|
|
|
this.error = null
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
this.construct(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 03:31:48 +02:00
|
|
|
// Sort number takes cd num into account
|
|
|
|
// get sortNumber() {
|
|
|
|
// if (this.manuallyVerified) return this.index
|
|
|
|
// var num = this.index
|
|
|
|
// if (this.cdNumFromFilename && !isNaN(this.cdNumFromFilename)) {
|
|
|
|
// num += (Number(this.cdNumFromFilename) * 1000)
|
|
|
|
// }
|
|
|
|
// return num
|
|
|
|
// }
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
index: this.index,
|
|
|
|
ino: this.ino,
|
|
|
|
filename: this.filename,
|
|
|
|
ext: this.ext,
|
|
|
|
path: this.path,
|
|
|
|
fullPath: this.fullPath,
|
|
|
|
addedAt: this.addedAt,
|
2021-08-26 14:09:23 +02:00
|
|
|
trackNumFromMeta: this.trackNumFromMeta,
|
|
|
|
trackNumFromFilename: this.trackNumFromFilename,
|
2021-10-24 03:31:48 +02:00
|
|
|
cdNumFromFilename: this.cdNumFromFilename,
|
2021-08-26 00:36:54 +02:00
|
|
|
manuallyVerified: !!this.manuallyVerified,
|
|
|
|
invalid: !!this.invalid,
|
2021-09-05 01:02:42 +02:00
|
|
|
exclude: !!this.exclude,
|
2021-08-26 00:36:54 +02:00
|
|
|
error: this.error || null,
|
|
|
|
format: this.format,
|
|
|
|
duration: this.duration,
|
|
|
|
size: this.size,
|
|
|
|
bitRate: this.bitRate,
|
|
|
|
language: this.language,
|
2021-10-01 01:52:32 +02:00
|
|
|
codec: this.codec,
|
2021-08-26 00:36:54 +02:00
|
|
|
timeBase: this.timeBase,
|
|
|
|
channels: this.channels,
|
|
|
|
channelLayout: this.channelLayout,
|
2021-09-08 16:15:54 +02:00
|
|
|
chapters: this.chapters,
|
2021-09-30 03:43:36 +02:00
|
|
|
embeddedCoverArt: this.embeddedCoverArt,
|
|
|
|
metadata: this.metadata ? this.metadata.toJSON() : {}
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(data) {
|
|
|
|
this.index = data.index
|
|
|
|
this.ino = data.ino
|
|
|
|
this.filename = data.filename
|
|
|
|
this.ext = data.ext
|
|
|
|
this.path = data.path
|
|
|
|
this.fullPath = data.fullPath
|
|
|
|
this.addedAt = data.addedAt
|
|
|
|
this.manuallyVerified = !!data.manuallyVerified
|
|
|
|
this.invalid = !!data.invalid
|
2021-09-05 01:02:42 +02:00
|
|
|
this.exclude = !!data.exclude
|
2021-08-26 00:36:54 +02:00
|
|
|
this.error = data.error || null
|
|
|
|
|
2021-12-05 18:29:42 +01:00
|
|
|
this.trackNumFromMeta = data.trackNumFromMeta
|
|
|
|
this.trackNumFromFilename = data.trackNumFromFilename
|
|
|
|
this.cdNumFromFilename = data.cdNumFromFilename
|
2021-08-26 14:09:23 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
this.format = data.format
|
|
|
|
this.duration = data.duration
|
|
|
|
this.size = data.size
|
|
|
|
this.bitRate = data.bitRate
|
|
|
|
this.language = data.language
|
2021-10-01 01:52:32 +02:00
|
|
|
this.codec = data.codec || null
|
2021-08-26 00:36:54 +02:00
|
|
|
this.timeBase = data.timeBase
|
|
|
|
this.channels = data.channels
|
|
|
|
this.channelLayout = data.channelLayout
|
2021-09-08 16:15:54 +02:00
|
|
|
this.chapters = data.chapters
|
2021-09-30 03:43:36 +02:00
|
|
|
this.embeddedCoverArt = data.embeddedCoverArt || null
|
|
|
|
|
|
|
|
// Old version of AudioFile used `tagAlbum` etc.
|
|
|
|
var isOldVersion = Object.keys(data).find(key => key.startsWith('tag'))
|
|
|
|
if (isOldVersion) {
|
|
|
|
this.metadata = new AudioFileMetadata(data)
|
|
|
|
} else {
|
|
|
|
this.metadata = new AudioFileMetadata(data.metadata || {})
|
|
|
|
}
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setData(data) {
|
|
|
|
this.index = data.index || null
|
2021-08-26 14:09:23 +02:00
|
|
|
this.ino = data.ino || null
|
2021-08-26 00:36:54 +02:00
|
|
|
this.filename = data.filename
|
|
|
|
this.ext = data.ext
|
|
|
|
this.path = data.path
|
|
|
|
this.fullPath = data.fullPath
|
|
|
|
this.addedAt = Date.now()
|
|
|
|
|
2021-12-05 18:29:42 +01:00
|
|
|
this.trackNumFromMeta = data.trackNumFromMeta
|
|
|
|
this.trackNumFromFilename = data.trackNumFromFilename
|
|
|
|
this.cdNumFromFilename = data.cdNumFromFilename
|
2021-08-26 14:09:23 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
this.manuallyVerified = !!data.manuallyVerified
|
|
|
|
this.invalid = !!data.invalid
|
2021-09-05 01:02:42 +02:00
|
|
|
this.exclude = !!data.exclude
|
2021-08-26 00:36:54 +02:00
|
|
|
this.error = data.error || null
|
|
|
|
|
|
|
|
this.format = data.format
|
|
|
|
this.duration = data.duration
|
|
|
|
this.size = data.size
|
2021-09-08 16:15:54 +02:00
|
|
|
this.bitRate = data.bit_rate || null
|
2021-08-26 00:36:54 +02:00
|
|
|
this.language = data.language
|
2021-10-01 01:52:32 +02:00
|
|
|
this.codec = data.codec || null
|
2021-08-26 00:36:54 +02:00
|
|
|
this.timeBase = data.time_base
|
|
|
|
this.channels = data.channels
|
|
|
|
this.channelLayout = data.channel_layout
|
2021-09-08 16:15:54 +02:00
|
|
|
this.chapters = data.chapters || []
|
2021-09-30 03:43:36 +02:00
|
|
|
this.embeddedCoverArt = data.embedded_cover_art || null
|
2021-08-26 00:36:54 +02:00
|
|
|
|
2021-09-30 03:43:36 +02:00
|
|
|
this.metadata = new AudioFileMetadata()
|
|
|
|
this.metadata.setData(data)
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 02:58:20 +01:00
|
|
|
// New scanner creates AudioFile from AudioFileScanner
|
2021-11-25 03:15:50 +01:00
|
|
|
setDataFromProbe(fileData, probeData) {
|
2021-11-23 02:58:20 +01:00
|
|
|
this.index = fileData.index || null
|
|
|
|
this.ino = fileData.ino || null
|
|
|
|
this.filename = fileData.filename
|
|
|
|
this.ext = fileData.ext
|
|
|
|
this.path = fileData.path
|
|
|
|
this.fullPath = fileData.fullPath
|
|
|
|
this.addedAt = Date.now()
|
2021-11-25 03:15:50 +01:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
this.trackNumFromMeta = fileData.trackNumFromMeta
|
|
|
|
this.trackNumFromFilename = fileData.trackNumFromFilename
|
|
|
|
this.cdNumFromFilename = fileData.cdNumFromFilename
|
2021-11-25 03:15:50 +01:00
|
|
|
|
|
|
|
this.format = probeData.format
|
|
|
|
this.duration = probeData.duration
|
|
|
|
this.size = probeData.size
|
|
|
|
this.bitRate = probeData.bitRate || null
|
|
|
|
this.language = probeData.language
|
|
|
|
this.codec = probeData.codec || null
|
|
|
|
this.timeBase = probeData.timeBase
|
|
|
|
this.channels = probeData.channels
|
|
|
|
this.channelLayout = probeData.channelLayout
|
|
|
|
this.chapters = probeData.chapters || []
|
|
|
|
this.metadata = probeData.audioFileMetadata
|
2021-11-26 01:39:02 +01:00
|
|
|
this.embeddedCoverArt = probeData.embeddedCoverArt
|
2021-11-25 03:15:50 +01:00
|
|
|
}
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
validateTrackIndex() {
|
2021-11-25 03:15:50 +01:00
|
|
|
var numFromMeta = isNullOrNaN(this.trackNumFromMeta) ? null : Number(this.trackNumFromMeta)
|
|
|
|
var numFromFilename = isNullOrNaN(this.trackNumFromFilename) ? null : Number(this.trackNumFromFilename)
|
|
|
|
|
|
|
|
if (numFromMeta !== null) return numFromMeta
|
|
|
|
if (numFromFilename !== null) return numFromFilename
|
|
|
|
|
|
|
|
this.invalid = true
|
|
|
|
this.error = 'Failed to get track number'
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
setDuplicateTrackNumber(num) {
|
|
|
|
this.invalid = true
|
|
|
|
this.error = 'Duplicate track number "' + num + '"'
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
syncChapters(updatedChapters) {
|
|
|
|
if (this.chapters.length !== updatedChapters.length) {
|
|
|
|
this.chapters = updatedChapters.map(ch => ({ ...ch }))
|
|
|
|
return true
|
|
|
|
} else if (updatedChapters.length === 0) {
|
|
|
|
if (this.chapters.length > 0) {
|
|
|
|
this.chapters = []
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasUpdates = false
|
|
|
|
for (let i = 0; i < updatedChapters.length; i++) {
|
|
|
|
if (JSON.stringify(updatedChapters[i]) !== JSON.stringify(this.chapters[i])) {
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasUpdates) {
|
|
|
|
this.chapters = updatedChapters.map(ch => ({ ...ch }))
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from audioFileScanner.js with scanData
|
|
|
|
updateMetadata(data) {
|
|
|
|
if (!this.metadata) this.metadata = new AudioFileMetadata()
|
|
|
|
|
|
|
|
var dataMap = {
|
|
|
|
format: data.format,
|
|
|
|
duration: data.duration,
|
|
|
|
size: data.size,
|
|
|
|
bitRate: data.bit_rate || null,
|
|
|
|
language: data.language,
|
|
|
|
codec: data.codec || null,
|
|
|
|
timeBase: data.time_base,
|
|
|
|
channels: data.channels,
|
|
|
|
channelLayout: data.channel_layout,
|
|
|
|
chapters: data.chapters || [],
|
2021-10-24 03:31:48 +02:00
|
|
|
embeddedCoverArt: data.embedded_cover_art || null,
|
|
|
|
trackNumFromMeta: data.trackNumFromMeta,
|
|
|
|
trackNumFromFilename: data.trackNumFromFilename,
|
|
|
|
cdNumFromFilename: data.cdNumFromFilename
|
2021-10-01 01:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var hasUpdates = false
|
|
|
|
for (const key in dataMap) {
|
|
|
|
if (key === 'chapters') {
|
|
|
|
var chaptersUpdated = this.syncChapters(dataMap.chapters)
|
|
|
|
if (chaptersUpdated) {
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
} else if (dataMap[key] !== this[key]) {
|
|
|
|
// Logger.debug(`[AudioFile] "${key}" from ${this[key]} => ${dataMap[key]}`)
|
|
|
|
this[key] = dataMap[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.metadata.updateData(data)) {
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:02:42 +02:00
|
|
|
clone() {
|
|
|
|
return new AudioFile(this.toJSON())
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:52:32 +02:00
|
|
|
// If the file or parent directory was renamed it is synced here
|
2021-08-26 00:36:54 +02:00
|
|
|
syncFile(newFile) {
|
|
|
|
var hasUpdates = false
|
|
|
|
var keysToSync = ['path', 'fullPath', 'ext', 'filename']
|
|
|
|
keysToSync.forEach((key) => {
|
|
|
|
if (newFile[key] !== undefined && newFile[key] !== this[key]) {
|
|
|
|
hasUpdates = true
|
|
|
|
this[key] = newFile[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return hasUpdates
|
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
updateFromScan(scannedAudioFile) {
|
|
|
|
var hasUpdated = false
|
|
|
|
|
|
|
|
var newjson = scannedAudioFile.toJSON()
|
|
|
|
if (this.manuallyVerified) newjson.manuallyVerified = true
|
|
|
|
if (this.exclude) newjson.exclude = true
|
|
|
|
newjson.addedAt = this.addedAt
|
|
|
|
|
|
|
|
for (const key in newjson) {
|
|
|
|
if (key === 'metadata') {
|
|
|
|
if (!this.metadata || !this.metadata.isEqual(scannedAudioFile.metadata)) {
|
|
|
|
this.metadata = scannedAudioFile.metadata
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
} else if (key === 'chapters') {
|
|
|
|
if (this.syncChapters(newjson.chapters || [])) {
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
} else if (this[key] !== newjson[key]) {
|
2021-12-05 18:29:42 +01:00
|
|
|
// console.log(this.filename, 'key', key, 'updated', this[key], newjson[key])
|
2021-11-26 01:39:02 +01:00
|
|
|
this[key] = newjson[key]
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasUpdated
|
|
|
|
}
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
module.exports = AudioFile
|