Skip to content

Commit

Permalink
Fix crash when sample count is too low
Browse files Browse the repository at this point in the history
  • Loading branch information
Vort committed May 15, 2022
1 parent 985928f commit 7e3c139
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions SoundReceiver/Detector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ public byte[] Detect(short[] signal, List<double> bitLevels, out double? snr, ou
double[] d23 = Integrate(d21, integrateBitLen2);
if (debug) SaveWav("d23.wav", SignalDtoS(d23));

double[] d24 = new double[d22.Length - integrateBitLen2];
double[] d25 = new double[d22.Length - integrateBitLen2];
int d24len = d22.Length - integrateBitLen2;
if (d24len <= 0)
throw new SignalException("Not enough samples");
double[] d24 = new double[d24len];
double[] d25 = new double[d24len];
MinMax(d22, integrateBitLen2, d24, d25);

if (debug) SaveWav("d24.wav", SignalDtoS(d24));
Expand Down Expand Up @@ -200,7 +203,6 @@ public byte[] Detect(short[] signal, List<double> bitLevels, out double? snr, ou


int deltaRange = Convert.ToInt32(estBitlen * 2);
//int deltaRange = 0;

int eqSize = (int)(10 * estBitlen) | 1;
Complex[] d3 = new Complex[signalEndFine - signalStartFine +
Expand Down
4 changes: 3 additions & 1 deletion SoundReceiver/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void StartButton_Click(object sender, RoutedEventArgs e)

// WaveIn needs to be reinitialized each time:
// https://github.com/naudio/NAudio/issues/49#issuecomment-280446686
waveIn?.Dispose();
waveIn = new WaveIn();
waveIn.WaveFormat = new WaveFormat(44100, 1);
waveIn.DataAvailable += waveIn_DataAvailable;
Expand All @@ -80,6 +81,8 @@ void StartButton_Click(object sender, RoutedEventArgs e)
{
if (ex.Result == MmResult.BadDeviceId)
MessageTextBox.Text = "[Recording device is not found]";
else
MessageTextBox.Text = $"[Recording error: {ex.Result}]";
}
}

Expand All @@ -88,7 +91,6 @@ void StopButton_Click(object sender, RoutedEventArgs e)
createWavFile = Keyboard.Modifiers.HasFlag(ModifierKeys.Control);

waveIn.StopRecording();
waveIn = null;
StopButton.IsEnabled = false;
}

Expand Down

0 comments on commit 7e3c139

Please sign in to comment.