glowers-radio/lib/RadioSource.js

127 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

"use-strict";
const { objSerialize, objBulkSet } = require("./util.js")
const { RadioMetadata } = require("./RadioMetadata.js")
const { RadioPreview } = require("./RadioPreview.js")
/**
* @public
* @constructor
* @params {string} type
* @params {string[]} [codecs] codecs
* @returns {RadioSource}
*/
const RadioSource = function (type, codecs) {
this.type = type
this.uri = null
this.previews = []
this.setTypeWCodec(type, codecs)
return this
}
/**
* @public
* @params {string} type
* @params {string[]} [codecs] codecs
* @returns {RadioSource}
*/
RadioSource.new = (type, codecs) => new RadioSource(type, codecs)
/**
* @public
* @params {string} uri
* @returns {RadioSource}
*/
RadioSource.prototype.setURI = function (uri) { this.uri = uri; return this }
/**
* Set metadata that specifically applies to *this* source. Used for items where the audio has a different attribution than
* the video.
* @public
* @params {string|undefined} title
* @params {string|undefined} artist
* @params {URL|string|undefined} [href] href
* @params {string} [extra] extra Playback effects. i.e: Pitch, speed
* @returns {RadioSource}
*/
RadioSource.prototype.setMetadata = function (title, artist, href, extra) {
if (!this.metadata)
this.metadata = RadioMetadata.new()
objBulkSet(this, "metadata", { title, artist, href, extra });
return this
}
/**
* @public
* @params {RadioPlaybackInfo} playbackInfo
* @returns {RadioSource}
*/
RadioSource.prototype.setPlaybackInfo = function (playbackInfo) { this.playbackinfo = playbackInfo; return this; }
/**
* @public
* @params {string} uri
* @returns {RadioSource}
*/
RadioSource.prototype.setTypeWCodec = function (type, codecs) {
this.type = type
this.codecs = codecs?.length ? codecs : undefined
return this
}
/**
* @public
* @params {string} uri
* @params {string} type
* @params {string} size
* @params {EDisplayType} [displayType] displayType
* @returns {RadioSource}
*/
/**
* @public
* @params {string} uri
* @params {string} type
* @params {string[]} codecs
* @params {String} size
* @params {EDisplayType} [displayType] displayType
* @returns {RadioSource}
*/
RadioSource.prototype.addPreview = function () {
let uri, type, codecs, size, displayType
if (typeof arguments[0] == "string" && typeof arguments[1] == "string" && arguments[2] instanceof Array) {
uri = arguments[0]
type = arguments[1]
codecs = arguments[2]
size = arguments[3]
displayType = arguments[4]
} else if (typeof arguments[0] == "string" && typeof arguments[1] == "string" && typeof arguments[2] == "string") {
uri = arguments[0]
type = arguments[1]
size = arguments[2]
displayType = arguments[3]
} else
throw new TypeError("Could not recognize arguments. Expecting [ LString, LString, LString, ...?] or [ LString, LString, LString[], ...?]")
this.previews.push(RadioPreview.new(uri, type, codecs, size, displayType))
return this
}
/**
* @deprecated
* @returns {Object}
*/
RadioSource.prototype.serialize = function () {
return objSerialize(this, [
"type",
"codecs",
"uri"
], (obj, self) => ({
...obj,
"metadata": self.metadata ? self.metadata.serialize() : undefined,
"playbackinfo": self.playbackinfo ? self.playbackinfo.serialize() : undefined,
"previews": this.previews.map(preview => preview.serialize(obj))
}))
}
module.exports = { RadioSource }