Code: Select all
Structure wavfileheader
;// RIFF Header
riff_header.l; // Contains "RIFF"
wav_size.l ; // Size of the wav portion of the file, which follows the first 8 bytes. File size - 8
Wav_header.l ; // Contains "WAVE"
;// Format Header
fmt_header.l; // Contains "fmt " (includes trailing space)
fmt_chunk_size.l; // Should be 16 for PCM
audio_format.w ; // Should be 1 for PCM. 3 for IEEE Float
num_channels.w ;
sample_rate.l ;
byte_rate.l ; // Number of bytes per second. sample_rate * num_channels * Bytes Per Sample
sample_alignment.w; // num_channels * Bytes Per Sample
bit_depth.w ; // Number of bits per sample
;// Data
data_header.l; // Contains "data"
data_bytes.l ; // Number of bytes in data. Number of samples * num_channels * sample byte size
;// uint8_t bytes[]; // Remainder of wave file is bytes
EndStructure
EnableExplicit
Procedure Wav_OpenFile(sFile.s,*swavfileheader.wavfileheader)
Protected hFile = ReadFile(#PB_Any,sFile,#PB_File_SharedRead)
If hFile = 0:ProcedureReturn 0 : EndIf
ReadData(hFile,*swavfileheader,SizeOf(wavfileheader))
While *swavfileheader\data_header <> $61746164
FileSeek(hFile,*swavfileheader\data_bytes,#PB_Relative)
If ReadData(hFile,@*swavfileheader\data_header,8) = 0 :CloseFile(hFile):ProcedureReturn 0 : EndIf
Wend
ProcedureReturn hFile
EndProcedure
Procedure Wav_CreateFile(sFile.s,*swavfileheader.wavfileheader)
Protected hFile = CreateFile(#PB_Any,sFile)
If hFile = 0:ProcedureReturn 0 : EndIf
WriteData(hFile,*swavfileheader,SizeOf(wavfileheader))
ProcedureReturn hFile
EndProcedure
Structure WavListInfo
ID.l
Qstartpos.q
QSize.q
EndStructure
Procedure Wav_MergeMultiTrackCreate(List slFile.s(), sFile.s) ; only for files have the same format
If ListSize(slFile()) = 0:ProcedureReturn 0:EndIf
FirstElement(slFile())
Protected swavfileheader.wavfileheader
Protected hfile = Wav_OpenFile(slFile(),swavfileheader)
If hfile = 0:ProcedureReturn 0:EndIf
CloseFile(hfile)
Protected hfallwav = Wav_CreateFile(sFile,swavfileheader)
If hfallwav = 0:ProcedureReturn 0:EndIf
Protected *pWavListInfo.WavListInfo = AllocateMemory(SizeOf(WavListInfo) * ListSize(slFile()))
Protected *psWavListInfo = *pWavListInfo
Protected qAllSize.q, pmemoir,Id
ForEach slFile()
hfile = Wav_OpenFile(slFile(),swavfileheader)
pmemoir = AllocateMemory(swavfileheader\data_bytes)
ReadData(hfile,pmemoir,swavfileheader\data_bytes)
CloseFile(hfile)
*pWavListInfo\ID = Id
*pWavListInfo\QSize = swavfileheader\data_bytes
*pWavListInfo\Qstartpos = SizeOf(wavfileheader) + qAllSize
*pWavListInfo + SizeOf(WavListInfo)
Id + 1
qAllSize + swavfileheader\data_bytes
WriteData(hfallwav,pmemoir,swavfileheader\data_bytes)
FreeMemory(pmemoir)
Next
WriteData(hfallwav,*psWavListInfo,MemorySize(*psWavListInfo))
FreeMemory(*psWavListInfo)
;fix wav header
FileSeek(hfallwav,0)
swavfileheader\wav_size = 36+qAllSize
swavfileheader\data_bytes = qAllSize
WriteData(hfallwav,swavfileheader,SizeOf(wavfileheader))
CloseFile(hfallwav)
EndProcedure
Procedure Wav_MergeMultiTrackPlay( sFile.s, ID.l) ; ID of sound 0 or 1 ..
Protected swavfileheader.wavfileheader
Protected hfile = Wav_OpenFile(sFile,swavfileheader)
FileSeek(hfile,swavfileheader\data_bytes, #PB_Relative)
Protected pWavListInfo.WavListInfo
Protected True = 0
While ReadData(hfile,pWavListInfo,SizeOf(WavListInfo))
If pWavListInfo\ID = ID
True= 1
Break
EndIf
Wend
If True = 1
FileSeek(hfile,pWavListInfo\Qstartpos)
Protected pmemoir = AllocateMemory(pWavListInfo\QSize + SizeOf(wavfileheader))
ReadData(hfile,pmemoir+ SizeOf(wavfileheader),pWavListInfo\QSize)
swavfileheader\wav_size = 36+pWavListInfo\QSize
swavfileheader\data_bytes = pWavListInfo\QSize
CopyMemory(swavfileheader, pmemoir,SizeOf(wavfileheader))
InitSound()
CatchSound(0,pmemoir)
PlaySound(0)
EndIf
CloseFile(hfile)
ProcedureReturn 0
EndProcedure
;select wav files
Define sSelectfile.s = OpenFileRequester("Select Wave files..","","Wav file|*.wav",0, #PB_Requester_MultiSelection)
If sSelectfile = "":End 1:EndIf
NewList slFile.s()
While sSelectfile
AddElement(slFile())
slFile() = sSelectfile
sSelectfile = NextSelectedFileName()
Wend
Wav_MergeMultiTrackCreate(slFile(),"WavData.Wav")
Wav_MergeMultiTrackPlay("WavData.Wav",4) ; play 4 sound
MessageRequester("","")