audio.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. // audio
  2. package al
  3. /*
  4. #cgo pkg-config: allegro_audio-5
  5. #cgo CFLAGS: -I/usr/local/include
  6. #cgo linux LDFLAGS: -lc_nonshared
  7. #include <stdlib.h>
  8. #include <allegro5/allegro.h>
  9. #include <allegro5/allegro_audio.h>
  10. #include "helpers.h"
  11. */
  12. import "C"
  13. import "runtime"
  14. import "unsafe"
  15. // User event type emitted when a stream fragment is ready to be
  16. // refilled with more audio data.
  17. const (
  18. EVENT_AUDIO_STREAM_FRAGMENT = C.ALLEGRO_EVENT_AUDIO_STREAM_FRAGMENT
  19. EVENT_AUDIO_STREAM_FINISHED = C.ALLEGRO_EVENT_AUDIO_STREAM_FINISHED
  20. EVENT_AUDIO_RECORDER_FRAGMENT = C.ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT
  21. )
  22. // Converts wrapper Event pointer to C Allegro audio recorder event
  23. func (self *Event) AUDIO_RECORDER_EVENT() *C.ALLEGRO_AUDIO_RECORDER_EVENT {
  24. return (*C.ALLEGRO_AUDIO_RECORDER_EVENT)(self.toPointer())
  25. }
  26. /*
  27. TODO?
  28. struct ALLEGRO_AUDIO_RECORDER_EVENT
  29. {
  30. _AL_EVENT_HEADER(struct ALLEGRO_AUDIO_RECORDER)
  31. struct ALLEGRO_USER_EVENT_DESCRIPTOR *__internal__descr;
  32. void *buffer;
  33. unsigned int samples;
  34. };
  35. */
  36. type AudioDepth int
  37. const (
  38. AUDIO_DEPTH_INT8 AudioDepth = C.ALLEGRO_AUDIO_DEPTH_INT8
  39. AUDIO_DEPTH_INT16 AudioDepth = C.ALLEGRO_AUDIO_DEPTH_INT16
  40. AUDIO_DEPTH_INT24 AudioDepth = C.ALLEGRO_AUDIO_DEPTH_INT24
  41. AUDIO_DEPTH_UINT8 AudioDepth = C.ALLEGRO_AUDIO_DEPTH_UINT8
  42. AUDIO_DEPTH_UINT16 AudioDepth = C.ALLEGRO_AUDIO_DEPTH_UINT16
  43. AUDIO_DEPTH_UINT24 AudioDepth = C.ALLEGRO_AUDIO_DEPTH_UINT24
  44. AUDIO_DEPTH_FLOAT32 AudioDepth = C.ALLEGRO_AUDIO_DEPTH_FLOAT32
  45. AUDIO_DEPTH_UNSIGNED AudioDepth = C.ALLEGRO_AUDIO_DEPTH_UNSIGNED
  46. )
  47. /*
  48. Speaker configuration (mono, stereo, 2.1, 3, etc). With regards to
  49. * behavior, most of this code makes no distinction between, say, 4.1 and
  50. * 5 speaker setups.. they both have 5 "channels". However, users would
  51. * like the distinction, and later when the higher-level stuff is added,
  52. * the differences will become more important. (v>>4)+(v&0xF) should yield
  53. * the total channel count.
  54. */
  55. type ChannelConf int
  56. const (
  57. CHANNEL_CONF_1 ChannelConf = C.ALLEGRO_CHANNEL_CONF_1
  58. CHANNEL_CONF_2 ChannelConf = C.ALLEGRO_CHANNEL_CONF_2
  59. CHANNEL_CONF_3 ChannelConf = C.ALLEGRO_CHANNEL_CONF_3
  60. CHANNEL_CONF_4 ChannelConf = C.ALLEGRO_CHANNEL_CONF_4
  61. CHANNEL_CONF_5_1 ChannelConf = C.ALLEGRO_CHANNEL_CONF_5_1
  62. CHANNEL_CONF_6_1 ChannelConf = C.ALLEGRO_CHANNEL_CONF_6_1
  63. CHANNEL_CONF_7_1 ChannelConf = C.ALLEGRO_CHANNEL_CONF_7_1
  64. MAX_CHANNELS ChannelConf = C.ALLEGRO_MAX_CHANNELS
  65. )
  66. type PlayMode int
  67. const (
  68. PLAYMODE_ONCE PlayMode = C.ALLEGRO_PLAYMODE_ONCE
  69. PLAYMODE_LOOP PlayMode = C.ALLEGRO_PLAYMODE_LOOP
  70. PLAYMODE_BIDIR PlayMode = C.ALLEGRO_PLAYMODE_BIDIR
  71. )
  72. type MixerQuality int
  73. const (
  74. MIXER_QUALITY_POINT MixerQuality = C.ALLEGRO_MIXER_QUALITY_POINT
  75. MIXER_QUALITY_LINEAR MixerQuality = C.ALLEGRO_MIXER_QUALITY_LINEAR
  76. MIXER_QUALITY_CUBIC MixerQuality = C.ALLEGRO_MIXER_QUALITY_CUBIC
  77. )
  78. const AUDIO_PAN_NONE = -1000.0
  79. type AudioEventType int
  80. const (
  81. ALLEGRO_EVENT_AUDIO_ROUTE_CHANGE = C.ALLEGRO_EVENT_AUDIO_ROUTE_CHANGE
  82. EVENT_AUDIO_INTERRUPTION = C.ALLEGRO_EVENT_AUDIO_INTERRUPTION
  83. EVENT_AUDIO_END_INTERRUPTION = C.ALLEGRO_EVENT_AUDIO_END_INTERRUPTION
  84. )
  85. type Sample struct {
  86. handle *C.ALLEGRO_SAMPLE
  87. }
  88. type SampleId C.ALLEGRO_SAMPLE_ID
  89. type SampleInstance struct {
  90. handle *C.ALLEGRO_SAMPLE_INSTANCE
  91. }
  92. type AudioStream struct {
  93. handle *C.ALLEGRO_AUDIO_STREAM
  94. }
  95. type Mixer struct {
  96. handle *C.ALLEGRO_MIXER
  97. }
  98. type Voice struct {
  99. handle *C.ALLEGRO_VOICE
  100. }
  101. type AudioRecorder struct {
  102. handle *C.ALLEGRO_AUDIO_RECORDER
  103. }
  104. func (self *Sample) toC() *C.ALLEGRO_SAMPLE {
  105. return (*C.ALLEGRO_SAMPLE)(self.handle)
  106. }
  107. func (self *Sample) Destroy() {
  108. if self.handle != nil {
  109. C.al_destroy_sample(self.toC())
  110. }
  111. self.handle = nil
  112. }
  113. func wrapSampleRaw(sample *C.ALLEGRO_SAMPLE) *Sample {
  114. if sample == nil {
  115. return nil
  116. }
  117. return &Sample{sample}
  118. }
  119. func wrapSample(sample *C.ALLEGRO_SAMPLE) *Sample {
  120. self := wrapSampleRaw(sample)
  121. if self != nil {
  122. runtime.SetFinalizer(self, func(me *Sample) { me.Destroy() })
  123. }
  124. return self
  125. }
  126. func createSample(data []byte, samples uint, freq uint, depth AudioDepth, chan_conf ChannelConf) *C.ALLEGRO_SAMPLE {
  127. // don't let allegro free the data, it's owned by Go.
  128. // XXX: copy data here in stead of using the go data???
  129. return C.al_create_sample(unsafe.Pointer(&data[0]), C.uint(samples),
  130. C.uint(freq), C.ALLEGRO_AUDIO_DEPTH(depth),
  131. C.ALLEGRO_CHANNEL_CONF(chan_conf), b2cb(false))
  132. }
  133. // Creates a Sample with a Destroy finalizer set. BEWARE! data must be big enough fort he params
  134. // or this will crash spectacularly. Also, make sure that data doesn't get collected by the GC, or
  135. // trouble will arise.
  136. func CreateSample(data []byte, samples uint, freq uint, depth AudioDepth, chan_conf ChannelConf) *Sample {
  137. return wrapSample(createSample(data, samples, freq, depth, chan_conf))
  138. }
  139. // Returns the frequency of the sample
  140. func (self *Sample) Frequency() uint {
  141. return uint(C.al_get_sample_frequency(self.handle))
  142. }
  143. // Returns the length of the sample
  144. func (self *Sample) Length() uint {
  145. return uint(C.al_get_sample_length(self.handle))
  146. }
  147. // Returns the depth of the sample
  148. func (self *Sample) Depth() uint {
  149. return uint(C.al_get_sample_depth(self.handle))
  150. }
  151. // returns the amount of channels the sample has
  152. func (self *Sample) Channels() uint {
  153. return uint(C.al_get_sample_channels(self.handle))
  154. }
  155. // Returns the raw data pointer of the sample data.
  156. func (self *Sample) DataRaw() unsafe.Pointer {
  157. return (C.al_get_sample_data(self.handle))
  158. }
  159. // Returns the frequency of the sample instance
  160. func (self *SampleInstance) Frequency() uint {
  161. return uint(C.al_get_sample_instance_frequency(self.handle))
  162. }
  163. // Returns the length of the sample instance
  164. func (self *SampleInstance) Length() uint {
  165. return uint(C.al_get_sample_instance_length(self.handle))
  166. }
  167. // Returns the position of the sample instance
  168. func (self *SampleInstance) Position() uint {
  169. return uint(C.al_get_sample_instance_position(self.handle))
  170. }
  171. // Returns the of the sample instance
  172. func (self *SampleInstance) Speed() float32 {
  173. return float32(C.al_get_sample_instance_speed(self.handle))
  174. }
  175. // Returns the of the sample instance
  176. func (self *SampleInstance) Gain() float32 {
  177. return float32(C.al_get_sample_instance_gain(self.handle))
  178. }
  179. // Returns the of the sample instance
  180. func (self *SampleInstance) Pan() float32 {
  181. return float32(C.al_get_sample_instance_pan(self.handle))
  182. }
  183. // Returns the of the sample instance
  184. func (self *SampleInstance) Time() float32 {
  185. return float32(C.al_get_sample_instance_time(self.handle))
  186. }
  187. // Returns the depth of the sample instance
  188. func (self *SampleInstance) Depth() AudioDepth {
  189. return AudioDepth(C.al_get_sample_instance_depth(self.handle))
  190. }
  191. // Returns the channel configuration of the sample instance
  192. func (self *SampleInstance) Channels() ChannelConf {
  193. return ChannelConf(C.al_get_sample_instance_channels(self.handle))
  194. }
  195. // Returns the play mode of the sample instance
  196. func (self *SampleInstance) Playmode() PlayMode {
  197. return PlayMode(C.al_get_sample_instance_playmode(self.handle))
  198. }
  199. // Returns wheter or not the sample instance is playing
  200. func (self *SampleInstance) Playing() bool {
  201. return cb2b(C.al_get_sample_instance_playing(self.handle))
  202. }
  203. // Returns wheter or not the sample instance is attached
  204. func (self *SampleInstance) Attached() bool {
  205. return cb2b(C.al_get_sample_instance_attached(self.handle))
  206. }
  207. // Sets the position of the sample instance.
  208. func (self *SampleInstance) SetPosition(val uint) bool {
  209. return cb2b(C.al_set_sample_instance_position(self.handle, C.uint(val)))
  210. }
  211. // Sets the length of the sample instance.
  212. func (self *SampleInstance) SetLength(val uint) bool {
  213. return cb2b(C.al_set_sample_instance_length(self.handle, C.uint(val)))
  214. }
  215. // Sets the speed of the sample instance.
  216. func (self *SampleInstance) SetSpeed(val float32) bool {
  217. return cb2b(C.al_set_sample_instance_speed(self.handle, C.float(val)))
  218. }
  219. // Sets the gain of the sample instance.
  220. func (self *SampleInstance) SetGain(val float32) bool {
  221. return cb2b(C.al_set_sample_instance_gain(self.handle, C.float(val)))
  222. }
  223. // Sets the pan of the sample instance.
  224. func (self *SampleInstance) SetPan(val float32) bool {
  225. return cb2b(C.al_set_sample_instance_pan(self.handle, C.float(val)))
  226. }
  227. // Sets the play mode of the sample instance.
  228. func (self *SampleInstance) SetPlaymode(val PlayMode) bool {
  229. return cb2b(C.al_set_sample_instance_playmode(self.handle, C.ALLEGRO_PLAYMODE(val)))
  230. }
  231. // Sets the play status of the sample instance.
  232. func (self *SampleInstance) SetPlaying(val bool) bool {
  233. return cb2b(C.al_set_sample_instance_playing(self.handle, b2cb(val)))
  234. }
  235. // Detaches the sample instance from it's player
  236. func (self *SampleInstance) Detach() bool {
  237. return cb2b(C.al_detach_sample_instance(self.handle))
  238. }
  239. // Sets the sample data to use for the sample instance
  240. func (self *SampleInstance) SetSample(val *Sample) bool {
  241. return cb2b(C.al_set_sample(self.handle, val.handle))
  242. }
  243. // Gets the RAW sample data that was linked to the sample instance
  244. func (self *SampleInstance) SampleRaw() *Sample {
  245. return wrapSampleRaw(C.al_get_sample(self.handle))
  246. }
  247. // Plays the sample instance
  248. func (self *SampleInstance) Play() bool {
  249. return cb2b(C.al_play_sample_instance(self.handle))
  250. }
  251. // Stops the sample instannce playback
  252. func (self *SampleInstance) Stop() bool {
  253. return cb2b(C.al_stop_sample_instance(self.handle))
  254. }
  255. func (self *AudioStream) toC() *C.ALLEGRO_AUDIO_STREAM {
  256. return (*C.ALLEGRO_AUDIO_STREAM)(self.handle)
  257. }
  258. // Destroys the audio stream.
  259. func (self *AudioStream) Destroy() {
  260. if self.handle != nil {
  261. C.al_destroy_audio_stream(self.toC())
  262. }
  263. self.handle = nil
  264. }
  265. func wrapAudioStreamRaw(data *C.ALLEGRO_AUDIO_STREAM) *AudioStream {
  266. if data == nil {
  267. return nil
  268. }
  269. return &AudioStream{data}
  270. }
  271. func wrapAudioStream(data *C.ALLEGRO_AUDIO_STREAM) *AudioStream {
  272. self := wrapAudioStreamRaw(data)
  273. if self != nil {
  274. runtime.SetFinalizer(self, func(me *AudioStream) { me.Destroy() })
  275. }
  276. return self
  277. }
  278. // Creates an audio stream, with finalizer installed.
  279. func CreateAudioStream(bufc, samples, freq uint, depth AudioDepth, chan_conf ChannelConf) *AudioStream {
  280. return wrapAudioStream(C.al_create_audio_stream(C.size_t(bufc), C.uint(samples),
  281. C.uint(freq), C.ALLEGRO_AUDIO_DEPTH(depth), C.ALLEGRO_CHANNEL_CONF(chan_conf)))
  282. }
  283. // Creates an audio stream, with NO finalizer installed.
  284. func CreateAudioStreamRaw(bufc, samples, freq uint, depth AudioDepth, chan_conf ChannelConf) *AudioStream {
  285. return wrapAudioStreamRaw(C.al_create_audio_stream(C.size_t(bufc), C.uint(samples),
  286. C.uint(freq), C.ALLEGRO_AUDIO_DEPTH(depth), C.ALLEGRO_CHANNEL_CONF(chan_conf)))
  287. }
  288. // Drains all data from an audio stream
  289. func (self *AudioStream) Drain() {
  290. C.al_drain_audio_stream(self.handle)
  291. }
  292. // Returns the frequency of the audio stream
  293. func (self *AudioStream) Frequency() uint {
  294. return uint(C.al_get_audio_stream_frequency(self.handle))
  295. }
  296. // Returns the length of the audio stream
  297. func (self *AudioStream) Length() uint {
  298. return uint(C.al_get_audio_stream_length(self.handle))
  299. }
  300. // Returns the speed of the audio stream
  301. func (self *AudioStream) Speed() float32 {
  302. return float32(C.al_get_audio_stream_speed(self.handle))
  303. }
  304. // Returns the amount of fragments of the audio stream
  305. func (self *AudioStream) Fragments() float32 {
  306. return float32(C.al_get_audio_stream_fragments(self.handle))
  307. }
  308. // Returns the amount of available fragments of the audio stream
  309. func (self *AudioStream) AvailableFragments() float32 {
  310. return float32(C.al_get_available_audio_stream_fragments(self.handle))
  311. }
  312. // Returns the gain of the audio stream
  313. func (self *AudioStream) Gain() float32 {
  314. return float32(C.al_get_audio_stream_gain(self.handle))
  315. }
  316. // Returns the pan of the audio stream
  317. func (self *AudioStream) Pan() float32 {
  318. return float32(C.al_get_audio_stream_pan(self.handle))
  319. }
  320. // Returns the depth of the audio stream
  321. func (self *AudioStream) Depth() AudioDepth {
  322. return AudioDepth(C.al_get_audio_stream_depth(self.handle))
  323. }
  324. // Returns the channel configuration of the audio stream
  325. func (self *AudioStream) Channels() ChannelConf {
  326. return ChannelConf(C.al_get_audio_stream_channels(self.handle))
  327. }
  328. // Returns the play mode of the audio stream
  329. func (self *AudioStream) Playmode() PlayMode {
  330. return PlayMode(C.al_get_audio_stream_playmode(self.handle))
  331. }
  332. // Returns wheter or not the audio stream is playing
  333. func (self *AudioStream) Playing() bool {
  334. return cb2b(C.al_get_audio_stream_playing(self.handle))
  335. }
  336. // Returns wheter or not the audio stream is attached
  337. func (self *AudioStream) Attached() bool {
  338. return cb2b(C.al_get_audio_stream_attached(self.handle))
  339. }
  340. // Returns an unsafe pointer to the audio stream's fragment
  341. func (self *AudioStream) Fragment() unsafe.Pointer {
  342. return C.al_get_audio_stream_fragment(self.handle)
  343. }
  344. // Sets the speed of the audio stream.
  345. func (self *AudioStream) SetSpeed(val float32) bool {
  346. return cb2b(C.al_set_audio_stream_speed(self.handle, C.float(val)))
  347. }
  348. // Sets the gain of the audio stream.
  349. func (self *AudioStream) SetGain(val float32) bool {
  350. return cb2b(C.al_set_audio_stream_gain(self.handle, C.float(val)))
  351. }
  352. // Sets the pan of the audio stream.
  353. func (self *AudioStream) SetPan(val float32) bool {
  354. return cb2b(C.al_set_audio_stream_pan(self.handle, C.float(val)))
  355. }
  356. // Sets the play mode of the audio stream.
  357. func (self *AudioStream) SetPlaymode(val PlayMode) bool {
  358. return cb2b(C.al_set_audio_stream_playmode(self.handle, C.ALLEGRO_PLAYMODE(val)))
  359. }
  360. // Sets the play status of the audio stream.
  361. func (self *AudioStream) SetPlaying(val bool) bool {
  362. return cb2b(C.al_set_audio_stream_playing(self.handle, b2cb(val)))
  363. }
  364. // Detaches the audio stream from it's player
  365. func (self *AudioStream) Detach() bool {
  366. return cb2b(C.al_detach_sample_instance(self.handle))
  367. }
  368. // Sets an unsafe pointer as the the audio stream's fragment
  369. func (self *AudioStream) SetFragment(ptr unsafe.Pointer) bool {
  370. return cb2b(C.al_set_audio_stream_fragment(self.handle, ptr))
  371. }
  372. // Rewinds the audio stream
  373. func (self *AudioStream) Rewind() bool {
  374. return cb2b(C.al_rewind_audio_stream(self.handle))
  375. }
  376. // Seeks to a position in the audio stream, expressed in seconds.
  377. func (self *AudioStream) SeekSeconds(secs float64) bool {
  378. return cb2b(C.al_seek_audio_stream_secs(self.handle, C.double(secs)))
  379. }
  380. // Gets the position in the audio stream, expressed in seconds.
  381. func (self *AudioStream) PositionSeconds() (secs float64) {
  382. return float64(C.al_get_audio_stream_position_secs(self.handle))
  383. }
  384. // Gets the length of the audio stream, expressed in seconds.
  385. func (self *AudioStream) LengthSeconds() (secs float64) {
  386. return float64(C.al_get_audio_stream_length_secs(self.handle))
  387. }
  388. // Sets up a loop in the audio stream, expressed in seconds.
  389. func (self *AudioStream) LoopSeconds(start, end float64) bool {
  390. return cb2b(C.al_set_audio_stream_loop_secs(self.handle, C.double(start), C.double(end)))
  391. }
  392. // Returns the event source to use to listen to events on the audio stream.
  393. func (self *AudioStream) Eventsource() *EventSource {
  394. return wrapEventSourceRaw(C.al_get_audio_stream_event_source(self.handle))
  395. }
  396. // Converts a mixer to it's underlying C pointer
  397. func (self *Mixer) toC() *C.ALLEGRO_MIXER {
  398. return (*C.ALLEGRO_MIXER)(self.handle)
  399. }
  400. // Destroys the mixer.
  401. func (self *Mixer) Destroy() {
  402. if self.handle != nil {
  403. C.al_destroy_mixer(self.toC())
  404. }
  405. self.handle = nil
  406. }
  407. // Wraps a C mixer into a go mixer
  408. func wrapMixerRaw(data *C.ALLEGRO_MIXER) *Mixer {
  409. if data == nil {
  410. return nil
  411. }
  412. return &Mixer{data}
  413. }
  414. // Wraps a C mixer into a go mixer and sets up a finalizer that calls Destroy()
  415. func wrapMixer(data *C.ALLEGRO_MIXER) *Mixer {
  416. self := wrapMixerRaw(data)
  417. if self != nil {
  418. runtime.SetFinalizer(self, func(me *Mixer) { me.Destroy() })
  419. }
  420. return self
  421. }
  422. func createMixer(freq uint, depth AudioDepth, chan_conf ChannelConf) *C.ALLEGRO_MIXER {
  423. return C.al_create_mixer(C.uint(freq), C.ALLEGRO_AUDIO_DEPTH(depth), C.ALLEGRO_CHANNEL_CONF(chan_conf))
  424. }
  425. // Creates a new mixer with no finaliser attached to it.
  426. func CreateMixerRaw(freq uint, depth AudioDepth, chan_conf ChannelConf) *Mixer {
  427. return wrapMixerRaw(createMixer(freq, depth, chan_conf))
  428. }
  429. // Creates a new mixer with a finaliser that will call Destroy attached to it.
  430. func CreateMixer(freq uint, depth AudioDepth, chan_conf ChannelConf) *Mixer {
  431. return wrapMixer(createMixer(freq, depth, chan_conf))
  432. }
  433. // Attaches a sample instance to the given mixer.
  434. func (mixer *Mixer) AttachSampleInstance(stream *SampleInstance) bool {
  435. return cb2b(C.al_attach_sample_instance_to_mixer(stream.handle, mixer.handle))
  436. }
  437. // Attaches the sample instance to the given mixer.
  438. func (stream *SampleInstance) Attach(mixer *Mixer) bool {
  439. return cb2b(C.al_attach_sample_instance_to_mixer(stream.handle, mixer.handle))
  440. }
  441. // Attaches an audio stream to the given mixer.
  442. func (mixer *Mixer) AttachAudioStream(stream *AudioStream) bool {
  443. return cb2b(C.al_attach_audio_stream_to_mixer(stream.handle, mixer.handle))
  444. }
  445. // Attaches the audio stream to the given mixer.
  446. func (stream *AudioStream) Attach(mixer *Mixer) bool {
  447. return cb2b(C.al_attach_audio_stream_to_mixer(stream.handle, mixer.handle))
  448. }
  449. // Attaches a mixer to the given mixer.
  450. func (mixer *Mixer) AttachMixer(stream *Mixer) bool {
  451. return cb2b(C.al_attach_mixer_to_mixer(stream.handle, mixer.handle))
  452. }
  453. // Attaches the given mixer to the latter mixer.
  454. func (stream *Mixer) Attach(mixer *Mixer) bool {
  455. return cb2b(C.al_attach_mixer_to_mixer(stream.handle, mixer.handle))
  456. }
  457. /*
  458. TODO:
  459. ALLEGRO_KCM_AUDIO_FUNC(bool, al_set_mixer_postprocess_callback, (
  460. ALLEGRO_MIXER *mixer,
  461. void (*cb)(void *buf, unsigned int samples, void *data),
  462. void *data));
  463. */
  464. // Returns the frequency of the mixer
  465. func (self *Mixer) Frequency() uint {
  466. return uint(C.al_get_mixer_frequency(self.handle))
  467. }
  468. // Returns the gain of the mixer
  469. func (self *Mixer) Gain() float32 {
  470. return float32(C.al_get_mixer_gain(self.handle))
  471. }
  472. // Returns the depth of the mixer
  473. func (self *Mixer) Depth() AudioDepth {
  474. return AudioDepth(C.al_get_mixer_depth(self.handle))
  475. }
  476. // Returns the channel configuration of the mixer
  477. func (self *Mixer) Channels() ChannelConf {
  478. return ChannelConf(C.al_get_mixer_channels(self.handle))
  479. }
  480. // Returns the quality of the mixer
  481. func (self *Mixer) Quality() MixerQuality {
  482. return MixerQuality(C.al_get_mixer_quality(self.handle))
  483. }
  484. // Returns wheter or not the mixer is playing
  485. func (self *Mixer) Playing() bool {
  486. return cb2b(C.al_get_mixer_playing(self.handle))
  487. }
  488. // Returns wheter or not the mixer is attached
  489. func (self *Mixer) Attached() bool {
  490. return cb2b(C.al_get_mixer_attached(self.handle))
  491. }
  492. // Sets the frequency of the mixer.
  493. func (self *Mixer) SetFrequency(val uint) bool {
  494. return cb2b(C.al_set_mixer_frequency(self.handle, C.uint(val)))
  495. }
  496. // Sets the quality of the mixer.
  497. func (self *Mixer) SetQuality(val MixerQuality) bool {
  498. return cb2b(C.al_set_mixer_quality(self.handle, C.ALLEGRO_MIXER_QUALITY(val)))
  499. }
  500. // Sets the gain of the mixer.
  501. func (self *Mixer) SetGain(val float32) bool {
  502. return cb2b(C.al_set_mixer_gain(self.handle, C.float(val)))
  503. }
  504. // Sets the play status of the mixer.
  505. func (self *Mixer) SetPlaying(val bool) bool {
  506. return cb2b(C.al_set_mixer_playing(self.handle, b2cb(val)))
  507. }
  508. // Detaches the mixer from it's player
  509. func (self *Mixer) Detach() bool {
  510. return cb2b(C.al_detach_mixer(self.handle))
  511. }
  512. // Converts a voice to it's underlying C pointer
  513. func (self *Voice) toC() *C.ALLEGRO_VOICE {
  514. return (*C.ALLEGRO_VOICE)(self.handle)
  515. }
  516. // Destroys the voice.
  517. func (self *Voice) Destroy() {
  518. if self.handle != nil {
  519. C.al_destroy_voice(self.toC())
  520. }
  521. self.handle = nil
  522. }
  523. // Wraps a C voice into a go mixer
  524. func wrapVoiceRaw(data *C.ALLEGRO_VOICE) *Voice {
  525. if data == nil {
  526. return nil
  527. }
  528. return &Voice{data}
  529. }
  530. // Wraps a C voice into a go mixer and sets up a finalizer that calls Destroy()
  531. func wrapVoice(data *C.ALLEGRO_VOICE) *Voice {
  532. self := wrapVoiceRaw(data)
  533. if self != nil {
  534. runtime.SetFinalizer(self, func(me *Voice) { me.Destroy() })
  535. }
  536. return self
  537. }
  538. // creates a C voice
  539. func createVoice(freq uint, depth AudioDepth, chan_conf ChannelConf) *C.ALLEGRO_VOICE {
  540. return C.al_create_voice(C.uint(freq), C.ALLEGRO_AUDIO_DEPTH(depth), C.ALLEGRO_CHANNEL_CONF(chan_conf))
  541. }
  542. // Creates a voice
  543. func CreateVoiceRaw(freq uint, depth AudioDepth, chan_conf ChannelConf) *Voice {
  544. return wrapVoiceRaw(createVoice(freq, depth, chan_conf))
  545. }
  546. // Creates a voice and setsup a finalizer on it that calls Destroy
  547. func CreateVoice(freq uint, depth AudioDepth, chan_conf ChannelConf) *Voice {
  548. return wrapVoice(createVoice(freq, depth, chan_conf))
  549. }
  550. // Attaches a sample instance to the given voice.
  551. func (voice *Voice) AttachSampleInstance(stream *SampleInstance) bool {
  552. return cb2b(C.al_attach_sample_instance_to_voice(stream.handle, voice.handle))
  553. }
  554. // Attaches the sample instance to the given voice.
  555. func (stream *SampleInstance) AttachToVoice(voice *Voice) bool {
  556. return cb2b(C.al_attach_sample_instance_to_voice(stream.handle, voice.handle))
  557. }
  558. // Attaches an audio stream to the given voice.
  559. func (voice *Voice) AttachAudioStream(stream *AudioStream) bool {
  560. return cb2b(C.al_attach_audio_stream_to_voice(stream.handle, voice.handle))
  561. }
  562. // Attaches the audio stream to the given voice.
  563. func (stream *AudioStream) AttachToVoice(voice *Voice) bool {
  564. return cb2b(C.al_attach_audio_stream_to_voice(stream.handle, voice.handle))
  565. }
  566. // Attaches the given mixer to the voice.
  567. func (mixer *Mixer) AttachToVoice(voice *Voice) bool {
  568. return cb2b(C.al_attach_mixer_to_voice(mixer.handle, voice.handle))
  569. }
  570. // Attaches the given voice to the mixer.
  571. func (voice *Voice) AttachMixer(mixer *Mixer) bool {
  572. return cb2b(C.al_attach_mixer_to_voice(mixer.handle, voice.handle))
  573. }
  574. // Detaches the voice.
  575. func (voice *Voice) Detach() {
  576. C.al_detach_voice(voice.handle)
  577. }
  578. /*
  579. ALLEGRO_KCM_AUDIO_FUNC(unsigned int, al_get_voice_frequency, (const ALLEGRO_VOICE *voice));
  580. ALLEGRO_KCM_AUDIO_FUNC(unsigned int, al_get_voice_position, (const ALLEGRO_VOICE *voice));
  581. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_CHANNEL_CONF, al_get_voice_channels, (const ALLEGRO_VOICE *voice));
  582. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_AUDIO_DEPTH, al_get_voice_depth, (const ALLEGRO_VOICE *voice));
  583. ALLEGRO_KCM_AUDIO_FUNC(bool, al_get_voice_playing, (const ALLEGRO_VOICE *voice));
  584. ALLEGRO_KCM_AUDIO_FUNC(bool, al_set_voice_position, (ALLEGRO_VOICE *voice, unsigned int val));
  585. ALLEGRO_KCM_AUDIO_FUNC(bool, al_set_voice_playing, (ALLEGRO_VOICE *voice, bool val));
  586. ALLEGRO_KCM_AUDIO_FUNC(bool, al_install_audio, (void));
  587. ALLEGRO_KCM_AUDIO_FUNC(void, al_uninstall_audio, (void));
  588. ALLEGRO_KCM_AUDIO_FUNC(bool, al_is_audio_installed, (void));
  589. ALLEGRO_KCM_AUDIO_FUNC(uint32_t, al_get_allegro_audio_version, (void));
  590. ALLEGRO_KCM_AUDIO_FUNC(size_t, al_get_channel_count, (ALLEGRO_CHANNEL_CONF conf));
  591. ALLEGRO_KCM_AUDIO_FUNC(size_t, al_get_audio_depth_size, (ALLEGRO_AUDIO_DEPTH conf));
  592. ALLEGRO_KCM_AUDIO_FUNC(bool, al_reserve_samples, (int reserve_samples));
  593. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_MIXER *, al_get_default_mixer, (void));
  594. ALLEGRO_KCM_AUDIO_FUNC(bool, al_set_default_mixer, (ALLEGRO_MIXER *mixer));
  595. ALLEGRO_KCM_AUDIO_FUNC(bool, al_restore_default_mixer, (void));
  596. ALLEGRO_KCM_AUDIO_FUNC(bool, al_play_sample, (ALLEGRO_SAMPLE *data,
  597. float32 gain, float32 pan, float32 speed, ALLEGRO_PLAYMODE loop, ALLEGRO_SAMPLE_ID *ret_id));
  598. ALLEGRO_KCM_AUDIO_FUNC(void, al_stop_sample, (ALLEGRO_SAMPLE_ID *spl_id));
  599. ALLEGRO_KCM_AUDIO_FUNC(void, al_stop_samples, (void));
  600. ALLEGRO_KCM_AUDIO_FUNC(bool, al_register_sample_loader, (const char *ext,
  601. ALLEGRO_SAMPLE *(*loader)(const char *filename)));
  602. ALLEGRO_KCM_AUDIO_FUNC(bool, al_register_sample_saver, (const char *ext,
  603. bool (*saver)(const char *filename, ALLEGRO_SAMPLE *spl)));
  604. ALLEGRO_KCM_AUDIO_FUNC(bool, al_register_audio_stream_loader, (const char *ext,
  605. ALLEGRO_AUDIO_STREAM *(*stream_loader)(const char *filename,
  606. size_t buffer_count, unsigned int samples)));
  607. ALLEGRO_KCM_AUDIO_FUNC(bool, al_register_sample_loader_f, (const char *ext,
  608. ALLEGRO_SAMPLE *(*loader)(ALLEGRO_FILE *fp)));
  609. ALLEGRO_KCM_AUDIO_FUNC(bool, al_register_sample_saver_f, (const char *ext,
  610. bool (*saver)(ALLEGRO_FILE *fp, ALLEGRO_SAMPLE *spl)));
  611. ALLEGRO_KCM_AUDIO_FUNC(bool, al_register_audio_stream_loader_f, (const char *ext,
  612. ALLEGRO_AUDIO_STREAM *(*stream_loader)(ALLEGRO_FILE *fp,
  613. size_t buffer_count, unsigned int samples)));
  614. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_SAMPLE *, al_load_sample, (const char *filename));
  615. ALLEGRO_KCM_AUDIO_FUNC(bool, al_save_sample, (const char *filename,
  616. ALLEGRO_SAMPLE *spl));
  617. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_AUDIO_STREAM *, al_load_audio_stream, (const char *filename,
  618. size_t buffer_count, unsigned int samples));
  619. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_SAMPLE *, al_load_sample_f, (ALLEGRO_FILE* fp, const char *ident));
  620. ALLEGRO_KCM_AUDIO_FUNC(bool, al_save_sample_f, (ALLEGRO_FILE* fp, const char *ident,
  621. ALLEGRO_SAMPLE *spl));
  622. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_AUDIO_STREAM *, al_load_audio_stream_f, (ALLEGRO_FILE* fp, const char *ident,
  623. size_t buffer_count, unsigned int samples));
  624. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_EVENT_SOURCE *, al_get_audio_event_source, (void));
  625. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_AUDIO_RECORDER *, al_create_audio_recorder, (size_t fragment_count,
  626. unsigned int samples, unsigned int freq, AudioDepth depth, ALLEGRO_CHANNEL_CONF chan_conf));
  627. ALLEGRO_KCM_AUDIO_FUNC(bool, al_start_audio_recorder, (ALLEGRO_AUDIO_RECORDER *r));
  628. ALLEGRO_KCM_AUDIO_FUNC(void, al_stop_audio_recorder, (ALLEGRO_AUDIO_RECORDER *r));
  629. ALLEGRO_KCM_AUDIO_FUNC(bool, al_is_audio_recorder_recording, (ALLEGRO_AUDIO_RECORDER *r));
  630. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_EVENT_SOURCE *, al_get_audio_recorder_event_source,
  631. (ALLEGRO_AUDIO_RECORDER *r));
  632. ALLEGRO_KCM_AUDIO_FUNC(ALLEGRO_AUDIO_RECORDER_EVENT *, al_get_audio_recorder_event, (ALLEGRO_EVENT *event));
  633. ALLEGRO_KCM_AUDIO_FUNC(void, al_destroy_audio_recorder, (ALLEGRO_AUDIO_RECORDER *r));
  634. */