Skip to content

Commit

Permalink
add float type
Browse files Browse the repository at this point in the history
  • Loading branch information
maruware committed Sep 1, 2020
1 parent 7bf0115 commit c84891a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const AudioContext = window.AudioContext || window.webkitAudioContext

type CalcFunction = (
bin: Uint8Array,
bin: Uint8Array | Float32Array,
sampleRate: number,
minHz: number | undefined,
maxHz: number | undefined
) => number

const averageFreqData: CalcFunction = (
bin: Uint8Array,
bin: Uint8Array | Float32Array,
sampleRate: number,
minHz: number | undefined,
maxHz: number | undefined
Expand All @@ -34,7 +34,7 @@ const averageFreqData: CalcFunction = (
}

const maxFreqData: CalcFunction = (
bin: Uint8Array,
bin: Uint8Array | Float32Array,
sampleRate: number,
minHz: number | undefined,
maxHz: number | undefined
Expand Down Expand Up @@ -62,6 +62,7 @@ interface WatchStreamAudioLevelOption {
minHz?: number
maxHz?: number
calcMethod?: 'average' | 'max'
valueType?: 'byte' | 'float'
}

export const watchStreamAudioLevel = (
Expand All @@ -70,6 +71,7 @@ export const watchStreamAudioLevel = (
opt?: WatchStreamAudioLevelOption
): (() => void) => {
const calcMethod = opt && opt.calcMethod ? opt.calcMethod : 'average'
const valueType = opt && opt.valueType ? opt.valueType : 'byte'

const audioContext = new AudioContext()
const analyser = audioContext.createAnalyser()
Expand All @@ -85,12 +87,22 @@ export const watchStreamAudioLevel = (

const calcF = calcMethod === 'average' ? averageFreqData : maxFreqData

const getFreqData = () => {
if (valueType === 'byte') {
const bin = new Uint8Array(analyser.frequencyBinCount)
analyser.getByteFrequencyData(bin)
return bin
}
const bin = new Float32Array(analyser.frequencyBinCount)
analyser.getFloatFrequencyData(bin)
return bin
}

const handler = () => {
const fftBin = new Uint8Array(analyser.frequencyBinCount)
analyser.getByteFrequencyData(fftBin)
const bin = getFreqData()

const v = calcF(
fftBin,
bin,
audioContext.sampleRate,
opt && opt.minHz,
opt && opt.maxHz
Expand Down

0 comments on commit c84891a

Please sign in to comment.