Source: RawAudioFormat.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. * A description of the format of raw PCM audio, such as that used by
  22. * Guacamole.RawAudioPlayer and Guacamole.RawAudioRecorder. This object
  23. * describes the number of bytes per sample, the number of channels, and the
  24. * overall sample rate.
  25. *
  26. * @constructor
  27. * @param {Guacamole.RawAudioFormat|Object} template
  28. * The object whose properties should be copied into the corresponding
  29. * properties of the new Guacamole.RawAudioFormat.
  30. */
  31. Guacamole.RawAudioFormat = function RawAudioFormat(template) {
  32. /**
  33. * The number of bytes in each sample of audio data. This value is
  34. * independent of the number of channels.
  35. *
  36. * @type {Number}
  37. */
  38. this.bytesPerSample = template.bytesPerSample;
  39. /**
  40. * The number of audio channels (ie: 1 for mono, 2 for stereo).
  41. *
  42. * @type {Number}
  43. */
  44. this.channels = template.channels;
  45. /**
  46. * The number of samples per second, per channel.
  47. *
  48. * @type {Number}
  49. */
  50. this.rate = template.rate;
  51. };
  52. /**
  53. * Parses the given mimetype, returning a new Guacamole.RawAudioFormat
  54. * which describes the type of raw audio data represented by that mimetype. If
  55. * the mimetype is not a supported raw audio data mimetype, null is returned.
  56. *
  57. * @param {String} mimetype
  58. * The audio mimetype to parse.
  59. *
  60. * @returns {Guacamole.RawAudioFormat}
  61. * A new Guacamole.RawAudioFormat which describes the type of raw
  62. * audio data represented by the given mimetype, or null if the given
  63. * mimetype is not supported.
  64. */
  65. Guacamole.RawAudioFormat.parse = function parseFormat(mimetype) {
  66. var bytesPerSample;
  67. // Rate is absolutely required - if null is still present later, the
  68. // mimetype must not be supported
  69. var rate = null;
  70. // Default for both "audio/L8" and "audio/L16" is one channel
  71. var channels = 1;
  72. // "audio/L8" has one byte per sample
  73. if (mimetype.substring(0, 9) === 'audio/L8;') {
  74. mimetype = mimetype.substring(9);
  75. bytesPerSample = 1;
  76. }
  77. // "audio/L16" has two bytes per sample
  78. else if (mimetype.substring(0, 10) === 'audio/L16;') {
  79. mimetype = mimetype.substring(10);
  80. bytesPerSample = 2;
  81. }
  82. // All other types are unsupported
  83. else
  84. return null;
  85. // Parse all parameters
  86. var parameters = mimetype.split(',');
  87. for (var i = 0; i < parameters.length; i++) {
  88. var parameter = parameters[i];
  89. // All parameters must have an equals sign separating name from value
  90. var equals = parameter.indexOf('=');
  91. if (equals === -1)
  92. return null;
  93. // Parse name and value from parameter string
  94. var name = parameter.substring(0, equals);
  95. var value = parameter.substring(equals+1);
  96. // Handle each supported parameter
  97. switch (name) {
  98. // Number of audio channels
  99. case 'channels':
  100. channels = parseInt(value);
  101. break;
  102. // Sample rate
  103. case 'rate':
  104. rate = parseInt(value);
  105. break;
  106. // All other parameters are unsupported
  107. default:
  108. return null;
  109. }
  110. };
  111. // The rate parameter is required
  112. if (rate === null)
  113. return null;
  114. // Return parsed format details
  115. return new Guacamole.RawAudioFormat({
  116. bytesPerSample : bytesPerSample,
  117. channels : channels,
  118. rate : rate
  119. });
  120. };