Sound.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import hxd.snd.NativeChannel;
  2. class NoiseChannel extends hxd.snd.NativeChannel {
  3. public function new() {
  4. super(4096);
  5. }
  6. override function onSample( buf : haxe.io.Float32Array ) {
  7. for( i in 0...buf.length )
  8. buf[i] = Math.random() * 2 - 1;
  9. }
  10. }
  11. class Sound extends SampleApp {
  12. var time = 0.;
  13. var slider : h2d.Slider;
  14. var music : hxd.snd.Channel;
  15. var musicPosition : h2d.Text;
  16. var beeper:Bool = true;
  17. var pitchFilter : hxd.snd.effect.Pitch;
  18. var pitchShift:Bool = false;
  19. var pitchSlider : h2d.Slider;
  20. override function init() {
  21. super.init();
  22. var res = if( hxd.res.Sound.supportedFormat(Mp3) || hxd.res.Sound.supportedFormat(OggVorbis) ) hxd.Res.music_loop else null;
  23. pitchFilter = new hxd.snd.effect.Pitch();
  24. var lowpass = new hxd.snd.effect.LowPass();
  25. var spatial = new hxd.snd.effect.Spatialization();
  26. if( res != null ) {
  27. trace("Playing "+res);
  28. music = res.play(true);
  29. //music.queueSound(...);
  30. music.onEnd = function() trace("LOOP");
  31. // Use effect processing on the channel
  32. music.addEffect(pitchFilter);
  33. music.addEffect(lowpass);
  34. music.addEffect(spatial);
  35. }
  36. slider = new h2d.Slider(300, 10);
  37. slider.onChange = function() {
  38. music.position = slider.value * music.duration;
  39. };
  40. musicPosition = new h2d.Text(getFont());
  41. // slider.x = 150;
  42. // slider.y = 80;
  43. // if( music == null ) slider.remove();
  44. // slider.onChange = function() {
  45. // music.position = slider.value * music.duration;
  46. // };
  47. // musicPosition.setPosition(460, 80);
  48. addSlider("Global vol", function() { return hxd.snd.Manager.get().masterVolume; }, function(v) { hxd.snd.Manager.get().masterVolume = v; });
  49. addCheck("Beeper", function() { return beeper; }, function(v) { beeper = v; });
  50. addButton("Play noise", function() {
  51. var c = new NoiseChannel();
  52. haxe.Timer.delay(c.stop, 1000);
  53. });
  54. if ( music != null ) {
  55. addCheck("Music mute", function() { return music.mute; }, function(v) { music.mute = v; });
  56. addSlider("Music vol", function() { return music.volume; }, function(v) { music.volume = v; });
  57. var f = new h2d.Flow(fui);
  58. f.horizontalSpacing = 5;
  59. var tf = new h2d.Text(getFont(), f);
  60. tf.text = "Music pos";
  61. tf.maxWidth = 70;
  62. tf.textAlign = Right;
  63. f.addChild(slider);
  64. f.addChild(musicPosition);
  65. pitchSlider = addSlider("Pitch val", function() { return pitchFilter.value; }, function(v) { pitchFilter.value = v; }, 0, 2);
  66. addCheck("Pitch shift", function() { return pitchShift; }, function (v) { pitchShift = v; });
  67. addSlider("Lowpass gain", function() { return lowpass.gainHF; }, function(v) { lowpass.gainHF = v; }, 0, 1);
  68. addText("Spatialization");
  69. addSlider("X", function() { return spatial.position.x; }, function(v) { spatial.position.x = v; }, -10, 10);
  70. addSlider("Y", function() { return spatial.position.y; }, function(v) { spatial.position.y = v; }, -10, 10);
  71. addSlider("Z", function() { return spatial.position.z; }, function(v) { spatial.position.z = v; }, -10, 10);
  72. addText("Spatialization Listener");
  73. var listener = hxd.snd.Manager.get().listener;
  74. addSlider("X", function() { return listener.position.x; }, function (v) { listener.position.x = v; }, -10, 10);
  75. addSlider("Y", function() { return listener.position.y; }, function (v) { listener.position.y = v; }, -10, 10);
  76. addSlider("Z", function() { return listener.position.z; }, function (v) { listener.position.z = v; }, -10, 10);
  77. }
  78. }
  79. override function update(dt:Float) {
  80. if ( beeper ) {
  81. time += dt;
  82. if( time > 1 ) {
  83. time--;
  84. hxd.Res.sound_fx.play();
  85. engine.backgroundColor = 0xFFFF0000;
  86. } else
  87. engine.backgroundColor = 0;
  88. }
  89. if ( pitchShift ) {
  90. pitchFilter.value = Math.max(Math.cos(hxd.Timer.lastTimeStamp / 4) + 1, 0.1);
  91. pitchSlider.value = pitchFilter.value;
  92. pitchSlider.onChange();
  93. }
  94. if( music != null ) {
  95. slider.value = music.position / music.duration;
  96. musicPosition.text = hxd.Math.fmt(music.position) + "/" + hxd.Math.fmt(music.duration);
  97. if( hxd.Key.isPressed(hxd.Key.M) ) {
  98. music.mute = !music.mute;
  99. }
  100. }
  101. if( hxd.Key.isPressed(hxd.Key.SPACE) ) {
  102. var c = new NoiseChannel();
  103. haxe.Timer.delay(c.stop, 1000);
  104. }
  105. }
  106. static function main() {
  107. hxd.Res.initEmbed();
  108. new Sound();
  109. }
  110. }