Source: AudioRecorder.js

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Guacamole = Guacamole || {};
  20. /**
  21. * Abstract audio recorder which streams arbitrary audio data to an underlying
  22. * Guacamole.OutputStream. It is up to implementations of this class to provide
  23. * some means of handling this Guacamole.OutputStream. Data produced by the
  24. * recorder is to be sent along the provided stream immediately.
  25. *
  26. * @constructor
  27. */
  28. Guacamole.AudioRecorder = function AudioRecorder() {
  29. /**
  30. * Callback which is invoked when the audio recording process has stopped
  31. * and the underlying Guacamole stream has been closed normally. Audio will
  32. * only resume recording if a new Guacamole.AudioRecorder is started. This
  33. * Guacamole.AudioRecorder instance MAY NOT be reused.
  34. *
  35. * @event
  36. */
  37. this.onclose = null;
  38. /**
  39. * Callback which is invoked when the audio recording process cannot
  40. * continue due to an error, if it has started at all. The underlying
  41. * Guacamole stream is automatically closed. Future attempts to record
  42. * audio should not be made, and this Guacamole.AudioRecorder instance
  43. * MAY NOT be reused.
  44. *
  45. * @event
  46. */
  47. this.onerror = null;
  48. };
  49. /**
  50. * Determines whether the given mimetype is supported by any built-in
  51. * implementation of Guacamole.AudioRecorder, and thus will be properly handled
  52. * by Guacamole.AudioRecorder.getInstance().
  53. *
  54. * @param {String} mimetype
  55. * The mimetype to check.
  56. *
  57. * @returns {Boolean}
  58. * true if the given mimetype is supported by any built-in
  59. * Guacamole.AudioRecorder, false otherwise.
  60. */
  61. Guacamole.AudioRecorder.isSupportedType = function isSupportedType(mimetype) {
  62. return Guacamole.RawAudioRecorder.isSupportedType(mimetype);
  63. };
  64. /**
  65. * Returns a list of all mimetypes supported by any built-in
  66. * Guacamole.AudioRecorder, in rough order of priority. Beware that only the
  67. * core mimetypes themselves will be listed. Any mimetype parameters, even
  68. * required ones, will not be included in the list. For example, "audio/L8" is
  69. * a supported raw audio mimetype that is supported, but it is invalid without
  70. * additional parameters. Something like "audio/L8;rate=44100" would be valid,
  71. * however (see https://tools.ietf.org/html/rfc4856).
  72. *
  73. * @returns {String[]}
  74. * A list of all mimetypes supported by any built-in
  75. * Guacamole.AudioRecorder, excluding any parameters.
  76. */
  77. Guacamole.AudioRecorder.getSupportedTypes = function getSupportedTypes() {
  78. return Guacamole.RawAudioRecorder.getSupportedTypes();
  79. };
  80. /**
  81. * Returns an instance of Guacamole.AudioRecorder providing support for the
  82. * given audio format. If support for the given audio format is not available,
  83. * null is returned.
  84. *
  85. * @param {Guacamole.OutputStream} stream
  86. * The Guacamole.OutputStream to send audio data through.
  87. *
  88. * @param {String} mimetype
  89. * The mimetype of the audio data to be sent along the provided stream.
  90. *
  91. * @return {Guacamole.AudioRecorder}
  92. * A Guacamole.AudioRecorder instance supporting the given mimetype and
  93. * writing to the given stream, or null if support for the given mimetype
  94. * is absent.
  95. */
  96. Guacamole.AudioRecorder.getInstance = function getInstance(stream, mimetype) {
  97. // Use raw audio recorder if possible
  98. if (Guacamole.RawAudioRecorder.isSupportedType(mimetype))
  99. return new Guacamole.RawAudioRecorder(stream, mimetype);
  100. // No support for given mimetype
  101. return null;
  102. };
  103. /**
  104. * Implementation of Guacamole.AudioRecorder providing support for raw PCM
  105. * format audio. This recorder relies only on the Web Audio API and does not
  106. * require any browser-level support for its audio formats.
  107. *
  108. * @constructor
  109. * @augments Guacamole.AudioRecorder
  110. * @param {Guacamole.OutputStream} stream
  111. * The Guacamole.OutputStream to write audio data to.
  112. *
  113. * @param {String} mimetype
  114. * The mimetype of the audio data to send along the provided stream, which
  115. * must be a "audio/L8" or "audio/L16" mimetype with necessary parameters,
  116. * such as: "audio/L16;rate=44100,channels=2".
  117. */
  118. Guacamole.RawAudioRecorder = function RawAudioRecorder(stream, mimetype) {
  119. /**
  120. * Reference to this RawAudioRecorder.
  121. *
  122. * @private
  123. * @type {Guacamole.RawAudioRecorder}
  124. */
  125. var recorder = this;
  126. /**
  127. * The size of audio buffer to request from the Web Audio API when
  128. * recording or processing audio, in sample-frames. This must be a power of
  129. * two between 256 and 16384 inclusive, as required by
  130. * AudioContext.createScriptProcessor().
  131. *
  132. * @private
  133. * @constant
  134. * @type {Number}
  135. */
  136. var BUFFER_SIZE = 2048;
  137. /**
  138. * The window size to use when applying Lanczos interpolation, commonly
  139. * denoted by the variable "a".
  140. * See: https://en.wikipedia.org/wiki/Lanczos_resampling
  141. *
  142. * @private
  143. * @contant
  144. * @type Number
  145. */
  146. var LANCZOS_WINDOW_SIZE = 3;
  147. /**
  148. * The format of audio this recorder will encode.
  149. *
  150. * @private
  151. * @type {Guacamole.RawAudioFormat}
  152. */
  153. var format = Guacamole.RawAudioFormat.parse(mimetype);
  154. /**
  155. * An instance of a Web Audio API AudioContext object, or null if the
  156. * Web Audio API is not supported.
  157. *
  158. * @private
  159. * @type {AudioContext}
  160. */
  161. var context = Guacamole.AudioContextFactory.getAudioContext();
  162. // Some browsers do not implement navigator.mediaDevices - this
  163. // shims in this functionality to ensure code compatibility.
  164. if (!navigator.mediaDevices)
  165. navigator.mediaDevices = {};
  166. // Browsers that either do not implement navigator.mediaDevices
  167. // at all or do not implement it completely need the getUserMedia
  168. // method defined. This shims in this function by detecting
  169. // one of the supported legacy methods.
  170. if (!navigator.mediaDevices.getUserMedia)
  171. navigator.mediaDevices.getUserMedia = (navigator.getUserMedia
  172. || navigator.webkitGetUserMedia
  173. || navigator.mozGetUserMedia
  174. || navigator.msGetUserMedia).bind(navigator);
  175. /**
  176. * Guacamole.ArrayBufferWriter wrapped around the audio output stream
  177. * provided when this Guacamole.RawAudioRecorder was created.
  178. *
  179. * @private
  180. * @type {Guacamole.ArrayBufferWriter}
  181. */
  182. var writer = new Guacamole.ArrayBufferWriter(stream);
  183. /**
  184. * The type of typed array that will be used to represent each audio packet
  185. * internally. This will be either Int8Array or Int16Array, depending on
  186. * whether the raw audio format is 8-bit or 16-bit.
  187. *
  188. * @private
  189. * @constructor
  190. */
  191. var SampleArray = (format.bytesPerSample === 1) ? window.Int8Array : window.Int16Array;
  192. /**
  193. * The maximum absolute value of any sample within a raw audio packet sent
  194. * by this audio recorder. This depends only on the size of each sample,
  195. * and will be 128 for 8-bit audio and 32768 for 16-bit audio.
  196. *
  197. * @private
  198. * @type {Number}
  199. */
  200. var maxSampleValue = (format.bytesPerSample === 1) ? 128 : 32768;
  201. /**
  202. * The total number of audio samples read from the local audio input device
  203. * over the life of this audio recorder.
  204. *
  205. * @private
  206. * @type {Number}
  207. */
  208. var readSamples = 0;
  209. /**
  210. * The total number of audio samples written to the underlying Guacamole
  211. * connection over the life of this audio recorder.
  212. *
  213. * @private
  214. * @type {Number}
  215. */
  216. var writtenSamples = 0;
  217. /**
  218. * The audio stream provided by the browser, if allowed. If no stream has
  219. * yet been received, this will be null.
  220. *
  221. * @type MediaStream
  222. */
  223. var mediaStream = null;
  224. /**
  225. * The source node providing access to the local audio input device.
  226. *
  227. * @private
  228. * @type {MediaStreamAudioSourceNode}
  229. */
  230. var source = null;
  231. /**
  232. * The script processing node which receives audio input from the media
  233. * stream source node as individual audio buffers.
  234. *
  235. * @private
  236. * @type {ScriptProcessorNode}
  237. */
  238. var processor = null;
  239. /**
  240. * The normalized sinc function. The normalized sinc function is defined as
  241. * 1 for x=0 and sin(PI * x) / (PI * x) for all other values of x.
  242. *
  243. * See: https://en.wikipedia.org/wiki/Sinc_function
  244. *
  245. * @private
  246. * @param {Number} x
  247. * The point at which the normalized sinc function should be computed.
  248. *
  249. * @returns {Number}
  250. * The value of the normalized sinc function at x.
  251. */
  252. var sinc = function sinc(x) {
  253. // The value of sinc(0) is defined as 1
  254. if (x === 0)
  255. return 1;
  256. // Otherwise, normlized sinc(x) is sin(PI * x) / (PI * x)
  257. var piX = Math.PI * x;
  258. return Math.sin(piX) / piX;
  259. };
  260. /**
  261. * Calculates the value of the Lanczos kernal at point x for a given window
  262. * size. See: https://en.wikipedia.org/wiki/Lanczos_resampling
  263. *
  264. * @private
  265. * @param {Number} x
  266. * The point at which the value of the Lanczos kernel should be
  267. * computed.
  268. *
  269. * @param {Number} a
  270. * The window size to use for the Lanczos kernel.
  271. *
  272. * @returns {Number}
  273. * The value of the Lanczos kernel at the given point for the given
  274. * window size.
  275. */
  276. var lanczos = function lanczos(x, a) {
  277. // Lanczos is sinc(x) * sinc(x / a) for -a < x < a ...
  278. if (-a < x && x < a)
  279. return sinc(x) * sinc(x / a);
  280. // ... and 0 otherwise
  281. return 0;
  282. };
  283. /**
  284. * Determines the value of the waveform represented by the audio data at
  285. * the given location. If the value cannot be determined exactly as it does
  286. * not correspond to an exact sample within the audio data, the value will
  287. * be derived through interpolating nearby samples.
  288. *
  289. * @private
  290. * @param {Float32Array} audioData
  291. * An array of audio data, as returned by AudioBuffer.getChannelData().
  292. *
  293. * @param {Number} t
  294. * The relative location within the waveform from which the value
  295. * should be retrieved, represented as a floating point number between
  296. * 0 and 1 inclusive, where 0 represents the earliest point in time and
  297. * 1 represents the latest.
  298. *
  299. * @returns {Number}
  300. * The value of the waveform at the given location.
  301. */
  302. var interpolateSample = function getValueAt(audioData, t) {
  303. // Convert [0, 1] range to [0, audioData.length - 1]
  304. var index = (audioData.length - 1) * t;
  305. // Determine the start and end points for the summation used by the
  306. // Lanczos interpolation algorithm (see: https://en.wikipedia.org/wiki/Lanczos_resampling)
  307. var start = Math.floor(index) - LANCZOS_WINDOW_SIZE + 1;
  308. var end = Math.floor(index) + LANCZOS_WINDOW_SIZE;
  309. // Calculate the value of the Lanczos interpolation function for the
  310. // required range
  311. var sum = 0;
  312. for (var i = start; i <= end; i++) {
  313. sum += (audioData[i] || 0) * lanczos(index - i, LANCZOS_WINDOW_SIZE);
  314. }
  315. return sum;
  316. };
  317. /**
  318. * Converts the given AudioBuffer into an audio packet, ready for streaming
  319. * along the underlying output stream. Unlike the raw audio packets used by
  320. * this audio recorder, AudioBuffers require floating point samples and are
  321. * split into isolated planes of channel-specific data.
  322. *
  323. * @private
  324. * @param {AudioBuffer} audioBuffer
  325. * The Web Audio API AudioBuffer that should be converted to a raw
  326. * audio packet.
  327. *
  328. * @returns {SampleArray}
  329. * A new raw audio packet containing the audio data from the provided
  330. * AudioBuffer.
  331. */
  332. var toSampleArray = function toSampleArray(audioBuffer) {
  333. // Track overall amount of data read
  334. var inSamples = audioBuffer.length;
  335. readSamples += inSamples;
  336. // Calculate the total number of samples that should be written as of
  337. // the audio data just received and adjust the size of the output
  338. // packet accordingly
  339. var expectedWrittenSamples = Math.round(readSamples * format.rate / audioBuffer.sampleRate);
  340. var outSamples = expectedWrittenSamples - writtenSamples;
  341. // Update number of samples written
  342. writtenSamples += outSamples;
  343. // Get array for raw PCM storage
  344. var data = new SampleArray(outSamples * format.channels);
  345. // Convert each channel
  346. for (var channel = 0; channel < format.channels; channel++) {
  347. var audioData = audioBuffer.getChannelData(channel);
  348. // Fill array with data from audio buffer channel
  349. var offset = channel;
  350. for (var i = 0; i < outSamples; i++) {
  351. data[offset] = interpolateSample(audioData, i / (outSamples - 1)) * maxSampleValue;
  352. offset += format.channels;
  353. }
  354. }
  355. return data;
  356. };
  357. /**
  358. * Requests access to the user's microphone and begins capturing audio. All
  359. * received audio data is resampled as necessary and forwarded to the
  360. * Guacamole stream underlying this Guacamole.RawAudioRecorder. This
  361. * function must be invoked ONLY ONCE per instance of
  362. * Guacamole.RawAudioRecorder.
  363. *
  364. * @private
  365. */
  366. var beginAudioCapture = function beginAudioCapture() {
  367. // Attempt to retrieve an audio input stream from the browser
  368. navigator.mediaDevices.getUserMedia({ 'audio' : true }, function streamReceived(stream) {
  369. // Create processing node which receives appropriately-sized audio buffers
  370. processor = context.createScriptProcessor(BUFFER_SIZE, format.channels, format.channels);
  371. processor.connect(context.destination);
  372. // Send blobs when audio buffers are received
  373. processor.onaudioprocess = function processAudio(e) {
  374. writer.sendData(toSampleArray(e.inputBuffer).buffer);
  375. };
  376. // Connect processing node to user's audio input source
  377. source = context.createMediaStreamSource(stream);
  378. source.connect(processor);
  379. // Save stream for later cleanup
  380. mediaStream = stream;
  381. }, function streamDenied() {
  382. // Simply end stream if audio access is not allowed
  383. writer.sendEnd();
  384. // Notify of closure
  385. if (recorder.onerror)
  386. recorder.onerror();
  387. });
  388. };
  389. /**
  390. * Stops capturing audio, if the capture has started, freeing all associated
  391. * resources. If the capture has not started, this function simply ends the
  392. * underlying Guacamole stream.
  393. *
  394. * @private
  395. */
  396. var stopAudioCapture = function stopAudioCapture() {
  397. // Disconnect media source node from script processor
  398. if (source)
  399. source.disconnect();
  400. // Disconnect associated script processor node
  401. if (processor)
  402. processor.disconnect();
  403. // Stop capture
  404. if (mediaStream) {
  405. var tracks = mediaStream.getTracks();
  406. for (var i = 0; i < tracks.length; i++)
  407. tracks[i].stop();
  408. }
  409. // Remove references to now-unneeded components
  410. processor = null;
  411. source = null;
  412. mediaStream = null;
  413. // End stream
  414. writer.sendEnd();
  415. };
  416. // Once audio stream is successfully open, request and begin reading audio
  417. writer.onack = function audioStreamAcknowledged(status) {
  418. // Begin capture if successful response and not yet started
  419. if (status.code === Guacamole.Status.Code.SUCCESS && !mediaStream)
  420. beginAudioCapture();
  421. // Otherwise stop capture and cease handling any further acks
  422. else {
  423. // Stop capturing audio
  424. stopAudioCapture();
  425. writer.onack = null;
  426. // Notify if stream has closed normally
  427. if (status.code === Guacamole.Status.Code.RESOURCE_CLOSED) {
  428. if (recorder.onclose)
  429. recorder.onclose();
  430. }
  431. // Otherwise notify of closure due to error
  432. else {
  433. if (recorder.onerror)
  434. recorder.onerror();
  435. }
  436. }
  437. };
  438. };
  439. Guacamole.RawAudioRecorder.prototype = new Guacamole.AudioRecorder();
  440. /**
  441. * Determines whether the given mimetype is supported by
  442. * Guacamole.RawAudioRecorder.
  443. *
  444. * @param {String} mimetype
  445. * The mimetype to check.
  446. *
  447. * @returns {Boolean}
  448. * true if the given mimetype is supported by Guacamole.RawAudioRecorder,
  449. * false otherwise.
  450. */
  451. Guacamole.RawAudioRecorder.isSupportedType = function isSupportedType(mimetype) {
  452. // No supported types if no Web Audio API
  453. if (!Guacamole.AudioContextFactory.getAudioContext())
  454. return false;
  455. return Guacamole.RawAudioFormat.parse(mimetype) !== null;
  456. };
  457. /**
  458. * Returns a list of all mimetypes supported by Guacamole.RawAudioRecorder. Only
  459. * the core mimetypes themselves will be listed. Any mimetype parameters, even
  460. * required ones, will not be included in the list. For example, "audio/L8" is
  461. * a raw audio mimetype that may be supported, but it is invalid without
  462. * additional parameters. Something like "audio/L8;rate=44100" would be valid,
  463. * however (see https://tools.ietf.org/html/rfc4856).
  464. *
  465. * @returns {String[]}
  466. * A list of all mimetypes supported by Guacamole.RawAudioRecorder,
  467. * excluding any parameters. If the necessary JavaScript APIs for recording
  468. * raw audio are absent, this list will be empty.
  469. */
  470. Guacamole.RawAudioRecorder.getSupportedTypes = function getSupportedTypes() {
  471. // No supported types if no Web Audio API
  472. if (!Guacamole.AudioContextFactory.getAudioContext())
  473. return [];
  474. // We support 8-bit and 16-bit raw PCM
  475. return [
  476. 'audio/L8',
  477. 'audio/L16'
  478. ];
  479. };