#Include "windows.bi"
#Include "win/mmsystem.bi"
#Include "xmp.bi"
#Include "crt.bi"


'\\\\\\\\\\\\\\\\ init variables and structure\\\\\\\\\\\\\
		Dim As Any Ptr lpCond=Condcreate()
		Dim Shared As Any Ptr Mutex
		Dim Shared Flag As Byte
		Dim As WAVEHDR hdr
		Dim As HWAVEOUT hWaveOut
		Dim As WAVEFORMATEX wfx
		Dim As xmp_context c
		Dim As xmp_frame_info mi
		Dim As xmp_module_info xmi
		Dim As ZString*XMP_NAME_SIZE szNamemodule, sztypeModule
		Dim Shared As UByte Ptr Buffer,bTempBuf
		
		With wfx
			.wFormatTag = WAVE_FORMAT_PCM
			.nChannels = 2
			.nSamplesPerSec = 44100
			.wBitsPerSample = 16
			.nBlockAlign = .nChannels * (.wBitsPerSample \ 8)
			.nAvgBytesPerSec = .nSamplesPerSec * .nBlockAlign
			.cbSize = SizeOf(WAVEFORMATEX)
		End With
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


'\\\\\\\\\\\\\\\\ Callback procedure\\\\\\\\\\\\\
		Sub waveOutProc (ByVal hDevice As HWAVEOUT, _
			ByVal msg     As UInteger, _
			ByVal lpUser  As Any Ptr, _
			ByVal unused1 As DWORD, _
			ByVal unused2 As DWORD)
			Dim As WAVEHDR Ptr lpHdr
			Select Case As Const msg
				Case WOM_DONE
					lpHdr=Cast(WAVEHDR Ptr,unused1)
					lpHdr->dwLoops=-1
					MutexLock(Mutex)
					flag = 1
					MutexUnLock(Mutex)
			End Select
		End Sub
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\



'\\\\\\\\\\\\\\\\ Create context and load module\\\\\\\\\\\\\
		c = xmp_create_context()
		If (xmp_load_module(c, "modules\vcol_g.it") <> 0) Then
			fprintf(stderr, "can't load module\n")
			Sleep
			End
		EndIf
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


'\\\\\\\\\\\\\\\\ Get info modules\\\\\\\\\\\\\
		xmp_start_player(c, 44100, 0)
		xmp_get_module_info(c,@xmi)
	   printf(!"Name: %s\n", xmi.mod_->name)
	   printf(!"Type: %s\n", xmi.mod_->type)
	   printf(!"Number of patterns: %d\n", xmi.mod_->pat)
	   printf(!"Number of tracks: %d\n", xmi.mod_->trk)
	   printf(!"Number of channels: %d\n", xmi.mod_->chn)
	   printf(!"Number of instruments: %d\n", xmi.mod_->ins)
	   printf(!"Number of samples: %d\n", xmi.mod_->smp)
	   printf(!"Initial speed: %d\n", xmi.mod_->spd)
	   printf(!"Initial BPM: %d\n", xmi.mod_->bpm)
	   printf(!"Length in patterns: %d\n", xmi.mod_->len)
		
		Dim As Integer iSize,itemp
		While (xmp_play_frame(c) = 0)
			xmp_get_frame_info(c, @mi)
			iSize+=mi.buffer_size
			If (mi.loop_count > 0) Then Exit While    '/* exit before looping */
		Wend
		Print "Size and position Module buffer in bytes = ";iSize;"/"
		xmp_end_player(c)
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

'\\\\\\\\\\\\\\\\ Copy data in buffer\\\\\\\\\\\\\
		xmp_start_player(c, 44100, 0)
		buffer = Allocate(iSize)
		bTempBuf = buffer
		While (xmp_play_frame(c) = 0)
			xmp_get_frame_info(c, @mi)
			If (mi.loop_count > 0) Then Exit While    '/* exit before looping */
			memcpy(bTempBuf+itemp, mi.buffer,mi.buffer_size )
			itemp = mi.buffer_size
			bTempBuf += itemp
		Wend
		xmp_end_player(c)
		xmp_release_module(c)       '/* unload module */
		xmp_free_context(c)         '/* destroy the player context */
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


'\\\\\\\\\\\\\\\\ Play \\\\\\\\\\\\\
		With hdr
			.lpData = buffer
			.dwBufferLength = iSize
		End With
		
		waveOutOpen(@hWaveOut,-1,CPtr(LPCWAVEFORMATEX,@wfx), _
						Cast(DWORD,@waveOutProc), _
						Cast(DWORD,lpCond), _
						CALLBACK_FUNCTION)
		
		waveOutPrepareHeader(hWaveOut, @hdr, SizeOf(WAVEHDR))
		waveOutWrite(hWaveOut, @hdr, SizeOf(WAVEHDR))
		
		Dim mmt As MMTIME
		mmt.wType = TIME_BYTES
		While flag = 0
         Sleep(10)
         waveOutGetPosition(hWaveOut,@mmt,SizeOf(mmt))
         Locate 11,52
         Print mmt.cb
		Wend	
		Print
		Print "End track, goodby!"
		Sleep(1000)
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


'\\\\\\\\\\\\\\\\ free resource \\\\\\\\\\\\\
		waveOutBreakLoop(hWaveOut)
		waveOutReset(hWaveOut)
		waveOutUnprepareHeader(hWaveOut, @hdr, SizeOf(WAVEHDR))
		waveOutClose(hWaveOut)
		DeAllocate(buffer)
		CondDestroy(lpCond)
		MutexDestroy(Mutex)
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\