2022-04-14 14:25:48 +02:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
|
2022-04-27 15:52:14 +02:00
|
|
|
const AudioBuffer = require('./AudioBuffer.js');
|
|
|
|
|
2022-04-14 14:25:48 +02:00
|
|
|
class Player extends EventEmitter {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.events = [];
|
|
|
|
}
|
|
|
|
|
2022-04-29 16:50:31 +02:00
|
|
|
async prepare(stream, settings) {
|
2022-04-14 14:25:48 +02:00
|
|
|
logger.debug('preparing audio player...');
|
2022-05-02 14:20:33 +02:00
|
|
|
this.settings = settings;
|
|
|
|
this.audiobuffer = new AudioBuffer(stream, this.settings);
|
2022-04-29 16:50:31 +02:00
|
|
|
this.audiobuffer.on(constants.THRESHOLD, () => {
|
|
|
|
this.#setState(constants.READY);
|
|
|
|
});
|
|
|
|
this.audiobuffer.on('play', () => {
|
|
|
|
this.#setState(constants.PLAYING);
|
2022-04-25 16:15:55 +02:00
|
|
|
});
|
2022-04-29 16:50:31 +02:00
|
|
|
this.audiobuffer.on('pause', () => {
|
|
|
|
this.#setState(constants.PAUSED);
|
2022-05-02 14:20:33 +02:00
|
|
|
});
|
|
|
|
this.audiobuffer.on('close', () => {
|
|
|
|
this.#setState(constants.STOPPED);
|
|
|
|
});
|
2022-04-25 14:55:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-27 15:52:14 +02:00
|
|
|
play() {
|
2022-05-02 14:20:33 +02:00
|
|
|
this.audiobuffer.play();
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
2022-04-29 16:50:31 +02:00
|
|
|
pause() {
|
|
|
|
this.audiobuffer.pause();
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async stop() {
|
2022-04-29 16:50:31 +02:00
|
|
|
this.audiobuffer.stop();
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isReady() {
|
2022-04-29 16:50:31 +02:00
|
|
|
return this.state === constants.READY;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isPlaying() {
|
2022-04-29 16:50:31 +02:00
|
|
|
return this.state === constants.PLAYING;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isPaused() {
|
2022-04-29 16:50:31 +02:00
|
|
|
return this.state === constants.PAUSED;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isFinished() {
|
2022-04-29 16:50:31 +02:00
|
|
|
return this.state === constants.STOPPED;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hasError() {
|
2022-04-29 16:50:31 +02:00
|
|
|
return this.state === constants.ERROR;
|
2022-04-20 16:15:51 +02:00
|
|
|
}
|
|
|
|
|
2022-04-29 16:50:31 +02:00
|
|
|
getProgress() {
|
2022-05-02 14:20:33 +02:00
|
|
|
return this.audiobuffer?.getProgress() || 0;
|
2022-04-14 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#setState(state, data) {
|
|
|
|
this.state = state;
|
|
|
|
logger.debug('setting state of audio player to \'' + state + '\'...');
|
2022-05-02 14:20:33 +02:00
|
|
|
// if (this.events.includes(state)) {
|
|
|
|
// return;
|
|
|
|
// }
|
2022-04-14 15:26:46 +02:00
|
|
|
logger.debug('emitting state \'' + state + '\' of audio player...');
|
2022-04-20 16:15:51 +02:00
|
|
|
this.emit(this.state, { data: data });
|
2022-04-29 16:50:31 +02:00
|
|
|
this.emit(constants.STATECHANGE, { state: this.state, progress: this.getProgress() });
|
2022-04-14 14:25:48 +02:00
|
|
|
this.events.push(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Player;
|