setup audio receiver and event dispatcher

This commit is contained in:
Lewis Moten
2024-05-11 03:33:40 -04:00
parent fdb6723e87
commit 6647e5b51d
6 changed files with 376 additions and 279 deletions

View File

@@ -1,5 +1,8 @@
import Dispatcher from "./Dispatcher";
import { bitsToInt } from "./converters";
const dispatcher = new Dispatcher('StreamManager', ['change']);
const BITS = [];
let BITS_PER_PACKET = 0;
let SEGMENTS_PER_PACKET = 0;
@@ -14,6 +17,16 @@ let PACKET_ENCODING = {
decode: bits => bits
}
export const addEventListener = dispatcher.addListener;
export const removeEventListener = dispatcher.removeListener;
export const reset = () => {
if(BITS.length !== 0) {
BITS.length = 0;
dispatcher.emit('change');
}
}
export const changeConfiguration = ({
segmentsPerPacket,
bitsPerPacket,
@@ -42,7 +55,20 @@ export const addBits = (
if(BITS[packetIndex] === undefined) {
BITS[packetIndex] = [];
}
const oldBits = BITS[packetIndex][segmentIndex];
BITS[packetIndex][segmentIndex] = bits;
if(hasNewBits(oldBits, bits))
dispatcher.emit('change');
}
const hasNewBits = (oldBits = [], bits = []) => {
if(oldBits.length === 0 && bits.length === BITS_PER_SEGMENT)
return true;
for(let i = 0; i < BITS_PER_SEGMENT; i++) {
let a = oldBits[i] ?? 0;
let b = bits[i] ?? 0;
if(a !== b) return true;
}
return false;
}
export const getPacketReceivedCount = () => {
if(BITS.length === 0) return 1;