|
| 1 | +import { useCallback, useEffect, useRef } from 'react'; |
| 2 | + |
| 3 | +class TonePlayer { |
| 4 | + private audioContext: AudioContext; |
| 5 | + |
| 6 | + private audioElement: HTMLAudioElement; |
| 7 | + |
| 8 | + private gainNode: GainNode; |
| 9 | + |
| 10 | + private filter: BiquadFilterNode; |
| 11 | + |
| 12 | + private destination: MediaStreamAudioDestinationNode; |
| 13 | + |
| 14 | + constructor() { |
| 15 | + this.audioContext = new AudioContext(); |
| 16 | + this.audioElement = new Audio(); |
| 17 | + |
| 18 | + // Route audio through an audio element |
| 19 | + // In order to be able to set the sink id |
| 20 | + this.destination = this.audioContext.createMediaStreamDestination(); |
| 21 | + this.audioElement.srcObject = this.destination.stream; |
| 22 | + |
| 23 | + // Audio volume control |
| 24 | + this.gainNode = this.audioContext.createGain(); |
| 25 | + this.gainNode.gain.value = 0.5; |
| 26 | + |
| 27 | + // This filter makes the sound more natural |
| 28 | + this.filter = this.audioContext.createBiquadFilter(); |
| 29 | + this.filter.type = 'lowpass'; |
| 30 | + this.filter.frequency.value = 8000; |
| 31 | + |
| 32 | + this.gainNode.connect(this.filter); |
| 33 | + this.filter.connect(this.destination); |
| 34 | + } |
| 35 | + |
| 36 | + public setSinkId(sinkId: string) { |
| 37 | + if (this.audioElement.setSinkId) { |
| 38 | + return this.audioElement.setSinkId(sinkId); |
| 39 | + } |
| 40 | + console.warn('setSinkId not supported on this browser'); |
| 41 | + } |
| 42 | + |
| 43 | + public static setupOscillator(audioCtx: AudioContext, filter: AudioNode) { |
| 44 | + const oscillator = audioCtx.createOscillator(); |
| 45 | + oscillator.type = 'sine'; |
| 46 | + oscillator.connect(filter); |
| 47 | + return oscillator; |
| 48 | + } |
| 49 | + |
| 50 | + public play(highFreq: number, lowFreq: number, durationMs?: number) { |
| 51 | + const highFrequencyOscillator = TonePlayer.setupOscillator(this.audioContext, this.gainNode); |
| 52 | + const lowFrequencyOscillator = TonePlayer.setupOscillator(this.audioContext, this.gainNode); |
| 53 | + |
| 54 | + lowFrequencyOscillator.frequency.value = lowFreq; |
| 55 | + highFrequencyOscillator.frequency.value = highFreq; |
| 56 | + |
| 57 | + lowFrequencyOscillator.start(); |
| 58 | + highFrequencyOscillator.start(); |
| 59 | + |
| 60 | + // Ensure audio element is playing |
| 61 | + if (this.audioElement.paused) { |
| 62 | + this.audioElement.play().catch((error) => { |
| 63 | + console.warn('Failed to play audio element:', error); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + setTimeout(() => { |
| 68 | + lowFrequencyOscillator.stop(); |
| 69 | + highFrequencyOscillator.stop(); |
| 70 | + highFrequencyOscillator.disconnect(); |
| 71 | + lowFrequencyOscillator.disconnect(); |
| 72 | + }, durationMs ?? 400); |
| 73 | + } |
| 74 | + |
| 75 | + public destroy() { |
| 76 | + this.audioContext.close(); |
| 77 | + this.audioElement.pause(); |
| 78 | + this.audioElement.srcObject = null; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +const DIGIT_TONE_MAP = { |
| 83 | + '1': [1209, 697], |
| 84 | + '2': [1336, 697], |
| 85 | + '3': [1477, 697], |
| 86 | + '4': [1209, 770], |
| 87 | + '5': [1336, 770], |
| 88 | + '6': [1477, 770], |
| 89 | + '7': [1209, 852], |
| 90 | + '8': [1336, 852], |
| 91 | + '9': [1477, 852], |
| 92 | + '*': [1209, 941], |
| 93 | + '0': [1336, 941], |
| 94 | + '#': [1477, 941], |
| 95 | +} as const; |
| 96 | + |
| 97 | +export const isValidTone = (tone: string): tone is keyof typeof DIGIT_TONE_MAP => { |
| 98 | + return Object.keys(DIGIT_TONE_MAP).includes(tone); |
| 99 | +}; |
| 100 | + |
| 101 | +export const useTonePlayer = (sinkId?: string) => { |
| 102 | + const tonePlayer = useRef<TonePlayer | null>(null); |
| 103 | + |
| 104 | + useEffect(() => { |
| 105 | + tonePlayer.current = new TonePlayer(); |
| 106 | + return () => tonePlayer.current?.destroy(); |
| 107 | + }, []); |
| 108 | + |
| 109 | + useEffect(() => { |
| 110 | + if (tonePlayer.current && sinkId) { |
| 111 | + tonePlayer.current.setSinkId(sinkId); |
| 112 | + } |
| 113 | + }, [sinkId]); |
| 114 | + |
| 115 | + const playTone = useCallback( |
| 116 | + (digit: keyof typeof DIGIT_TONE_MAP) => { |
| 117 | + if (!tonePlayer.current) { |
| 118 | + return; |
| 119 | + } |
| 120 | + tonePlayer.current.play(DIGIT_TONE_MAP[digit][0], DIGIT_TONE_MAP[digit][1], 250); |
| 121 | + }, |
| 122 | + [tonePlayer], |
| 123 | + ); |
| 124 | + |
| 125 | + return playTone; |
| 126 | +}; |
0 commit comments