<commit_before>CocoClass = require 'lib/CocoClass'
AudioPlayer = require 'lib/AudioPlayer'
{me} = require 'lib/auth'

CROSSFADE_LENGTH = 1500

module.exports = class MusicPlayer extends CocoClass
  currentMusic: null
  standingBy: null

  subscriptions:
    'level-play-music': 'onPlayMusic'
    'audio-player:loaded': 'onAudioLoaded'

  constructor: ->
    super arguments...
    me.on 'change:music', @onMusicSettingChanged, @

  onAudioLoaded: ->
    @onPlayMusic(@standingBy) if @standingBy

  onPlayMusic: (e) ->
    src = e.file
    if src
      src = "/file#{src}#{AudioPlayer.ext}"
      return @currentMusic.play() if src is @currentMusic?.src
      media = AudioPlayer.getStatus(src)
      if not media?.loaded
        AudioPlayer.preloadSound(src)
        @standingBy = e
        return

    @standingBy = null
    if @currentMusic
      f = -> @stop()
      createjs.Tween.get(@currentMusic).to({volume:0.0}, CROSSFADE_LENGTH).call(f)

    @currentMusic = createjs.Sound.play(src, 'none', 0, 0, -1, 0.3) if src and e.play
    return unless @currentMusic
    @currentMusic.volume = 0.0
    if me.get('music')
      createjs.Tween.get(@currentMusic).to({volume:1.0}, CROSSFADE_LENGTH)

  onMusicSettingChanged: ->
    @updateMusicVolume()

  updateMusicVolume: ->
    return unless @currentMusic
    createjs.Tween.removeTweens(@currentMusic)<commit_msg>Fixed a bug with the music player where sometimes it wouldn't loop.<commit_after>CocoClass = require 'lib/CocoClass'
AudioPlayer = require 'lib/AudioPlayer'
{me} = require 'lib/auth'

CROSSFADE_LENGTH = 1500

module.exports = class MusicPlayer extends CocoClass
  currentMusic: null
  standingBy: null

  subscriptions:
    'level-play-music': 'onPlayMusic'
    'audio-player:loaded': 'onAudioLoaded'

  constructor: ->
    super arguments...
    me.on 'change:music', @onMusicSettingChanged, @

  onAudioLoaded: ->
    @onPlayMusic(@standingBy) if @standingBy

  onPlayMusic: (e) ->
    src = e.file
    if src
      src = "/file#{src}#{AudioPlayer.ext}"
      return @currentMusic.play('none', 0, 0, -1, 0.3) if src is @currentMusic?.src
      media = AudioPlayer.getStatus(src)
      if not media?.loaded
        AudioPlayer.preloadSound(src)
        @standingBy = e
        return

    @standingBy = null
    if @currentMusic
      f = -> @stop()
      createjs.Tween.get(@currentMusic).to({volume:0.0}, CROSSFADE_LENGTH).call(f)

    @currentMusic = createjs.Sound.play(src, 'none', 0, 0, -1, 0.3) if src and e.play
    return unless @currentMusic
    @currentMusic.volume = 0.0
    if me.get('music')
      createjs.Tween.get(@currentMusic).to({volume:1.0}, CROSSFADE_LENGTH)

  onMusicSettingChanged: ->
    @updateMusicVolume()

  updateMusicVolume: ->
    return unless @currentMusic
    createjs.Tween.removeTweens(@currentMusic)
{'fraction_non_alphanumeric': 0.12772029967891546, 'fraction_numerical': 0.012486621476988941, 'mean_word_length': 3.3608087091757386, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5774710', 'n_tokens_mistral': 952, 'n_tokens_neox': 906, 'n_words': 200}