OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * Function to convert an array of bytes to a base64 string | 8 * Function to convert an array of bytes to a base64 string |
9 * TODO(rkc): Change this to use a Uint8array instead of a string. | 9 * TODO(rkc): Change this to use a Uint8array instead of a string. |
10 * @param {string} bytes String containing the bytes we want to convert. | 10 * @param {string} bytes String containing the bytes we want to convert. |
(...skipping 26 matching lines...) Expand all Loading... |
37 * @param {Object} whisperNacl The NaclBridge object to use to communicate with | 37 * @param {Object} whisperNacl The NaclBridge object to use to communicate with |
38 * the whispernet wrapper. | 38 * the whispernet wrapper. |
39 */ | 39 */ |
40 function WhisperEncoder(params, whisperNacl) { | 40 function WhisperEncoder(params, whisperNacl) { |
41 params = params || {}; | 41 params = params || {}; |
42 this.repetitions_ = params.repetitions || 3; | 42 this.repetitions_ = params.repetitions || 3; |
43 | 43 |
44 this.whisperNacl_ = whisperNacl; | 44 this.whisperNacl_ = whisperNacl; |
45 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); | 45 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); |
46 | 46 |
47 var symbolCoder = {}; | |
48 symbolCoder.sample_rate = params.sampleRate || 48000.0; | |
49 symbolCoder.upsampling_factor = params.bitsPerSample || 16; | |
50 symbolCoder.desired_carrier_frequency = params.carrierFrequency || 18500.0; | |
51 symbolCoder.bits_per_symbol = 4; | |
52 symbolCoder.min_cycles_per_frame = 4; | |
53 symbolCoder.baseband_decimation_factor = 4; | |
54 | |
55 var msg = { | 47 var msg = { |
56 type: 'initialize_encoder', | 48 type: 'initialize_encoder', |
57 symbol_coder: symbolCoder, | 49 sample_rate: params.sampleRate || 48000.0, |
58 encoder_params: { | 50 upsampling_factor: params.bitsPerSample || 16, |
59 bytes_per_token: 6, | |
60 include_parity_symbol: true, | |
61 single_sideband: true | |
62 } | |
63 }; | 51 }; |
64 this.whisperNacl_.send(JSON.stringify(msg)); | 52 this.whisperNacl_.send(JSON.stringify(msg)); |
65 } | 53 } |
66 | 54 |
67 /** | 55 /** |
68 * Method to encode a token. | 56 * Method to encode a token. |
69 * @param {string} token Token to encode. | 57 * @param {string} token Token to encode. |
| 58 * @param {boolean} audible Whether we should use encode audible samples. |
70 * @param {boolean} raw Whether we should return the encoded samples in raw | 59 * @param {boolean} raw Whether we should return the encoded samples in raw |
71 * format or as a Wave file. | 60 * format or as a Wave file. |
72 */ | 61 */ |
73 WhisperEncoder.prototype.encode = function(token, raw) { | 62 WhisperEncoder.prototype.encode = function(token, audible, raw) { |
74 var msg = { | 63 var msg = { |
75 type: 'encode_token', | 64 type: 'encode_token', |
76 // Trying to send the token in binary form to Nacl doesn't work correctly. | 65 // Trying to send the token in binary form to Nacl doesn't work correctly. |
77 // We end up with the correct string + a bunch of extra characters. This is | 66 // We end up with the correct string + a bunch of extra characters. This is |
78 // true of returning a binary string too; hence we communicate back and | 67 // true of returning a binary string too; hence we communicate back and |
79 // forth by converting the bytes into an array of integers. | 68 // forth by converting the bytes into an array of integers. |
80 token: stringToArray(token), | 69 token: stringToArray(token), |
81 repetitions: this.repetitions_, | 70 repetitions: this.repetitions_, |
| 71 use_dtmf: audible, |
82 return_raw_samples: raw | 72 return_raw_samples: raw |
83 }; | 73 }; |
84 this.whisperNacl_.send(JSON.stringify(msg)); | 74 this.whisperNacl_.send(JSON.stringify(msg)); |
85 }; | 75 }; |
86 | 76 |
87 /** | 77 /** |
88 * Method to set the callback for encoded audio data received from the encoder | 78 * Method to set the callback for encoded audio data received from the encoder |
89 * when we finish encoding a token. | 79 * when we finish encoding a token. |
90 * @param {function(string, ArrayBuffer)} callback Callback which will receive | 80 * @param {function(string, ArrayBuffer)} callback Callback which will receive |
91 * the audio samples. | 81 * the audio samples. |
(...skipping 23 matching lines...) Expand all Loading... |
115 * the whispernet wrapper. | 105 * the whispernet wrapper. |
116 */ | 106 */ |
117 function WhisperDecoder(params, whisperNacl) { | 107 function WhisperDecoder(params, whisperNacl) { |
118 params = params || {}; | 108 params = params || {}; |
119 | 109 |
120 this.whisperNacl_ = whisperNacl; | 110 this.whisperNacl_ = whisperNacl; |
121 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); | 111 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); |
122 | 112 |
123 var msg = { | 113 var msg = { |
124 type: 'initialize_decoder', | 114 type: 'initialize_decoder', |
125 num_channels: params.channels, | 115 channels: params.channels || 1, |
126 symbol_coder: { | 116 sample_rate: params.sampleRate || 48000.0, |
127 sample_rate: params.sampleRate || 48000.0, | 117 upsampling_factor: params.bitsPerSample || 16, |
128 upsampling_factor: params.bitsPerSample || 16, | 118 max_candidates: 1, |
129 desired_carrier_frequency: params.carrierFrequency || 18500.0, | 119 max_buffer_duration_in_seconds: 3 |
130 bits_per_symbol: 4, | |
131 min_cycles_per_frame: 4, | |
132 baseband_decimation_factor: 4 | |
133 }, | |
134 decoder_params: { | |
135 bytes_per_token: 6, | |
136 include_parity_symbol: true, | |
137 max_candidates: 1, | |
138 broadcaster_stopped_threshold_in_seconds: 10 | |
139 }, | |
140 acquisition_params: { | |
141 max_buffer_duration_in_seconds: 3 | |
142 } | |
143 }; | 120 }; |
144 this.whisperNacl_.send(JSON.stringify(msg)); | 121 this.whisperNacl_.send(JSON.stringify(msg)); |
145 } | 122 } |
146 | 123 |
147 /** | 124 /** |
148 * Method to request the decoder to wipe its internal buffer. | 125 * Method to request the decoder to wipe its internal buffer. |
149 */ | 126 */ |
150 WhisperDecoder.prototype.wipeDecoder = function() { | 127 WhisperDecoder.prototype.wipeDecoder = function() { |
151 var msg = { | 128 var msg = { |
152 type: 'wipe_decode_buffer' | 129 type: 'wipe_decode_buffer' |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 */ | 192 */ |
216 WhisperDecoder.prototype.handleCandidates_ = function(candidates) { | 193 WhisperDecoder.prototype.handleCandidates_ = function(candidates) { |
217 if (!this.tokenCallback_ || !candidates || candidates.length == 0) | 194 if (!this.tokenCallback_ || !candidates || candidates.length == 0) |
218 return; | 195 return; |
219 | 196 |
220 var returnCandidates = []; | 197 var returnCandidates = []; |
221 for (var i = 0; i < candidates.length; ++i) | 198 for (var i = 0; i < candidates.length; ++i) |
222 returnCandidates[i] = bytesToBase64(candidates[i]); | 199 returnCandidates[i] = bytesToBase64(candidates[i]); |
223 this.tokenCallback_(returnCandidates); | 200 this.tokenCallback_(returnCandidates); |
224 }; | 201 }; |
OLD | NEW |