Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bake sound #109

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/cookies.mp3
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export function TransactionsReducer(
switch (action.type) {
case "NEW": {
return {
...state,
new: action.payload.data,
submitted: [...state.submitted],
};
}
case "SET_SUBMITTED": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "./TransactionsReducer";
import { useWaitForTransaction } from "wagmi";
import { useToast } from "../ToastContext/ToastContext";
import { useAudio } from "../../hooks/useAudio/useAudio";

export function TransactionWatcher({
transaction,
Expand All @@ -15,6 +16,7 @@ export function TransactionWatcher({
}) {
const { status, hash } = transaction;
const { toastDispatch } = useToast();
const { playSound } = useAudio({ src: "/cookies.mp3" });

const { data: waitData } = useWaitForTransaction({ hash });

Expand All @@ -30,6 +32,7 @@ export function TransactionWatcher({
if (status !== "SUBMITTED") return;
if (waitData.status === "success") {
transactionsDispatch({ type: "SET_SUCCESS", payload: { hash } });
if (transaction.data.type === "BAKE") playSound();
toastDispatch({
type: "NEW",
payload: { toastType: "CONFIRMED", txHash: hash },
Expand Down
20 changes: 20 additions & 0 deletions src/app/core/hooks/useAudio/audioContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let audioContext: AudioContext | undefined;

export function getAudioContext() {
if (!audioContext) {
audioContext = new AudioContext();
}
return audioContext;
}

export function resumeAudioContext() {
if (audioContext && audioContext.state === "suspended") {
return audioContext.resume();
}
}

export function suspendAudioContext() {
if (audioContext && audioContext.state === "running") {
return audioContext.suspend();
}
}
55 changes: 55 additions & 0 deletions src/app/core/hooks/useAudio/useAudio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useEffect, useState } from "react";
import { getAudioContext } from "./audioContext";

export function useAudio({ src }: { src: string }) {
const [audioBuffer, setAudioBuffer] = useState<AudioBuffer | null>(null);

useEffect(() => {
async function fetchAudio() {
const audioContext = getAudioContext();
if (!audioContext) return;
const arrayBuffer = await fetch(src).then((res) => res.arrayBuffer());
audioContext
.decodeAudioData(arrayBuffer)
.then((audioBuffer) => {
setAudioBuffer(audioBuffer);
})
.catch((err): void => {
console.log({ err });
});
}
fetchAudio();
}, [src]);

async function playSound() {
if (!audioBuffer) return;
const audioContext = getAudioContext();
const src = audioContext.createBufferSource();
const gain = audioContext.createGain();
const now = audioContext.currentTime;
if (audioContext.state === "suspended") {
await audioContext.resume();
}
src.buffer = audioBuffer;
src.loop = false;
src.connect(gain);
gain.connect(audioContext.destination);
src.start(now);
gain.gain.setValueAtTime(1, now);
gain.gain.setValueAtTime(1, now + 0.4);
gain.gain.linearRampToValueAtTime(0, now + 1.2);

// console.log("playing sound");
// console.log("playing sound", audioContext.state);
// if (audioContext.state === "suspended") {
// await audioContext.resume();
// console.log("ctx state after:", audioContext.state);
// source.onended = () => initSource(audioBuffer);
// return;
// }
// source.start();
// source.onended = () => initSource(audioBuffer);
}

return { playSound };
}