Skip to content

Commit

Permalink
Add DataBuffer class
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni M. Brotons authored and Toni M. Brotons committed Jan 28, 2025
1 parent 158fa5d commit 43d1608
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions gui_dev/src/utils/DataBuffer.js
Original file line number Diff line number Diff line change
@@ -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.<String>} 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;
}
}

0 comments on commit 43d1608

Please sign in to comment.