Sound.hx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 hxd.App {
  12. var time = 0.;
  13. var slider : h2d.Slider;
  14. var music : hxd.snd.Channel;
  15. override function init() {
  16. var res = if( hxd.res.Sound.supportedFormat(Mp3) || hxd.res.Sound.supportedFormat(OggVorbis) ) hxd.Res.music_loop else null;
  17. if( res != null ) {
  18. trace("Playing "+res);
  19. music = res.play(true);
  20. //music.queueSound(...);
  21. music.onEnd = function() trace("LOOP");
  22. }
  23. slider = new h2d.Slider(300, 10, s2d);
  24. slider.x = 150;
  25. slider.y = 80;
  26. if( music == null ) slider.remove();
  27. slider.onChange = function() {
  28. music.position = slider.value * music.duration;
  29. };
  30. }
  31. override function update(dt:Float) {
  32. time += dt/60;
  33. if( time > 1 ) {
  34. time--;
  35. hxd.Res.sound_fx.play();
  36. engine.backgroundColor = 0xFFFF0000;
  37. } else
  38. engine.backgroundColor = 0;
  39. if( music != null ) {
  40. slider.value = music.position / music.duration;
  41. }
  42. if( hxd.Key.isPressed(hxd.Key.SPACE) ) {
  43. var c = new NoiseChannel();
  44. haxe.Timer.delay(c.stop, 1000);
  45. }
  46. }
  47. static function main() {
  48. hxd.Res.initEmbed();
  49. new Sound();
  50. }
  51. }