Skip to content

Commit

Permalink
Polished audio
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrove committed Apr 13, 2012
1 parent be19af7 commit c3c467d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
9 changes: 5 additions & 4 deletions app/assets/javascripts/backbone/helpers/audio.js.coffee
@@ -1,12 +1,13 @@
class Kandan.Helpers.Audio
@audioChannels: ->
@audioHolder: ->
$('.audio_channels')[0]

@audioChannels: ->
$('audio')

@createAudioChannel: (id) ->
channel = $("<audio class='audio_#{id}'></audio>")
console.log(@audioChannels())
console.log(channel)
channel.appendTo(@audioChannels())
channel.appendTo(@audioHolder())

@destroyAudioChannel: (id) ->
channel = $(".audio_#{id}")
Expand Down
41 changes: 14 additions & 27 deletions app/assets/javascripts/backbone/plugins/music_player.js.coffee
Expand Up @@ -44,7 +44,7 @@ class Kandan.Plugins.MusicPlayer
# TODO: Add support for sounds
@init: (pluginId)->
@pluginId = pluginId
Kandan.Data.Channels.registerCallback(@onChannelChange)
Kandan.Data.Channels.registerCallback("change", $.proxy(@onChannelChange, this))
@registerModifier()
@registerWidget()

Expand All @@ -55,14 +55,11 @@ class Kandan.Plugins.MusicPlayer

@registerModifier: ()->
Kandan.Modifiers.register @playRegex, (message, state) =>
console.log("state:" + state)
url = $.trim(message.content.substr(message.content.indexOf(" ") + 1));
if true and Kandan.Helpers.Channels.getActiveChannelId()? # state == Kandan.Helpers.Activities.ACTIVE_STATE
console.log("message: " + message.content)
console.log("url: " + url)
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))
else
console.log "song is history"
console.log "Not playing stale song"

message.content = @playTemplate({url: url})
return Kandan.Helpers.Activities.buildFromBaseTemplate message
Expand Down Expand Up @@ -96,26 +93,20 @@ class Kandan.Plugins.MusicPlayer
fullUrl = sounds[url]
fullUrl ||= url

@volumes = {}

@audioChannels: ->
Kandan.Helpers.Audio.audioChannels()

@audioChannel: (id) ->
Kandan.Helpers.Audio.audioChannel(id)

@mute: (channelId) ->
channel = @audioChannel(channelId)
console.log(channel)
@volumes[channelId] = channel.volume
console.log(@volumes)
@setVolume(channelId, 0)

@unmute: (channelId) ->
@setVolume(channelId, @volumes[channelId])
@setVolume(channelId, 1)

@toggle: (channelId) ->
if @volumes[channelId] == 0
if @audioChannel(channelId).volume == 0
@unmute(channelId)
else
@mute(channelId)
Expand All @@ -127,25 +118,21 @@ class Kandan.Plugins.MusicPlayer
@audioChannel(channelId).setAttribute('src', url)

@playUrl: (channelId, url) ->
console.log(channelId, url)
@setAudioUrl(channelId, url)
@audioChannel(channelId).play()

@currentChannel: () ->
$(document).data("activeChannelId")
Kandan.Data.Channels.activeChannelId()

@onChannelChange: () ->
channelId = @currentChannel()
console.log("Current channel: #{channelId}")
if channelId
for channel in @audioChannels()
console.log("muting all channels!")
console.log(channel)
id = channel.id.split("_")[1]
@mute(id) if id? and id != channelId

if @audioChannel(channelId)?
console.log("unmuting channel #{channelId}")
@unmute(channelId)
for channel in @audioChannels()
raw = $(channel).attr('class').split("_")[1]
id = parseInt(raw)
continue if isNaN(id)
@mute(id)

if @audioChannel(channelId)?
@unmute(channelId)

# Kandan.Plugins.register "Kandan.Plugins.MusicPlayer"
Expand Up @@ -7,6 +7,7 @@ class Kandan.Views.ChannelPane extends Backbone.View
$(container).html @paginatedActivitiesView()
$(container).append @chatboxView()
@setIdAndData(container)
Kandan.Helpers.Audio.createAudioChannel(@options.channel.get('id'))
@

setIdAndData: (container)->
Expand Down

0 comments on commit c3c467d

Please sign in to comment.