void swap(Sound& first, Sound& second) { second.mLoops.store(first.mLoops.exchange(second.mLoops.load())); // swap the atomic loop counts // store the state of the sound's rtaudio stream bool firstSetUp = first.mRtAudioSetup; bool secondSetUp = second.mRtAudioSetup; // ensure both sound's rtaudio streams are closed first.CloseRtAudioStream(); second.CloseRtAudioStream(); std::swap(first.mSampleRate, second.mSampleRate); std::swap(first.mBufferFrames, second.mBufferFrames); std::swap(first.mNumChannels, second.mNumChannels); std::swap(first.mSamplePosition, second.mSamplePosition); std::swap(first.mSampleDataPtr, second.mSampleDataPtr); std::swap(first.mSoundBuffer, second.mSoundBuffer); if (secondSetUp) { // if the second sound's rtaudio stream was initialised... first.OpenRtAudioStream(first.mNumChannels); // initialise the first sound's rtaudio stream } if (firstSetUp) { second.OpenRtAudioStream(second.mNumChannels); } }