1+ #define NOMINMAX
12#include < imgui.h>
23#include < utils/flog.h>
34#include < module.h>
910#include < filesystem>
1011#include < regex>
1112#include < gui/tuner.h>
13+ #include < algorithm>
14+ #include < stdexcept>
1215
1316#define CONCAT (a, b ) ((std::string(a) + b).c_str())
1417
@@ -121,6 +124,12 @@ class FileSourceModule : public ModuleManager::Instance {
121124 }
122125 try {
123126 _this->reader = new WavReader (_this->fileSelect .path );
127+ if (_this->reader ->getSampleRate () == 0 ) {
128+ _this->reader ->close ();
129+ delete _this->reader ;
130+ _this->reader = NULL ;
131+ throw std::runtime_error (" Sample rate may not be zero" );
132+ }
124133 _this->sampleRate = _this->reader ->getSampleRate ();
125134 core::setInputSampleRate (_this->sampleRate );
126135 std::string filename = std::filesystem::path (_this->fileSelect .path ).filename ().string ();
@@ -130,7 +139,7 @@ class FileSourceModule : public ModuleManager::Instance {
130139 // gui::freqSelect.maxFreq = _this->centerFreq + (_this->sampleRate/2);
131140 // gui::freqSelect.limitFreq = true;
132141 }
133- catch (std::exception e) {
142+ catch (std::exception& e) {
134143 flog::error (" Error: {0}" , e.what ());
135144 }
136145 config.acquire ();
@@ -144,8 +153,8 @@ class FileSourceModule : public ModuleManager::Instance {
144153
145154 static void worker (void * ctx) {
146155 FileSourceModule* _this = (FileSourceModule*)ctx;
147- double sampleRate = _this->reader ->getSampleRate ();
148- int blockSize = sampleRate / 200 .0f ;
156+ double sampleRate = std::max ( _this->reader ->getSampleRate (), ( uint32_t ) 1 );
157+ int blockSize = std::min (( int )( sampleRate / 200 .0f ), ( int )STREAM_BUFFER_SIZE) ;
149158 int16_t * inBuf = new int16_t [blockSize * 2 ];
150159
151160 while (true ) {
@@ -159,8 +168,8 @@ class FileSourceModule : public ModuleManager::Instance {
159168
160169 static void floatWorker (void * ctx) {
161170 FileSourceModule* _this = (FileSourceModule*)ctx;
162- double sampleRate = _this->reader ->getSampleRate ();
163- int blockSize = sampleRate / 200 .0f ;
171+ double sampleRate = std::max ( _this->reader ->getSampleRate (), ( uint32_t ) 1 );
172+ int blockSize = std::min (( int )( sampleRate / 200 .0f ), ( int )STREAM_BUFFER_SIZE) ;
164173 dsp::complex_t * inBuf = new dsp::complex_t [blockSize];
165174
166175 while (true ) {
@@ -214,4 +223,4 @@ MOD_EXPORT void _DELETE_INSTANCE_(void* instance) {
214223MOD_EXPORT void _END_ () {
215224 config.disableAutoSave ();
216225 config.save ();
217- }
226+ }
0 commit comments