 //
-// Copyright (c) 2006 - 2020 Stephen F. Booth <me@sbooth.org>
+// Copyright (c) 2006 - 2021 Stephen F. Booth <me@sbooth.org>
 // See https://github.com/sbooth/SFBAudioEngine/blob/master/LICENSE.txt for license information
 //
 

		}
		else if mFormatID == kAudioFormatAppleLossless {
			var sourceBitDepth: UInt32 = 0;
			switch mFormatFlags  {
			case kAppleLosslessFormatFlag_16BitSourceData:		sourceBitDepth = 16
			case kAppleLosslessFormatFlag_20BitSourceData:		sourceBitDepth = 20
			case kAppleLosslessFormatFlag_24BitSourceData:		sourceBitDepth = 24

		guard isPCM else {
			return nil
		}
		return makeASBDForLinearPCM(sampleRate: mSampleRate, channelsPerFrame: mChannelsPerFrame, validBitsPerChannel: 32, totalBitsPerChannel: 32, isFloat: true, isBigEndian: kAudioFormatFlagIsBigEndian == kAudioFormatFlagsNativeEndian, isNonInterleaved: true)
	}

	/// Resets `self` to the default state
	public mutating func reset() {
		memset(&self, 0, MemoryLayout<AudioStreamBasicDescription>.stride);
	}

}

extension AudioStreamBasicDescription: Equatable {
	public static func == (lhs: AudioStreamBasicDescription, rhs: AudioStreamBasicDescription) -> Bool {
		return
			lhs.mFormatID == rhs.mFormatID &&
			lhs.mFormatFlags == rhs.mFormatFlags &&
			lhs.mSampleRate == rhs.mSampleRate &&
			lhs.mChannelsPerFrame == rhs.mChannelsPerFrame &&
			lhs.mBitsPerChannel == rhs.mBitsPerChannel &&
			lhs.mBytesPerPacket == rhs.mBytesPerPacket &&
			lhs.mFramesPerPacket == rhs.mFramesPerPacket &&
			lhs.mBytesPerFrame == rhs.mBytesPerFrame
	}
}

private func makeLinearPCMFlags(validBitsPerChannel: UInt32, totalBitsPerChannel: UInt32, isFloat float: Bool, isBigEndian bigEndian: Bool, isNonInterleaved nonInterleaved: Bool) -> AudioFormatFlags
{
	return (float ? kAudioFormatFlagIsFloat : kAudioFormatFlagIsSignedInteger) | (bigEndian ? kAudioFormatFlagIsBigEndian : 0) | ((validBitsPerChannel == totalBitsPerChannel) ? kAudioFormatFlagIsPacked : kAudioFormatFlagIsAlignedHigh) | (nonInterleaved ? kAudioFormatFlagIsNonInterleaved : 0);
}

private func makeASBDForLinearPCM(sampleRate: Float64, channelsPerFrame: UInt32, validBitsPerChannel: UInt32, totalBitsPerChannel: UInt32, isFloat float: Bool, isBigEndian bigEndian: Bool, isNonInterleaved nonInterleaved: Bool) -> AudioStreamBasicDescription
{
	var asbd = AudioStreamBasicDescription()

	asbd.mFormatID = kAudioFormatLinearPCM;
	asbd.mFormatFlags = makeLinearPCMFlags(validBitsPerChannel: validBitsPerChannel, totalBitsPerChannel: totalBitsPerChannel, isFloat: float, isBigEndian: bigEndian, isNonInterleaved: nonInterleaved);

	asbd.mSampleRate = sampleRate;
	asbd.mChannelsPerFrame = channelsPerFrame;
