wavloader.bmx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. Strict
  2. Rem
  3. bbdoc: Audio/WAV loader
  4. about:The WAV loader module provides the ability to load WAV format #{audio samples}.
  5. End Rem
  6. Module BRL.WAVLoader
  7. ModuleInfo "Version: 1.02"
  8. ModuleInfo "Author: Simon Armstrong"
  9. ModuleInfo "License: zlib/libpng"
  10. ModuleInfo "Copyright: Blitz Research Ltd"
  11. ModuleInfo "Modserver: BRL"
  12. Import BRL.AudioSample
  13. Import BRL.EndianStream
  14. Private
  15. Function ReadTag:String( stream:TStream )
  16. Local tag:Byte[4]
  17. If stream.ReadBytes( tag,4 )<>4 Return
  18. Return Chr(tag[0])+Chr(tag[1])+Chr(tag[2])+Chr(tag[3])
  19. End Function
  20. Public
  21. Type TAudioSampleLoaderWAV Extends TAudioSampleLoader
  22. Method LoadAudioSample:TAudioSample( stream:TStream ) Override
  23. stream=LittleEndianStream(stream)
  24. If ReadTag( stream )<>"RIFF" Return
  25. Local w_len=stream.ReadInt()-8
  26. If ReadTag( stream )<>"WAVE" Return
  27. If ReadTag( stream )<>"fmt " Return
  28. Local w_len2=stream.ReadInt()
  29. Local w_comp=stream.ReadShort()
  30. If w_comp<>1 Return
  31. Local w_chans=stream.ReadShort()
  32. Local w_hz=stream.ReadInt()
  33. Local w_bytespersec=stream.ReadInt()
  34. Local w_pad=stream.ReadShort()
  35. Local w_bits=stream.ReadShort()
  36. Local format=0
  37. Select True
  38. Case w_bits=8 And w_chans=1
  39. format=SF_MONO8
  40. Case w_bits=8 And w_chans=2
  41. format=SF_STEREO8
  42. Case w_bits=16 And w_chans=1
  43. format=SF_MONO16LE
  44. Case w_bits=16 And w_chans=2
  45. format=SF_STEREO16LE
  46. Default
  47. Return
  48. End Select
  49. If w_len2>16 stream.SkipBytes( w_len2-16 )
  50. While Not stream.Eof()
  51. Local tag:String=Readtag( stream )
  52. If tag<>"data"
  53. Local sz=stream.ReadInt()
  54. stream.SkipBytes( sz )
  55. Continue
  56. EndIf
  57. Local w_sizebytes=stream.ReadInt()
  58. Local length=w_sizebytes/BytesPerSample[format],hertz=w_hz
  59. Local t:TAudioSample=TAudioSample.Create( length,hertz,format )
  60. stream.ReadBytes t.samples,w_sizebytes
  61. Return t
  62. Wend
  63. End Method
  64. End Type
  65. AddAudioSampleLoader New TAudioSampleLoaderWAV