public Task <bool> LoadDataAsync(CancellationToken token = new CancellationToken()) { return(Task.Factory.StartNew(() => { try { if (string.IsNullOrEmpty(Filename)) { _samples = null; SampleCount = 0; Max = 0; Length = TimeSpan.Zero; Loading = false; IsEmpty = true; return false; } IsEmpty = false; Console.WriteLine($"- Reading {Filename}"); _samples = new SampleBuffer(Filename); SampleRate = _samples.SampleRate; Length = _samples.Length; token.ThrowIfCancellationRequested(); _samples.Analyze(); // We don't care about ones where the samples are all equal if (Math.Abs(_samples.Min - _samples.Max) < 0.0001) { Console.WriteLine($"- {Filename} is silent"); // So we skip steps here _samples.Dispose(); _samples = null; SampleCount = 0; Max = 0; Loading = false; return false; } SampleCount = _samples.Count; token.ThrowIfCancellationRequested(); Max = Math.Max(Math.Abs(_samples.Max), Math.Abs(_samples.Min)); Console.WriteLine($"- Peak sample amplitude for {Filename} is {Max}"); Loading = false; return true; } catch (TaskCanceledException) { // Blank out if cancelled Max = 0; SampleRate = 0; Length = TimeSpan.Zero; _samples?.Dispose(); _samples = null; Loading = false; return false; } catch (Exception ex) { ErrorMessage = ex.ToString(); Max = 0; SampleRate = 0; Length = TimeSpan.Zero; _samples?.Dispose(); _samples = null; Loading = false; return false; } finally { Changed?.Invoke(this, false); } }, token)); }
public Task <bool> LoadDataAsync(CancellationToken token = new CancellationToken()) { return(Task.Factory.StartNew(() => { try { ErrorMessage = ""; if (string.IsNullOrEmpty(Filename)) { _samples = null; SampleCount = 0; Max = 0; Length = TimeSpan.Zero; Loading = false; IsEmpty = true; return false; } IsEmpty = false; Console.WriteLine($"- Reading {Filename}"); _samples = new SampleBuffer(Filename, Side, HighPassFilter); SampleRate = _samples.SampleRate; Length = _samples.Length; token.ThrowIfCancellationRequested(); _samples.Analyze(); SampleCount = _samples.Count; token.ThrowIfCancellationRequested(); Max = Math.Max(Math.Abs(_samples.Max), Math.Abs(_samples.Min)); Console.WriteLine($"- Peak sample amplitude for {Filename} is {Max}"); if (string.IsNullOrEmpty(ExternalTriggerFilename)) { // Point at the same SampleBuffer _samplesForTrigger = _samples; } else { _samplesForTrigger = new SampleBuffer(ExternalTriggerFilename, Side, HighPassFilter); } Loading = false; return true; } catch (TaskCanceledException) { // Blank out if cancelled Max = 0; SampleRate = 0; Length = TimeSpan.Zero; _samples?.Dispose(); _samples = null; Loading = false; return false; } catch (Exception ex) { ErrorMessage = ex.ToString(); Max = 0; SampleRate = 0; Length = TimeSpan.Zero; _samples?.Dispose(); _samples = null; Loading = false; return false; } finally { Changed?.Invoke(this, false); } }, token)); }