From 3ccbaa3268edce2c1b14096ff0378bcdc762509b Mon Sep 17 00:00:00 2001 From: whes1015 Date: Tue, 25 Jul 2023 21:40:03 +0800 Subject: [PATCH] refactor: audio --- src/core/index/api.js | 4 +-- src/core/index/event.js | 16 ++++----- src/core/index/loop.js | 73 ++++++++++++++--------------------------- src/core/index/main.js | 7 ++-- src/core/index/rts.js | 12 +++---- 5 files changed, 42 insertions(+), 70 deletions(-) diff --git a/src/core/index/api.js b/src/core/index/api.js index 5b99665b..d0aff5ee 100644 --- a/src/core/index/api.js +++ b/src/core/index/api.js @@ -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(), @@ -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(), diff --git a/src/core/index/event.js b/src/core/index/event.js index b76402ec..ad53cb90 100644 --- a/src/core/index/event.js +++ b/src/core/index/event.js @@ -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); @@ -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); @@ -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; @@ -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; @@ -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", "此地震可能造成災害"); } @@ -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; @@ -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); } @@ -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", "此地震可能造成災害"); } diff --git a/src/core/index/loop.js b/src/core/index/loop.js index 9da5eff4..ce626581 100644 --- a/src/core/index/loop.js +++ b/src/core/index/loop.js @@ -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; @@ -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]; @@ -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(); }); }; }); @@ -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) { @@ -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()}`);} } } } diff --git a/src/core/index/main.js b/src/core/index/main.js index 2e651b3d..e20f2401 100644 --- a/src/core/index/main.js +++ b/src/core/index/main.js @@ -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, }, diff --git a/src/core/index/rts.js b/src/core/index/rts.js index 110fa828..fc181c7a 100644 --- a/src/core/index/rts.js +++ b/src/core/index/rts.js @@ -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", "請留意 中央氣象局
是否發布 地震預警", 15000); if (!skip && speecd_use) speech.speak({ text: "地震檢測,請留意中央氣象局是否發布地震預警" }); } @@ -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}`, @@ -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}`, @@ -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}`, @@ -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"); }