mirror of
https://github.com/lewismoten/data-over-audio.git
synced 2026-04-21 13:37:32 +08:00
Send and receive data length of data across all packets
This commit is contained in:
53
index.js
53
index.js
@@ -11,6 +11,7 @@ var sentDataTextArea;
|
|||||||
var receivedGraph;
|
var receivedGraph;
|
||||||
var receivedData = [];
|
var receivedData = [];
|
||||||
var MAX_AMPLITUDE = 300; // Higher than 255 to give us space
|
var MAX_AMPLITUDE = 300; // Higher than 255 to give us space
|
||||||
|
const MAXIMUM_PACKETIZATION_SIZE_BITS = 16;
|
||||||
|
|
||||||
// bits after error correction is applied
|
// bits after error correction is applied
|
||||||
let RECEIVED_STREAM_DECODED_BITS = [];
|
let RECEIVED_STREAM_DECODED_BITS = [];
|
||||||
@@ -301,7 +302,7 @@ function showSpeed() {
|
|||||||
function updatePacketStats() {
|
function updatePacketStats() {
|
||||||
const text = textToSend.value;
|
const text = textToSend.value;
|
||||||
const bits = textToBits(text);
|
const bits = textToBits(text);
|
||||||
const bitCount = bits.length;
|
const bitCount = getPacketizationBitCount(bits.length);
|
||||||
const byteCount = bitCount / 8;
|
const byteCount = bitCount / 8;
|
||||||
document.getElementById('data-byte-count').innerText = byteCount.toLocaleString();
|
document.getElementById('data-byte-count').innerText = byteCount.toLocaleString();
|
||||||
document.getElementById('data-bit-count').innerText = bitCount.toLocaleString();
|
document.getElementById('data-bit-count').innerText = bitCount.toLocaleString();
|
||||||
@@ -505,14 +506,22 @@ function sendBytes(bytes) {
|
|||||||
if(byteCount === 0) {
|
if(byteCount === 0) {
|
||||||
logSent('Nothing to send!');
|
logSent('Nothing to send!');
|
||||||
return;
|
return;
|
||||||
|
} else if(byteCount > 0xFFFF) {
|
||||||
|
logSent('Too much to send!');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bits = bytesToBits(bytes);
|
const bits = bytesToBits(bytes);
|
||||||
const bitCount = bits.length;
|
|
||||||
|
|
||||||
SENT_ORIGINAL_TEXT = bytesToText(bytes);
|
SENT_ORIGINAL_TEXT = bytesToText(bytes);
|
||||||
SENT_ORIGINAL_BYTES = bytes;
|
SENT_ORIGINAL_BYTES = bytes;
|
||||||
SENT_ORIGINAL_BITS = bits.slice();
|
SENT_ORIGINAL_BITS = bits.slice();
|
||||||
|
|
||||||
|
// packetization headers
|
||||||
|
// data length
|
||||||
|
bits.unshift(...intToBits(bytes.length, MAXIMUM_PACKETIZATION_SIZE_BITS));
|
||||||
|
const bitCount = bits.length;
|
||||||
|
|
||||||
SENT_TRANSFER_BITS.length = 0;
|
SENT_TRANSFER_BITS.length = 0;
|
||||||
SENT_ENCODED_BITS.length = 0;
|
SENT_ENCODED_BITS.length = 0;
|
||||||
|
|
||||||
@@ -618,6 +627,9 @@ function getSegmentTransferDurationSeconds() {
|
|||||||
function getPacketByteCount() {
|
function getPacketByteCount() {
|
||||||
return 2 ** PACKET_SIZE_BITS;
|
return 2 ** PACKET_SIZE_BITS;
|
||||||
}
|
}
|
||||||
|
function getPacketizationBitCount(bitCount) {
|
||||||
|
return bitCount + MAXIMUM_PACKETIZATION_SIZE_BITS;
|
||||||
|
}
|
||||||
function getPacketBitCount() {
|
function getPacketBitCount() {
|
||||||
return getPacketByteCount() * 8;
|
return getPacketByteCount() * 8;
|
||||||
}
|
}
|
||||||
@@ -943,6 +955,7 @@ function resetStream() {
|
|||||||
RECEIVED_STREAM_RAW_BITS.length = 0;
|
RECEIVED_STREAM_RAW_BITS.length = 0;
|
||||||
RECEIVED_STREAM_ENCODED_BITS.length = 0;
|
RECEIVED_STREAM_ENCODED_BITS.length = 0;
|
||||||
RECEIVED_STREAM_DECODED_BITS.length = 0;
|
RECEIVED_STREAM_DECODED_BITS.length = 0;
|
||||||
|
RECEIVED_STREAM_DATA_LENGTH = -1;
|
||||||
}
|
}
|
||||||
function resetPacket() {
|
function resetPacket() {
|
||||||
RECEIVED_PACKET_RAW_BITS.length = 0;
|
RECEIVED_PACKET_RAW_BITS.length = 0;
|
||||||
@@ -1030,6 +1043,16 @@ function updateReceivedData() {
|
|||||||
...RECEIVED_STREAM_DECODED_BITS,
|
...RECEIVED_STREAM_DECODED_BITS,
|
||||||
...RECEIVED_PACKET_DECODED_BITS
|
...RECEIVED_PACKET_DECODED_BITS
|
||||||
]
|
]
|
||||||
|
|
||||||
|
let dataLength = allDecodedBits.length / 8;
|
||||||
|
if(allDecodedBits.length >= MAXIMUM_PACKETIZATION_SIZE_BITS) {
|
||||||
|
dataLength = bitsToInt(allDecodedBits.slice(0, MAXIMUM_PACKETIZATION_SIZE_BITS), MAXIMUM_PACKETIZATION_SIZE_BITS);
|
||||||
|
}
|
||||||
|
// remove size header
|
||||||
|
allDecodedBits.splice(0, MAXIMUM_PACKETIZATION_SIZE_BITS);
|
||||||
|
// remove excessive bits
|
||||||
|
allDecodedBits.splice(dataLength * 8);
|
||||||
|
|
||||||
const encodedBitCount = SENT_ENCODED_BITS.length;
|
const encodedBitCount = SENT_ENCODED_BITS.length;
|
||||||
const decodedBitCount = SENT_ORIGINAL_BITS.length;
|
const decodedBitCount = SENT_ORIGINAL_BITS.length;
|
||||||
const rawBitCount = SENT_TRANSFER_BITS.length;
|
const rawBitCount = SENT_TRANSFER_BITS.length;
|
||||||
@@ -1176,7 +1199,31 @@ function getAudioContext() {
|
|||||||
}
|
}
|
||||||
return audioContext;
|
return audioContext;
|
||||||
}
|
}
|
||||||
|
function bitsToInt(bits, bitLength) {
|
||||||
|
// only grab the bits we need
|
||||||
|
const bitString = bits.slice(0, bitLength)
|
||||||
|
// combine into string
|
||||||
|
.join('')
|
||||||
|
// Assume missing bits were zeros
|
||||||
|
.padEnd(bitLength, '0');
|
||||||
|
// parse as int
|
||||||
|
return parseInt(bitString, 2);
|
||||||
|
}
|
||||||
|
function intToBits(int, bitLength) {
|
||||||
|
// max number for bit length
|
||||||
|
const max = Math.pow(2, bitLength) - 1;
|
||||||
|
// overflow numbers too high
|
||||||
|
const value = (int & max);
|
||||||
|
return value
|
||||||
|
// convert to bit string
|
||||||
|
.toString(2)
|
||||||
|
// prefix with zeros to fill bit length
|
||||||
|
.padStart(bitLength, '0')
|
||||||
|
// convert to array
|
||||||
|
.split('')
|
||||||
|
// convert strings to numbers
|
||||||
|
.map(Number);
|
||||||
|
}
|
||||||
function bytesToText(bytes, encoding='utf-8') {
|
function bytesToText(bytes, encoding='utf-8') {
|
||||||
return new TextDecoder(encoding).decode(bytes);
|
return new TextDecoder(encoding).decode(bytes);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user