1 /* 2 * Copyright (C) 2013 Glyptodon LLC 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 * THE SOFTWARE. 21 */ 22 23 var Guacamole = Guacamole || {}; 24 25 /** 26 * Abstract audio channel which queues and plays arbitrary audio data. 27 * @constructor 28 */ 29 Guacamole.AudioChannel = function() { 30 31 /** 32 * Reference to this AudioChannel. 33 * @private 34 */ 35 var channel = this; 36 37 /** 38 * When the next packet should play. 39 * @private 40 */ 41 var next_packet_time = 0; 42 43 /** 44 * Queues up the given data for playing by this channel once all previously 45 * queued data has been played. If no data has been queued, the data will 46 * play immediately. 47 * 48 * @param {String} mimetype The mimetype of the data provided. 49 * @param {Number} duration The duration of the data provided, in 50 * milliseconds. 51 * @param {Blob} data The blob data to play. 52 */ 53 this.play = function(mimetype, duration, data) { 54 55 var packet = 56 new Guacamole.AudioChannel.Packet(mimetype, data); 57 58 var now = Guacamole.AudioChannel.getTimestamp(); 59 60 // If underflow is detected, reschedule new packets relative to now. 61 if (next_packet_time < now) 62 next_packet_time = now; 63 64 // Schedule next packet 65 packet.play(next_packet_time); 66 next_packet_time += duration; 67 68 }; 69 70 }; 71 72 // Define context if available 73 if (window.webkitAudioContext) { 74 Guacamole.AudioChannel.context = new webkitAudioContext(); 75 } 76 77 /** 78 * Returns a base timestamp which can be used for scheduling future audio 79 * playback. Scheduling playback for the value returned by this function plus 80 * N will cause the associated audio to be played back N milliseconds after 81 * the function is called. 82 * 83 * @return {Number} An arbitrary channel-relative timestamp, in milliseconds. 84 */ 85 Guacamole.AudioChannel.getTimestamp = function() { 86 87 // If we have an audio context, use its timestamp 88 if (Guacamole.AudioChannel.context) 89 return Guacamole.AudioChannel.context.currentTime * 1000; 90 91 // If we have high-resolution timers, use those 92 if (window.performance) { 93 94 if (window.performance.now) 95 return window.performance.now(); 96 97 if (window.performance.webkitNow) 98 return window.performance.webkitNow(); 99 100 } 101 102 // Fallback to millisecond-resolution system time 103 return new Date().getTime(); 104 105 }; 106 107 /** 108 * Abstract representation of an audio packet. 109 * 110 * @constructor 111 * 112 * @param {String} mimetype The mimetype of the data contained by this packet. 113 * @param {Blob} data The blob of sound data contained by this packet. 114 */ 115 Guacamole.AudioChannel.Packet = function(mimetype, data) { 116 117 /** 118 * Schedules this packet for playback at the given time. 119 * 120 * @function 121 * @param {Number} when The time this packet should be played, in 122 * milliseconds. 123 */ 124 this.play = undefined; // Defined conditionally depending on support 125 126 // If audio API available, use it. 127 if (Guacamole.AudioChannel.context) { 128 129 var readyBuffer = null; 130 131 // By default, when decoding finishes, store buffer for future 132 // playback 133 var handleReady = function(buffer) { 134 readyBuffer = buffer; 135 }; 136 137 // Read data and start decoding 138 var reader = new FileReader(); 139 reader.onload = function() { 140 Guacamole.AudioChannel.context.decodeAudioData( 141 reader.result, 142 function(buffer) { handleReady(buffer); } 143 ); 144 }; 145 reader.readAsArrayBuffer(data); 146 147 // Set up buffer source 148 var source = Guacamole.AudioChannel.context.createBufferSource(); 149 source.connect(Guacamole.AudioChannel.context.destination); 150 151 // Use noteOn() instead of start() if necessary 152 if (!source.start) 153 source.start = source.noteOn; 154 155 var play_when; 156 157 function playDelayed(buffer) { 158 source.buffer = buffer; 159 source.start(play_when / 1000); 160 } 161 162 /** @ignore */ 163 this.play = function(when) { 164 165 play_when = when; 166 167 // If buffer available, play it NOW 168 if (readyBuffer) 169 playDelayed(readyBuffer); 170 171 // Otherwise, play when decoded 172 else 173 handleReady = playDelayed; 174 175 }; 176 177 } 178 179 else { 180 181 var play_on_load = false; 182 183 // Create audio element to house and play the data 184 var audio = new Audio(); 185 186 // Read data and start decoding 187 var reader = new FileReader(); 188 reader.onload = function() { 189 190 var binary = ""; 191 var bytes = new Uint8Array(reader.result); 192 193 // Produce binary string from bytes in buffer 194 for (var i=0; i<bytes.byteLength; i++) 195 binary += String.fromCharCode(bytes[i]); 196 197 // Convert to data URI 198 audio.src = "data:" + mimetype + ";base64," + window.btoa(binary); 199 200 // Play if play was attempted but packet wasn't loaded yet 201 if (play_on_load) 202 audio.play(); 203 204 }; 205 reader.readAsArrayBuffer(data); 206 207 function play() { 208 209 // If audio data is ready, play now 210 if (audio.src) 211 audio.play(); 212 213 // Otherwise, play when loaded 214 else 215 play_on_load = true; 216 217 } 218 219 /** @ignore */ 220 this.play = function(when) { 221 222 // Calculate time until play 223 var now = Guacamole.AudioChannel.getTimestamp(); 224 var delay = when - now; 225 226 // Play now if too late 227 if (delay < 0) 228 play(); 229 230 // Otherwise, schedule later playback 231 else 232 window.setTimeout(play, delay); 233 234 }; 235 236 } 237 238 }; 239