Skip to content

Commit

Permalink
feat: player: implement a basic simple 10 band music EQ
Browse files Browse the repository at this point in the history
Signed-off-by: EndCredits <[email protected]>
  • Loading branch information
EndCredits committed Jan 26, 2024
1 parent 6c2e84a commit b2dc285
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/web/utils/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ interface TrackListSource {
id: number
}

interface Filter {
gain: GainNode;
filter: BiquadFilterNode;
}

export enum Mode {
TrackList = 'trackList',
FM = 'fm',
Expand Down Expand Up @@ -370,6 +375,34 @@ export class Player {
this._howlerOnEndCallback()
},
})

const audioCtx: AudioContext = Howler.ctx

const filters: Filter[] = []

const bandsNum = 10;
const gainFrequencies = [ 30, 58, 90, 170, 200, 500, 10000, 20000, 80000, 160000]

gainFrequencies.forEach((freq, index) => {
const gainNode: GainNode = audioCtx.createGain()
gainNode.gain.value = 1

const filter: BiquadFilterNode = audioCtx.createBiquadFilter()
filter.type = 'peaking'
filter.frequency.value = freq
filter.Q.value = 1
filter.gain.value = 0;

filters.push({
gain: gainNode,
filter: filter
})

if (index > 0) {
filters[index - 1].filter.connect(filters[index].gain);
}
})

_howler = howler
;(window as any).howler = howler
if (autoplay) {
Expand All @@ -378,6 +411,8 @@ export class Player {
}
_howler.once('load', () => {
this._cacheAudio((_howler as any)._src)
const source: MediaElementAudioSourceNode = Howler.ctx.createMediaElementSource((_howler as any)._sounds[0]._node)
source.connect(filters[0].gain)
})

if (!this._progressInterval) {
Expand Down

0 comments on commit b2dc285

Please sign in to comment.