From 43d1608a8c40dda952d646cee7eacf4a0cbb1681 Mon Sep 17 00:00:00 2001 From: "Toni M. Brotons" Date: Tue, 28 Jan 2025 12:07:32 +0100 Subject: [PATCH] Add DataBuffer class --- gui_dev/src/utils/DataBuffer.js | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 gui_dev/src/utils/DataBuffer.js diff --git a/gui_dev/src/utils/DataBuffer.js b/gui_dev/src/utils/DataBuffer.js new file mode 100644 index 00000000..bbcc6c0f --- /dev/null +++ b/gui_dev/src/utils/DataBuffer.js @@ -0,0 +1,63 @@ +export class DataBuffer { + /** + * Creates an instance of DataBuffer + * @param {number} capacity - The capacity of the buffer + * @param {number} sampleRate - The sample rate of the data + * @param {number} numChannels - The number of channels in the data + */ + constructor(capacity, numDataPoints) { + this.capacity = capacity; + this.numDataPoints = numDataPoints; + + this.data = new Float32Array(capacity * numDataPoints); + + this.writeHead = 0; + this.readHead = 0; + } + + /** + * Store the header of the data, in form an array of strings + * @param {Array.} header + */ + setHeader(header) { + this.header = header; + } + + /** + * Adds a datapoint to the buffer + * @param {Float32Array} data - The data to add + */ + addTimePoint(data) { + for (let i = 0; i < data.length; i++) { + this.data[this.writeHead * this.numDataPoints + i] = data[i]; + } + + this.writeHead = (this.writeHead + 1) % this.capacity; + + // Overwrite the oldest data point if the buffer is full (maybe prevent this?) + if (this.writeHead === this.readHead) { + this.readHead = (this.readHead + 1) % this.capacity; + } + } + + /** + * Get the next data point from the buffer + * @returns {Float32Array} - The data points + */ + getNextDataPoint() { + if (this.readHead === this.writeHead) { + return null; + } + + // Create a view into the buffer for the current read position + const result = this.data.subarray( + this.readHead * this.numDataPoints, + (this.readHead + 1) * this.numDataPoints + ); + + // Advance the read head + this.readHead = (this.readHead + 1) % this.capacity; + + return result; + } +}