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