OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 'use strict'; |
| 6 |
| 7 /** |
| 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. |
| 10 * @param {string} bytes String containing the bytes we want to convert. |
| 11 * @return {string} String containing the base64 representation. |
| 12 */ |
| 13 function bytesToBase64(bytes) { |
| 14 var bstr = ''; |
| 15 for (var i = 0; i < bytes.length; ++i) |
| 16 bstr += String.fromCharCode(bytes[i]); |
| 17 return btoa(bstr); |
| 18 } |
| 19 |
| 20 /** |
| 21 * Function to convert a string to an array of bytes. |
| 22 * @param {string} str String to convert. |
| 23 * @return {Array} Array containing the string. |
| 24 */ |
| 25 function stringToArray(str) { |
| 26 var buffer = []; |
| 27 for (var i = 0; i < str.length; ++i) |
| 28 buffer[i] = str.charCodeAt(i); |
| 29 return buffer; |
| 30 } |
| 31 |
| 32 /** |
| 33 * Creates a whispernet encoder. |
| 34 * @constructor |
| 35 * @param {Object} params Dictionary of parameters used to initialize the |
| 36 * whispernet encoder. |
| 37 * @param {Object} whisperNacl The NaclBridge object to use to communicate with |
| 38 * the whispernet wrapper. |
| 39 */ |
| 40 function WhisperEncoder(params, whisperNacl) { |
| 41 params = params || {}; |
| 42 this.repetitions_ = params.repetitions || 3; |
| 43 |
| 44 this.whisperNacl_ = whisperNacl; |
| 45 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); |
| 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 = { |
| 56 type: 'initialize_encoder', |
| 57 symbol_coder: symbolCoder, |
| 58 encoder_params: { |
| 59 bytes_per_token: 6, |
| 60 include_parity_symbol: true, |
| 61 single_sideband: true |
| 62 } |
| 63 }; |
| 64 this.whisperNacl_.send(JSON.stringify(msg)); |
| 65 } |
| 66 |
| 67 /** |
| 68 * Method to encode a token. |
| 69 * @param {string} token Token to encode. |
| 70 * @param {boolean} raw Whether we should return the encoded samples in raw |
| 71 * format or as a Wave file. |
| 72 */ |
| 73 WhisperEncoder.prototype.encode = function(token, raw) { |
| 74 var msg = { |
| 75 type: 'encode_token', |
| 76 // 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 |
| 78 // true of returning a binary string too; hence we communicate back and |
| 79 // forth by converting the bytes into an array of integers. |
| 80 token: stringToArray(token), |
| 81 repetitions: this.repetitions_, |
| 82 return_raw_samples: raw |
| 83 }; |
| 84 this.whisperNacl_.send(JSON.stringify(msg)); |
| 85 }; |
| 86 |
| 87 /** |
| 88 * Method to set the callback for encoded audio data received from the encoder |
| 89 * when we finish encoding a token. |
| 90 * @param {function(string, ArrayBuffer)} callback Callback which will receive |
| 91 * the audio samples. |
| 92 */ |
| 93 WhisperEncoder.prototype.setAudioDataCallback = function(callback) { |
| 94 this.audioDataCallback_ = callback; |
| 95 }; |
| 96 |
| 97 /** |
| 98 * Method to handle messages from the whispernet NaCl wrapper. |
| 99 * @param {Event} e Event from the whispernet wrapper. |
| 100 * @private |
| 101 */ |
| 102 WhisperEncoder.prototype.onNaclMessage_ = function(e) { |
| 103 var msg = e.data; |
| 104 if (msg.type == 'encode_token_response') { |
| 105 this.audioDataCallback_(bytesToBase64(msg.token), msg.samples); |
| 106 } |
| 107 }; |
| 108 |
| 109 /** |
| 110 * Creates a whispernet decoder. |
| 111 * @constructor |
| 112 * @param {Object} params Dictionary of parameters used to initialize the |
| 113 * whispernet decoder. |
| 114 * @param {Object} whisperNacl The NaclBridge object to use to communicate with |
| 115 * the whispernet wrapper. |
| 116 */ |
| 117 function WhisperDecoder(params, whisperNacl) { |
| 118 params = params || {}; |
| 119 |
| 120 this.whisperNacl_ = whisperNacl; |
| 121 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); |
| 122 |
| 123 var msg = { |
| 124 type: 'initialize_decoder', |
| 125 num_channels: params.channels, |
| 126 symbol_coder: { |
| 127 sample_rate: params.sampleRate || 48000.0, |
| 128 upsampling_factor: params.bitsPerSample || 16, |
| 129 desired_carrier_frequency: params.carrierFrequency || 18500.0, |
| 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 }; |
| 144 this.whisperNacl_.send(JSON.stringify(msg)); |
| 145 } |
| 146 |
| 147 /** |
| 148 * Method to request the decoder to wipe its internal buffer. |
| 149 */ |
| 150 WhisperDecoder.prototype.wipeDecoder = function() { |
| 151 var msg = { |
| 152 type: 'wipe_decode_buffer' |
| 153 }; |
| 154 this.whisperNacl_.send(JSON.stringify(msg)); |
| 155 }; |
| 156 |
| 157 /** |
| 158 * Method to request the decoder to detect a broadcast. |
| 159 */ |
| 160 WhisperDecoder.prototype.detectBroadcast = function() { |
| 161 var msg = { |
| 162 type: 'detect_broadcast' |
| 163 }; |
| 164 this.whisperNacl_.send(JSON.stringify(msg)); |
| 165 }; |
| 166 |
| 167 /** |
| 168 * Method to request the decoder to process samples. |
| 169 * @param {ArrayBuffer} samples Array of samples to process. |
| 170 */ |
| 171 WhisperDecoder.prototype.processSamples = function(samples) { |
| 172 // For sample processing, the Nacl module doesn't expect any frills in the |
| 173 // message, just send the samples directly. |
| 174 this.whisperNacl_.send(samples); |
| 175 }; |
| 176 |
| 177 /** |
| 178 * Method to set the callback for decoded tokens received from the decoder. |
| 179 * @param {function(!Array.string)} callback Callback to receive the list of |
| 180 * decoded tokens. |
| 181 */ |
| 182 WhisperDecoder.prototype.setReceiveCallback = function(callback) { |
| 183 this.tokenCallback_ = callback; |
| 184 }; |
| 185 |
| 186 /** |
| 187 * Method to set the callback for receiving the detect callback status received |
| 188 * from the decoder. |
| 189 * @param {function()} callback Callback to set to receive the detect broadcast |
| 190 * status. |
| 191 */ |
| 192 WhisperDecoder.prototype.onDetectBroadcast = function(callback) { |
| 193 this.detectBroadcastCallback_ = callback; |
| 194 }; |
| 195 |
| 196 /** |
| 197 * Method to handle messages from the whispernet NaCl wrapper. |
| 198 * @param {Event} e Event from the whispernet wrapper. |
| 199 * @private |
| 200 */ |
| 201 WhisperDecoder.prototype.onNaclMessage_ = function(e) { |
| 202 var msg = e.data; |
| 203 if (msg.type == 'decode_tokens_response') { |
| 204 this.handleCandidates_(JSON.parse(msg.tokens)); |
| 205 } else if (msg.type == 'detect_broadcast_response') { |
| 206 this.detectBroadcastCallback_(msg.detected); |
| 207 } |
| 208 }; |
| 209 |
| 210 /** |
| 211 * Method to receive tokens from the decoder and process and forward them to the |
| 212 * token callback registered with us. |
| 213 * @param {!Array.string} candidates Array of token candidates. |
| 214 * @private |
| 215 */ |
| 216 WhisperDecoder.prototype.handleCandidates_ = function(candidates) { |
| 217 if (!this.tokenCallback_ || !candidates || candidates.length == 0) |
| 218 return; |
| 219 |
| 220 var returnCandidates = []; |
| 221 for (var i = 0; i < candidates.length; ++i) |
| 222 returnCandidates[i] = bytesToBase64(candidates[i]); |
| 223 this.tokenCallback_(returnCandidates); |
| 224 }; |
OLD | NEW |