Advertisement
TTFTCUTS

Boyd audio class

Nov 9th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.13 KB | None | 0 0
  1. part of LD35;
  2.  
  3. class Audio {
  4.     static Audio instance;
  5.    
  6.     AudioContext ctx;
  7.     String extension = "ogg";
  8.     GainNode volumeNode;
  9.    
  10.     double volume = 1.0;
  11.     bool muted = false;
  12.    
  13.     RangeInputElement slider;
  14.     DivElement mutebutton;
  15.    
  16.     Audio() {
  17.         instance = this;
  18.         this.ctx = new AudioContext();
  19.        
  20.         volumeNode = this.ctx.createGain()..connectNode(ctx.destination);
  21.  
  22.         AudioElement a = new AudioElement();
  23.         if (a.canPlayType("audio/mpeg;codecs=mp3") != "") {
  24.             extension = "mp3";
  25.         };
  26.        
  27.         mutebutton = querySelector("#volume");
  28.         slider = querySelector("#volumeslider");
  29.        
  30.         mutebutton.onClick.listen((Event e) {
  31.             toggleMute();
  32.         });
  33.        
  34.         slider.onChange.listen((Event e) {
  35.             setVolume(double.parse(slider.value), slider:false);
  36.         });
  37.        
  38.         setVolume(0.5);
  39.     }
  40.    
  41.     static void setVolume(num vol, {bool mute : false, bool slider : true}) {
  42.         instance.volume = clamp(vol.toDouble(), 0.0, 1.0);
  43.         instance.volumeNode.gain.value = mute ? 0.0 : instance.volume;
  44.         instance.muted = mute;
  45.         if (slider) {
  46.             instance.slider.value = "${instance.volume}";
  47.         }
  48.         instance.updateMuteElement();
  49.     }
  50.    
  51.     static void play(String soundname, [String subtitle=null]) {
  52.         if (soundname == null) { return; }
  53.         if (Assets.sounds.containsKey(soundname)) {
  54.             AudioBuffer sound = Assets.sounds[soundname];
  55.            
  56.             var source = instance.ctx.createBufferSource()
  57.                 ..buffer = sound
  58.                 ..connectNode(instance.volumeNode)
  59.                 ..start(0);
  60.            
  61.             if (subtitle != null) {
  62.                
  63.             }
  64.         }
  65.     }
  66.    
  67.     static void playRandom(List<String> soundnames, [List<String> subtitle=null]) {
  68.         if (soundnames == null || soundnames.isEmpty) { return; }
  69.         int r = rand.nextInt(soundnames.length);
  70.         String sub = subtitle == null ? null : subtitle[r];
  71.         play(soundnames[r], sub);
  72.     }
  73.    
  74.     static void toggleMute() {
  75.         setVolume(instance.volume, mute:!instance.muted);
  76.     }
  77.    
  78.     void updateMuteElement() {
  79.         if (this.muted) {
  80.             mutebutton.classes.remove("glyphicon-volume-up");
  81.             mutebutton.classes.add("glyphicon-volume-off");
  82.         } else {
  83.             mutebutton.classes.remove("glyphicon-volume-off");
  84.             mutebutton.classes.add("glyphicon-volume-up");
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement