-
Notifications
You must be signed in to change notification settings - Fork 14
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
1 parent
f3e7c13
commit 7eefe6e
Showing
6 changed files
with
63 additions
and
467 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
export class Mixer { | ||
inputs: { [id: string]: Input } = {}; | ||
pcm: { [id: string]: Buffer } = {}; | ||
onData: (buf: Buffer) => void = () => {}; | ||
|
||
constructor(private args: { bit: number }) {} | ||
|
||
input() { | ||
const input = new Input(this); | ||
this.inputs[input.id] = input; | ||
|
||
return input; | ||
} | ||
|
||
mixing() { | ||
if (Object.keys(this.pcm).length === Object.keys(this.inputs).length) { | ||
const inputs = Object.values(this.pcm); | ||
const base = inputs.shift(); | ||
this.pcm = {}; | ||
const res = inputs.reduce( | ||
(acc: number[], cur) => { | ||
const next = acc.map((v, i) => this.mix(v, cur[i])); | ||
return next; | ||
}, | ||
[...base] | ||
); | ||
this.onData(Buffer.from(res)); | ||
} | ||
} | ||
|
||
private mix(a: number, b: number) { | ||
const res = a + b; | ||
const max = 1 << (this.args.bit - 1); | ||
if (max < res) { | ||
return max; | ||
} else if (0 > res) { | ||
return res; | ||
} | ||
return res; | ||
} | ||
} | ||
|
||
export class Input { | ||
id = Math.random().toString(); | ||
constructor(private mixer: Mixer) {} | ||
|
||
write(buf: Buffer) { | ||
this.mixer.pcm[this.id] = buf; | ||
this.mixer.mixing(); | ||
} | ||
|
||
remove() { | ||
delete this.mixer.inputs[this.id]; | ||
delete this.mixer.pcm[this.id]; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.