
unit MIDI
{
	//[i] linksysconst_uint8 ID;
	public const uint8 ID = 0;
	
	//[i] interface_tone tone1;
	public unit tone1
	{
		public link void play( int8 channal, int8 tone, uint8 vol ) {}
		public link void stop( int8 channal, int8 s ) {}
		public link uint8 switch_time( uint8 t ) {}
		public link bool isBusy;
		public link bool isNeed;
	}
	tone1.play = play_tone;
	tone1.stop = stop_tone;
	tone1.switch_time = switch_time;
	tone1.isBusy = #.linker.isBusy;
	tone1.isNeed = #.linker.isNeed;
	
	int8 last;
	
	int8 Channel;
	int8 Vol;
	
	//---------------------------------------------------
	//初始化
	//[i] function_void OS_init;
	public void OS_init()
	{
		last = 0;
		Channel = 1;
		Vol = 100;
	}
	//---------------------------------------------------
	//设置通道
	//[i] function_void SetChannel int32;
	public void SetChannel( int32 c )
	{
		Channel = (int8)(int16)c;
	}
	//---------------------------------------------------
	//设置音色
	//[i] function_void SetTimbre int32 int32;
	public void SetTimbre( int32 t )
	{
		uint32 d;
		d.0(uint8) = (uint)Channel;
		d.8(uint8) = (uint)(int8)t;
		#.linker.SetValue( ID, 0x01, d );
	}
	//---------------------------------------------------
	//设置音量
	//[i] function_void SetVol int32;
	public void SetVol( int32 v )
	{
		Vol = (int8)(int16)v;
	}
	//---------------------------------------------------
	//播放一个音符
	//[i] function_void PlayTone int32;
	public void PlayTone( int32 scale )
	{
		play_tone( Channel, (int8)(int16)scale, Vol );
	}
	//---------------------------------------------------
	//停止一个音符
	//[i] function_void StopTone int32;
	public void StopTone( int32 scale )
	{
		stop_tone( Channel, (int8)(int16)scale );
	}
	//==============================================================
	
	//播放一个音符
	public void play_tone( int8 channel, int8 scale, int8 vol )
	{
		uint32 d;
		
		d.0(uint8) = (uint)channel;
		d.8(uint8) = (uint)last;
		d.16(uint8) = (uint)vol;
		#.linker.SetValue( ID, 0x03, d );
		
		d.0(uint8) = (uint)channel;
		d.8(uint8) = (uint)scale;
		d.16(uint8) = (uint)vol;
		#.linker.SetValue( ID, 0x02, d );
		
		last = scale;
	}
	//停止一个音符
	public void stop_tone( int8 channel, int8 scale )
	{
		uint32 d;
		d.0(uint8) = (uint)channel;
		d.8(uint8) = (uint)scale;
		#.linker.SetValue( ID, 0x03, d );
	}
	uint8 switch_time( uint8 t )
	{
		return t;
	}
}








