openaltest.monkey2 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Namespace openaltest
  2. #import "<std>"
  3. #import "<mojo>"
  4. #import "<openal>"
  5. #import "fine_morning.wav"
  6. Using std..
  7. Using mojo..
  8. Using openal..
  9. Function ALFormat:ALenum( format:AudioFormat )
  10. Local alFormat:ALenum
  11. Select format
  12. Case AudioFormat.Mono8
  13. alFormat=AL_FORMAT_MONO8
  14. Case AudioFormat.Mono16
  15. alFormat=AL_FORMAT_MONO16
  16. Case AudioFormat.Stereo8
  17. alFormat=AL_FORMAT_STEREO8
  18. Case AudioFormat.Stereo16
  19. alFormat=AL_FORMAT_STEREO16
  20. End
  21. Return alFormat
  22. End
  23. Function ALSourceState:String( src:ALuint )
  24. Local alState:ALint
  25. alGetSourcei( src,AL_SOURCE_STATE,Varptr alState )
  26. Local state:=String( alState )+" ?????"
  27. Select alState
  28. Case AL_INITIAL
  29. state="initial"
  30. Case AL_PLAYING
  31. state="playing"
  32. Case AL_PAUSED
  33. state="paused"
  34. Case AL_STOPPED
  35. state="stopped"
  36. End
  37. Return state
  38. End
  39. Function LoadALBuffer:ALuint( path:String )
  40. Local audio:=AudioData.Load( "asset::fine_morning.wav" )
  41. If Not audio Return 0
  42. Local buf:ALuint
  43. alGenBuffers( 1,Varptr buf )
  44. alBufferData( buf,ALFormat( audio.Format ),audio.Data,audio.Size,audio.Hertz )
  45. audio.Discard()
  46. Return buf
  47. End
  48. Class MyWindow Extends Window
  49. Field bufs:=New ALuint[2]
  50. Field src:ALuint
  51. Field proc:ALuint
  52. Method New()
  53. Local device:=alcOpenDevice( Null )
  54. Assert( device,"Failed to open OpenAL device" )
  55. Local context:=alcCreateContext( device,Null )
  56. Assert( context,"Failed to create OpenAL context" )
  57. Assert( alcMakeContextCurrent( context ) )
  58. bufs[0]=LoadALBuffer( "asset::find_morning.wav" )
  59. bufs[1]=LoadALBuffer( "asset::find_morning.wav" )
  60. Assert( bufs[0] And bufs[1],"Failed to load buffer" )
  61. alGenSources( 1,Varptr src )
  62. alSourceQueueBuffers( src,2,Varptr bufs[0] )
  63. End
  64. Method OnRender( canvas:Canvas ) Override
  65. App.RequestRender()
  66. Local state:=ALSourceState( src )
  67. Local proc:ALint
  68. alGetSourcei( src,AL_BUFFERS_PROCESSED,Varptr proc )
  69. If proc<>Self.proc
  70. Local tmp:ALuint
  71. alSourceUnqueueBuffers( src,1,Varptr tmp )
  72. alSourceQueueBuffers( src,1,Varptr tmp )
  73. Self.proc=proc
  74. Endif
  75. canvas.DrawText( "source state="+state+" (SPACE to change) proc="+proc,0,0 )
  76. If Keyboard.KeyPressed( Key.Space )
  77. Select state
  78. Case "initial","stopped","paused"
  79. alSourcePlay( src )
  80. Case "playing"
  81. alSourcePause( src )
  82. End
  83. Print state+"->"+ALSourceState( src )
  84. Endif
  85. End
  86. End
  87. Function Main()
  88. New AppInstance
  89. New MyWindow
  90. App.Run()
  91. End