Reputation: 49
I was wondering if you guys have an solution for this: I want to record and hold a Audio buffer of 10 seconds (dosent has to be 10), but only The 10 last recorded seconds! Ex. I record for ten seconds, then The first recorded seconds is overwritten and a new One is added to the end! My first thougth was a circular buffer where The start pointer just moves along! My problem is not The circular buffer, The problem lies With how to handle Audio samples into an buffer! Its a bit tricky for Me to understand! And thougths, or you need further explonation?
(might be bad english, stupid auto correct)
Pre thank you!
Upvotes: 1
Views: 697
Reputation: 4654
Here example assumed usage of AudioRecord.
short[] myCircularBuffer = new short[samplingRate * lengthInSeconds];
int bufPos = 0;
AudioRecord audioRecord = ...
audioRecord.startRecording();
while (!finish_condition) {
int cnt = audioRecord.read(myCircularBuffer, bufPos, myCircularBuffer.length - bufPos);
if (cnt < 0) break; // some error was
bufPos += cnt;
if (bufPos >= myCircularBuffer.length) bufPos -= myCircularBuffer.length;
// *
}
// * - at this point you have 10 seconds in your buffer in two parts, from bufPos thru (myCircularBuffer.length - 1), inclusive, and then from 0 thru (bufPos - 1), inclusive.
Note, when record just started, buffer will contain unerased previous data (initially zeroes), so it is better to handle when buffer is not completely filled. For example:
boolean bufferWrapped = false;
while (...) {
...
if (bufPos >= myCirculatBuffer.length) {
bufPos -= myCirculatBuffer.length;
bufferWrapped = true;
}
}
at end, you need to use tail part (from bufPos thru end) only if bufferWrapped == true
Upvotes: 2
Reputation: 1984
You can record two audio files every ten seconds and reuse the last one (since you have another 10 seconds record you won't need that). And when the user stops the record, you combine the current record which will have let's say 6 seconds with the last (10-6) seconds of the previously recorded file. I can't think of anything else right now. It is, however, tricky to implement, but I believe it can be done.
Upvotes: 0