Skip to content

Commit

Permalink
Audio refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrove committed Apr 13, 2012
1 parent 880fc6d commit 7110c83
Showing 1 changed file with 55 additions and 14 deletions.
69 changes: 55 additions & 14 deletions app/assets/javascripts/backbone/plugins/music_player.js.coffee
Expand Up @@ -5,11 +5,14 @@ class Kandan.Plugins.MusicPlayer
@widgetTitle: "Player"
@playRegex: /^/play .+/
@stopRegex: /^/stop/
@resumeRegex: /^/resume/
@localSongData: false


@playTemplate: _.template('<strong><a class="audio-play">playing</a> <a target="_blank" href="<%- url %>"><%- url %></a></strong>')
@songTemplate: _.template('<li><%= song.split("/").pop() %></li>')
@playTemplate: _.template('<strong><a class="audio-play">playing</a> <a target="_blank" href="<%- url %>"><%- url %></a></strong>')
@stopTemplate: _.template('<strong><a class="audio-play">stopping</a> the music.')
@resumeTemplate: _.template('<strong><a class="audio-play">resuming</a> the music.')
@songTemplate: _.template('<li><%= song.split("/").pop() %></li>')


@setError: (errorMessage)->
Expand Down Expand Up @@ -45,25 +48,50 @@ class Kandan.Plugins.MusicPlayer
@init: (pluginId)->
@pluginId = pluginId
Kandan.Data.Channels.registerCallback("change", $.proxy(@onChannelChange, this))
@registerModifier()
@registerWidget()
@registerPlayModifier()
@registerStopModifier()
@registerResumeModifier()
# Disabled for now
#@registerWidget()


@registerWidget: ()->
Kandan.Widgets.register @pluginNamespace


@registerModifier: ()->
@registerPlayModifier: ()->
Kandan.Modifiers.register @playRegex, (message, state) =>
url = $.trim(message.content.substr(message.content.indexOf(" ") + 1));
if true and Kandan.Data.Channels.activeChannelId()? # and state == Kandan.Helpers.Activities.ACTIVE_STATE commented out because state == undefined for some reason
@playUrl(message.channel_id, Kandan.Helpers.Utils.unescape(url))
rawInput = Kandan.Helpers.Utils.unescape(url)
soundUrl = null
soundUrl = @localSounds(rawInput)
soundUrl ?= rawInput

@playUrl(message.channel_id, soundUrl)
else
console.log "Not playing stale song"

message.content = @playTemplate({url: url})
return Kandan.Helpers.Activities.buildFromBaseTemplate message

@registerStopModifier: ()->
Kandan.Modifiers.register @stopRegex, (message, state) =>
url = $.trim(message.content.substr(message.content.indexOf(" ") + 1));
if true and Kandan.Data.Channels.activeChannelId()?
@stopSound(message.channel_id)

message.content = @stopTemplate()
return Kandan.Helpers.Activities.buildFromBaseTemplate message

@registerResumeModifier: ()->
Kandan.Modifiers.register @resumeRegex, (message, state) =>
if true and Kandan.Data.Channels.activeChannelId()?
@play(message.channel_id)

message.content = @resumeTemplate()
return Kandan.Helpers.Activities.buildFromBaseTemplate message


# TODO display error about song not being added by creating an activity locally
@storeSong: (url)->
Expand All @@ -81,17 +109,18 @@ class Kandan.Plugins.MusicPlayer
@getSongs: (callbacks)->
Kandan.Store.get @pluginId, callbacks

@nicknameToUrl: (url) ->
@localFileUrl: (fileName) ->
"http://#{ window.location.hostname }:#{ window.location.port }/sounds/#{ fileName }"

@localSounds: (name) ->
sounds = {
'claps' : 'golfclap.mp3',
'cheers' : 'cheers.mp3',
'ding' : 'ding.wav',
'gong' : 'gong.mp3'
'claps' : @localFileUrl('golfclap.mp3')
'cheers' : @localFileUrl('cheers.mp3')
'ding' : @localFileUrl('ding.wav')
'gong' : @localFileUrl('gong.mp3')
}

fullUrl = null
fullUrl = sounds[url]
fullUrl ||= url
sounds[name]

@audioChannels: ->
Kandan.Helpers.Audio.audioChannels()
Expand Down Expand Up @@ -119,8 +148,14 @@ class Kandan.Plugins.MusicPlayer

@playUrl: (channelId, url) ->
@setAudioUrl(channelId, url)
@play(channelId)

@play: (channelId) ->
@audioChannel(channelId).play()

@stopSound: (channelId) ->
@audioChannel(channelId).pause()

@currentChannel: () ->
Kandan.Data.Channels.activeChannelId()

Expand All @@ -135,4 +170,10 @@ class Kandan.Plugins.MusicPlayer
if @audioChannel(channelId)?
@unmute(channelId)

@playAudioNotice: ->
url = @localFileUrl('ding.wav')
player = $('.audio_private')[0]
player.setAttribute('src', url)
player.play()

# Kandan.Plugins.register "Kandan.Plugins.MusicPlayer"

0 comments on commit 7110c83

Please sign in to comment.