To start, I am using SFML 2.4.2 and GCC 4.9.2 TDM (SJLJ) - 32-bit on Windows 10.
I have been trying to use SFML to process audio samples from my microphone close to real-time and I have been getting incorrect samples and number of samples going to the onProcessSamples function in my recorder class inherited from sf::SoundRecorder (
https://www.sfml-dev.org/tutorials/2.4/audio-recording.php).
In my main file I create a window with a button that, when clicked, starts the recorder with the following code:
SpeechRecorder micInput;
if(SpeechRecorder::isAvailable())
{
std::cout << "Audio capture available." << std::endl;
//Start listener
unsigned int sampleRate = 44100;
micInput.start(sampleRate);
}
In my files for the custom sound recorder I have the class declaration in the .h file:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <math.h>
using namespace std;
using namespace sf;
class SpeechRecorder : public SoundRecorder
{
public:
bool onStart() override;
bool onProcessSamples(const Int16* samples, size_t sampleCount) override;
void onStop() override;
};
Then in my .cpp file I defined the onStart function to set the processing interval to 10 milliseconds:
bool SpeechRecorder::onStart()
{
// Initialize whatever has to be done before the capture starts
setProcessingInterval(sf::milliseconds(10));
// Return true to start playing
return true;
}
Also in the .cpp, onProcessSamples is where I want to be doing the analysis of the audio but this is where I noticed that the values are not what I expected. With a sampling rate of 44100 I believe that each frame of samples should have 441 samples but instead, I get 1378 while the samples themselves are only in the range of about +/-5 when I try talking into the microphone.
bool SpeechRecorder::onProcessSamples(const Int16* samples, size_t sampleCount)
{
// Do something with the new chunk of samples (store them, send them, ...)
std::cout << "Sample count: " << sampleCount << std::endl;
int maxSample = 0;
for(int i(0); i < sampleCount; i++)
{
if (abs(samples[i]) > maxSample)
maxSample = abs(samples[i]);
}
std::cout << "Max sample: " << maxSample << std::endl;
// Return true to continue playing
return true;
}
Is there something preventing onProcessSamples being called every 10 milliseconds and can it be worked around? More importantly, I would appreciate help troubleshooting getting the correct samples from the microphone. I noticed on this topic (
https://en.sfml-dev.org/forums/index.php?topic=23725.0) someone reported a similar range of +/-4. Testing getSamples on a SoundBuffer loaded from a wave file I was able to get correct amplitudes but I am at a loss for why the samples being passed into onProcessSamples are not in the full range of Int16. Would it mean my microphone is not picking up any sound?
I am still kind of new to the site so let me know if I need to supply more information or improve my formatting.