<commit_before>JSIL.Audio = {};

JSIL.Audio.InstancePrototype = {
  play: function () {
    this._isPlaying = true;
    this._isPaused = false;

    if (this.$play)
      this.$play();
  },
  pause: function () {
    this._isPlaying = false;
    this._isPaused = true;

    if (this.$pause)
      this.$pause();
  },
  resume: function () {
    this._isPlaying = true;
    this._isPaused = false;

    if (this.$resume)
      this.$resume();
  },
  stop: function () {
    this._isPlaying = false;
    this._isPaused = false;

    if (this.$stop)
      this.$stop();
  },
  dispose: function () {
    // TODO: Return to free instance pool

    if (this.$dispose)
      this.$dispose();
    else
      this.stop();
  },
  get_volume: function () {
    return this._volume;
  },
  set_volume: function (value) {
    this._volume = value;

    if (this.$set_volume)
      this.$set_volume(this._volume * this._volumeMultiplier);
  },
  get_volumeMultiplier: function () {
    return this._volumeMultiplier;
  },
  set_volumeMultiplier: function (value) {
    this._volumeMultiplier = value;

    if (this.$set_volume)
      this.$set_volume(this._volume * this._volumeMultiplier);
  },
  get_loop: function () {
    return this._loop;
  },
  set_loop: function (value) {
    this._loop = value;

    if (this.$set_loop)
      this.$set_loop(value);
  },
  get_isPlaying: function () {
    if (this.$get_isPlaying)
      return this.$get_isPlaying();
    else
      return this._isPlaying;
  },
  get_isPaused: function () {
    if (this.$get_isPaused)
      return this.$get_isPaused();
    else
      return this._isPaused;
  }
};

Object.defineProperty(JSIL.Audio.InstancePrototype, "volume", {
  get: JSIL.Audio.InstancePrototype.get_volume,
  set: JSIL.Audio.InstancePrototype.set_volume,
  configurable: true,
  enumerable: true
});

Object.defineProperty(JSIL.Audio.InstancePrototype, "volumeMultiplier", {
  get: JSIL.Audio.InstancePrototype.get_volume,
  set: JSIL.Audio.InstancePrototype.set_volume,
  configurable: true,
  enumerable: true
});

Object.defineProperty(JSIL.Audio.InstancePrototype, "loop", {
  get: JSIL.Audio.InstancePrototype.get_loop,
  set: JSIL.Audio.InstancePrototype.set_loop,
  configurable: true,
  enumerable: true
});

Object.defineProperty(JSIL.Audio.InstancePrototype, "isPlaying", {
  get: JSIL.Audio.InstancePrototype.get_isPlaying,
  configurable: true,
  enumerable: true
});

Object.defineProperty(JSIL.Audio.InstancePrototype, "isPaused", {
  get: JSIL.Audio.InstancePrototype.get_isPaused,
  configurable: true,
  enumerable: true
});


JSIL.Audio.HTML5Instance = function (audioInfo, node, loop) {
  this._isPlaying = false;
  this._isPaused = false;
  this.node = node;
  this.node.loop = loop;
  this._volume = 1.0;
  this._volumeMultiplier = 1.0;

  this.$bindEvents();
};
JSIL.Audio.HTML5Instance.prototype = Object.create(JSIL.Audio.InstancePrototype);

JSIL.Audio.HTML5Instance.prototype.$bindEvents = function () {
  this.$onEndedListener = JSIL.Browser.RegisterOneShotEventListener(this.node, "ended", true, this.on_ended.bind(this));
};

JSIL.Audio.HTML5Instance.prototype.$unbindEvents = function () {
  if (this.$onEndedListener)
    this.$onEndedListener.unregister();
  
  this.$onEndedListener = null;
};

JSIL.Audio.HTML5Instance.prototype.$play = function () {
  this.node.play();
}

JSIL.Audio.HTML5Instance.prototype.$pause = function () {
  this.node.pause();
}

JSIL.Audio.HTML5Instance.prototype.$resume = function () {
  this.node.play();
}

JSIL.Audio.HTML5Instance.prototype.$stop = function () {
  this.node.pause();
}

JSIL.Audio.HTML5Instance.prototype.$set_volume = function (value) {
  return this.node.volume = value;
}

JSIL.Audio.HTML5Instance.prototype.$set_loop = function (value) {
  return this.node.loop = value;
}

JSIL.Audio.HTML5Instance.prototype.on_ended = function () {
  this._isPlaying = false;
  this.dispose();
};

JSIL.Audio.HTML5Instance.prototype.$dispose = function () {
  this.node.pause();

  // Manually unregister the event listener because apparently it's 1996 and 
  //  browser GCs still can't actually collect cycles
  this.$unbindEvents();
  
  // HACK: This forces Gecko-based browsers to free the resources previously 
  //  used for audio playback.
  try {
    this.node.removeAttribute("src");
    this.node.load();
  } catch (exc) {
  }
};

JSIL.Audio.WebKitInstance = function (audioInfo, buffer, loop) {
  this.gainNode = audioInfo.audioContext.createGainNode();
  this.gainNode.connect(audioInfo.audioContext.destination);

  this.$createBufferSource = function () {
    this.bufferSource = audioInfo.audioContext.createBufferSource();
    this.bufferSource.buffer = buffer;
    this.bufferSource.loop = loop;
    this.bufferSource.connect(this.gainNode);
  };

  this.context = audioInfo.audioContext;
  this.started = 0;
  this.pausedAtOffset = null;
  
  this._volume = 1.0;
  this._volumeMultiplier = 1.0;
};
JSIL.Audio.WebKitInstance.prototype = Object.create(JSIL.Audio.InstancePrototype);

JSIL.Audio.WebKitInstance.prototype.$play = function () {
  this.$createBufferSource();
  this.started = this.context.currentTime;
  this.pausedAtOffset = null;
  this.bufferSource.start(0);<commit_msg>Clamp range of HTML5 audio volume values<commit_after>JSIL.Audio = {};

JSIL.Audio.InstancePrototype = {
  play: function () {
    this._isPlaying = true;
    this._isPaused = false;

    if (this.$play)
      this.$play();
  },
  pause: function () {
    this._isPlaying = false;
    this._isPaused = true;

    if (this.$pause)
      this.$pause();
  },
  resume: function () {
    this._isPlaying = true;
    this._isPaused = false;

    if (this.$resume)
      this.$resume();
  },
  stop: function () {
    this._isPlaying = false;
    this._isPaused = false;

    if (this.$stop)
      this.$stop();
  },
  dispose: function () {
    // TODO: Return to free instance pool

    if (this.$dispose)
      this.$dispose();
    else
      this.stop();
  },
  get_volume: function () {
    return this._volume;
  },
  set_volume: function (value) {
    this._volume = value;

    if (this.$set_volume)
      this.$set_volume(this._volume * this._volumeMultiplier);
  },
  get_volumeMultiplier: function () {
    return this._volumeMultiplier;
  },
  set_volumeMultiplier: function (value) {
    this._volumeMultiplier = value;

    if (this.$set_volume)
      this.$set_volume(this._volume * this._volumeMultiplier);
  },
  get_loop: function () {
    return this._loop;
  },
  set_loop: function (value) {
    this._loop = value;

    if (this.$set_loop)
      this.$set_loop(value);
  },
  get_isPlaying: function () {
    if (this.$get_isPlaying)
      return this.$get_isPlaying();
    else
      return this._isPlaying;
  },
  get_isPaused: function () {
    if (this.$get_isPaused)
      return this.$get_isPaused();
    else
      return this._isPaused;
  }
};

Object.defineProperty(JSIL.Audio.InstancePrototype, "volume", {
  get: JSIL.Audio.InstancePrototype.get_volume,
  set: JSIL.Audio.InstancePrototype.set_volume,
  configurable: true,
  enumerable: true
});

Object.defineProperty(JSIL.Audio.InstancePrototype, "volumeMultiplier", {
  get: JSIL.Audio.InstancePrototype.get_volume,
  set: JSIL.Audio.InstancePrototype.set_volume,
  configurable: true,
  enumerable: true
});

Object.defineProperty(JSIL.Audio.InstancePrototype, "loop", {
  get: JSIL.Audio.InstancePrototype.get_loop,
  set: JSIL.Audio.InstancePrototype.set_loop,
  configurable: true,
  enumerable: true
});

Object.defineProperty(JSIL.Audio.InstancePrototype, "isPlaying", {
  get: JSIL.Audio.InstancePrototype.get_isPlaying,
  configurable: true,
  enumerable: true
});

Object.defineProperty(JSIL.Audio.InstancePrototype, "isPaused", {
  get: JSIL.Audio.InstancePrototype.get_isPaused,
  configurable: true,
  enumerable: true
});


JSIL.Audio.HTML5Instance = function (audioInfo, node, loop) {
  this._isPlaying = false;
  this._isPaused = false;
  this.node = node;
  this.node.loop = loop;
  this._volume = 1.0;
  this._volumeMultiplier = 1.0;

  this.$bindEvents();
};
JSIL.Audio.HTML5Instance.prototype = Object.create(JSIL.Audio.InstancePrototype);

JSIL.Audio.HTML5Instance.prototype.$bindEvents = function () {
  this.$onEndedListener = JSIL.Browser.RegisterOneShotEventListener(this.node, "ended", true, this.on_ended.bind(this));
};

JSIL.Audio.HTML5Instance.prototype.$unbindEvents = function () {
  if (this.$onEndedListener)
    this.$onEndedListener.unregister();
  
  this.$onEndedListener = null;
};

JSIL.Audio.HTML5Instance.prototype.$play = function () {
  this.node.play();
}

JSIL.Audio.HTML5Instance.prototype.$pause = function () {
  this.node.pause();
}

JSIL.Audio.HTML5Instance.prototype.$resume = function () {
  this.node.play();
}

JSIL.Audio.HTML5Instance.prototype.$stop = function () {
  this.node.pause();
}

JSIL.Audio.HTML5Instance.prototype.$set_volume = function (value) {
  if (value < 0)
    value = 0;
  else if (value > 1)
    value = 1;
  
  return this.node.volume = value;
}

JSIL.Audio.HTML5Instance.prototype.$set_loop = function (value) {
  return this.node.loop = value;
}

JSIL.Audio.HTML5Instance.prototype.on_ended = function () {
  this._isPlaying = false;
  this.dispose();
};

JSIL.Audio.HTML5Instance.prototype.$dispose = function () {
  this.node.pause();

  // Manually unregister the event listener because apparently it's 1996 and 
  //  browser GCs still can't actually collect cycles
  this.$unbindEvents();
  
  // HACK: This forces Gecko-based browsers to free the resources previously 
  //  used for audio playback.
  try {
    this.node.removeAttribute("src");
    this.node.load();
  } catch (exc) {
  }
};

JSIL.Audio.WebKitInstance = function (audioInfo, buffer, loop) {
  this.gainNode = audioInfo.audioContext.createGainNode();
  this.gainNode.connect(audioInfo.audioContext.destination);

  this.$createBufferSource = function () {
    this.bufferSource = audioInfo.audioContext.createBufferSource();
    this.bufferSource.buffer = buffer;
    this.bufferSource.loop = loop;
    this.bufferSource.connect(this.gainNode);
  };

  this.context = audioInfo.audioContext;
  this.started = 0;
  this.pausedAtOffset = null;

  this._volume = 1.0;
  this._volumeMultiplier = 1.0;
};
JSIL.Audio.WebKitInstance.prototype = Object.create(JSIL.Audio.InstancePrototype);

JSIL.Audio.WebKitInstance.prototype.$play = function () {
  this.$createBufferSource();
  this.started = this.context.currentTime;
  this.pausedAtOffset = null;
  this.bufferSource.start(0);
{'fraction_non_alphanumeric': 0.13730151183797662, 'fraction_numerical': 0.00541979651992013, 'mean_word_length': 3.706040268456376, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2245063', 'n_tokens_mistral': 3547, 'n_tokens_neox': 3489, 'n_words': 602}