<commit_before>var engine = require('player-engine')
var fs = require('file-system')(64 * 1024 * 1024, ['audio/mp3', 'audio/wav', 'audio/ogg'])
var async = require('async')
var rusha = new (require('rusha'))()
var musicMetadata = require('music-metadata')

var seeking = false
var current_song_index = -1

// In-memory storage with persistence through localStorage
var meta_storage = {
  cache: JSON.parse(window.localStorage.getItem('metadata')) || {},
  get: function (key) {
    return this.cache[key]
  },
  set: function (key, meta) {
    this.cache[key] = meta
    window.localStorage.setItem('metadata', JSON.stringify(this.cache))
  }
}

// Initialize the player engine & event listeners
engine = engine()

// Setup the volume bar
engine.volume(0.2)
document.querySelector('#volumeBar').min = 0
document.querySelector('#volumeBar').max = 1
document.querySelector('#volumeBar').step = 0.01
document.querySelector('#volumeBar').value = 0.2

// Toggle the playing state
engine.on('playingState', function (playing) {
  document.querySelector('#play').innerHTML = playing ? '<i class="fa fa-pause"></i>' : '<i class="fa fa-play"></i>'
})

// Toggle the "back" state
engine.on('backState', function (back_possible) {
  document.querySelector('#previous').disabled = !back_possible
})

// Update songs
engine.on('songState', function (song) {
  var meta = meta_storage.get(song.name)
  document.querySelector('#fileContainer').innerHTML = '<strong>Currently playing:</strong> ' + meta.album + ' - ' + meta.artist + ' - ' + meta.title
  document.querySelector('#currentTime').innerHTML = duration(0)
  document.querySelector('#progressBar').min = 0
  document.querySelector('#progressBar').value = 0
  document.querySelector('#progressBar').max = song.duration
  document.querySelector('#songLength').innerHTML = duration(song.duration)
  current_song_index = song.index
  renderView()
})

// Update progress
engine.on('progress', function (progress) {
  if (!seeking) {
    document.querySelector('#currentTime').innerHTML = duration(progress)
    document.querySelector('#progressBar').value = Math.floor(progress)
  }
})

function hash (string) {
  return rusha.digestFromString(string)
}

// Add files to the file system
function addFiles (files) {
  // Asynchronously map the files with the hash function over their contents
  async.map(files, function (file, callback) {
    var reader = new window.FileReader()
    reader.readAsDataURL(file)
    reader.onloadend = function () {
      file.hashName = hash(this.result) + file.name.replace(/^.*(\.[A-Za-z0-9]{3})$/, '$1')
      musicMetadata(file, function (meta) {
        meta_storage.set(file.hashName, meta)
        callback(null, file)
      })
    }
  }, function (error, results) {
    // Add the files into the file system, which uses "hashName" as a priority
    if (error) throw error
    fs.add(results, attachFiles)
  })
}

// Update the engine's files with the one's from the file system<commit_msg>Save the original file name (peermusic/user-stories#20)<commit_after>var engine = require('player-engine')
var fs = require('file-system')(64 * 1024 * 1024, ['audio/mp3', 'audio/wav', 'audio/ogg'])
var async = require('async')
var rusha = new (require('rusha'))()
var musicMetadata = require('music-metadata')

var seeking = false
var current_song_index = -1

// In-memory storage with persistence through localStorage
var meta_storage = {
  cache: JSON.parse(window.localStorage.getItem('metadata')) || {},
  get: function (key) {
    return this.cache[key]
  },
  set: function (key, meta) {
    this.cache[key] = meta
    window.localStorage.setItem('metadata', JSON.stringify(this.cache))
  }
}

// Initialize the player engine & event listeners
engine = engine()

// Setup the volume bar
engine.volume(0.2)
document.querySelector('#volumeBar').min = 0
document.querySelector('#volumeBar').max = 1
document.querySelector('#volumeBar').step = 0.01
document.querySelector('#volumeBar').value = 0.2

// Toggle the playing state
engine.on('playingState', function (playing) {
  document.querySelector('#play').innerHTML = playing ? '<i class="fa fa-pause"></i>' : '<i class="fa fa-play"></i>'
})

// Toggle the "back" state
engine.on('backState', function (back_possible) {
  document.querySelector('#previous').disabled = !back_possible
})

// Update songs
engine.on('songState', function (song) {
  var meta = meta_storage.get(song.name)
  document.querySelector('#fileContainer').innerHTML = '<strong>Currently playing:</strong> ' + meta.album + ' - ' + meta.artist + ' - ' + meta.title
  document.querySelector('#currentTime').innerHTML = duration(0)
  document.querySelector('#progressBar').min = 0
  document.querySelector('#progressBar').value = 0
  document.querySelector('#progressBar').max = song.duration
  document.querySelector('#songLength').innerHTML = duration(song.duration)
  current_song_index = song.index
  renderView()
})

// Update progress
engine.on('progress', function (progress) {
  if (!seeking) {
    document.querySelector('#currentTime').innerHTML = duration(progress)
    document.querySelector('#progressBar').value = Math.floor(progress)
  }
})

function hash (string) {
  return rusha.digestFromString(string)
}

// Add files to the file system
function addFiles (files) {
  // Asynchronously map the files with the hash function over their contents
  async.map(files, function (file, callback) {
    var reader = new window.FileReader()
    reader.readAsDataURL(file)
    reader.onloadend = function () {
      file.hashName = hash(this.result) + file.name.replace(/^.*(\.[A-Za-z0-9]{3})$/, '$1')
      musicMetadata(file, function (meta) {
        meta.originalName = file.name
        meta_storage.set(file.hashName, meta)
        callback(null, file)
      })
    }
  }, function (error, results) {
    // Add the files into the file system, which uses "hashName" as a priority
    if (error) throw error
    fs.add(results, attachFiles)
  })
}

// Update the engine's files with the one's from the file system
{'fraction_non_alphanumeric': 0.15387171817879694, 'fraction_numerical': 0.009637753406447324, 'mean_word_length': 4.359750667853962, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6280354', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1772, 'n_words': 432}