-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { F } from "h2o-wave" | ||
|
||
export const | ||
averageChannels = (channel1: Float32Array, channel2?: Float32Array) => { | ||
if (!channel2) return channel1 | ||
const result = new Float32Array(channel1.length) | ||
for (let i = 0; i < channel1.length; i++) { | ||
result[i] = (channel1[i] + channel2[i]) / 2 | ||
} | ||
return result | ||
}, | ||
parseAudioData = (samples: F, rawData: F[]) => { | ||
if (!samples) return [] | ||
console.log('parse', samples) | ||
|
||
const blockSize = Math.floor(rawData.length / samples) | ||
const filteredData = new Array<F>(samples) | ||
for (let i = 0; i < samples; i++) { | ||
const blockStart = blockSize * i // the location of the first sample in the block | ||
let sum = 0 | ||
for (let j = 0; j < blockSize; j++) { | ||
sum += Math.abs(rawData[blockStart + j]) // find the sum of all the samples in the block | ||
} | ||
filteredData[i] = sum / blockSize // divide the sum by the block size to get the average | ||
} | ||
const multiplier = Math.pow(Math.max(...filteredData), -1) | ||
return filteredData.map(n => n * multiplier) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters