audiosample.bmx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. SuperStrict
  2. Rem
  3. bbdoc: Audio/Audio samples
  4. End Rem
  5. Module BRL.AudioSample
  6. ModuleInfo "Version: 1.04"
  7. ModuleInfo "Author: Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: 1.04 Release"
  12. ModuleInfo "History: ChannelsPerSample array added"
  13. Import BRL.Stream
  14. Import "sample.bmx"
  15. Rem
  16. bbdoc: Audio sample type
  17. end rem
  18. Type TAudioSample
  19. Rem
  20. bbdoc: Byte pointer to sample data
  21. end rem
  22. Field samples:Byte Ptr
  23. Rem
  24. bbdoc: Length, in samples, of the sample data
  25. end rem
  26. Field length:Int
  27. Rem
  28. bbdoc: Sample rate
  29. end rem
  30. Field hertz:Int
  31. Rem
  32. bbdoc: Sample format
  33. end rem
  34. Field format:Int
  35. Rem
  36. bbdoc: Allocated memory in bytes
  37. End Rem
  38. Field capacity:Long
  39. Method Delete()
  40. If capacity>=0 Then MemFree( samples )
  41. End Method
  42. Rem
  43. bbdoc: Copy audio sample
  44. returns: A new audio sample object
  45. end rem
  46. Method Copy:TAudioSample()
  47. Local t:TAudioSample = Create( length,hertz,format )
  48. CopySamples( samples,t.samples,format,length )
  49. Return t
  50. End Method
  51. Rem
  52. bbdoc: Convert audio sample
  53. returns: A new audio sample object in the specified format
  54. end rem
  55. Method Convert:TAudioSample( to_format:Int )
  56. Local t:TAudioSample = Create( length,hertz,to_format )
  57. ConvertSamples( samples,format,t.samples,to_format,length )
  58. Return t
  59. End Method
  60. Rem
  61. bbdoc: Create an audio sample
  62. returns: A new audio sample object
  63. end rem
  64. Function Create:TAudioSample( length:Int,hertz:Int,format:Int )
  65. Local t:TAudioSample = New TAudioSample
  66. Local capacity:Long = length*BytesPerSample[format]
  67. t.samples = MemAlloc( Size_T(capacity) )
  68. t.length = length
  69. t.hertz = hertz
  70. t.format = format
  71. t.capacity = capacity
  72. Return t
  73. End Function
  74. Rem
  75. bbdoc: Create a static audio sample
  76. returns: A new audio sample object that references an existing block of memory
  77. end rem
  78. Function CreateStatic:TAudioSample( samples:Byte Ptr,length:Int,hertz:Int,format:Int )
  79. Local t:TAudioSample = New TAudioSample
  80. t.samples = samples
  81. t.length = length
  82. t.hertz = hertz
  83. t.format = format
  84. t.capacity = -1
  85. Return t
  86. End Function
  87. End Type
  88. Private
  89. Global sample_loaders:TAudioSampleLoader
  90. Public
  91. 'deprecated
  92. Function AddAudioSampleLoader( loader:TAudioSampleLoader )
  93. ' If( loader._succ ) Return
  94. ' loader._succ=sample_loaders
  95. ' sample_loaders=loader
  96. End Function
  97. Rem
  98. bbdoc: Audio sample loader type
  99. about: To create your own audio sample loaders, you should extend this type and
  100. provide a @LoadAudioSample method. To add your audio sample loader to the system,
  101. simply create an instance of it using @New.
  102. end rem
  103. Type TAudioSampleLoader
  104. Field _succ:TAudioSampleLoader
  105. Method New()
  106. _succ = sample_loaders
  107. sample_loaders = Self
  108. End Method
  109. Rem
  110. bbdoc: Load an audio sample
  111. returns: A new audio sample object, or Null if sample could not be loaded
  112. about: Extending types must implement this method.
  113. end rem
  114. Method LoadAudioSample:TAudioSample( stream:TStream ) Abstract
  115. End Type
  116. Rem
  117. bbdoc: Create an audio sample
  118. returns: An audio sample object
  119. about:
  120. @length is the number of samples to allocate for the sample. @hertz is the frequency in samples per second (hz)
  121. the audio sample will be played. @format should be one of:
  122. [ @Format | @Description
  123. * &SF_MONO8 | Mono unsigned 8 bit
  124. * &SF_MONO16LE | Mono signed 16 bit little endian
  125. * &SF_MONO16BE | Mono signed 16 bit big endian
  126. * &SF_STEREO8 | Stereo unsigned 8 bit
  127. * &SF_STEREO16LE | Stereo signed 16 bit little endian
  128. * &SF_STEREO16BE | Stereo signed 16 bit big endian
  129. ]
  130. End Rem
  131. Function CreateAudioSample:TAudioSample( length:Int,hertz:Int,format:Int )
  132. Return TAudioSample.Create( length,hertz,format )
  133. End Function
  134. Rem
  135. bbdoc: Create an audio sample with existing data
  136. returns: An audio sample object that references an existing block of memory
  137. about:
  138. The memory referenced by a static audio sample is not released when the audio sample is
  139. deleted.
  140. See #CreateAudioSample for possile @format values.
  141. End Rem
  142. Function CreateStaticAudioSample:TAudioSample( samples:Byte Ptr,length:Int,hertz:Int,format:Int )
  143. Return TAudioSample.CreateStatic( samples,length,hertz,format )
  144. End Function
  145. Rem
  146. bbdoc: Load an audio sample
  147. returns: An audio sample object
  148. end rem
  149. Function LoadAudioSample:TAudioSample( url:Object )
  150. Local stream:TStream = ReadStream( url )
  151. If Not stream then Return Null
  152. Local pos:Long = stream.Pos()
  153. If pos = -1 Then RuntimeError "Stream is not seekable"
  154. Local sample:TAudioSample
  155. Local loader:TAudioSampleLoader = sample_loaders
  156. While loader
  157. stream.Seek( pos )
  158. Try
  159. sample = loader.LoadAudioSample( stream )
  160. Catch ex:TStreamException
  161. End Try
  162. If sample Then Exit
  163. loader = loader._succ
  164. Wend
  165. stream.Close()
  166. Return sample
  167. End Function