<commit_before>(function (models, modules, ui) {

  //~~~ MODELS

  var network = new zound.Network();
  var users = new zound.models.Users([ window.CURRENT_USER ]);

  var song = song_sample1();
  window._song = song; // for DEBUG only
  var pattern = song.patterns.first();

  var midiController = new models.MIDIController();

  var playerController = new models.PlayerController({
    length: song.get("length"),
    bpm: song.get("bpm")
  });
  song.on("change:length", function (song, length) {
    playerController.set("length", length);
  });
  song.on("change:bpm", function (song, bpm) {
    playerController.set("bpm", bpm);
  });
  playerController.setAudioContext(song.ctx);
  /*
  // FIXME: blur is too aggressive, we more need a "on tab change"
  $(window).blur(function () {
    playerController.stop();
  });
  */

  var availableModules = new models.Modules([
    new modules.Generator({ title: "Generator" }),
    new modules.Drum({ title: "Drum" }),
    new modules.MultiSynth({ title: "MultiSynth" }),
    new modules.Filter({ title: "Filter" }),
    new modules.Delay({ title: "Delay" }),
    new modules.Reverb({ title: "Reverb" })
  ]);

  availableModules.on("selectModule", function (module) {
    var m = module.clone();
    var title = m.get("title");
    if (title.length > 6) title = title.substring(0,5)+".";
    m.set("title", title+(song.moduleIdCounter+1));
    song.addModule(m);
  });

  users.on("change:slot", function (user, value) {
    var previous = user.previous("slot");
    console.log(value, previous);
    if (previous) {
      tracker.getSlot(previous.track, previous.slot).setUserSelect(null);
    }
    if (value) {
      tracker.getSlot(value.track, value.slot).setUserSelect(user.id);
    }
  });

  //~~~ VIEWS 

  var midiControllerNotification = new ui.MIDIControllerNotification({
    model: midiController
  });
  $("#midiNotification").append(midiControllerNotification.el);

  var nodeEditor = new ui.NodeEditor({
    model: song,
    el: '#node-editor'
  });

  var tracker = new zound.ui.Tracker({
    model: pattern
  });
  $("#tracker").append(tracker.el);

  var player = new ui.Player({
    model: playerController
  });
  $('#toolbar').append(player.el);

  var moduleChooser = new ui.ModulesChooser({
    model: availableModules
  });
  $('#module-collection').append(moduleChooser.el);

  var currentPropertiesEditor;
  CURRENT_USER.on("change:module", function (user, moduleId) {
    var module = song.modules.get(moduleId);
    if (currentPropertiesEditor) {
      currentPropertiesEditor.remove();
    }
    currentPropertiesEditor = new zound.ui.ModulePropertiesEditor({
      model: module
    });
    $('#module-properties').append(currentPropertiesEditor.el);
  });
  var m = song.modules.find(function (module) {
    return module instanceof zound.models.SynthModule;
  });
  if (m) {
    nodeEditor.selectModule(m);
  }

  playerController.on("change:recording", function (model, recording) {
    if (recording && !CURRENT_USER.get("slot"))
      CURRENT_USER.set("slot", { track: 0, slot: 0 });
  });
  playerController.on("tick", function (lineNumber, time) {
    song.scheduleNote(lineNumber, time);
    if(playerController.get("recording"))
      CURRENT_USER.moveTo(lineNumber);
    tracker.highlightLine(lineNumber);
  });
  playerController.on("change:playing", function (model, playing) {
    if (!playing)
      tracker.highlightLine(null);
  });

  // bind user style
  var users_style_template = _.template(document.getElementById('users_style_template').innerHTML);
  function updateUsersStyle (users) {
    $('#users_style').html(users.map(function (user) {
      return users_style_template(user.attributes);
    }).join('\n'));
  }
  users.on("add remove", function(user) {
    updateUsersStyle(users);
  });
  updateUsersStyle(users);


  var trackerIncrement = new zound.ui.TrackerIncrement({
    model: CURRENT_USER,
    id: "tracker-increment"
  });
  $('#tracker').append(trackerIncrement.el);

  var keyboardController = new models.KeyboardController(_.extend({
    user: CURRENT_USER
  }, models.KeyboardController[window.KEYBOARD_LAYOUT+"config"]));

  var handleNote = function (note) {
    var module = song.modules.get(CURRENT_USER.get("module"));
    var slot = CURRENT_USER.get("slot");

    if (module && module.canPlayNote())
      module.noteOn(note, song.ctx, song.ctx.currentTime);

    if (module && module.canPlayNote() && slot) {
      var slotModel = pattern.getSlot(slot.track, slot.slot);
      slotModel.set({
        note: note,
        module: module.id
      });
      CURRENT_USER.moveTrackerSelection(0, CURRENT_USER.get("trackerIncrement"), pattern.tracks.size(), pattern.get("length"));
    }
  };

  midiController.on("note", handleNote);

  keyboardController.on({
    "note": handleNote,
    "tracker-move": function (incrX, incrY) {
      CURRENT_USER.moveTrackerSelection(incrX, incrY, pattern.tracks.size(), pattern.get("length"));
    },
    "tracker-backspace": function () {
      var slot = CURRENT_USER.get("slot");
      var slotModel = pattern.getSlot(slot.track, slot.slot);
      slotModel.set({ note: null, module: null });
      CURRENT_USER.moveTrackerSelection(0, -1, pattern.tracks.size(), pattern.get("length"));
    },
    "tracker-delete": function () {
      var slot = CURRENT_USER.get("slot");
      var slotModel = pattern.getSlot(slot.track, slot.slot);
      slotModel.set({ note: null, module: null });
      CURRENT_USER.moveTrackerSelection(0, CURRENT_USER.get("trackerIncrement"), pattern.tracks.size(), pattern.get("length"));
    },
    "unselect": function () {
      CURRENT_USER.set("slot", null);
    },
    "play-pause": function () {
      if (!playerController.get("playing"))
        playerController.play();
      else
        playerController.stop();<commit_msg>fix midi noteOn<commit_after>(function (models, modules, ui) {

  //~~~ MODELS

  var network = new zound.Network();
  var users = new zound.models.Users([ window.CURRENT_USER ]);

  var song = song_sample1();
  window._song = song; // for DEBUG only
  var pattern = song.patterns.first();

  var midiController = new models.MIDIController();

  var playerController = new models.PlayerController({
    length: song.get("length"),
    bpm: song.get("bpm")
  });
  song.on("change:length", function (song, length) {
    playerController.set("length", length);
  });
  song.on("change:bpm", function (song, bpm) {
    playerController.set("bpm", bpm);
  });
  playerController.setAudioContext(song.ctx);
  /*
  // FIXME: blur is too aggressive, we more need a "on tab change"
  $(window).blur(function () {
    playerController.stop();
  });
  */

  var availableModules = new models.Modules([
    new modules.Generator({ title: "Generator" }),
    new modules.Drum({ title: "Drum" }),
    new modules.MultiSynth({ title: "MultiSynth" }),
    new modules.Filter({ title: "Filter" }),
    new modules.Delay({ title: "Delay" }),
    new modules.Reverb({ title: "Reverb" })
  ]);

  availableModules.on("selectModule", function (module) {
    var m = module.clone();
    var title = m.get("title");
    if (title.length > 6) title = title.substring(0,5)+".";
    m.set("title", title+(song.moduleIdCounter+1));
    song.addModule(m);
  });

  users.on("change:slot", function (user, value) {
    var previous = user.previous("slot");
    console.log(value, previous);
    if (previous) {
      tracker.getSlot(previous.track, previous.slot).setUserSelect(null);
    }
    if (value) {
      tracker.getSlot(value.track, value.slot).setUserSelect(user.id);
    }
  });

  //~~~ VIEWS 

  var midiControllerNotification = new ui.MIDIControllerNotification({
    model: midiController
  });
  $("#midiNotification").append(midiControllerNotification.el);

  var nodeEditor = new ui.NodeEditor({
    model: song,
    el: '#node-editor'
  });

  var tracker = new zound.ui.Tracker({
    model: pattern
  });
  $("#tracker").append(tracker.el);

  var player = new ui.Player({
    model: playerController
  });
  $('#toolbar').append(player.el);

  var moduleChooser = new ui.ModulesChooser({
    model: availableModules
  });
  $('#module-collection').append(moduleChooser.el);

  var currentPropertiesEditor;
  CURRENT_USER.on("change:module", function (user, moduleId) {
    var module = song.modules.get(moduleId);
    if (currentPropertiesEditor) {
      currentPropertiesEditor.remove();
    }
    currentPropertiesEditor = new zound.ui.ModulePropertiesEditor({
      model: module
    });
    $('#module-properties').append(currentPropertiesEditor.el);
  });
  var m = song.modules.find(function (module) {
    return module instanceof zound.models.SynthModule;
  });
  if (m) {
    nodeEditor.selectModule(m);
  }

  playerController.on("change:recording", function (model, recording) {
    if (recording && !CURRENT_USER.get("slot"))
      CURRENT_USER.set("slot", { track: 0, slot: 0 });
  });
  playerController.on("tick", function (lineNumber, time) {
    song.scheduleNote(lineNumber, time);
    if(playerController.get("recording"))
      CURRENT_USER.moveTo(lineNumber);
    tracker.highlightLine(lineNumber);
  });
  playerController.on("change:playing", function (model, playing) {
    if (!playing)
      tracker.highlightLine(null);
  });

  // bind user style
  var users_style_template = _.template(document.getElementById('users_style_template').innerHTML);
  function updateUsersStyle (users) {
    $('#users_style').html(users.map(function (user) {
      return users_style_template(user.attributes);
    }).join('\n'));
  }
  users.on("add remove", function(user) {
    updateUsersStyle(users);
  });
  updateUsersStyle(users);


  var trackerIncrement = new zound.ui.TrackerIncrement({
    model: CURRENT_USER,
    id: "tracker-increment"
  });
  $('#tracker').append(trackerIncrement.el);

  var keyboardController = new models.KeyboardController(_.extend({
    user: CURRENT_USER
  }, models.KeyboardController[window.KEYBOARD_LAYOUT+"config"]));

  var handleNote = function (note) {
    var module = song.modules.get(CURRENT_USER.get("module"));
    var slot = CURRENT_USER.get("slot");

    if (module && module.canPlayNote())
      module.noteOn(note, song.ctx, song.ctx.currentTime);

    if (module && module.canPlayNote() && slot) {
      var slotModel = pattern.getSlot(slot.track, slot.slot);
      slotModel.set({
        note: note,
        module: module.id
      });
      CURRENT_USER.moveTrackerSelection(0, CURRENT_USER.get("trackerIncrement"), pattern.tracks.size(), pattern.get("length"));
    }
  };

  midiController.on("noteOn", handleNote);

  keyboardController.on({
    "note": handleNote,
    "tracker-move": function (incrX, incrY) {
      CURRENT_USER.moveTrackerSelection(incrX, incrY, pattern.tracks.size(), pattern.get("length"));
    },
    "tracker-backspace": function () {
      var slot = CURRENT_USER.get("slot");
      var slotModel = pattern.getSlot(slot.track, slot.slot);
      slotModel.set({ note: null, module: null });
      CURRENT_USER.moveTrackerSelection(0, -1, pattern.tracks.size(), pattern.get("length"));
    },
    "tracker-delete": function () {
      var slot = CURRENT_USER.get("slot");
      var slotModel = pattern.getSlot(slot.track, slot.slot);
      slotModel.set({ note: null, module: null });
      CURRENT_USER.moveTrackerSelection(0, CURRENT_USER.get("trackerIncrement"), pattern.tracks.size(), pattern.get("length"));
    },
    "unselect": function () {
      CURRENT_USER.set("slot", null);
    },
    "play-pause": function () {
      if (!playerController.get("playing"))
        playerController.play();
      else
        playerController.stop();
{'fraction_non_alphanumeric': 0.16046631236070633, 'fraction_numerical': 0.0018858220469741129, 'mean_word_length': 3.7407557903291346, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 5, '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': '901386', 'n_tokens_mistral': 3716, 'n_tokens_neox': 3609, 'n_words': 697}