Skip to content

Commit

Permalink
refactor: audio
Browse files Browse the repository at this point in the history
  • Loading branch information
whes1015 committed Jul 25, 2023
1 parent 580c1a3 commit 3ccbaa3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/core/index/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ async function refresh_report_list(_fetch = false, data = {}) {
report_click_replay.className = "report_click_text fa-regular fa-square fa-2x";
report_now_id = originTime.getTime();
rts_replay_timestamp = originTime.getTime();
rts_replay_time = originTime.getTime() - 5000;
rts_replay_time = originTime.getTime() - 3000;
replay_run();
if (storage.getItem("report_eew")) get_data({
"originTime" : originTime.getTime(),
Expand Down Expand Up @@ -384,7 +384,7 @@ async function refresh_report_list(_fetch = false, data = {}) {
report_click_replay.className = "report_click_text fa-regular fa-square fa-2x";
report_now_id = originTime.getTime();
rts_replay_timestamp = originTime.getTime();
rts_replay_time = originTime.getTime() - 5000;
rts_replay_time = originTime.getTime() - 3000;
replay_run();
if (storage.getItem("report_eew")) get_data({
"originTime" : originTime.getTime(),
Expand Down
16 changes: 8 additions & 8 deletions src/core/index/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function get_data(data, type = "websocket") {
if (!rts_replay_time) on_rts_data(data.raw);
} else if (data.type == "palert") {
show_screen("palert");
if (TREM.palert_report_time == 0 && (storage.getItem("audio.palert") ?? true)) TREM.audio.minor.push("palert");
if (TREM.palert_report_time == 0 && (storage.getItem("audio.palert") ?? true)) TREM.audio.push("palert");
TREM.palert_report_time = now_time();
refresh_report_list(false, data);
on_palert(data);
Expand Down Expand Up @@ -104,7 +104,7 @@ function get_data(data, type = "websocket") {
});
show_screen("report");
}
if (storage.getItem("audio.Report") ?? true) TREM.audio.minor.push("Report");
if (storage.getItem("audio.Report") ?? true) TREM.audio.push("Report");
TREM.palert_report_time = 0;
TREM.report_time = now_time();
refresh_report_list(false, data);
Expand Down Expand Up @@ -181,7 +181,7 @@ function on_eew(data, type) {
};
if (!eew_cache.includes(data.id + data.number)) {
eew_cache.push(data.id + data.number);
if (!skip && (storage.getItem("audio.EEW") ?? true)) TREM.audio.main.push("EEW");
if (!skip && (storage.getItem("audio.EEW") ?? true)) TREM.audio.push("EEW");
}
} else {
if (!data.location) data.location = TREM.EQ_list[data.id].data.location;
Expand All @@ -202,7 +202,7 @@ function on_eew(data, type) {
}
}
if (data.type == "eew-trem" && TREM.EQ_list[data.id].trem) {
if (!skip && (storage.getItem("audio.EEW") ?? true)) TREM.audio.main.push("EEW");
if (!skip && (storage.getItem("audio.EEW") ?? true)) TREM.audio.push("EEW");
delete TREM.EQ_list[data.id].trem;
TREM.EQ_list[data.id].epicenterIcon.remove();
delete TREM.EQ_list[data.id].epicenterIcon;
Expand Down Expand Up @@ -297,7 +297,7 @@ function draw_intensity(skip) {
TREM.EQ_list[_key].alert = true;
if (!TREM.alert) {
TREM.alert = true;
if (!skip && (storage.getItem("audio.EEW2") ?? true)) TREM.audio.minor.push("EEW2");
if (!skip && (storage.getItem("audio.EEW2") ?? true)) TREM.audio.push("EEW2");
if (!skip && speecd_use) speech.speak({ text: "注意強震,此地震可能造成災害" });
add_info("fa-solid fa-bell fa-2x info_icon", "#FF0080", "注意強震", "#00EC00", "此地震可能造成災害");
}
Expand Down Expand Up @@ -352,7 +352,7 @@ function report_off() {
function on_tsunami(data, type) {
if (!data.cancel) {
if (speecd_use) speech.speak({ text: "海嘯警報已發布,請迅速疏散至安全場所" });
if (data.number == 1) TREM.audio.main.push("Water");
if (data.number == 1) TREM.audio.push("Water");
document.getElementById("tsunami_box").style.display = "flex";
for (let i = 0; i < data.area.length; i++) {
if (!data.area[i].arrivalTime) continue;
Expand Down Expand Up @@ -509,7 +509,7 @@ function on_trem(data, type) {
};
if (!eew_cache.includes(data.id + data.number)) {
eew_cache.push(data.id + data.number);
TREM.audio.main.push("Note");
TREM.audio.push("Note");
show_icon(true, data.max);
add_info("fa-solid fa-flask fa-2x info_icon", "#FF8000", "TREM EEW", "#0072E3", "僅供參考(實驗性)", 30000);
}
Expand All @@ -520,7 +520,7 @@ function on_trem(data, type) {
if (TREM.EQ_list[data.id].eew > 4 && !TREM.alert) {
TREM.alert = true;
TREM.EQ_list[data.id].alert = true;
TREM.audio.minor.push("EEW2");
TREM.audio.push("EEW2");
if (speecd_use) speech.speak({ text: "注意強震,此地震可能造成災害" });
add_info("fa-solid fa-bell fa-2x info_icon", "#FF0080", "注意強震", "#00EC00", "此地震可能造成災害");
}
Expand Down
73 changes: 24 additions & 49 deletions src/core/index/loop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* eslint-disable no-undef */
require("expose-gc");
let player_1 = false;
let player_2 = false;
let drawer_lock = false;
let focus_lock = false;
let Zoom = false;
Expand Down Expand Up @@ -250,13 +248,11 @@ setInterval(() => {
check_ota();
}
}, 60_000);
const audioContext = new AudioContext();
const audioContext1 = new AudioContext();

setInterval(() => {
if (TREM.audio.main.length) {
if (player_1) return;
player_1 = true;
const nextAudioPath = TREM.audio.main.shift();
if (TREM.audio.length) {
const audioContext = new AudioContext();
const nextAudioPath = TREM.audio.shift();
if (!source_data[nextAudioPath]) source_data[nextAudioPath] = fs.readFileSync(path.resolve(app.getAppPath(), `./resource/audios/${nextAudioPath}.wav`)).buffer;
audioContext.decodeAudioData(source_data[nextAudioPath], (buffer) => {
delete source_data[nextAudioPath];
Expand All @@ -267,30 +263,9 @@ setInterval(() => {
source.start();
source.onended = () => {
source.disconnect();
player_1 = false;
fs.readFile(path.resolve(app.getAppPath(), `./resource/audios/${nextAudioPath}.wav`), (err, data) => {
source_data[nextAudioPath] = data.buffer;
});
};
});
}
if (TREM.audio.minor.length) {
if (player_2) return;
player_2 = true;
const nextAudioPath = TREM.audio.minor.shift();
if (!source_data[nextAudioPath]) source_data[nextAudioPath] = fs.readFileSync(path.resolve(app.getAppPath(), `./resource/audios/${nextAudioPath}.wav`)).buffer;
audioContext1.decodeAudioData(source_data[nextAudioPath], (buffer) => {
delete source_data[nextAudioPath];
const source = audioContext1.createBufferSource();
source.buffer = buffer;
source.connect(audioContext1.destination);
source.playbackRate = 1.1;
source.start();
source.onended = () => {
source.disconnect();
player_2 = false;
fs.readFile(path.resolve(app.getAppPath(), `./resource/audios/${nextAudioPath}.wav`), (err, data) => {
source_data[nextAudioPath] = data.buffer;
audioContext.close();
});
};
});
Expand Down Expand Up @@ -459,17 +434,17 @@ setInterval(() => {
if (user_max_intensity > 0 && (storage.getItem("eew-level") ?? -1) <= user_max_intensity && eew_audio_type != "3") {
document.getElementById("reciprocal").style.display = "flex";
if (!TREM.arrive)
if (!TREM.audio.main.length && s_time < 100 && now_time() - reciprocal > 950) {
if (s_time < 100 && now_time() - reciprocal > 950) {
if (audio_reciprocal == -1) audio_reciprocal = s_time;
if (audio_reciprocal > s_time) {
audio_reciprocal = s_time;
reciprocal = now_time();
if (!audio_intensity) {
audio_intensity = true;
TREM.audio.main.push(`1/${_intensity.replace("⁻", "").replace("⁺", "")}`);
if (_intensity.includes("⁺")) TREM.audio.main.push("1/intensity-strong");
else if (_intensity.includes("⁻")) TREM.audio.main.push("1/intensity-weak");
else TREM.audio.main.push("1/intensity");
TREM.audio.push(`1/${_intensity.replace("⁻", "").replace("⁺", "")}`);
if (_intensity.includes("⁺")) TREM.audio.push("1/intensity-strong");
else if (_intensity.includes("⁻")) TREM.audio.push("1/intensity-weak");
else TREM.audio.push("1/intensity");
} else if (eew_audio_type == "2") {
void 0;
} else if (!audio_second) {
Expand All @@ -478,33 +453,33 @@ setInterval(() => {
if (s_time < 99 && s_time > 0) {
if (s_time > 20)
if (s_time % 10 == 0) {
TREM.audio.main.push(`1/${s_time.toString().substring(0, 1)}x`);
TREM.audio.main.push("1/x0");
TREM.audio.push(`1/${s_time.toString().substring(0, 1)}x`);
TREM.audio.push("1/x0");
} else {
TREM.audio.main.push(`1/${s_time.toString().substring(0, 1)}x`);
TREM.audio.main.push(`1/x${s_time.toString().substring(1, 2)}`);
TREM.audio.push(`1/${s_time.toString().substring(0, 1)}x`);
TREM.audio.push(`1/x${s_time.toString().substring(1, 2)}`);
}
else if (s_time > 10)
if (s_time % 10 == 0) TREM.audio.main.push("1/x0");
else TREM.audio.main.push(`1/x${s_time.toString().substring(1, 2)}`);
else TREM.audio.main.push(`1/${s_time}`);
TREM.audio.main.push("1/second");
if (s_time % 10 == 0) TREM.audio.push("1/x0");
else TREM.audio.push(`1/x${s_time.toString().substring(1, 2)}`);
else TREM.audio.push(`1/${s_time}`);
TREM.audio.push("1/second");
}
} else
if (s_time <= 0) {
if (arrive_count == 0) {
TREM.audio.main.push("1/arrive");
TREM.audio.push("1/arrive");
arrive_count++;
} else if (arrive_count <= 5) {
if (storage.getItem("audio.1/ding") ?? true) TREM.audio.main.push("1/ding");
if (storage.getItem("audio.1/ding") ?? true) TREM.audio.push("1/ding");
arrive_count++;
} else {TREM.arrive = true;}
} else if (s_time > 10) {
if (s_time % 10 != 0) {if (storage.getItem("audio.1/ding") ?? true) TREM.audio.main.push("1/ding");} else {
TREM.audio.main.push(`1/${s_time.toString().substring(0, 1)}x`);
TREM.audio.main.push("1/x0");
if (s_time % 10 != 0) {if (storage.getItem("audio.1/ding") ?? true) TREM.audio.push("1/ding");} else {
TREM.audio.push(`1/${s_time.toString().substring(0, 1)}x`);
TREM.audio.push("1/x0");
}
} else {TREM.audio.main.push(`1/${s_time.toString()}`);}
} else {TREM.audio.push(`1/${s_time.toString()}`);}
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/core/index/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ const TREM = {
setting : {
rts_station: "H-711-11334880-12",
},
audio: {
main : [],
minor : [],
},
rts_audio: {
audio : [],
rts_audio : {
intensity : -1,
pga : 0,
},
Expand Down
12 changes: 6 additions & 6 deletions src/core/index/rts.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function on_rts_data(data) {
if (data.eew) {
if (!eew_alert_state) {
eew_alert_state = true;
TREM.audio.minor.push("Warn");
TREM.audio.push("Warn");
add_info("fa-solid fa-bell fa-2x info_icon", "#FF0080", "地震檢測", "#00EC00", "請留意 <b>中央氣象局</b><br>是否發布 <b>地震預警</b>", 15000);
if (!skip && speecd_use) speech.speak({ text: "地震檢測,請留意中央氣象局是否發布地震預警" });
}
Expand All @@ -182,7 +182,7 @@ function on_rts_data(data) {
const loc = detection_location[0] ?? "未知區域";
if (max_intensity > 3) {
TREM.rts_audio.intensity = 10;
if (!skip && (storage.getItem("audio.Shindo2") ?? true)) TREM.audio.minor.push("Shindo2");
if (!skip && (storage.getItem("audio.Shindo2") ?? true)) TREM.audio.push("Shindo2");
if (!skip && speecd_use) speech.speak({ text: `強震檢測,${loc}` });
new Notification("🟥 強震檢測", {
body : `${loc}`,
Expand All @@ -192,7 +192,7 @@ function on_rts_data(data) {
plugin.emit("rtsDetectionStrong");
} else if (max_intensity > 1) {
TREM.rts_audio.intensity = 3;
if (!skip && (storage.getItem("audio.Shindo1") ?? true)) TREM.audio.minor.push("Shindo1");
if (!skip && (storage.getItem("audio.Shindo1") ?? true)) TREM.audio.push("Shindo1");
if (!skip && speecd_use) speech.speak({ text: `震動檢測,${loc}` });
new Notification("🟨 震動檢測", {
body : `${loc}`,
Expand All @@ -202,7 +202,7 @@ function on_rts_data(data) {
plugin.emit("rtsDetectionShake");
} else {
TREM.rts_audio.intensity = 1;
if (!skip && (storage.getItem("audio.Shindo0") ?? true)) TREM.audio.minor.push("Shindo0");
if (!skip && (storage.getItem("audio.Shindo0") ?? true)) TREM.audio.push("Shindo0");
if (!skip && speecd_use) speech.speak({ text: `弱反應,${loc}` });
new Notification("🟩 弱反應", {
body : `${loc}`,
Expand All @@ -215,12 +215,12 @@ function on_rts_data(data) {
if (max_pga > TREM.rts_audio.pga && TREM.rts_audio.pga <= 200)
if (max_pga > 200) {
TREM.rts_audio.pga = 250;
if (!skip && (storage.getItem("audio.PGA2") ?? true)) TREM.audio.minor.push("PGA2");
if (!skip && (storage.getItem("audio.PGA2") ?? true)) TREM.audio.push("PGA2");
rts_screenshot();
plugin.emit("rtsPgaHigh");
} else if (max_pga > 8) {
TREM.rts_audio.pga = 200;
if (!skip && (storage.getItem("audio.PGA1") ?? true)) TREM.audio.minor.push("PGA1");
if (!skip && (storage.getItem("audio.PGA1") ?? true)) TREM.audio.push("PGA1");
rts_screenshot();
plugin.emit("rtsPgaLow");
}
Expand Down

0 comments on commit 3ccbaa3

Please sign in to comment.