123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #include "<libc>"
- Namespace std.audio
- Private
- Using std.stream
- Struct WAV_Header
- '
- Field RIFF:Int
- Field len:Int
- Field WAVE:Int
- Field fmt:Int
- Field headerLen:Int
- '
- Field formatTag:Short
- Field numChannels:Short
- Field samplesPerSec:Int
- Field avgBytesPerSec:Int
- Field blockalignment:Short
- Field bitsPerSample:Short
- '
- End
- Function ReadWAV:AudioData( stream:std.stream.Stream )
- Local header:=New WAV_Header
- Local header_sz:=libc.sizeof( header )
-
- If stream.Read( Varptr header,header_sz )<>header_sz Return null
- If header.RIFF<>$46464952 Return Null
- Local format:AudioFormat
-
- If header.numChannels=1 And header.bitsPerSample=8
- format=AudioFormat.Mono8
- Else If header.numChannels=1 And header.bitsPerSample=16
- format=AudioFormat.Mono16
- Else If header.numChannels=2 And header.bitsPerSample=8
- format=AudioFormat.Stereo8
- Else If header.numChannels=2 And header.bitsPerSample=16
- format=AudioFormat.Stereo16
- Else
- Return Null
- Endif
-
- Local skip:=header.headerLen-16
- If skip>0 stream.Skip( skip )
-
- While Not stream.Eof
-
- Local tag:=stream.ReadInt()
- Local size:=stream.ReadInt()
-
- If tag<>$61746164 '"DATA"
- stream.Skip( size )
- Continue
- Endif
-
- Local data:=New AudioData( size/BytesPerSample( format ),format,header.samplesPerSec )
-
- stream.Read( data.Data,size )
-
- Return data
- Wend
-
- Return Null
- End
- Public
- Function LoadAudioData_WAV:AudioData( path:String )
- Local stream:=std.stream.Stream.Open( path,"r" )
- If Not stream Return Null
-
- Local data:=ReadWAV( stream )
-
- stream.Close()
- Return data
- End
|