Skip to content

Commit 4201816

Browse files
authored
Merge pull request #33 from W3AXL/console-performance
console animation performance fixes
2 parents d6d9212 + 1ab026f commit 4201816

1 file changed

Lines changed: 55 additions & 23 deletions

File tree

console/client.js

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ function populateRadios() {
562562
console.info("Adding radio " + radio.name);
563563
console.debug(radio);
564564
// Add the radio card
565-
addRadioCard("radio" + String(index), radio.name, radio.color);
565+
addRadioCard(index, "radio" + String(index), radio.name, radio.color);
566566
// Update edit list
567567
addRadioToEditTable(radio);
568568
// Populate its text
@@ -587,9 +587,9 @@ function clearRadios() {
587587
* @param {string} id ID of the card element
588588
* @param {string} name Name to display in header
589589
*/
590-
function addRadioCard(id, name, color) {
590+
function addRadioCard(idx, id, name, color) {
591591
// Log
592-
console.debug(`Adding card for radio ${name} (id ${id})`);
592+
console.debug(`Adding card for radio ${idx} (${name}), html id ${id}`);
593593

594594
// New, much easier way to add new cards
595595
var newCard = radioCardTemplate.content.cloneNode(true);
@@ -612,6 +612,15 @@ function addRadioCard(id, name, color) {
612612
})
613613

614614
$("#main-layout").append(newCard);
615+
616+
radios[idx].elements = {};
617+
618+
// Retrieve and store the new element as a javascript object in the radio array
619+
radios[idx].elements.card = document.querySelector(`#${id}`);
620+
621+
// Store the audio bars as discrete elements, so we can update them in the animation callback without querying for them every time
622+
radios[idx].elements.rxbar = radios[idx].elements.card.querySelector("#rx-bar");
623+
radios[idx].elements.txbar = radios[idx].elements.card.querySelector("#tx-bar");
615624
}
616625

617626
/**
@@ -824,7 +833,7 @@ window.electronAPI.saveRadioConfig((event, radioConfig) => {
824833
console.log("Adding radio " + newRadio.name);
825834

826835
// Add the radio card
827-
addRadioCard("radio" + String(newRadioIdx), newRadio.name, newRadio.color);
836+
addRadioCard(newRadioIdx, "radio" + String(newRadioIdx), newRadio.name, newRadio.color);
828837

829838
// Populate its text
830839
updateRadioCard(newRadioIdx);
@@ -1387,6 +1396,27 @@ function stopAlert() {
13871396
alertTonesInProgress = false;
13881397
}
13891398

1399+
/**
1400+
* Returns true if a radio is receiving clear or encrypted traffic
1401+
* @param {int} idx radio index
1402+
* @returns bool
1403+
*/
1404+
function isRadioReceiving(idx) {
1405+
// Radio is receiving if card has "receiving" or "encrypted" classes
1406+
const classes = radios[idx].elements.card.classList;
1407+
return (classes.contains("receiving") || classes.contains("encrypted"));
1408+
}
1409+
1410+
/**
1411+
* Returns true if a radio is receiving or transmitting
1412+
* @param {int} idx radio index
1413+
* @returns bool
1414+
*/
1415+
function isRadioActive(idx) {
1416+
const classes = radios[idx].elements.card.classList;
1417+
return (classes.contains("receiving") || classes.contains("encrypted") || classes.contains("transmitting"));
1418+
}
1419+
13901420
/***********************************************************************************
13911421
Global UI Functions
13921422
***********************************************************************************/
@@ -2345,15 +2375,15 @@ function audioMeterCallback() {
23452375
radios.forEach((radio, idx) => {
23462376
// Ignore radios with no connected audio
23472377
if (radios[idx].audioSrc == null) {
2348-
if ($(`.radio-card#radio${idx} #rx-bar`).width != 0) {
2349-
$(`.radio-card#radio${idx} #rx-bar`).width(0);
2378+
if (radios[idx].elements.rxbar.style.width != 0) {
2379+
radios[idx].elements.rxbar.style.width = 0;
23502380
}
23512381
return
23522382
}
23532383
// Ignore radio that isn't receiving (checking for the class compensates for the rx delay)
2354-
if (!($(`.radio-card#radio${idx}`).hasClass("receiving") || $(`.radio-card#radio${idx}`).hasClass("encrypted"))) {
2355-
if ($(`.radio-card#radio${idx} #rx-bar`).width != 0) {
2356-
$(`.radio-card#radio${idx} #rx-bar`).width(0);
2384+
if (!isRadioReceiving(idx)) {
2385+
if (radios[idx].elements.rxbar.style.width != 0) {
2386+
radios[idx].elements.rxbar.style.width = 0;
23572387
}
23582388
return
23592389
}
@@ -2364,20 +2394,22 @@ function audioMeterCallback() {
23642394
for (const amplitude of radios[idx].audioSrc.analyzerData) { sumSquares += (amplitude * amplitude); }
23652395
// We just scale this summed squared value by a constant to avoid actually doing an RMS calculation every single frame
23662396
const newPct = String(Math.sqrt(sumSquares / radios[idx].audioSrc.analyzerData.length).toFixed(3) * 300);
2367-
$(`.radio-card#radio${idx} #rx-bar`).width(newPct);
2397+
radios[idx].elements.rxbar.style.width = newPct;
23682398
});
23692399

23702400
// Input meter (only show when PTT)
2371-
if (pttActive) {
2372-
// Get data from mic
2373-
audio.inputAnalyzer.getFloatTimeDomainData(audio.inputPcmData);
2374-
sumSquares = 0.0;
2375-
for (const amplitude of audio.inputPcmData) { sumSquares += amplitude * amplitude; }
2376-
const newPct = String(Math.sqrt(sumSquares / audio.outputPcmData.length).toFixed(3) * 300);
2377-
// Apply to selected radio only
2378-
$(`.radio-card#radio${selectedRadioIdx} #tx-bar`).width(newPct);
2379-
} else {
2380-
$(`.radio-card#radio${selectedRadioIdx} #tx-bar`).width(0);
2401+
if (selectedRadioIdx != null && selectedRadioIdx >= 0) {
2402+
if (pttActive) {
2403+
// Get data from mic
2404+
audio.inputAnalyzer.getFloatTimeDomainData(audio.inputPcmData);
2405+
sumSquares = 0.0;
2406+
for (const amplitude of audio.inputPcmData) { sumSquares += amplitude * amplitude; }
2407+
const newPct = String(Math.sqrt(sumSquares / audio.outputPcmData.length).toFixed(3) * 300);
2408+
// Apply to selected radio only
2409+
radios[selectedRadioIdx].elements.txbar.style.width = newPct;
2410+
} else {
2411+
radios[selectedRadioIdx].elements.txbar.style.width = 0;
2412+
}
23812413
}
23822414

23832415
// Request next frame
@@ -2390,7 +2422,7 @@ function checkAudioMeterCallback()
23902422
console.debug("Checking if any radio's audio is active");
23912423
audio_active = false;
23922424
radios.forEach((radio, idx) => {
2393-
if ($(`.radio-card#radio${idx}`).hasClass("receiving") || $(`.radio-card#radio${idx}`).hasClass("encrypted") || $(`.radio-card#radio${idx}`).hasClass("transmitting"))
2425+
if (isRadioActive(idx))
23942426
{
23952427
console.debug(`${radio.name} audio active`);
23962428
audio_active = true;
@@ -2415,8 +2447,8 @@ function checkAudioMeterCallback()
24152447
function zeroAudioMeters()
24162448
{
24172449
radios.forEach((radio, idx) => {
2418-
$(`.radio-card#radio${idx} #rx-bar`).width(0);
2419-
$(`.radio-card#radio${idx} #tx-bar`).width(0);
2450+
radios[idx].elements.rxbar.style.width = 0;
2451+
radios[idx].elements.txbar.style.width = 0;
24202452
});
24212453
}
24222454

0 commit comments

Comments
 (0)