Skip to content

Commit

Permalink
fix equalizer initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
afkcodes committed Jul 31, 2024
1 parent a9c3fb5 commit d0e04d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "audio_x",
"version": "1.0.10-beta.4",
"version": "1.0.10-beta.7",
"description": "The audio player for the gen-x",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
49 changes: 37 additions & 12 deletions src/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ class AudioX {
}

if (enableEQ) {
this.isEqEnabled = enableEQ;
this.attachEq();
if (this.eqInstance) {
this.isEqEnabled = enableEQ;
}
}

if (enableHls) {
Expand All @@ -132,6 +135,14 @@ class AudioX {
return;
}

const queue = this.getQueue();
if (isValidArray(queue)) {
const index = queue.findIndex((track) => mediaTrack.id === track.id);
if (index > -1) {
this._currentQueueIndex = index;
}
}

const mediaType = mediaTrack.source.includes('.m3u8') ? 'HLS' : 'DEFAULT';

if (this.isPlayLogEnabled) {
Expand Down Expand Up @@ -165,7 +176,7 @@ class AudioX {
}

attachEq() {
if (this.isEqEnabled && this.eqStatus === 'IDEAL') {
if (this.eqStatus === 'IDEAL') {
try {
const eq = new Equalizer();
this.eqStatus = eq.status();
Expand Down Expand Up @@ -218,7 +229,6 @@ class AudioX {
this.addMedia(currentTrack).then(() => {
if (audioInstance.HAVE_ENOUGH_DATA === READY_STATE.HAVE_ENOUGH_DATA) {
setTimeout(async () => {
this.attachEq();
await this.play();
}, 950);
}
Expand Down Expand Up @@ -323,15 +333,27 @@ class AudioX {
}

setPreset(id: keyof Preset) {
this.eqInstance.setPreset(id);
if (this.isEqEnabled) {
this.eqInstance.setPreset(id);
} else {
console.error('Equalizer not initialized, please set enableEq at init');
}
}

setCustomEQ(gains: number[]) {
this.eqInstance.setCustomEQ(gains);
if (this.isEqEnabled) {
this.eqInstance.setCustomEQ(gains);
} else {
console.error('Equalizer not initialized, please set enableEq at init');
}
}

setBassBoost(enabled: boolean, boost: number) {
this.eqInstance.setBassBoost(enabled, boost);
if (this.isEqEnabled) {
this.eqInstance.setBassBoost(enabled, boost);
} else {
console.error('Equalizer not initialized, please set enableEq at init');
}
}

addQueue(queue: MediaTrack[], playbackType: QueuePlaybackType) {
Expand Down Expand Up @@ -360,20 +382,23 @@ class AudioX {
}

playNext() {
if (this._queue.length > this._currentQueueIndex + 1) {
this._currentQueueIndex++;
const nextTrack = this._queue[this._currentQueueIndex];
const index = this._currentQueueIndex + 1;
if (this._queue.length > index) {
const nextTrack = this._queue[index];
this.addMediaAndPlay(nextTrack, this._fetchFn);
this._currentQueueIndex = index;
} else {
console.warn('Queue ended');
}
}

playPrevious() {
if (this._currentQueueIndex > 0) {
this._currentQueueIndex--;
const previousTrack = this._queue[this._currentQueueIndex];
const index = this._currentQueueIndex - 1;

if (index > 0) {
const previousTrack = this._queue[index];
this.addMediaAndPlay(previousTrack, this._fetchFn);
this._currentQueueIndex = index;
} else {
console.log('At the beginning of the queue');
}
Expand Down

0 comments on commit d0e04d9

Please sign in to comment.