1964js/coffee/audio.coffee
schibo4@gmail.com eae23be5fc disable audio for now
git-svn-id: http://1964js.googlecode.com/svn/trunk@254 0378edba-076e-5dc0-2bb2-d87a714dcd81
2013-02-24 12:38:22 +00:00

44 lines
No EOL
1.3 KiB
CoffeeScript

class C1964jsAudio
audioContext = undefined
audioBuffer = undefined
constructor: ->
processAudio: (memory, from, length) ->
return
try
return if audioContext is "unsupported"
audioContext = new webkitAudioContext() if audioContext is `undefined`
catch error
#log "Your browser doesn't support Web Audio."
audioContext = "unsupported"
return false
PI_2_400 = 1.0 / (Math.PI * 2 * 400)
# Create/set audio buffer for each chunk
source = audioContext.createBufferSource()
return if length < 4
audioBuffer = audioContext.createBuffer(2, length / 2 / 2, 44100) if audioBuffer is `undefined`
left = audioBuffer.getChannelData(0)
right = audioBuffer.getChannelData(1)
i = from
k = 0
while k < length
left[k] = ((memory.rdramUint8Array[i] << 8 | memory.rdramUint8Array[i + 1]) << 16 >> 16) * PI_2_400
right[k] = ((memory.rdramUint8Array[i + 2] << 8 | memory.rdramUint8Array[i + 3]) << 16 >> 16) * PI_2_400
i += 4
k++
source.buffer = audioBuffer
@startTime += audioBuffer.duration
source.connect audioContext.destination
source.loop = false
source.noteOn @startTime
return true
#hack global space until we export classes properly
#node.js uses exports; browser uses this (window)
root = exports ? this
root.C1964jsAudio = C1964jsAudio