#
# Split multichannel PCM stream to channel groups
#

#
# Macro SplitPCM
#
# Application variable:
#   @SplitPCM - defined, return channel description using UCM values
#
# Arguments:
#   Name - PCM name (alsa-lib)
#   Direction - "Playback" or "Capture"
#   [Format] - sample format like S16_LE or S32_LE
#   Channels - application channels
#   HWChannels - hardware channels
#   HWChannelPos0 - channel position (MONO FR FL etc. - see alsa-lib's strings)
#   [HWChannelPos1] - channel position (MONO FR FL etc. - see alsa-lib's strings)
#

DefineMacro.SplitPCM.If.0 {
	Condition {
		Type String
		Empty "${var:@SplitPCM}"
	}
	True {
		If.period_time {
			Condition {
				Type String
				Empty "${var:-SplitPCMPeriodTime}"
			}
			True.Define.__period_time 20000
			False.Define.__period_time "${var:SplitPCMPeriodTime}"
		}
		If.buffer_time {
			Condition {
				Type String
				Empty "${var:-SplitPCMBufferTime}"
			}
			True.Define.__buffer_time 500000
			False.Define.__buffer_time "${var:SplitPCMBufferTime}"
		}
		If.format {
			Condition {
				Type String
				Empty "${var:-__Format}"
			}
			True.Define.__Format S16_LE
		}

		LibraryConfig.pcm.SubstiConfig.pcm."${var:__Name}" {
			@args [ CARD DEV CHN0 ]
			@args {
				CARD.type string
				DEV.type integer
				CHN0.type integer
			}
			ipc_key 6678293
			type dshare
			slave {
				pcm {
					type hw
					card $CARD
					device $DEV
					chmap [ "${var:__HWChannelPos0}" ]
				}
				format "${var:__Format}"
				channels "${evali:$__HWChannels}"
				period_time "${evali:$__period_time}"
				buffer_time "${evali:$__buffer_time}"
			}
			bindings.0 $CHN0
		}

		If.pos1 {
			Condition {
				Type String
				Empty "${var:__HWChannelPos1}"
			}
			False.LibraryConfig.pos1.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:__HWChannelPos1}" ]
			}
		}
		If.pos2 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos2}"
			}
			False.LibraryConfig.pos2.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos2}" ]
			}
		}
		If.pos3 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos3}"
			}
			False.LibraryConfig.pos3.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos3}" ]
			}
		}
		If.pos4 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos4}"
			}
			False.LibraryConfig.pos4.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos4}" ]
			}
		}
		If.pos5 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos5}"
			}
			False.LibraryConfig.pos4.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos5}" ]
			}
		}
		If.pos6 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos6}"
			}
			False.LibraryConfig.pos6.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos6}" ]
			}
		}
		If.pos7 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos7}"
			}
			False.LibraryConfig.pos7.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos7}" ]
			}
		}
		If.pos8 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos8}"
			}
			False.LibraryConfig.pos8.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos8}" ]
			}
		}
		If.pos9 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos9}"
			}
			False.LibraryConfig.pos9.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos9}" ]
			}
		}
		If.pos10 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos10}"
			}
			False.LibraryConfig.pos10.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos10}" ]
			}
		}
		If.pos11 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos11}"
			}
			False.LibraryConfig.pos11.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos11}" ]
			}
		}
		If.pos12 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos12}"
			}
			False.LibraryConfig.pos12.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos12}" ]
			}
		}
		If.pos13 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos13}"
			}
			False.LibraryConfig.pos13.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos13}" ]
			}
		}
		If.pos14 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos14}"
			}
			False.LibraryConfig.pos14.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos14}" ]
			}
		}
		If.pos15 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos15}"
			}
			False.LibraryConfig.pos15.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos15}" ]
			}
		}
		If.pos16 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos16}"
			}
			False.LibraryConfig.pos16.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos16}" ]
			}
		}
		If.pos17 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos17}"
			}
			False.LibraryConfig.pos17.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos17}" ]
			}
		}
		If.pos18 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos18}"
			}
			False.LibraryConfig.pos18.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos18}" ]
			}
		}
		If.pos19 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos19}"
			}
			False.LibraryConfig.pos19.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos19}" ]
			}
		}
		If.pos20 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos20}"
			}
			False.LibraryConfig.pos20.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos20}" ]
			}
		}
		If.pos21 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos21}"
			}
			False.LibraryConfig.pos21.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos21}" ]
			}
		}
		If.pos22 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos22}"
			}
			False.LibraryConfig.pos22.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos22}" ]
			}
		}
		If.pos23 {
			Condition {
				Type String
				Empty "${var:-__HWChannelPos23}"
			}
			False.LibraryConfig.pos23.SubstiConfig.pcm."${var:__Name}" {
				slave.pcm.chmap [ "${var:-__HWChannelPos23}" ]
			}
		}

		If.ch1 {
			Condition {
				Type RegexMatch
				Regex "^([2-9]|[1-9][0-9])$"
				String "${var:__Channels}"
			}
			True.LibraryConfig.ch1.SubstiConfig.pcm."${var:__Name}" {
				@args.3 CHN1
				@args { CHN1.type integer }
				bindings.1 $CHN1
			}
		}

		If.dir {
			Condition {
				Type String
				String1 "${var:__Direction}"
				String2 "Capture"
			}
			True.LibraryConfig.dir.Config.pcm."${var:__Name}".type dsnoop
		}
	}
}

#
# Macro SplitPCMDevice
#
# Application variable:
#   @SplitPCM - defined, return channel description using UCM values
#
# Arguments:
#   Name - PCM name (alsa-lib)
#   Direction - "Playback" or "Capture"
#   Device - hardware PCM device number (optional - default 0)
#   Channels - count of application channels
#   HWChannels - total channels (in hardware)
#   Channel0 - channel index in stream for the destination channel 0
#   [Channel1] - channel index in stream for the destination channel 1 (optional)
#   ChannelPos0 - channel position (MONO FR FL etc. - see alsa-lib's strings
#   [ChannelPos1] - channel position (MONO FR FL etc. - see alsa-lib's strings)
#

DefineMacro.SplitPCMDevice {
	If.0 {
		Condition {
			Type String
			Empty "${var:-__Device}"
		}
		True.Define.__Device "0"
	}
	If.1 {
		Condition {
			Type String
			Empty "${var:@SplitPCM}"
		}
		False {
			Value {
				"${var:__Direction}Channels" "${var:__HWChannels}"
				"${var:__Direction}PCM" "hw:${CardId},${var:__Device}"
				"${var:__Direction}Channel0" "${var:__Channel0}"
				"${var:__Direction}ChannelPos0" "${var:__ChannelPos0}"
			}
			If.ch1 {
				Condition {
					Type String
					Empty "${var:-__Channel1}"
				}
				False.Value {
					"${var:__Direction}Channel1" "${var:__Channel1}"
					"${var:__Direction}ChannelPos1" "${var:__ChannelPos1}"
				}
			}
		}
		True {
			Define.__pcmdev "${var:__Name}:${CardId},${var:__Device},${var:__Channel0}"
			If.ch1 {
				Condition {
					Type String
					Empty "${var:-__Channel1}"
				}
				False.Define.__pcmdev "${var:__pcmdev},${var:__Channel1}"
			}
			Value {
				"${var:__Direction}Channels" "${var:__Channels}"
				"${var:__Direction}PCM" "${var:__pcmdev}"
			}
		}
	}
}
