<commit_before>(function (Module) {

var GENERATOR_TYPES = [
  ["sine", OscillatorNode.SINE],
  ["triangle", OscillatorNode.TRIANGLE],
  ["square", OscillatorNode.SQUARE],
  ["saw", OscillatorNode.SAWTOOTH]
];
var GENERATOR_TYPES_NAME = _.pluck(GENERATOR_TYPES, 0);
var GENERATOR_TYPES_OSCVALUE = _.pluck(GENERATOR_TYPES, 1);

zound.modules.Generator = Module.extend({
  defaults: _.extend({}, Module.prototype.defaults, {
    title: "Generator",
    color: "#622"
  }),
  initialize: function () {
    Module.prototype.initialize.call(this);
    this.pVolume = new zound.models.ModulePropertyRange({ min: 0, max: 100, title: "Volume", value: 100 });
    this.pType = new zound.models.ModulePropertySelect({ values: GENERATOR_TYPES_NAME, title: "Type" });
    this.pAttack = new zound.models.ModulePropertyRange({ min: 0, max: 1000, title: "Attack", value: 10 });
    this.pDecay = new zound.models.ModulePropertyRange({ min: 0, max: 1000, title: "Decay", value: 200 });
    this.properties.add([this.pVolume, this.pType, this.pAttack, this.pDecay]);
  },
  canHaveInputs: function () {
    return false;
  },
  canPlayNote: function () {
    return true;
  },
  noteOn: function (note, ctx, time) {
    var osc = ctx.createOscillator();
    osc.type = GENERATOR_TYPES_OSCVALUE[this.pType.get("value")];
    osc.frequency.value = zound.AudioMath.noteToFrequency(note);
    osc.start(time);
    osc.stop(time + 0.2);
    var gain = ctx.createGain();
    gain.gain.value = this.pVolume.getPercent();

    osc.connect(gain);
    this.broadcastToOutputs(gain, ctx);

    // Handle envelope
    gain.gain.cancelScheduledValues(time);
    gain.gain.setValueAtTime(0, time);
    gain.gain.linearRampToValueAtTime(this.pVolume.getPercent(), time + (this.pAttack.getValue() / 1000));
    gain.gain.linearRampToValueAtTime(0, time + (this.pAttack.getValue() / 1000) + (this.pDecay.getValue() / 1000));
  },
  noteOff: function () {
    // needed?<commit_msg>add glide-to-note to generators<commit_after>(function (Module) {

var GENERATOR_TYPES = [
  ["sine", OscillatorNode.SINE],
  ["triangle", OscillatorNode.TRIANGLE],
  ["square", OscillatorNode.SQUARE],
  ["saw", OscillatorNode.SAWTOOTH]
];
var GENERATOR_TYPES_NAME = _.pluck(GENERATOR_TYPES, 0);
var GENERATOR_TYPES_OSCVALUE = _.pluck(GENERATOR_TYPES, 1);

zound.modules.Generator = Module.extend({
  defaults: _.extend({}, Module.prototype.defaults, {
    title: "Generator",
    color: "#622"
  }),
  initialize: function () {
    Module.prototype.initialize.call(this);
    this.lastNote = null;
    this.pVolume = new zound.models.ModulePropertyRange({ min: 0, max: 100, title: "Volume", value: 100 });
    this.pType = new zound.models.ModulePropertySelect({ values: GENERATOR_TYPES_NAME, title: "Type" });
    this.pAttack = new zound.models.ModulePropertyRange({ min: 0, max: 1000, title: "Attack", value: 10 });
    this.pDecay = new zound.models.ModulePropertyRange({ min: 0, max: 1000, title: "Decay", value: 200 });
    this.pGlide = new zound.models.ModulePropertyRange({ min: 0, max: 100, title: "Glide", value: 0 });
    this.properties.add([this.pVolume, this.pType, this.pAttack, this.pDecay, this.pGlide]);
  },
  canHaveInputs: function () {
    return false;
  },
  canPlayNote: function () {
    return true;
  },
  noteOn: function (note, ctx, time) {
    var osc = ctx.createOscillator();
    osc.type = GENERATOR_TYPES_OSCVALUE[this.pType.get("value")];
    osc.frequency.value = zound.AudioMath.noteToFrequency(note);
    osc.start(time);
    osc.stop(time + 0.2);
    var gain = ctx.createGain();
    gain.gain.value = this.pVolume.getPercent();

    osc.connect(gain);
    this.broadcastToOutputs(gain, ctx);

    // Note envelope (Attack/Delay)
    var attackTime = this.pAttack.getValue() / 1000;
    var decayTime = this.pDecay.getValue() / 1000;

    gain.gain.cancelScheduledValues(time);
    gain.gain.setValueAtTime(0, time);
    gain.gain.linearRampToValueAtTime(this.pVolume.getPercent(), time + attackTime);
    gain.gain.linearRampToValueAtTime(0, time + attackTime + decayTime);


    // Glide to note
    var glideTime = this.pGlide.getValue() / 100;
    if (glideTime > 0 && this.lastNote) {

      osc.frequency.setValueAtTime(zound.AudioMath.noteToFrequency(this.lastNote), time);
      osc.frequency.linearRampToValueAtTime(zound.AudioMath.noteToFrequency(note), time + ((attackTime + decayTime) * glideTime));

    }
    this.lastNote = note;

  },
  noteOff: function () {
    // needed?
{'fraction_non_alphanumeric': 0.1533482142857143, 'fraction_numerical': 0.0203125, 'mean_word_length': 4.057562076749436, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5384834', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1550, 'n_words': 292}