Skip to content

Commit

Permalink
✨ 增加一个示例声音
Browse files Browse the repository at this point in the history
  • Loading branch information
Littlefean committed Nov 26, 2024
1 parent a1b6894 commit ad4de27
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/core/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export namespace Settings {
moveAmplitude: number;
moveFriction: number;
gamepadDeadzone: number;

// 音效相关
soundEnabled: boolean;
cuttingLineStartSoundFile: string;
// github 相关
githubToken: string;
githubUser: string;
Expand Down Expand Up @@ -73,6 +75,9 @@ export namespace Settings {
moveAmplitude: 2,
moveFriction: 0.1,
gamepadDeadzone: 0.1,
// 音效相关
soundEnabled: true,
cuttingLineStartSoundFile: "",

// github 相关
githubToken: "",
Expand Down
37 changes: 32 additions & 5 deletions src/core/SoundService.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@

// 实测发现 不可行:
// @tauri-apps/plugin-fs 只能读取文本文件,不能强行读取流文件并强转为ArrayBuffer
// import { readTextFile } from "@tauri-apps/plugin-fs";

import { invoke } from "@tauri-apps/api/core";
import { StringDict } from "./dataStruct/StringDict";
import { Settings } from "./Settings";

/**
* 播放音效的服务
* 这个音效播放服务是用户自定义的
*/
export namespace SoundService {
export function testPlay() {
loadAndPlaySound("");

let cuttingLineStartSoundFile = "";

export function init() {
Settings.watch("cuttingLineStartSoundFile", (value) => {
cuttingLineStartSoundFile = value;
});
}

export namespace play {
// 开始切断
export function cuttingLineStart() {
loadAndPlaySound(cuttingLineStartSoundFile);
}

// 开始连接
// 连接吸附到目标点

// 自动保存执行特效
// 自动备份执行特效

// 框选增加物体音效

// 切断特效声音
// 连接成功
}

const audioContext = new window.AudioContext();


async function loadAndPlaySound(filePath: string) {
if (filePath.trim() === "") {
console.log("filePath is empty");
return;
}

// 解码音频数据
const audioBuffer = await getAudioBufferByFilePath(filePath);
const source = audioContext.createBufferSource();
Expand All @@ -29,7 +57,6 @@ export namespace SoundService {
const pathAudioBufferMap = new StringDict<AudioBuffer>();

async function getAudioBufferByFilePath(filePath: string) {

// 先从缓存中获取音频数据
if (pathAudioBufferMap.hasId(filePath)) {
const result = pathAudioBufferMap.getById(filePath);
Expand Down
3 changes: 3 additions & 0 deletions src/core/controller/concrete/ControllerCutting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CircleFlameEffect } from "../../effect/concrete/CircleFlameEffect";
import { LineCuttingEffect } from "../../effect/concrete/LineCuttingEffect";
import { EdgeRenderer } from "../../render/canvas2d/entityRenderer/edge/EdgeRenderer";
import { Renderer } from "../../render/canvas2d/renderer";
import { SoundService } from "../../SoundService";
import { Stage } from "../../stage/Stage";
import { StageManager } from "../../stage/stageManager/StageManager";
import { Section } from "../../stageObject/entity/Section";
Expand Down Expand Up @@ -45,6 +46,8 @@ ControllerCutting.mousedown = (event: MouseEvent) => {
cuttingStartLocation,
cuttingStartLocation.clone(),
);
// 添加音效提示
SoundService.play.cuttingLineStart();
} else {
Stage.isCutting = false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { StartFilesManager } from "./core/StartFilesManager";
import "./index.pcss";
import { DialogProvider } from "./utils/dialog";
import { PopupDialogProvider } from "./utils/popupDialog";
import { SoundService } from "./core/SoundService";

const router = createMemoryRouter(routes);
const Routes = () => <RouterProvider router={router} />;
Expand Down Expand Up @@ -66,6 +67,7 @@ async function loadSyncModules() {
Stage.init();
StageHistoryManager.init();
StageStyleManager.init();
SoundService.init();
}

/** 加载语言文件 */
Expand Down
1 change: 1 addition & 0 deletions src/pages/settings/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function SettingsLayout() {
<NavLink to="/settings/control">{t("control")}</NavLink>
<NavLink to="/settings/ai">{t("ai")}</NavLink>
<NavLink to="/settings/github">{t("github")}</NavLink>
<NavLink to="/settings/sounds">sounds</NavLink>
</div>
<div className="container mx-auto flex-1 overflow-auto">
<Outlet />
Expand Down
16 changes: 16 additions & 0 deletions src/pages/settings/sounds.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Folder } from "lucide-react";
import { SettingField } from "./_field";

export default function Sounds() {
return (
<>
<SettingField
icon={<Folder />}
settingKey="cuttingLineStartSoundFile"
title={"切割线开始音效"}
details={"右键按下时刚开始创建切割线准备切割东西时播放的音效文件。"}
type="text"
/>
</>
);
}
2 changes: 1 addition & 1 deletion src/pages/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default function TestPage() {
</div>
<Button onClick={handleTestHttp}>test http</Button>
<Button onClick={handleTestImageBase64}>getImageBase64</Button>
<Button onClick={SoundService.testPlay}>test sound</Button>
<Button onClick={SoundService.play.cuttingLineStart}>test sound</Button>
last launch: {LastLaunch.version}
</div>
);
Expand Down
1 change: 1 addition & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type Path =
| `/settings/github`
| `/settings/performance`
| `/settings/physical`
| `/settings/sounds`
| `/settings/visual`
| `/test`
| `/welcome`;
Expand Down

0 comments on commit ad4de27

Please sign in to comment.