-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpacketReader.js
More file actions
530 lines (338 loc) · 16.8 KB
/
packetReader.js
File metadata and controls
530 lines (338 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/****************************************************************************
* packetReader.js
* openacousticdevices.info
* April 2020
*****************************************************************************/
'use strict';
const audiomoth = require('audiomoth-hid');
const constants = require('./constants.js');
function fourBytesToNumber (buffer, offset) {
return (buffer[offset] & 0xFF) + ((buffer[offset + 1] & 0xFF) << 8) + ((buffer[offset + 2] & 0xFF) << 16) + ((buffer[offset + 3] & 0xFF) << 24);
}
function twoBytesToSignedNumber (buffer, offset) {
let value = (buffer[offset] & 0xFF) + ((buffer[offset + 1] & 0xFF) << 8);
value = value >= 32768 ? value - 65536 : value;
return value;
}
function twoBytesToNumber (buffer, offset) {
return (buffer[offset] & 0xFF) + ((buffer[offset + 1] & 0xFF) << 8);
}
function digitWithLeadingZero (value) {
const formattedString = '0' + value;
return formattedString.substring(formattedString.length - 2);
}
function formatTime (minutes) {
return digitWithLeadingZero(Math.floor(minutes / constants.MINUTES_IN_HOUR)) + ':' + digitWithLeadingZero(minutes % constants.MINUTES_IN_HOUR);
}
function formatDate (date) {
return (date.valueOf() / 1000) + ' - ' + date.toISOString().replace(/T/, ' ').replace(/\..+/, '') + ' (UTC)';
}
function formatPercentage (mantissa, exponent) {
let response = '';
if (exponent < 0) {
response += '0.0000'.substring(0, 1 - exponent);
}
response += mantissa;
for (let i = 0; i < exponent; i += 1) response += '0';
return response;
}
/*
typedef struct {
uint16_t startMinutes;
uint16_t endMinutes;
} recordingPeriod_t;
typedef struct {
uint32_t time;
AM_gainSetting_t gain;
uint8_t clockDivider;
uint8_t acquisitionCycles;
uint8_t oversampleRate;
uint32_t sampleRate;
uint8_t sampleRateDivider;
uint16_t sleepDuration;
uint16_t recordDuration;
uint8_t enableLED;
union {
struct {
uint8_t activeRecordingPeriods;
recordingPeriod_t recordingPeriods[MAX_RECORDING_PERIODS];
};
struct {
uint8_t sunRecordingMode : 3;
uint8_t sunRecordingEvent : 2;
int16_t latitude;
int16_t longitude;
uint8_t sunRoundingMinutes;
uint16_t beforeSunriseMinutes : 10;
uint16_t afterSunriseMinutes : 10;
uint16_t beforeSunsetMinutes : 10;
uint16_t afterSunsetMinutes : 10;
};
};
int8_t timezoneHours;
uint8_t enableLowVoltageCutoff;
uint8_t disableBatteryLevelDisplay;
int8_t timezoneMinutes;
uint8_t disableSleepRecordCycle : 1;
uint8_t enableFilenameWithDeviceID : 1;
uint8_t enableTimeSettingBeforeAndAfterRecordings: 1;
uint8_t gpsTimeSettingPeriod: 4;
uint32_t earliestRecordingTime;
uint32_t latestRecordingTime;
uint16_t lowerFilterFreq;
uint16_t higherFilterFreq;
union {
uint16_t amplitudeThreshold;
uint16_t frequencyTriggerCentreFrequency;
};
uint8_t requireAcousticConfiguration : 1;
AM_batteryLevelDisplayType_t batteryLevelDisplayType : 1;
uint8_t minimumTriggerDuration : 6;
union {
struct {
uint8_t frequencyTriggerWindowLengthShift : 4;
uint8_t frequencyTriggerThresholdPercentageMantissa : 4;
int8_t frequencyTriggerThresholdPercentageExponent : 3;
};
struct {
uint8_t enableAmplitudeThresholdDecibelScale : 1;
uint8_t amplitudeThresholdDecibels : 7;
uint8_t enableAmplitudeThresholdPercentageScale : 1;
uint8_t amplitudeThresholdPercentageMantissa : 4;
int8_t amplitudeThresholdPercentageExponent : 3;
};
};
uint8_t enableEnergySaverMode : 1;
uint8_t disable48HzDCBlockingFilter : 1;
uint8_t enableTimeSettingFromGPS : 1;
uint8_t enableMagneticSwitch : 1;
uint8_t enableLowGainRange : 1;
uint8_t enableFrequencyTrigger : 1;
uint8_t enableDailyFolders : 1;
uint8_t enableSunRecording : 1;
} configSettings_t;
*/
/**
* Extract four 10-bit numbers from a buffer, starting from a given index
* @param {buffer} buffer Buffer containing data
* @param {integer} start Where in the buffer to start looking for the four values
* @returns An array containing four 10-bit numbers
*/
function fiveBytesToFourNumbers (buffer, start) {
const byte0 = buffer[start];
const byte1 = buffer[start + 1];
const byte2 = buffer[start + 2];
const byte3 = buffer[start + 3];
const byte4 = buffer[start + 4];
const value0 = (byte0 & 0b0011111111) | ((byte1 << 8) & 0b1100000000);
const value1 = ((byte1 >> 2) & 0b0000111111) | ((byte2 << 6) & 0b1111000000);
const value2 = ((byte2 >> 4) & 0b0000001111) | ((byte3 << 4) & 0b1111110000);
const value3 = ((byte3 >> 6) & 0b0000000011) | ((byte4 << 2) & 0b1111111100);
return [value0, value1, value2, value3];
}
function convertCoordMinsToObject (mins) {
const positiveDirection = mins > 0;
mins = Math.abs(mins);
const degrees = Math.floor(mins / 100);
const hundredths = mins % 100;
return {
degrees,
hundredths,
positiveDirection
};
}
exports.read = (packet) => {
/* Read and decode configuration packet */
const time = audiomoth.convertFourBytesFromBufferToDate(packet, 0);
const gain = packet[4];
const clockDivider = packet[5];
const acquisitionCycles = packet[6];
const oversampleRate = packet[7];
const sampleRate = fourBytesToNumber(packet, 8);
const sampleRateDivider = packet[12];
const sleepDuration = twoBytesToNumber(packet, 13);
const recordDuration = twoBytesToNumber(packet, 15);
const enableLED = packet[17];
/* Read advanced settings */
const packedByte3 = packet[61];
const energySaverModeEnabled = packedByte3 & 1;
const disable48DCFilter = (packedByte3 >> 1) & 1;
const timeSettingFromGPSEnabled = (packedByte3 >> 2) & 1;
const magneticSwitchEnabled = (packedByte3 >> 3) & 1;
const lowGainRangeEnabled = (packedByte3 >> 4) & 1;
const enableFrequencyFilter = (packedByte3 >> 5) & 1;
const dailyFolders = (packedByte3 >> 6) & 1;
const sunRecordingEnabled = (packedByte3 >> 7) & 1;
/* Read recording schedule or sunrise/sunset settings */
let sunMode;
let sunEvent;
let latitude, longitude;
let sunRounding;
let sunriseBefore, sunriseAfter, sunsetBefore, sunsetAfter;
let activeRecordingPeriods, recordingPeriods;
if (sunRecordingEnabled) {
const packedByte4 = packet[18];
sunMode = packedByte4 & 0b111;
sunEvent = (packedByte4 >> 3) & 0b11;
latitude = convertCoordMinsToObject(twoBytesToSignedNumber(packet, 19));
longitude = convertCoordMinsToObject(twoBytesToSignedNumber(packet, 21));
sunRounding = packet[23];
/* packet[24], packet[25], packet[26], packet[27], packet[28] */
const numbers = fiveBytesToFourNumbers(packet, 24);
sunriseBefore = numbers[0];
sunriseAfter = numbers[1];
sunsetBefore = numbers[2];
sunsetAfter = numbers[3];
} else {
activeRecordingPeriods = packet[18];
recordingPeriods = [];
for (let i = 0; i < activeRecordingPeriods; i += 1) {
const startMinutes = twoBytesToNumber(packet, 19 + 4 * i);
const endMinutes = twoBytesToNumber(packet, 21 + 4 * i);
recordingPeriods.push({startMinutes, endMinutes});
}
}
const timeZoneHours = packet[39] > 127 ? packet[39] - 256 : packet[39];
/* Low voltage cutoff is now always enabled */
// const enableLowVoltageCutoff = packet[40];
const disableBatteryLevelDisplay = packet[41];
const timeZoneMinutes = packet[42] > 127 ? packet[42] - 256 : packet[42];
const packedByte5 = packet[43];
const disableSleepRecordCycle = packedByte5 & 1;
const filenameWithDeviceIDEnabled = (packedByte5 >> 1) & 1;
const acquireGpsFixBeforeAfter = ((packedByte5 >> 2) & 1) === 1 ? 'individual' : 'period';
const gpsFixTime = (packedByte5 >> 3) & 0b1111;
const earliestRecordingTime = audiomoth.convertFourBytesFromBufferToDate(packet, 44);
const latestRecordingTime = audiomoth.convertFourBytesFromBufferToDate(packet, 48);
const lowerFilterFreq = twoBytesToNumber(packet, 52);
const higherFilterFreq = twoBytesToNumber(packet, 54);
const packedByte0 = packet[58];
const requireAcousticConfig = packedByte0 & 1;
const displayVoltageRange = (packedByte0 >> 1) & 1;
const minimumTriggerDuration = (packedByte0 >> 2) & 0b111111;
/* Indices contain either amplitude or frequency threshold, so use enableFrequencyFilter to tell which is which */
const packedByte1 = packet[59];
const packedByte2 = packet[60];
let amplitudeThreshold = 0;
let goertzelFilterFrequency = 0;
let goertzelFilterWindowLengthShift, goertzelFilterThresholdPercentageMantissa, goertzelFilterThresholdPercentageExponent, goertzelFilterThresholdPercentage;
let enableAmplitudeThresholdDecibelScale, amplitudeThresholdDecibel;
let enableAmplitudeThresholdPercentageScale, amplitudeThresholdPercentageMantissa, amplitudeThresholdPercentageExponent, amplitudeThresholdPercentage, amplitudeThresholdScale;
if (enableFrequencyFilter) {
goertzelFilterFrequency = twoBytesToNumber(packet, 56);
goertzelFilterFrequency *= 100;
/* Frequency threshold */
goertzelFilterWindowLengthShift = packedByte1 & 0b1111;
goertzelFilterWindowLengthShift = 1 << goertzelFilterWindowLengthShift;
goertzelFilterThresholdPercentageMantissa = (packedByte1 >> 4) & 0b1111;
goertzelFilterThresholdPercentageExponent = packedByte2 & 0b111;
goertzelFilterThresholdPercentageExponent = goertzelFilterThresholdPercentageExponent < 0b100 ? goertzelFilterThresholdPercentageExponent : goertzelFilterThresholdPercentageExponent - 0b1000;
goertzelFilterThresholdPercentage = goertzelFilterThresholdPercentageMantissa * Math.pow(10, goertzelFilterThresholdPercentageExponent);
} else {
amplitudeThreshold = twoBytesToNumber(packet, 56);
/* Amplitude threshold decibel scale */
/* Read bottom 7 bits */
enableAmplitudeThresholdDecibelScale = packedByte1 & 1;
amplitudeThresholdDecibel = -1 * ((packedByte1 >> 1) & 0b1111111);
/* Amplitude threshold percentage scale */
enableAmplitudeThresholdPercentageScale = packedByte2 & 1;
/* Read the percentage value as a 4-bit mantissa and a 3-bit exponent */
amplitudeThresholdPercentageMantissa = (packedByte2 >> 1) & 0b1111;
/* Read final 3 bits and read as 3-bit two's complement */
amplitudeThresholdPercentageExponent = (packedByte2 >> 5) & 0b111;
amplitudeThresholdPercentageExponent = amplitudeThresholdPercentageExponent < 0b100 ? amplitudeThresholdPercentageExponent : amplitudeThresholdPercentageExponent - 0b1000;
amplitudeThresholdPercentage = formatPercentage(amplitudeThresholdPercentageMantissa, amplitudeThresholdPercentageExponent) + '%';
/* Which amplitude threshold scale should be displayed */
if (enableAmplitudeThresholdPercentageScale === 1 && enableAmplitudeThresholdDecibelScale === 0) {
amplitudeThresholdScale = 'Percentage';
} else if (enableAmplitudeThresholdDecibelScale === 1 && enableAmplitudeThresholdPercentageScale === 0) {
amplitudeThresholdScale = 'Decibel';
} else {
amplitudeThresholdScale = '16-Bit';
}
}
/* Display configuration */
console.log('Current time: ', formatDate(time));
console.log('TimeZone Hours:', timeZoneHours);
console.log('TimeZone Minutes:', timeZoneMinutes);
console.log('Gain:', gain);
console.log('Clock divider:', clockDivider);
console.log('Acquisition cycles:', acquisitionCycles);
console.log('Oversample rate:', oversampleRate);
console.log('Sample rate:', sampleRate);
console.log('Sample rate divider:', sampleRateDivider);
console.log('Enable sleep/record cyclic recording:', disableSleepRecordCycle === 0);
console.log('Enable file name with device ID:', filenameWithDeviceIDEnabled === 1);
console.log('Sleep duration:', sleepDuration);
console.log('Recording duration:', recordDuration);
console.log('Enable LED:', enableLED === 1);
console.log('Enable battery level indication:', disableBatteryLevelDisplay === 0);
if (sunRecordingEnabled) {
console.log('Sunrise/sunset schedule enabled');
console.log('\tSun mode:', sunMode);
const sunDefinitionStrings = ['Sunrise/sunset', 'Civil', 'Nautical', 'Astronomical'];
console.log('\tSun event:', sunEvent, '-', sunDefinitionStrings[sunEvent]);
const latitudeDirection = latitude.positiveDirection ? 'N' : 'S';
console.log('\tLatitude:', latitude.degrees, '.', latitude.hundredths, '°', latitudeDirection);
const longitudeDirection = longitude.positiveDirection ? 'E' : 'W';
console.log('\tLongitude:', longitude.degrees, '.', longitude.hundredths, '°', longitudeDirection);
console.log('\tSun rounding:', sunRounding);
console.log('\tBefore sunrise mins:', sunriseBefore);
console.log('\tAfter sunrise mins:', sunriseAfter);
console.log('\tBefore sunset mins:', sunsetBefore);
console.log('\tAfter sunset mins:', sunsetAfter);
} else {
console.log('Standard schedule enabled');
console.log('\tActive recording periods:', activeRecordingPeriods);
for (let j = 0; j < activeRecordingPeriods; j++) {
const startMins = recordingPeriods[j].startMinutes;
const endMins = recordingPeriods[j].endMinutes;
console.log('\tStart: ' + formatTime(startMins) + ' (' + startMins + ') - End: ' + formatTime(endMins) + ' (' + endMins + ')');
}
}
console.log('Earliest recording time:', formatDate(earliestRecordingTime));
console.log('Latest recording time:', formatDate(latestRecordingTime));
if (lowerFilterFreq === constants.UINT16_MAX) {
// Low-pass
console.log('Low-pass filter set at:', higherFilterFreq / 10, 'kHz');
} else if (higherFilterFreq === constants.UINT16_MAX) {
// High-pass
console.log('High-pass filter set at:', lowerFilterFreq / 10, 'kHz');
} else if (lowerFilterFreq === 0 && higherFilterFreq === 0) {
// None
console.log('Frequency filter disabled');
} else {
// Band-pass
console.log('Band-pass filter set at:', lowerFilterFreq / 10, 'kHz -', higherFilterFreq / 10, 'kHz');
}
console.log('Minimum trigger duration:', minimumTriggerDuration);
if (enableFrequencyFilter) {
console.log('Goertzel filter window length:', goertzelFilterWindowLengthShift);
console.log('Goertzel filter frequency:', goertzelFilterFrequency / 1000, 'kHz');
console.log('Frequency filter threshold mantissa:', goertzelFilterThresholdPercentageMantissa);
console.log('Frequency filter threshold exponent:', goertzelFilterThresholdPercentageExponent);
console.log('Frequency filter threshold:', goertzelFilterThresholdPercentage);
} else {
console.log('Amplitude threshold percentage flag:', (enableAmplitudeThresholdPercentageScale === 1));
console.log('Amplitude threshold decibel flag:', (enableAmplitudeThresholdDecibelScale === 1));
console.log('Amplitude threshold (16-bit):', amplitudeThreshold);
if (amplitudeThresholdScale === 'Percentage') {
console.log('Percentage exponent:', amplitudeThresholdPercentageExponent);
console.log('Percentage mantissa:', amplitudeThresholdPercentageMantissa);
console.log('Amplitude threshold (percentage):', amplitudeThresholdPercentage);
} else if (amplitudeThresholdScale === 'Decibel') {
console.log('Amplitude threshold (decibels):', amplitudeThresholdDecibel);
}
console.log('Displayed amplitude threshold scale:', amplitudeThresholdScale);
}
console.log('Acoustic configuration required:', requireAcousticConfig === 1);
console.log('Use NiMH/LiPo voltage range for battery level indication:', displayVoltageRange === 1);
console.log('Energy saver mode enabled:', energySaverModeEnabled === 1);
console.log('48 Hz DC blocking filter disabled:', disable48DCFilter === 1);
console.log('GPS clock setting enabled:', timeSettingFromGPSEnabled === 1);
console.log('GPS fixes between recordings enabled:', acquireGpsFixBeforeAfter);
console.log('Time before recording/period for GPS fix:', gpsFixTime);
console.log('Magnetic switch delay enabled:', magneticSwitchEnabled === 1);
console.log('Low gain range enabled: ', lowGainRangeEnabled === 1);
console.log('Daily folders enabled:', dailyFolders === 1);
};