Class: Pachelbel::AudioContext
- Inherits:
-
Object
- Object
- Pachelbel::AudioContext
- Defined in:
- lib/project/Pachelbel.rb
Constant Summary collapse
- HTML =
'<script>' + 'window.AudioContext = window.AudioContext || window.webkitAudioContext;' + 'var audio_context = new AudioContext();' + 'var audio_nodes = [audio_context.destination];' + '</script>'
Instance Attribute Summary collapse
-
#locked ⇒ Object
readonly
Returns the value of attribute locked.
-
#webview ⇒ Object
readonly
Returns the value of attribute webview.
Instance Method Summary collapse
- #createGain ⇒ Object
-
#createOscillator ⇒ Object
API Constructors.
-
#currentTime ⇒ Object
API Attributes.
- #destination ⇒ Object
-
#initialize ⇒ AudioContext
constructor
A new instance of AudioContext.
- #listener ⇒ Object
- #on_ready(&block) ⇒ Object
- #run_js(script) ⇒ Object
- #sampleRate ⇒ Object
- #webView(_, shouldStartLoadWithRequest: request, navigationType: _) ⇒ Object
- #webViewDidFinishLoad(_) ⇒ Object
Constructor Details
#initialize ⇒ AudioContext
Returns a new instance of AudioContext.
17 18 19 20 21 22 23 |
# File 'lib/project/Pachelbel.rb', line 17 def initialize @locked = true @on_ready = [] @webview = UIWebView.new @webview.delegate = self @webview.loadHTMLString HTML, baseURL: nil end |
Instance Attribute Details
#locked ⇒ Object (readonly)
Returns the value of attribute locked.
4 5 6 |
# File 'lib/project/Pachelbel.rb', line 4 def locked @locked end |
#webview ⇒ Object (readonly)
Returns the value of attribute webview.
3 4 5 |
# File 'lib/project/Pachelbel.rb', line 3 def webview @webview end |
Instance Method Details
#createGain ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/project/Pachelbel.rb', line 78 def createGain object = GainNode.allocate object._audio_context = self js_method = UIDevice.currentDevice.systemVersion[0] == '6' ? 'createGainNode' : 'createGain' object._index = run_js("audio_nodes.push( audio_context.#{js_method}() );").to_i - 1 object end |
#createOscillator ⇒ Object
API Constructors
71 72 73 74 75 76 |
# File 'lib/project/Pachelbel.rb', line 71 def createOscillator object = OscillatorNode.allocate object._audio_context = self object._index = run_js('audio_nodes.push( audio_context.createOscillator() );').to_i - 1 object end |
#currentTime ⇒ Object
API Attributes
48 49 50 |
# File 'lib/project/Pachelbel.rb', line 48 def currentTime run_js('audio_context.currentTime;').to_f end |
#destination ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/project/Pachelbel.rb', line 52 def destination @destination ||= begin object = AudioDestinationNode.allocate object._audio_context = self object._index = 0 object end end |
#listener ⇒ Object
61 62 63 |
# File 'lib/project/Pachelbel.rb', line 61 def listener run_js('audio_context.listener;') end |
#on_ready(&block) ⇒ Object
9 10 11 |
# File 'lib/project/Pachelbel.rb', line 9 def on_ready(&block) @locked ? @on_ready.push(block) : yield end |
#run_js(script) ⇒ Object
13 14 15 |
# File 'lib/project/Pachelbel.rb', line 13 def run_js(script) @webview.stringByEvaluatingJavaScriptFromString script end |
#sampleRate ⇒ Object
65 66 67 |
# File 'lib/project/Pachelbel.rb', line 65 def sampleRate run_js('audio_context.sampleRate;').to_f end |
#webView(_, shouldStartLoadWithRequest: request, navigationType: _) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/project/Pachelbel.rb', line 34 def webView(_, shouldStartLoadWithRequest: request, navigationType: _) case request.URL.absoluteString when "let://freedom.ring" @locked = false @on_ready.each(&:call) @on_ready = nil return false else return true end end |
#webViewDidFinishLoad(_) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/project/Pachelbel.rb', line 25 def webViewDidFinishLoad(_) run_js 'var unlock_iframe = document.createElement("IFRAME"); unlock_iframe.setAttribute("src", "let://freedom.ring");' run_js 'var pollster = function() { if (audio_context.currentTime === 0) { setTimeout(pollster, 0); } else { document.documentElement.appendChild(unlock_iframe); } }' run_js 'audio_context.createOscillator().noteOn(0); setTimeout(pollster, 0);' end |