-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.coffee
109 lines (93 loc) · 3.39 KB
/
player.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
window.AudioContext = window.AudioContext||window.webkitAudioContext
class Player
constructor: (@flickr_keys, @status) ->
@source = null
@photoset = {}
set_status: (text) ->
@status.text(text)
extract_audio: (idata, length) ->
view = new Uint8Array(idata)
odata = new Uint8Array(length)
count = 0
for d, i in view
p = Math.floor(i / 4)
if p % 2 == 0 && i % 4 != 3
odata[count] = d
count += 1
if count == length
break
odata.buffer
play: (url) ->
@set_status("Loading file...")
xhr = new XMLHttpRequest()
xhr.open("GET", url, true)
xhr.responseType = "arraybuffer"
xhr.onload = =>
data = new Uint8Array(xhr.response || xhr.mozResponseArrayBuffer)
@png = new PNG(data)
dat = @png.decodePixels()
header = dat.buffer.slice(0, 4)
header_u32 = new Uint32Array(header)
body = dat.buffer.slice(4)
body = @extract_audio(body, header_u32[0])
@stop()
try
context = new window.AudioContext()
catch e
alert('Unable to create AudioContext')
location.replace(location)
source = context.createBufferSource()
@set_status("Decoding file...")
context.decodeAudioData body, (buf) =>
source.buffer = buf
source.loop = false
source.connect(context.destination)
source.start(0)
@source = source
@set_status("Now playing")
, (err) ->
console.error(err)
xhr.send(null)
stop: ->
@source?.stop(0)
@source?.disconnect(0)
@source = null
@set_status("Stopped")
get_photoset: ->
url = "http://api.flickr.com/services/rest/" +
"?method=flickr.photosets.getPhotos&api_key=" +
@flickr_keys.api_key + "&photoset_id=" +
@flickr_keys.photoset_id + "&format=json" +
"&nojsoncallback=1"
$.getJSON(url).done (data) =>
photoset = data.photoset.photo
for photo in photoset
@get_sizes(photo.id)
get_sizes: (photo_id) ->
url = "http://api.flickr.com/services/rest/" +
"?method=flickr.photos.getSizes&api_key=" +
@flickr_keys.api_key + "&photo_id=" +
photo_id + "&format=json" +
"&nojsoncallback=1"
$.getJSON(url).done (data) =>
size = data.sizes.size
for item in size
if item.label == "Small"
img = $("<img />").attr("src", item.source)
handler = (pid, this_) ->
->
this_.on_item_click(pid)
img.on('click', handler(photo_id, this))
$("#photoset").append(img)
if item.label = "Original"
photoset[photo_id] = item.source
on_item_click: (photo_id) ->
url = photoset[photo_id]
proxy_url = @get_proxy_url(url)
$("#nowplaying").attr("src", proxy_url)
$("#nowplaying_a").attr("href", proxy_url)
window.the_url = proxy_url
@play(proxy_url)
get_proxy_url: (url) ->
"/proxy/?url=" + url
window.Player = Player