audiodata.monkey2 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. Namespace std.audio
  2. #rem monkeydoc The AudioData class.
  3. #end
  4. Class AudioData Extends Resource
  5. #rem monkeydoc Creates a new AudioData object
  6. #end
  7. Method New( length:Int,format:AudioFormat,hertz:Int )
  8. _length=length
  9. _format=format
  10. _hertz=hertz
  11. _owned=True
  12. _data=Cast<UByte Ptr>( GCMalloc( BytesPerSample( format )*length ) )
  13. End
  14. Method New( length:Int,format:AudioFormat,hertz:Int,data:Void Ptr )
  15. _length=length
  16. _format=format
  17. _hertz=hertz
  18. _owned=false
  19. _data=Cast<UByte Ptr>( data )
  20. End
  21. #rem monkeydoc The length, in samples, of the audio.
  22. #end
  23. Property Length:Int()
  24. Return _length
  25. End
  26. #rem monkeydoc The format of the audio.
  27. #end
  28. Property Format:AudioFormat()
  29. Return _format
  30. End
  31. #rem monkeydoc The playback rate of the audio.
  32. #end
  33. Property Hertz:Int()
  34. Return _hertz
  35. End
  36. #rem monkeydoc The duration, in seconds, of the audio.
  37. #end
  38. Property Duration:Double()
  39. Return Double(_length)/Double(_hertz)
  40. End
  41. #rem monkeydoc The actual audio data.
  42. #end
  43. Property Data:UByte Ptr()
  44. Return _data
  45. End
  46. #rem monkeydoc The size, in bytes of the audio data.
  47. #end
  48. Property Size:Int()
  49. Return BytesPerSample( _format ) * _length
  50. End
  51. #rem monkeydoc Sets a sample at a given sample index.
  52. `index` must be in the range [0,Length).
  53. #end
  54. Method SetSample( index:Int,sample:Float,channel:Int=0 )
  55. DebugAssert( index>=0 And index<_length )
  56. Select _format
  57. Case AudioFormat.Mono8
  58. _data[index]=Clamp( sample * 128.0 + 128.0,0.0,255.0 )
  59. Case AudioFormat.Stereo8
  60. _data[index*2+(channel&1)]=Clamp( sample * 128.0 + 128.0,0.0,255.0 )
  61. Case AudioFormat.Mono16
  62. Cast<Short Ptr>( _data )[index]=Clamp( sample * 32768.0,-32768.0,32767.0 )
  63. Case AudioFormat.Stereo16
  64. Cast<Short Ptr>( _data )[index*2+(channel&1)]=Clamp( sample * 32768.0,-32768.0,32767.0 )
  65. End
  66. End
  67. #rem monkeydoc Gets a sample at a given sample index.
  68. `index` must be in the range [0,Length).
  69. #end
  70. Method GetSample:Float( index:Int,channel:Int=0 )
  71. 'Ok, note that this never returns quite +1.0 as there is one less int above 0 than below
  72. 'eg: range of signed ints is [-128,+127]
  73. '
  74. DebugAssert( index>=0 And index<_length )
  75. Select _format
  76. Case AudioFormat.Mono8
  77. Return ( _data[index] - 128.0 ) / 128.0
  78. Case AudioFormat.Stereo8
  79. Return ( _data[index*2+(channel&1)] - 128.0 ) / 128.0
  80. Case AudioFormat.Mono16
  81. Return Cast<Short Ptr>( _data )[index]/32768.0
  82. Case AudioFormat.Stereo16
  83. Return Cast<Short Ptr>( _data )[index*2+(channel&1)]/32768.0
  84. End
  85. Return 0
  86. End
  87. #rem monkeydoc Loads audio data from a file.
  88. The file must be in "wav" or ".ogg" format.
  89. #end
  90. Function Load:AudioData( path:String )
  91. Select ExtractExt( path ).ToLower()
  92. Case ".wav" Return LoadAudioData_WAV( path )
  93. Case ".ogg" Return LoadAudioData_OGG( path )
  94. End
  95. Return Null
  96. End
  97. Protected
  98. #rem monkeydoc @hidden
  99. #end
  100. Method OnDiscard() Override
  101. If _owned GCFree( _data )
  102. _data=Null
  103. End
  104. #rem monkeydoc @hidden
  105. #end
  106. Method OnFinalize() Override
  107. If _owned GCFree( _data )
  108. End
  109. Private
  110. Field _length:Int
  111. Field _format:AudioFormat
  112. Field _hertz:Int
  113. Field _owned:Bool
  114. Field _data:UByte Ptr
  115. End