private void QueueNextSound() { Channel playingChannel = Channels[Slot ^ 1]; int newIndex = RNG.Next(0, Sounds.Length); Sound newSound = Sounds[newIndex]; Channel newChannel = System.PlaySound(newSound, paused: true); if (playingChannel != null) { ulong startDelay; uint soundLength; float soundFreq; Sound PlayingSound; playingChannel.GetDelay(out startDelay, out _, out _); PlayingSound = playingChannel.CurrentSound; soundLength = PlayingSound.GetLength(TimeUnit.PCM); soundFreq = playingChannel.Frequency; soundLength *= (uint)OutputRate; soundLength /= (uint)soundFreq; startDelay += soundLength; newChannel.SetDelay(startDelay, 0); } else { uint bufferLen; ulong startDelay; System.GetDSPBufferSize(out bufferLen, out _); newChannel.GetDSPClock(out _, out startDelay); startDelay += 2 * bufferLen; newChannel.SetDelay(startDelay, 0); } float val = newChannel.Frequency; float variation = (float)RNG.NextDouble() * 2f - 1f; val *= 1f - (variation * 0.2f); newChannel.Frequency = val; val = newChannel.Volume; variation = (float)RNG.NextDouble(); val *= 1f - (variation * 0.2f); newChannel.Volume = val; newChannel.Paused = false; Channels[Slot] = newChannel; Slot ^= 1; }