Skip to content

Commit

Permalink
fix: audio source list modified when mix data
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYunPlayer committed Jul 22, 2024
1 parent 1826f3a commit a69fe62
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
32 changes: 18 additions & 14 deletions TuneLab/Audio/AudioEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<double, double>(
Amplitude2Db(amp[0]), //L
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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];
}
4 changes: 2 additions & 2 deletions TuneLab/Audio/AudioGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public double EndTime
}


List<IAudioTrack> mTracks = new();
List<IAudioTrack> mTracks = [];

object mTrackLockObject = new();
readonly object mTrackLockObject = new();
}
4 changes: 2 additions & 2 deletions TuneLab/Audio/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void AddData(int count, float[] buffer, int offset)
int position = 0;
}

HashSet<AudioClip> mAudioClips = [];
readonly HashSet<AudioClip> mAudioClips = [];

object mLockObject = new();
readonly object mLockObject = new();
}

0 comments on commit a69fe62

Please sign in to comment.