diff --git a/TuneLab/Audio/AudioEngine.cs b/TuneLab/Audio/AudioEngine.cs index d297fda..ef020e8 100644 --- a/TuneLab/Audio/AudioEngine.cs +++ b/TuneLab/Audio/AudioEngine.cs @@ -17,9 +17,9 @@ internal static class AudioEngine { public static event Action? PlayStateChanged; public static event Action? ProgressChanged; - public static bool IsPlaying => mAudioProvider.IsPlaying; - public static int SamplingRate => mAudioProvider.SamplingRate; - public static double CurrentTime => mAudioProvider.CurrentTime; + public static bool IsPlaying => mAudioProvider!.IsPlaying; + public static int SamplingRate => mAudioProvider!.SamplingRate; + public static double CurrentTime => mAudioProvider!.CurrentTime; public static void Init(IAudioPlaybackHandler playbackHandler) { @@ -49,19 +49,19 @@ public static void Destroy() public static void Play() { - mAudioProvider.IsPlaying = true; + mAudioProvider!.IsPlaying = true; PlayStateChanged?.Invoke(); } public static void Pause() { - mAudioProvider.IsPlaying = false; + mAudioProvider!.IsPlaying = false; PlayStateChanged?.Invoke(); } public static void Seek(double time) { - mAudioProvider.Seek(time); + mAudioProvider?.Seek(time); ProgressChanged?.Invoke(); } @@ -121,7 +121,7 @@ double Amplitude2Db(double amplitude) float[] buffer = new float[sampleWindow * 2]; int position = (CurrentTime * SamplingRate).Ceil(); AudioGraph.AddData(track, position, position + sampleWindow, true, buffer, 0); - for (int i = 0; i < sampleWindow * 2; i = i + 2) { amp[0] = (float)Math.Max(amp[0], Sample2Amplitude(buffer[i])); amp[1] = (float)Math.Max(amp[1], Sample2Amplitude(buffer[i + 1])); }; + for (int i = 0; i < sampleWindow * 2; i += 2) { amp[0] = (float)Math.Max(amp[0], Sample2Amplitude(buffer[i])); amp[1] = (float)Math.Max(amp[1], Sample2Amplitude(buffer[i + 1])); }; } amplitude = new Tuple( Amplitude2Db(amp[0]), //L @@ -212,7 +212,11 @@ public void Read(float[] buffer, int offset, int count) { int position = mGraphPosition; int endPosition = position + count; - mAudioGraph.MixData(position, endPosition, true, buffer, offset); + try + { + mAudioGraph.MixData(position, endPosition, true, buffer, offset); + } + catch { } mGraphPosition = endPosition; } } @@ -227,16 +231,16 @@ public void Seek(double time) } } - AudioPlayer mAudioPlayer = new(); - AudioGraph mAudioGraph = new(samplingRate); + readonly AudioPlayer mAudioPlayer = new(); + readonly AudioGraph mAudioGraph = new(samplingRate); int mGraphPosition = 0; - object mSeekLockObject = new(); + readonly object mSeekLockObject = new(); } static IAudioPlaybackHandler? mAudioPlaybackHandler; - static AudioProvider mAudioProvider; - static AudioGraph AudioGraph => mAudioProvider.AudioGraph; - static AudioPlayer AudioPlayer => mAudioProvider.AudioPlayer; + static AudioProvider? mAudioProvider; + static AudioGraph AudioGraph => mAudioProvider!.AudioGraph; + static AudioPlayer AudioPlayer => mAudioProvider!.AudioPlayer; static readonly IAudioData?[] mKeySamples = new IAudioData?[MusicTheory.PITCH_COUNT]; } diff --git a/TuneLab/Audio/AudioGraph.cs b/TuneLab/Audio/AudioGraph.cs index 70e87a0..c1c7dee 100644 --- a/TuneLab/Audio/AudioGraph.cs +++ b/TuneLab/Audio/AudioGraph.cs @@ -107,7 +107,7 @@ public double EndTime } - List mTracks = new(); + List mTracks = []; - object mTrackLockObject = new(); + readonly object mTrackLockObject = new(); } diff --git a/TuneLab/Audio/AudioPlayer.cs b/TuneLab/Audio/AudioPlayer.cs index 1adf3e1..12681ab 100644 --- a/TuneLab/Audio/AudioPlayer.cs +++ b/TuneLab/Audio/AudioPlayer.cs @@ -50,7 +50,7 @@ public void AddData(int count, float[] buffer, int offset) int position = 0; } - HashSet mAudioClips = []; + readonly HashSet mAudioClips = []; - object mLockObject = new(); + readonly object mLockObject = new(); }