<commit_before>var View = require('./view');
var allen = require('../lib/allen');
var Rack = require('../lib/rack');
var when = require('../lib/when');
var context = allen.getAudioContext();
var EXT = allen.canPlayType('mp3') ? '.mp3' : '.ogg';

module.exports = View.extend({
  name : 'player',
  events: {
    'click .samples a' : 'handleSampleChange',
    'click .play-button' : 'handlePlayPause',
    'click .hide-button' : 'handleHide'
  },

  initialize: function () {
    this.showLoading();
    this.sample = 'vox';

    this.model.injectBuild().then(function () {
      var Node = window.require(this.model.get('name'));
      this.node = new Node(context);
      this.initializeRack();
      this.hideLoading();
      return this.node;
    }.bind(this))
      .then(this.getBuffer.bind(this), this.handleError.bind(this))
      .then(this.connect.bind(this), this.handleError.bind(this));
  },

  afterRender: function () {
    this.$play = this.$('.play-button');
  },

  destroy: function () {
    this.disconnect();
    this.remove();
  },

  initializeRack: function () {
    this.rack = new Rack(this.node);
    this.$('.module').html(this.rack.el);
  },

  getBuffer: function () {
    var deferred = when.defer();
    this.disableControls();
    allen.getBuffer('samples/' + this.sample + EXT, function (xhr) {
      this.enableControls();
      deferred.resolve(xhr.target.response);
    }.bind(this));
    return deferred.promise;
  },

  connect: function (buffer) {
    this.source = context.createBufferSource();
    this.source.buffer = context.createBuffer(buffer, false);
    this.source.connect(this.node.input);
    return this.node.connect(context.destination);
  },

  disconnect: function () {
    if (this.source && this.source.disconnect) {
      this.source.disconnect();
      this.source.noteOff(0);
    }
    if (this.node)
      this.node.disconnect();
  },

  playSample: function () {
    this.stopSample();
    this.getBuffer().then(function (buffer) {
      this.connect(buffer);
      this.source.noteOn(0);
      this.$play.data('playing', true)
        .find('i')
        .removeClass('icon-play')
        .addClass('icon-pause');
    }.bind(this));
  },

  stopSample : function () {
    this.$play.data('playing', false)
      .find('i')
      .addClass('icon-play')
      .removeClass('icon-pause');
    this.disconnect();
  },

  // Loading GUI<commit_msg>Handle FF24s automatic playing of buffers<commit_after>var View = require('./view');
var allen = require('../lib/allen');
var Rack = require('../lib/rack');
var when = require('../lib/when');
var context = allen.getAudioContext();
var EXT = allen.canPlayType('mp3') ? '.mp3' : '.ogg';

module.exports = View.extend({
  name : 'player',
  events: {
    'click .samples a' : 'handleSampleChange',
    'click .play-button' : 'handlePlayPause',
    'click .hide-button' : 'handleHide'
  },

  initialize: function () {
    this.showLoading();
    this.sample = 'vox';

    this.model.injectBuild().then(function () {
      var Node = window.require(this.model.get('name'));
      this.node = new Node(context);
      this.initializeRack();
      this.hideLoading();
      return this.node;
    }.bind(this))
      .then(this.getBuffer.bind(this), this.handleError.bind(this))
      .then(this.connect.bind(this), this.handleError.bind(this));
  },

  afterRender: function () {
    this.$play = this.$('.play-button');
  },

  destroy: function () {
    this.disconnect();
    this.remove();
  },

  initializeRack: function () {
    this.rack = new Rack(this.node);
    this.$('.module').html(this.rack.el);
  },

  getBuffer: function () {
    var deferred = when.defer();
    this.disableControls();
    allen.getBuffer('samples/' + this.sample + EXT, function (xhr) {
      this.enableControls();
      deferred.resolve(xhr.target.response);
    }.bind(this));
    return deferred.promise;
  },

  connect: function (buffer) {
    this.source = context.createBufferSource();
    this.source.buffer = context.createBuffer(buffer, false);
    this.source.connect(this.node.input);
    return this.node.connect(context.destination);
  },

  disconnect: function () {
    if (this.source && this.source.disconnect) {
      this.source.disconnect();
      // In FF24, this throws an error if not yet playing
      try {
        this.source.noteOff(0);
      } catch (e) { console.error(e); }
    }
    if (this.node)
      this.node.disconnect();
  },

  playSample: function () {
    this.stopSample();
    this.getBuffer().then(function (buffer) {
      this.connect(buffer);
      this.source.noteOn(0);
      this.$play.data('playing', true)
        .find('i')
        .removeClass('icon-play')
        .addClass('icon-pause');
    }.bind(this));
  },

  stopSample : function () {
    this.$play.data('playing', false)
      .find('i')
      .addClass('icon-play')
      .removeClass('icon-pause');
    this.disconnect();
  },

  // Loading GUI
{'fraction_non_alphanumeric': 0.1808811641067098, 'fraction_numerical': 0.002425222312045271, 'mean_word_length': 2.8968503937007872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7449171', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1571, 'n_words': 278}