Skip to content

Commit

Permalink
Prevent acqusition when audio sample rate is less than 44.1 kHz
Browse files Browse the repository at this point in the history
  • Loading branch information
anjaldoshi committed Sep 7, 2024
1 parent 5314dd0 commit 4ef45b2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
31 changes: 28 additions & 3 deletions Source/Processors/FileReader/FileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ FileReader::FileReader() : GenericProcessor ("File Reader"),
m_sysSampleRate (44100),
playbackActive (true),
gotNewFile (true),
loopPlayback (true)
loopPlayback (true),
sampleRateWarningShown (false)
{
/* Add built-in file source (Binary Format) */
supportedExtensions.set ("oebin", 1);
Expand Down Expand Up @@ -500,8 +501,32 @@ void FileReader::updateSettings()
adm.getAudioDeviceSetup (ads);
m_sysSampleRate = ads.sampleRate;
m_bufferSize = ads.bufferSize;

if (m_sysSampleRate < 44100.0)
{
if (! sampleRateWarningShown)
{
LOGE ("File Reader: Sample rate is less than 44100 Hz. Disabling processor.");

if (! headlessMode)
{
AlertWindow::showMessageBox (AlertWindow::WarningIcon,
"[File Reader] Invalid Sample Rate",
"The sample rate of the audio device is less than 44.1 kHz. File Reader is disabled."
"\n\nTry adjusting the sample rate in the audio settings by clicking the 'Latency' button in the Control Panel "
"and setting it to 44.1 kHz or higher. After making this change, please remove and re-add the File Reader to the signal chain.");
}

sampleRateWarningShown = true;
}

isEnabled = false;
return;
}

if (m_bufferSize == 0)
m_bufferSize = 1024;

m_samplesPerBuffer.set (m_bufferSize * (getDefaultSampleRate() / m_sysSampleRate));

bufferA.malloc (currentNumChannels * m_bufferSize * BUFFER_WINDOW_CACHE_SIZE);
Expand Down Expand Up @@ -602,7 +627,7 @@ void FileReader::process (AudioBuffer<float>& buffer)

if (! playbackActive && playbackSamplePos + samplesNeededPerBuffer > stopSample)
{
samplesNeededPerBuffer = int(stopSample - playbackSamplePos);
samplesNeededPerBuffer = int (stopSample - playbackSamplePos);
switchNeeded = true;
}
else
Expand Down Expand Up @@ -745,7 +770,7 @@ void FileReader::readAndFillBufferCache (HeapBlock<float>& cacheBuffer)
// if reached end of file stream
if ((currentSample + samplesToRead) > stopSample)
{
samplesToRead = int(stopSample - currentSample);
samplesToRead = int (stopSample - currentSample);
if (samplesToRead > 0)
input->readData (cacheBuffer + samplesRead * currentNumChannels, samplesToRead);

Expand Down
3 changes: 3 additions & 0 deletions Source/Processors/FileReader/FileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ class FileReader : public GenericProcessor,
/** Flag if a new file has been loaded */
bool gotNewFile;

/** Flag if the invalid sample rate warning is shown */
bool sampleRateWarningShown;

/** Total number of samples played back since most recent start of acquisition */
int64 playbackSamplePos;

Expand Down
23 changes: 23 additions & 0 deletions Source/UI/ControlPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,29 @@ void ControlPanel::startAcquisition (bool recordingShouldAlsoStart)
return;
}


if (audio->getSampleRate() < 44100)
{
playButton->setToggleState (false, dontSendNotification);
recordButton->setToggleState (false, dontSendNotification);

String errorMsg = "Sample rate too low. Unable to start acquisition!";
LOGE (errorMsg);

if (! isConsoleApp)
{
errorMsg += "\n\nThe sample rate must be at least 44.1 kHz to process data. "
"Try changing the sample rate in the audio configuration window (click on the \"Latency\" button in the Control Panel) "
"to a value of 44.1 kHz or higher. If no such option is available, try changing the output device type.";

AlertWindow::showMessageBox (AlertWindow::WarningIcon,
"Acquisition Error!",
errorMsg);
}

return;
}

if (! isConsoleApp && audioEditor->isAudioConfigurationWindowVisible())
{
audioEditor->disable();
Expand Down

0 comments on commit 4ef45b2

Please sign in to comment.