Source: main/webapp/modules/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. * @private
  222. * @type {MediaStream}
  223. */
  224. var mediaStream = null;
  225. /**
  226. * The source node providing access to the local audio input device.
  227. *
  228. * @private
  229. * @type {MediaStreamAudioSourceNode}
  230. */
  231. var source = null;
  232. /**
  233. * The script processing node which receives audio input from the media
  234. * stream source node as individual audio buffers.
  235. *
  236. * @private
  237. * @type {ScriptProcessorNode}
  238. */
  239. var processor = null;
  240. /**
  241. * The normalized sinc function. The normalized sinc function is defined as
  242. * 1 for x=0 and sin(PI * x) / (PI * x) for all other values of x.
  243. *
  244. * See: https://en.wikipedia.org/wiki/Sinc_function
  245. *
  246. * @private
  247. * @param {!number} x
  248. * The point at which the normalized sinc function should be computed.
  249. *
  250. * @returns {!number}
  251. * The value of the normalized sinc function at x.
  252. */
  253. var sinc = function sinc(x) {
  254. // The value of sinc(0) is defined as 1
  255. if (x === 0)
  256. return 1;
  257. // Otherwise, normlized sinc(x) is sin(PI * x) / (PI * x)
  258. var piX = Math.PI * x;
  259. return Math.sin(piX) / piX;
  260. };
  261. /**
  262. * Calculates the value of the Lanczos kernal at point x for a given window
  263. * size. See: https://en.wikipedia.org/wiki/Lanczos_resampling
  264. *
  265. * @private
  266. * @param {!number} x
  267. * The point at which the value of the Lanczos kernel should be
  268. * computed.
  269. *
  270. * @param {!number} a
  271. * The window size to use for the Lanczos kernel.
  272. *
  273. * @returns {!number}
  274. * The value of the Lanczos kernel at the given point for the given
  275. * window size.
  276. */
  277. var lanczos = function lanczos(x, a) {
  278. // Lanczos is sinc(x) * sinc(x / a) for -a < x < a ...
  279. if (-a < x && x < a)
  280. return sinc(x) * sinc(x / a);
  281. // ... and 0 otherwise
  282. return 0;
  283. };
  284. /**
  285. * Determines the value of the waveform represented by the audio data at
  286. * the given location. If the value cannot be determined exactly as it does
  287. * not correspond to an exact sample within the audio data, the value will
  288. * be derived through interpolating nearby samples.
  289. *
  290. * @private
  291. * @param {!Float32Array} audioData
  292. * An array of audio data, as returned by AudioBuffer.getChannelData().
  293. *
  294. * @param {!number} t
  295. * The relative location within the waveform from which the value
  296. * should be retrieved, represented as a floating point number between
  297. * 0 and 1 inclusive, where 0 represents the earliest point in time and
  298. * 1 represents the latest.
  299. *
  300. * @returns {!number}
  301. * The value of the waveform at the given location.
  302. */
  303. var interpolateSample = function getValueAt(audioData, t) {
  304. // Convert [0, 1] range to [0, audioData.length - 1]
  305. var index = (audioData.length - 1) * t;
  306. // Determine the start and end points for the summation used by the
  307. // Lanczos interpolation algorithm (see: https://en.wikipedia.org/wiki/Lanczos_resampling)
  308. var start = Math.floor(index) - LANCZOS_WINDOW_SIZE + 1;
  309. var end = Math.floor(index) + LANCZOS_WINDOW_SIZE;
  310. // Calculate the value of the Lanczos interpolation function for the
  311. // required range
  312. var sum = 0;
  313. for (var i = start; i <= end; i++) {
  314. sum += (audioData[i] || 0) * lanczos(index - i, LANCZOS_WINDOW_SIZE);
  315. }
  316. return sum;
  317. };
  318. /**
  319. * Converts the given AudioBuffer into an audio packet, ready for streaming
  320. * along the underlying output stream. Unlike the raw audio packets used by
  321. * this audio recorder, AudioBuffers require floating point samples and are
  322. * split into isolated planes of channel-specific data.
  323. *
  324. * @private
  325. * @param {!AudioBuffer} audioBuffer
  326. * The Web Audio API AudioBuffer that should be converted to a raw
  327. * audio packet.
  328. *
  329. * @returns {!SampleArray}
  330. * A new raw audio packet containing the audio data from the provided
  331. * AudioBuffer.
  332. */
  333. var toSampleArray = function toSampleArray(audioBuffer) {
  334. // Track overall amount of data read
  335. var inSamples = audioBuffer.length;
  336. readSamples += inSamples;
  337. // Calculate the total number of samples that should be written as of
  338. // the audio data just received and adjust the size of the output
  339. // packet accordingly
  340. var expectedWrittenSamples = Math.round(readSamples * format.rate / audioBuffer.sampleRate);
  341. var outSamples = expectedWrittenSamples - writtenSamples;
  342. // Update number of samples written
  343. writtenSamples += outSamples;
  344. // Get array for raw PCM storage
  345. var data = new SampleArray(outSamples * format.channels);
  346. // Convert each channel
  347. for (var channel = 0; channel < format.channels; channel++) {
  348. var audioData = audioBuffer.getChannelData(channel);
  349. // Fill array with data from audio buffer channel
  350. var offset = channel;
  351. for (var i = 0; i < outSamples; i++) {
  352. data[offset] = interpolateSample(audioData, i / (outSamples - 1)) * maxSampleValue;
  353. offset += format.channels;
  354. }
  355. }
  356. return data;
  357. };
  358. /**
  359. * getUserMedia() callback which handles successful retrieval of an
  360. * audio stream (successful start of recording).
  361. *
  362. * @private
  363. * @param {!MediaStream} stream
  364. * A MediaStream which provides access to audio data read from the
  365. * user's local audio input device.
  366. */
  367. var streamReceived = function streamReceived(stream) {
  368. // Create processing node which receives appropriately-sized audio buffers
  369. processor = context.createScriptProcessor(BUFFER_SIZE, format.channels, format.channels);
  370. processor.connect(context.destination);
  371. // Send blobs when audio buffers are received
  372. processor.onaudioprocess = function processAudio(e) {
  373. writer.sendData(toSampleArray(e.inputBuffer).buffer);
  374. };
  375. // Connect processing node to user's audio input source
  376. source = context.createMediaStreamSource(stream);
  377. source.connect(processor);
  378. // Attempt to explicitly resume AudioContext, as it may be paused
  379. // by default
  380. if (context.state === 'suspended')
  381. context.resume();
  382. // Save stream for later cleanup
  383. mediaStream = stream;
  384. };
  385. /**
  386. * getUserMedia() callback which handles audio recording denial. The
  387. * underlying Guacamole output stream is closed, and the failure to
  388. * record is noted using onerror.
  389. *
  390. * @private
  391. */
  392. var streamDenied = function streamDenied() {
  393. // Simply end stream if audio access is not allowed
  394. writer.sendEnd();
  395. // Notify of closure
  396. if (recorder.onerror)
  397. recorder.onerror();
  398. };
  399. /**
  400. * Requests access to the user's microphone and begins capturing audio. All
  401. * received audio data is resampled as necessary and forwarded to the
  402. * Guacamole stream underlying this Guacamole.RawAudioRecorder. This
  403. * function must be invoked ONLY ONCE per instance of
  404. * Guacamole.RawAudioRecorder.
  405. *
  406. * @private
  407. */
  408. var beginAudioCapture = function beginAudioCapture() {
  409. // Attempt to retrieve an audio input stream from the browser
  410. var promise = navigator.mediaDevices.getUserMedia({
  411. 'audio' : true
  412. }, streamReceived, streamDenied);
  413. // Handle stream creation/rejection via Promise for newer versions of
  414. // getUserMedia()
  415. if (promise && promise.then)
  416. promise.then(streamReceived, streamDenied);
  417. };
  418. /**
  419. * Stops capturing audio, if the capture has started, freeing all associated
  420. * resources. If the capture has not started, this function simply ends the
  421. * underlying Guacamole stream.
  422. *
  423. * @private
  424. */
  425. var stopAudioCapture = function stopAudioCapture() {
  426. // Disconnect media source node from script processor
  427. if (source)
  428. source.disconnect();
  429. // Disconnect associated script processor node
  430. if (processor)
  431. processor.disconnect();
  432. // Stop capture
  433. if (mediaStream) {
  434. var tracks = mediaStream.getTracks();
  435. for (var i = 0; i < tracks.length; i++)
  436. tracks[i].stop();
  437. }
  438. // Remove references to now-unneeded components
  439. processor = null;
  440. source = null;
  441. mediaStream = null;
  442. // End stream
  443. writer.sendEnd();
  444. };
  445. // Once audio stream is successfully open, request and begin reading audio
  446. writer.onack = function audioStreamAcknowledged(status) {
  447. // Begin capture if successful response and not yet started
  448. if (status.code === Guacamole.Status.Code.SUCCESS && !mediaStream)
  449. beginAudioCapture();
  450. // Otherwise stop capture and cease handling any further acks
  451. else {
  452. // Stop capturing audio
  453. stopAudioCapture();
  454. writer.onack = null;
  455. // Notify if stream has closed normally
  456. if (status.code === Guacamole.Status.Code.RESOURCE_CLOSED) {
  457. if (recorder.onclose)
  458. recorder.onclose();
  459. }
  460. // Otherwise notify of closure due to error
  461. else {
  462. if (recorder.onerror)
  463. recorder.onerror();
  464. }
  465. }
  466. };
  467. };
  468. Guacamole.RawAudioRecorder.prototype = new Guacamole.AudioRecorder();
  469. /**
  470. * Determines whether the given mimetype is supported by
  471. * Guacamole.RawAudioRecorder.
  472. *
  473. * @param {!string} mimetype
  474. * The mimetype to check.
  475. *
  476. * @returns {!boolean}
  477. * true if the given mimetype is supported by Guacamole.RawAudioRecorder,
  478. * false otherwise.
  479. */
  480. Guacamole.RawAudioRecorder.isSupportedType = function isSupportedType(mimetype) {
  481. // No supported types if no Web Audio API
  482. if (!Guacamole.AudioContextFactory.getAudioContext())
  483. return false;
  484. return Guacamole.RawAudioFormat.parse(mimetype) !== null;
  485. };
  486. /**
  487. * Returns a list of all mimetypes supported by Guacamole.RawAudioRecorder. Only
  488. * the core mimetypes themselves will be listed. Any mimetype parameters, even
  489. * required ones, will not be included in the list. For example, "audio/L8" is
  490. * a raw audio mimetype that may be supported, but it is invalid without
  491. * additional parameters. Something like "audio/L8;rate=44100" would be valid,
  492. * however (see https://tools.ietf.org/html/rfc4856).
  493. *
  494. * @returns {!string[]}
  495. * A list of all mimetypes supported by Guacamole.RawAudioRecorder,
  496. * excluding any parameters. If the necessary JavaScript APIs for recording
  497. * raw audio are absent, this list will be empty.
  498. */
  499. Guacamole.RawAudioRecorder.getSupportedTypes = function getSupportedTypes() {
  500. // No supported types if no Web Audio API
  501. if (!Guacamole.AudioContextFactory.getAudioContext())
  502. return [];
  503. // We support 8-bit and 16-bit raw PCM
  504. return [
  505. 'audio/L8',
  506. 'audio/L16'
  507. ];
  508. };