Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
mnemitz committed Feb 26, 2025
1 parent 50cbb79 commit 0488e57
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 139 deletions.
12 changes: 8 additions & 4 deletions examples/nextjs-flow/components/AudioProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import {
} from 'react';

export function AudioProvider({ children }: PropsWithChildren) {
// Separate audio contexts for recording and playback.
// In practice they will be the same AudioContext, except in Firefox where sample rates may differ
// See bug tracked here: https://bugzilla.mozilla.org/show_bug.cgi?id=1725336https://bugzilla.mozilla.org/show_bug.cgi?id=1725336
// TODO: If/when the bug is fixed, we can use the same audio context for both recording and playback
// Get audio contexts for input and playback
// (see note above `useAudioContexts` for more info)
const { inputAudioContext, playbackAudioContext } = useAudioContexts();

return (
Expand All @@ -32,6 +30,10 @@ export function AudioProvider({ children }: PropsWithChildren) {
);
}

// This hook returns audio contexts for recording and playback.
// In practice they will be the same AudioContext, except in Firefox where sample rates may differ
// See bug tracked here: https://bugzilla.mozilla.org/show_bug.cgi?id=1725336https://bugzilla.mozilla.org/show_bug.cgi?id=1725336
// TODO: If/when the bug is fixed, we can use the same audio context for both recording and playback
function useAudioContexts() {
const hydrated = useHydrated();
const inputAudioContext = useMemo(
Expand All @@ -53,13 +55,15 @@ function useAudioContexts() {
return { inputAudioContext, playbackAudioContext };
}

// Lets us know if we're rendering client side or not
const useHydrated = () =>
useSyncExternalStore(
() => () => {},
() => true,
() => false,
);

// Close audio context when component unmounts
function useCleanupAudioContext(context?: AudioContext) {
useEffect(() => {
return () => {
Expand Down
102 changes: 0 additions & 102 deletions examples/nextjs-flow/hooks/useFlowWithBrowserAudio.ts

This file was deleted.

31 changes: 0 additions & 31 deletions examples/nextjs-flow/hooks/useStartFlowSession.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type UsePCMAudioRecorderReturn = {

export function usePCMAudioRecorder(
workletScriptURL: string,
audioContext?: AudioContext,
audioContext: AudioContext | undefined,
): UsePCMAudioRecorderReturn {
const recorder = useMemo(
() => new PCMRecorder(workletScriptURL),
Expand Down
2 changes: 1 addition & 1 deletion packages/web-pcm-player-react/src/use-pcm-audio-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { PCMPlayer } from '@speechmatics/web-pcm-player';
import { useMemo, useCallback, useSyncExternalStore } from 'react';

export function usePCMAudioPlayer(audioContext?: AudioContext) {
export function usePCMAudioPlayer(audioContext: AudioContext | undefined) {
const player = useMemo(() => {
if (!audioContext) {
return null;
Expand Down

0 comments on commit 0488e57

Please sign in to comment.