2022-04-16 19:37:10 +02:00
|
|
|
const { AudioMimeType } = require('../../utils/constants')
|
2022-03-10 02:23:17 +01:00
|
|
|
const AudioMetaTags = require('../metadata/AudioMetaTags')
|
|
|
|
const FileMetadata = require('../metadata/FileMetadata')
|
2021-09-30 03:43:36 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
class AudioFile {
|
|
|
|
constructor(data) {
|
|
|
|
this.index = null
|
|
|
|
this.ino = null
|
2023-09-04 18:50:55 +02:00
|
|
|
/** @type {FileMetadata} */
|
2022-03-10 02:23:17 +01:00
|
|
|
this.metadata = null
|
2021-08-26 00:36:54 +02:00
|
|
|
this.addedAt = null
|
2022-03-10 02:23:17 +01:00
|
|
|
this.updatedAt = null
|
2021-08-26 00:36:54 +02:00
|
|
|
|
2021-08-26 14:09:23 +02:00
|
|
|
this.trackNumFromMeta = null
|
2022-01-10 00:36:25 +01:00
|
|
|
this.discNumFromMeta = null
|
2021-08-26 14:09:23 +02:00
|
|
|
this.trackNumFromFilename = null
|
2022-01-10 00:36:25 +01:00
|
|
|
this.discNumFromFilename = null
|
2021-08-26 14:09:23 +02:00
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
this.format = null
|
|
|
|
this.duration = 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
|
2023-09-04 18:50:55 +02:00
|
|
|
/** @type {AudioMetaTags} */
|
2022-03-10 02:23:17 +01:00
|
|
|
this.metaTags = null
|
2021-08-26 00:36:54 +02:00
|
|
|
|
|
|
|
this.manuallyVerified = 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
index: this.index,
|
|
|
|
ino: this.ino,
|
2022-03-10 02:23:17 +01:00
|
|
|
metadata: this.metadata.toJSON(),
|
2021-08-26 00:36:54 +02:00
|
|
|
addedAt: this.addedAt,
|
2022-03-10 02:23:17 +01:00
|
|
|
updatedAt: this.updatedAt,
|
2021-08-26 14:09:23 +02:00
|
|
|
trackNumFromMeta: this.trackNumFromMeta,
|
2022-01-10 00:36:25 +01:00
|
|
|
discNumFromMeta: this.discNumFromMeta,
|
2021-08-26 14:09:23 +02:00
|
|
|
trackNumFromFilename: this.trackNumFromFilename,
|
2022-01-10 00:36:25 +01:00
|
|
|
discNumFromFilename: this.discNumFromFilename,
|
2021-08-26 00:36:54 +02:00
|
|
|
manuallyVerified: !!this.manuallyVerified,
|
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,
|
|
|
|
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,
|
2023-08-28 00:19:57 +02:00
|
|
|
metaTags: this.metaTags?.toJSON() || {},
|
2022-03-18 01:10:47 +01:00
|
|
|
mimeType: this.mimeType
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(data) {
|
|
|
|
this.index = data.index
|
|
|
|
this.ino = data.ino
|
2022-03-10 02:23:17 +01:00
|
|
|
this.metadata = new FileMetadata(data.metadata || {})
|
2021-08-26 00:36:54 +02:00
|
|
|
this.addedAt = data.addedAt
|
2022-03-10 02:23:17 +01:00
|
|
|
this.updatedAt = data.updatedAt
|
2021-08-26 00:36:54 +02:00
|
|
|
this.manuallyVerified = !!data.manuallyVerified
|
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
|
2022-01-10 00:36:25 +01:00
|
|
|
this.discNumFromMeta = data.discNumFromMeta
|
2021-12-05 18:29:42 +01:00
|
|
|
this.trackNumFromFilename = data.trackNumFromFilename
|
2022-01-10 00:36:25 +01:00
|
|
|
|
|
|
|
if (data.cdNumFromFilename !== undefined) this.discNumFromFilename = data.cdNumFromFilename // TEMP:Support old var name
|
|
|
|
else this.discNumFromFilename = data.discNumFromFilename
|
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.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
|
|
|
|
|
2022-03-10 02:23:17 +01:00
|
|
|
this.metaTags = new AudioMetaTags(data.metaTags || {})
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
get mimeType() {
|
2022-12-26 23:08:53 +01:00
|
|
|
const format = this.metadata.format.toUpperCase()
|
2022-04-16 19:37:10 +02:00
|
|
|
if (AudioMimeType[format]) {
|
|
|
|
return AudioMimeType[format]
|
|
|
|
} else {
|
|
|
|
return AudioMimeType.MP3
|
2022-03-18 01:10:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 20:59:37 +02:00
|
|
|
// New scanner creates AudioFile from AudioFileScanner
|
2022-03-13 00:45:32 +01:00
|
|
|
setDataFromProbe(libraryFile, probeData) {
|
|
|
|
this.ino = libraryFile.ino || null
|
2022-03-10 02:23:17 +01:00
|
|
|
|
2023-09-03 00:49:28 +02:00
|
|
|
if (libraryFile.metadata instanceof FileMetadata) {
|
|
|
|
this.metadata = libraryFile.metadata.clone()
|
|
|
|
} else {
|
|
|
|
this.metadata = new FileMetadata(libraryFile.metadata)
|
|
|
|
}
|
|
|
|
|
2021-11-23 02:58:20 +01:00
|
|
|
this.addedAt = Date.now()
|
2022-03-10 02:23:17 +01:00
|
|
|
this.updatedAt = Date.now()
|
2021-11-25 03:15:50 +01:00
|
|
|
|
|
|
|
this.format = probeData.format
|
|
|
|
this.duration = probeData.duration
|
|
|
|
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 || []
|
2023-01-02 23:35:39 +01:00
|
|
|
this.metaTags = probeData.audioMetaTags
|
2021-11-26 01:39:02 +01:00
|
|
|
this.embeddedCoverArt = probeData.embeddedCoverArt
|
2021-11-25 03:15:50 +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
|
|
|
|
}
|
|
|
|
|
2022-12-26 23:08:53 +01:00
|
|
|
let hasUpdates = false
|
2021-10-01 01:52:32 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:02:42 +02:00
|
|
|
clone() {
|
|
|
|
return new AudioFile(this.toJSON())
|
|
|
|
}
|
|
|
|
|
2023-08-28 00:19:57 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {AudioFile} scannedAudioFile
|
|
|
|
* @returns {boolean} true if updates were made
|
|
|
|
*/
|
2021-11-26 01:39:02 +01:00
|
|
|
updateFromScan(scannedAudioFile) {
|
2022-12-26 23:08:53 +01:00
|
|
|
let hasUpdated = false
|
2021-11-26 01:39:02 +01:00
|
|
|
|
2022-12-26 23:08:53 +01:00
|
|
|
const newjson = scannedAudioFile.toJSON()
|
2023-08-28 00:19:57 +02:00
|
|
|
const ignoreKeys = ['manuallyVerified', 'ctimeMs', 'addedAt', 'updatedAt']
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
for (const key in newjson) {
|
2022-03-13 00:45:32 +01:00
|
|
|
if (key === 'metadata') {
|
|
|
|
if (this.metadata.update(newjson[key])) {
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
} else if (key === 'metaTags') {
|
|
|
|
if (!this.metaTags || !this.metaTags.isEqual(scannedAudioFile.metaTags)) {
|
|
|
|
this.metaTags = scannedAudioFile.metaTags.clone()
|
2021-11-26 01:39:02 +01:00
|
|
|
hasUpdated = true
|
|
|
|
}
|
|
|
|
} else if (key === 'chapters') {
|
|
|
|
if (this.syncChapters(newjson.chapters || [])) {
|
|
|
|
hasUpdated = true
|
|
|
|
}
|
2023-01-02 23:35:39 +01:00
|
|
|
} else if (!ignoreKeys.includes(key) && 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
|