Main window .cpp file (shortened)
...
void JOP::on_btnStartRec_clicked()
{
cat = new SignalCatcher(&bufferCat, 12345, 44100);
cat->start();
}
void JOP::on_btnStopRec_clicked()
{
qDebug() << cat->cat_buffer.GetSamplesCount();
delete cat;
}
...
jop_signalcatcher.h
#ifndef JOP_SIGNALCATCHER_H
#define JOP_SIGNALCATCHER_H
#include <SFML/Audio.hpp>
#include "jop_core.h"
class MyRecorder : public QObject, public sf::SoundBufferRecorder
{
Q_OBJECT
private:
int samplesCount;
public:
MyRecorder(int samplesCount);
MyRecorder();
virtual bool OnProcessSamples(const sf::Int16* Samples, std::size_t SamplesCount);
signals:
void processFinished();
};
class SignalCatcher : public QObject
{
Q_OBJECT
private:
int sampleRate;
const unsigned int samplesCount;
public:
SignalCatcher(sf::SoundBuffer* snd_buffer, int samplesCount, int samplerate);
sf::SoundBuffer cat_buffer;
void setSampleRate(int smprate);
int getSampleRate();
bool start();
bool stop();
MyRecorder *recorder;
const sf::Int16* Samples;
public slots:
virtual void onProcessFinished();
};
#endif // JOP_SIGNALCATCHER_H
jop_signalcatcher.cpp
#include "jop_signalcatcher.h"
SignalCatcher::SignalCatcher(sf::SoundBuffer* snd_buffer, int samplesCount, int sampleRate = 44100): samplesCount(samplesCount)
{
this->recorder = new MyRecorder(samplesCount);
this->cat_buffer = *snd_buffer;
this->sampleRate = sampleRate;
QObject::connect(recorder, SIGNAL(processFinished()),
this, SLOT(onProcessFinished()));
}
bool SignalCatcher::start()
{
if (sf::SoundRecorder::CanCapture())
{
recorder->Start();
return true;
}
else {
return false;
}
}
bool SignalCatcher::stop()
{
recorder->Stop();
this->cat_buffer = recorder->GetBuffer();
qDebug() << "stopped";
return true;
}
bool MyRecorder::OnProcessSamples(const sf::Int16* Samples, std::size_t SamplesCount) {
SamplesCount = this->samplesCount;
emit processFinished();
return true;
}
MyRecorder::MyRecorder(int samplesCount) {
this->samplesCount = samplesCount;
}
MyRecorder::MyRecorder() {
this->samplesCount = 44100;
}
void SignalCatcher::onProcessFinished() {
qDebug() << "finished";
this->stop();
}
void SignalCatcher::setSampleRate(int smprate)
{
this->sampleRate = smprate;
}
int SignalCatcher::getSampleRate()
{
return sampleRate;
}