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