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         var play_when;
152 
153         function playDelayed(buffer) {
154             source.buffer = buffer;
155             source.noteOn(play_when / 1000);
156         }
157 
158         /** @ignore */
159         this.play = function(when) {
160             
161             play_when = when;
162             
163             // If buffer available, play it NOW
164             if (readyBuffer)
165                 playDelayed(readyBuffer);
166 
167             // Otherwise, play when decoded
168             else
169                 handleReady = playDelayed;
170 
171         };
172 
173     }
174 
175     else {
176 
177         var play_on_load = false;
178 
179         // Create audio element to house and play the data
180         var audio = new Audio();
181 
182         // Read data and start decoding
183         var reader = new FileReader();
184         reader.onload = function() {
185 
186             var binary = "";
187             var bytes = new Uint8Array(reader.result);
188 
189             // Produce binary string from bytes in buffer
190             for (var i=0; i<bytes.byteLength; i++)
191                 binary += String.fromCharCode(bytes[i]);
192 
193             // Convert to data URI 
194             audio.src = "data:" + mimetype + ";base64," + window.btoa(binary);
195 
196             // Play if play was attempted but packet wasn't loaded yet
197             if (play_on_load)
198                 audio.play();
199 
200         };
201         reader.readAsArrayBuffer(data);
202    
203         function play() {
204 
205             // If audio data is ready, play now
206             if (audio.src)
207                 audio.play();
208 
209             // Otherwise, play when loaded
210             else
211                 play_on_load = true;
212 
213         }
214         
215         /** @ignore */
216         this.play = function(when) {
217             
218             // Calculate time until play
219             var now = Guacamole.AudioChannel.getTimestamp();
220             var delay = when - now;
221             
222             // Play now if too late
223             if (delay < 0)
224                 play();
225 
226             // Otherwise, schedule later playback
227             else
228                 window.setTimeout(play, delay);
229 
230         };
231 
232     }
233 
234 };
235