So for my own purposes, I pulled together bits and pieces of code, and wrote:
CatchWavAttribute(*WAV.i, Attribute.i) and
LoadWavAttribute(File.s, Attribute.i)
(in the style of CatchSound and LoadSound). All constants and return values are described in the code.
Note: this currently works ONLY with files of the most basic PCM format. (No FLAC, OGG, etc.)
Code: Select all
; +-------------------+-------+
; | WavAttributes.pbi | kenmo |
; +-------------------+-------+
; | 5.02.2011 - Version 1.0
; | 6.01. - Version 1.1 (removed a debug, fixed Samples attribute)
;-
;- WAV Constants
; _Attribute _Value _Description _Units
#WAV_BitDepth = $00 ; Sample depth * bits / sample
#WAV_BitRate = $01 ; Total data rate bits / second
#WAV_ByteDepth = $02 ; Sample depth * bytes / sample
#WAV_ByteRate = $03 ; Total data rate bytes / second
#WAV_Channels = $04 ; Number of channels channels
#WAV_ChunkSize = $05 ; Total audio data size bytes
#WAV_Format = $06 ; File format PCM or Unknown
#WAV_Milliseconds = $07 ; Total audio length milliseconds
#WAV_SampleRate = $08 ; Sample rate * samples / second
#WAV_Samples = $09 ; Number of samples * samples
#WAV_Seconds = $0A ; Total audio length seconds
; * refers to one channel only
; Return Values
#WAV_PCM = 0
#WAV_Unknown = -1
#WAV_NoFile = -2
#WAV_ReadFail = -3
#WAV_NoBuffer = -4
;-
;- WAV Procedures
Procedure.i CatchWavAttribute(*WAV.i, Attribute.i)
Protected Value.i = #WAV_Unknown
If (*Wav)
If ((PeekL(*Wav) = 'FFIR') And (PeekL(*Wav + 12) = ' tmf') And (PeekL(*Wav + 36) = 'atad'))
Select (Attribute)
Case #WAV_BitDepth
Value = PeekU(*WAV + 34)
Case #WAV_BitRate
Value = PeekL(*WAV + 28)<<3
Case #WAV_ByteDepth
Value = PeekU(*WAV + 34)>>3
Case #WAV_ByteRate
Value = PeekL(*WAV + 28)
Case #WAV_Channels
Value = PeekU(*WAV + 22)
Case #WAV_ChunkSize
Value = PeekL(*WAV + 40)
Case #WAV_Format
Value = #WAV_PCM
Case #WAV_Milliseconds
Value = PeekL(*WAV + 40) / (PeekL(*Wav + 28) / 100) * 10
Case #WAV_Samples
Value = PeekL(*WAV + 40) / PeekU(*Wav + 32)
Case #WAV_SampleRate
Value = PeekL(*WAV + 24)
Case #WAV_Seconds
Value = PeekL(*WAV + 40) / PeekL(*Wav + 28)
EndSelect
EndIf
EndIf
ProcedureReturn (Value)
EndProcedure
Procedure.i LoadWavAttribute(File.s, Attribute.i)
Protected Value.i = #WAV_NoFile
Protected FID.i = #Null
Protected *Buffer.i = #Null
If (File)
FID = ReadFile(#PB_Any, File)
If (FID)
*Buffer = AllocateMemory(44)
If (*Buffer)
If (ReadData(FID, *Buffer, 44) = 44)
Value = CatchWavAttribute(*Buffer, Attribute)
Else
Value = #WAV_ReadFail
EndIf
FreeMemory(*Buffer)
Else
Value = #WAV_NoBuffer
EndIf
CloseFile(FID)
Else
Value = #WAV_ReadFail
EndIf
EndIf
ProcedureReturn (Value)
EndProcedure
;-
; EOF
Code: Select all
XIncludeFile "WavAttributes.pbi"
File.s = OpenFileRequester("Open WAV", "", "WAV Files|*.wav|All Files|*.*", 0)
If (File)
Debug "File: " + GetFilePart(File)
Debug ""
Format.i = LoadWavAttribute(File, #WAV_Format)
If (Format = #WAV_Unknown)
Debug "WAV_Format: Unknown"
ElseIf (Format < 0)
Debug "Load Error: " + Str(Format)
Else
Select (Format)
Case #WAV_PCM
Debug "WAV_Format: PCM"
EndSelect
Debug "WAV_SampleRate: " + Str(LoadWavAttribute(File, #WAV_SampleRate ))
Debug "WAV_BitDepth: " + Str(LoadWavAttribute(File, #WAV_BitDepth ))
Debug "WAV_ByteDepth: " + Str(LoadWavAttribute(File, #WAV_ByteDepth ))
Debug "WAV_Channels: " + Str(LoadWavAttribute(File, #WAV_Channels ))
Debug "WAV_BitRate: " + Str(LoadWavAttribute(File, #WAV_BitRate ))
Debug "WAV_ByteRate: " + Str(LoadWavAttribute(File, #WAV_ByteRate ))
Debug "WAV_ChunkSize: " + Str(LoadWavAttribute(File, #WAV_ChunkSize ))
Debug "WAV_Samples: " + Str(LoadWavAttribute(File, #WAV_Samples ))
Debug "WAV_Milliseconds: " + Str(LoadWavAttribute(File, #WAV_Milliseconds))
Debug "WAV_Seconds: " + Str(LoadWavAttribute(File, #WAV_Seconds ))
EndIf
EndIf
Should come in handy once in a while!
* edit: got rid of a Debug that shouldn't have been left in the procedure
* edit June 1, 2011: (fixed Total # Samples bug)