PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/audio_test/Main.hx

http://github.com/geekrelief/gr_sound
Haxe | 132 lines | 104 code | 20 blank | 8 comment | 14 complexity | 7f5e73546a7d13d29d127ad01ac761b5 MD5 | raw file
  1. import flash.display.Sprite;
  2. import flash.Memory;
  3. import flash.utils.ByteArray;
  4. import flash.utils.Endian;
  5. import flash.events.Event;
  6. import flash.events.MouseEvent;
  7. import flash.system.ApplicationDomain;
  8. import gr.sound.SampleTrackNode;
  9. import gr.sound.SampleTrackManager;
  10. class Main extends Sprite{
  11. static function main(){
  12. var m = new Main();
  13. flash.Lib.current.addChild(m);
  14. }
  15. var stm:SampleTrackManager;
  16. public function new() {
  17. super();
  18. addEventListener(Event.ADDED_TO_STAGE, init);
  19. }
  20. static var BREAK:String = "break";
  21. static var SELECT:String = "select";
  22. //var bytes:ByteArray;
  23. function init(_e:Event):Void {
  24. // Allocate haxe Memory for the SampleTrackManager
  25. //bytes = gr.HaxeMemory.allocate(8192<<3);
  26. // If haxe Memory were allocated elsewhere in your program for other classes, then specify an offset into it
  27. // with at least 16KB memory for sound mixing.
  28. //var soundMemoryOffset:Int = 512;
  29. //stm = new SampleTrackManager(bytes, soundMemoryOffset);
  30. var soundMemoryOffset:Int = 0;
  31. stm = new SampleTrackManager(SampleTrackManager.allocateMemory(), soundMemoryOffset);
  32. stm.registerSample(SampleTrackManager.MUSIC, Break, BREAK); // register a sample on the fx track
  33. stm.registerSample(SampleTrackManager.AMBIENCE, Select, SELECT); // register a sample on the ambience track
  34. stm.init();
  35. createCircle(100, 100, 0xff0000, mdown, mup);
  36. createCircle(300, 100, 0x00ff00, vup, null);
  37. createCircle(500, 100, 0x0000ff, vdown, null);
  38. createCircle(300, 300, 0xff00ff, tdown, null);
  39. createCircle(100, 500, 0xffff00, pdown, pup);
  40. createCircle(300, 500, 0xeeaa22, sdown, null);
  41. stm.musicVolume = 0.5;
  42. }
  43. function createCircle(_x:Float, _y:Float, _color:UInt, _down:MouseEvent->Void, _up:MouseEvent->Void):Void {
  44. var s = new Sprite();
  45. s.graphics.beginFill(_color);
  46. s.graphics.drawCircle(_x, _y, 100);
  47. s.graphics.endFill();
  48. if (_down != null)
  49. s.addEventListener(MouseEvent.MOUSE_DOWN, _down);
  50. if (_up != null)
  51. s.addEventListener(MouseEvent.MOUSE_UP, _up);
  52. addChild(s);
  53. }
  54. var breakStn:SampleTrackNode;
  55. function mdown(_e:MouseEvent):Void {
  56. breakStn = stm.play(BREAK);
  57. breakStn.volume = 0.5; // adjust this instance's volume
  58. breakStn.addEventListener(Event.COMPLETE, onFinished); // listen for when it finishes to play another sample
  59. }
  60. function onFinished(_e:Event):Void {
  61. var stn:SampleTrackNode = cast _e.target;
  62. stn.removeEventListener(Event.COMPLETE, onFinished);
  63. trace("finished "+stn.sample.name);
  64. var fstn = stm.play(SELECT);
  65. trace('fstn '+fstn.id);
  66. }
  67. function mup(_e:MouseEvent):Void {
  68. stm.stopById(breakStn.id);
  69. }
  70. function vup(_e:MouseEvent):Void {
  71. trace('before '+stm.getTrackVolume(SampleTrackManager.MUSIC));
  72. stm.musicVolume += 0.1;
  73. trace('after '+stm.getTrackVolume(SampleTrackManager.MUSIC));
  74. }
  75. function vdown(_e:MouseEvent):Void {
  76. trace('before '+stm.getTrackVolume(SampleTrackManager.MUSIC));
  77. stm.musicVolume -= 0.1;
  78. trace('after '+stm.getTrackVolume(SampleTrackManager.MUSIC));
  79. }
  80. var toggle:Bool;
  81. var tStn:SampleTrackNode;
  82. function tdown(_e:MouseEvent):Void {
  83. if (!toggle) {
  84. for(i in 0...3) { // play 3 sounds synchronized
  85. tStn = stm.play(BREAK, true); // loop
  86. tStn.volume = 1/3.0; // each contributing 1/3 of their volume to mix
  87. }
  88. } else {
  89. stm.stopByName(BREAK); // stop them all on next clicking
  90. }
  91. toggle = !toggle;
  92. }
  93. function pdown(_e:MouseEvent):Void {
  94. stm.pause();
  95. }
  96. function pup(_e:MouseEvent):Void {
  97. stm.unpause();
  98. }
  99. var sStn:SampleTrackNode;
  100. function sdown(_e:MouseEvent):Void {
  101. if (sStn == null || (!stm.isPlayingById(sStn.id))) {
  102. // if not playing, play
  103. sStn = stm.play("select");
  104. } else if (stm.isPlayingById(sStn.id)) {
  105. if (stm.isPausedById(sStn.id)) {
  106. stm.unpauseById(sStn.id);
  107. } else {
  108. stm.pauseById(sStn.id);
  109. }
  110. }
  111. }
  112. }