Advertisement
Shiny_

Untitled

Nov 19th, 2014
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. URL = "Gesaffelstein%20&%20Jesper%20Kyd%20-%20In%20Pursuit%20for%20Ezio%27s%20Family.ogg"
  2.  
  3. unless window.AudioContext
  4.     alert "Nie znaleziono obsługi dla AudioContext!" unless window.webkitAudioContext
  5.     window.AudioContext             = window.webkitAudioContext
  6.  
  7. context                             = new AudioContext()
  8. ctx                                 = $("#music").get()[0].getContext("2d")
  9. gradient                            = ctx.createLinearGradient(0, 0, 0, 325)
  10. javascriptNode                      = 0
  11. sourceNode                          = 0
  12.  
  13. gradient.addColorStop 1, "#FFFFFF"
  14. gradient.addColorStop 0.75, "#00FFFF"
  15. gradient.addColorStop 0.25, "#0000FF"
  16. gradient.addColorStop 0, "#000000"
  17.  
  18. setupAudioNodes()
  19.  
  20. setupAudioNodes = ->
  21.     javascriptNode                  = context.createScriptProcessor(2048, 1, 1)
  22.     javascriptNode.connect(context.destination)
  23.     analyser                        = context.createAnalyser()
  24.     analyser.smoothingTimeConstant  = 0.75
  25.     analyser.fftsize                = 512
  26.     sourceNode                      = context.createBufferSource()
  27.     sourceNode.connect(analyser)
  28.     analyser.connect(javascriptNode)
  29.     sourceNode.connect(context.destination)
  30.     return
  31.  
  32. loadSound = (url) ->
  33.     request                         = new XMLHttpRequest()
  34.     request.open                    "GET", url, true
  35.     request.responseType            = "arraybuffer"
  36.     request.onload = () ->
  37.         context.decodeAudioData request.response, (buffer) ->
  38.             playSound buffer
  39.             return
  40.         , onError
  41.         return
  42.     request.send()
  43.     return
  44.  
  45. playSound = (buffer) ->
  46.     sourceNode.buffer = buffer
  47.     sourceNode.start(0)
  48.     return
  49.  
  50. onError = (e) ->
  51.     console.log(e)
  52.     return
  53.  
  54. javascriptNode.onaudioprocess = ->
  55.     array = new Uint8Array(analyser.frequencyBinCount)
  56.     analyser.getByteFrequencyData(array)
  57.     ctx.clearRect 0, 0, 1024, 325
  58.     ctx.fillStyle = gradient
  59.     drawSpectrum array
  60.     return
  61.  
  62. drawSpectrum = (array) ->
  63.     i = 0
  64.     while i < (array.length)
  65.         val = array[i]
  66.         ctx.fillRect(i * 5, 325 - val, 3, 325)
  67.     return
  68.  
  69. $(document).ready ->
  70.     $.ajax
  71.         url: URL,
  72.         success: ->
  73.             $("#play_button").show()
  74.             return
  75.     return
  76.  
  77. loadSound(URL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement