1
Audio / Re: How to process audio recording and manage graphic in (almost) real time ?
« on: June 16, 2020, 04:07:29 pm »
Ok I got it working !
I was confused at first because I didn't understand how to retrieve the data from the sound recorder ! I thought i had to stop it every time I wanted to get access to the audio data, but in reality you can manage the data in realtime ! I didn't realise you could just create a buffer to capture the incoming audio data
I wrote my class like this :
This is just a quick test and not the final result, but it works enough for a first try. I used a vector<sf::Int16> to store all the samples because my old function needed a vector. I fill this vector (named buffer in the code) in a circular manner, and then I give it to my fft function. Works like a charm.
Thanks for your advice eXpl0it3r !
Louis
I was confused at first because I didn't understand how to retrieve the data from the sound recorder ! I thought i had to stop it every time I wanted to get access to the audio data, but in reality you can manage the data in realtime ! I didn't realise you could just create a buffer to capture the incoming audio data
I wrote my class like this :
Code: [Select]
#include <cstdlib>
#include <iostream>
#include <SFML/Audio.hpp>
#include <vector>
using namespace std;
using namespace sf;
class FastRecorder : public SoundRecorder
{
public:
bool onStart() override;
bool onProcessSamples(const Int16 *samples, std::size_t sampleCount) override;
void onStop() override;
void setBufferSize(int bufferSize);
vector<Int16> getBuffer();
int getBufferSize();
private:
Int16 globalmax;
const Int16* msamples;
Uint64 msamplecount;
vector<Int16> mbuffer;
int mark;
int mbufferSize;
};
Code: [Select]
#include "fastrecorder.h"
bool FastRecorder::onStart()
{
globalmax = 0;
mark = 0;
mbufferSize = 512;
mbuffer.resize(mbufferSize);
setProcessingInterval(milliseconds(20));
return true;
}
bool FastRecorder::onProcessSamples(const Int16 *samples, std::size_t sampleCount)
{
// Do something with the new chunk of samples (store them, send them, ...)
msamples = samples;
msamplecount = sampleCount;
//If the whole samples array can fit in the vector just fill it
//If not, then fill it as much as we can then go back to the beginning of the vector.
for(int i = 0; i<msamplecount;i++){
mbuffer[(i+mark)%mbufferSize] = samples[i];
}
mark = (msamplecount-1+mark) % mbufferSize;
// Return true to continue playing
return true;
}
void FastRecorder::onStop()
{}
void FastRecorder::setBufferSize(int bufferSize){
mbufferSize = bufferSize;
mbuffer.resize(mbufferSize);
}
vector<Int16> FastRecorder::getBuffer()
{
return mbuffer;
}
int FastRecorder::getBufferSize()
{
return mbufferSize;
}
This is just a quick test and not the final result, but it works enough for a first try. I used a vector<sf::Int16> to store all the samples because my old function needed a vector. I fill this vector (named buffer in the code) in a circular manner, and then I give it to my fft function. Works like a charm.
Thanks for your advice eXpl0it3r !
Louis