//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "GlobalUnit.h"
#include "MidiTrack.h"
#include "AudioTrack.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

//---------------------------------------------------------------------------
bool IsTrackMute( int iType, int iTrack )
{
	TBaseTrack* pBase = iType==ST_MIDI ? (TBaseTrack*)g_aMidiTracks[iTrack]
										: (TBaseTrack*)g_aAudioTracks[iTrack];

	if( pBase->IsMute() )
		return true;
	else if( !pBase->IsSolo() ){
		for( int i=0; i<g_aMidiTracks.size(); i++ ){
			if( g_aMidiTracks[i]->IsSolo() )
				return true;
		}

		int iATStart = g_iPreStage==0 ? 0 : 1,
			iATEnd = g_iPreStage==0 ? 1 : g_aAudioTracks.size();

		for( int i=iATStart; i<iATEnd; i++ ){
			if( g_aAudioTracks[i]->IsSolo() )
				return true;
		}
	}

	return false;
}
//---------------------------------------------------------------------------

void SetTrackParams( bool bSolo )
{
	vector<TBaseTrack*> aSoloTracks;

	int iATStart = g_iPreStage==0 ? 0 : 1,
		iATEnd = g_iPreStage==0 ? 1 : g_aAudioTracks.size();

	if( bSolo ){
		for( int i=iATStart; i<iATEnd; i++ ){
			if( g_aAudioTracks[i]->IsSolo() )
				aSoloTracks.push_back( g_aAudioTracks[i] );
		}
		for( int i=0; i<g_aMidiTracks.size(); i++ ){
			if( g_aMidiTracks[i]->IsSolo() )
				aSoloTracks.push_back( g_aMidiTracks[i] );
		}
	}

	for( int i=iATStart; i<iATEnd; i++ ){
		TAudioTrack* pAT = g_aAudioTracks[i];

		if( pAT->IsMute() || !aSoloTracks.empty() && !pAT->IsSolo() )
			f_MM_SetAudioTrackParam( i, 0, 0 );
		else
			f_MM_SetAudioTrackParam( i, 0, (float)(pAT->volume / 100.0) );

		f_MM_SetAudioTrackParam( i, 1, (float)(pAT->pan / 100.0) );
		f_MM_SetAudioTrackParam( i, 2, (float)(pAT->gain / 100.0) );
	}
	for( int i=0; i<g_aMidiTracks.size(); i++ ){
		TMidiTrack* pMT = g_aMidiTracks[i];

		if( pMT->IsMute() || !aSoloTracks.empty() && !pMT->IsSolo() )
			f_MM_SetMidiTrackParam( pMT->file, pMT->chno, 0, 0 );
		else{
			f_MM_SetMidiTrackParam( pMT->file, pMT->chno, 0, pMT->volume );
		}
	}
}
//---------------------------------------------------------------------------

void ChangeTrackState( int iType, int iTrack, int iState, bool& bSet, bool& bUnset )
{
	TBaseTrack* pBaseTrk = iType==ST_AUDIO ? (TBaseTrack*)g_aAudioTracks[iTrack]
											: (TBaseTrack*)g_aMidiTracks[iTrack];
	int iATStart = g_iPreStage==0 ? 0 : 1,
		iATEnd = g_iPreStage==0 ? 1 : g_aAudioTracks.size();

	bSet = !(pBaseTrk->state & iState);

	vector<TBaseTrack*> aSoloTracks;
	for( int i=iATStart; i<iATEnd; i++ ){
		if( g_aAudioTracks[i]->IsSolo() )
			aSoloTracks.push_back( g_aAudioTracks[i] );
	}
	for( int i=0; i<g_aMidiTracks.size(); i++ ){
		if( g_aMidiTracks[i]->IsSolo() )
			aSoloTracks.push_back( g_aMidiTracks[i] );
	}

	if( aSoloTracks.empty() ){
		if( iState==TRACK_MUTE ){
			if( iType==ST_AUDIO ){
				f_MM_SetAudioTrackParam( iTrack, 0, bSet ? 0 : pBaseTrk->volume / 100.0 );
			}
			else{
				TMidiTrack* pMT = (TMidiTrack*)pBaseTrk;
				f_MM_SetMidiTrackParam( pMT->file, pMT->chno, 0, bSet ? 0 : pMT->volume );
			}

			pBaseTrk->Mute( bSet );
		}
		else{
			for( int i=iATStart; i<iATEnd; i++ ){
				if( g_aAudioTracks[i]==pBaseTrk ){
					if( pBaseTrk->IsMute() ){
						f_MM_SetAudioTrackParam( i, 0, pBaseTrk->volume / 100.0 );
						pBaseTrk->Mute( false );
						bUnset = true;
					}
				}
				else{
					f_MM_SetAudioTrackParam( i, 0, 0 );
				}
			}

			for( int i=0; i<g_aMidiTracks.size(); i++ ){
				TMidiTrack* pMT = g_aMidiTracks[i];
				if( pMT==pBaseTrk ){
					if( pBaseTrk->IsMute() ){
						f_MM_SetMidiTrackParam( pMT->file, pMT->chno, 0, pMT->volume );

						pMT->Mute( false );
						bUnset = true;
					}
				}
				else{
					f_MM_SetMidiTrackParam( pMT->file, pMT->chno, 0, 0 );
					DebugPrint( "Midi mute file %d, chno %d\n", pMT->file, pMT->chno );
				}
			}

			pBaseTrk->Solo();
        }
	}
	else if( pBaseTrk->IsSolo() ){
		if( iState==TRACK_MUTE || aSoloTracks.size()>1 ){
			if( iType==ST_AUDIO )
				f_MM_SetAudioTrackParam( iTrack, 0, 0 );
			else{
				TMidiTrack* pMT = (TMidiTrack*)pBaseTrk;
				f_MM_SetMidiTrackParam( pMT->file, pMT->chno, 0, 0 );
			}

			if( iState==TRACK_MUTE ){
				pBaseTrk->Mute();
				bUnset = true;
			}
		}

		if( aSoloTracks.size()==1 ){
			for( int i=iATStart; i<iATEnd; i++ ){
				if( !g_aAudioTracks[i]->IsMute() ){
					f_MM_SetAudioTrackParam( i, 0, g_aAudioTracks[i]->volume / 100.0 );
				}
			}
			for( int i=0; i<g_aMidiTracks.size(); i++ ){
				TMidiTrack* pMT = g_aMidiTracks[i];
				if( !pMT->IsMute() ){
					f_MM_SetMidiTrackParam( pMT->file, pMT->chno, 0, pMT->volume );
				}
			}
		}

		pBaseTrk->Solo( false );
	}
	else{
		if( iState==TRACK_MUTE ){
			pBaseTrk->Mute( bSet );
		}
		else{
			if( iType==ST_AUDIO ){
				f_MM_SetAudioTrackParam( iTrack, 0, bSet ? pBaseTrk->volume / 100.0 : 0 );
			}
			else{
				TMidiTrack* pMT = (TMidiTrack*)pBaseTrk;
				f_MM_SetMidiTrackParam( pMT->file, pMT->chno, 0, bSet ? pMT->volume : 0 );
			}

			pBaseTrk->Solo( bSet );

			if( bSet && pBaseTrk->IsMute() ){
				pBaseTrk->Mute( false );
				bUnset = true;
			}
        }
	}
}
//---------------------------------------------------------------------------

