-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
171 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
"use client"; | ||
|
||
import { useState } from "react"; | ||
import { createNoiseNode, getNoiseTypes, NoiseWorkletNode } from "synthlet"; | ||
import { useSynth } from "./useSynth"; | ||
|
||
export function NoiseExample() { | ||
return ( | ||
<ExampleButton label="Start Noise example"> | ||
<NoiseSynthUI /> | ||
</ExampleButton> | ||
); | ||
} | ||
|
||
function NoiseSynthUI() { | ||
const [selectedNoiseType, setSelectedNoiseType] = useState(0); | ||
const [volume, setVolume] = useState(-60); | ||
const synth = useSynth((context) => new NoiseSynth(context)); | ||
if (!synth) return null; | ||
|
||
return ( | ||
<div className="bg-slate-800 text-white p-2 border border-slate-600 rounded"> | ||
<div className="text-xl mb-4">Noise generator example</div> | ||
<div className="flex items-center gap-2"> | ||
<div className="w-16 text-right">Type:</div> | ||
<select | ||
className="p-1 rounded bg-slate-700 text-slate-200" | ||
value={selectedNoiseType} | ||
onChange={(e) => { | ||
console.log(e.target.value); | ||
const noiseType = parseInt(e.target.value); | ||
setSelectedNoiseType(noiseType); | ||
synth.noise.type.setValueAtTime(noiseType, 0); | ||
}} | ||
> | ||
{getNoiseTypes().map((type) => ( | ||
<option key={type.value} value={type.value}> | ||
{type.name} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
<div className="flex items-center gap-2 mt-1"> | ||
<div className="w-16 text-right">Volume:</div> | ||
<input | ||
type="range" | ||
className="w-64" | ||
min={-60} | ||
max={0} | ||
step={1} | ||
value={volume} | ||
onChange={(e) => { | ||
const db = e.target.valueAsNumber; | ||
setVolume(db); | ||
const gain = db < -60 ? 0 : Math.pow(10, db / 20); | ||
synth.gain.gain.setValueAtTime(gain, 0); | ||
}} | ||
/> | ||
<div>{volume <= -60 ? "Muted" : volume + "dB"}</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
class NoiseSynth { | ||
gain: GainNode; | ||
noise: NoiseWorkletNode; | ||
constructor(context: AudioContext) { | ||
this.gain = new GainNode(context, { gain: 0 }); | ||
|
||
this.noise = createNoiseNode(context, { | ||
type: "White Random", | ||
}); | ||
this.noise.connect(this.gain).connect(context.destination); | ||
} | ||
|
||
disconnect() { | ||
this.gain.disconnect(); | ||
this.noise.disconnect(); | ||
} | ||
} | ||
|
||
function ExampleButton({ | ||
label, | ||
children, | ||
}: { | ||
label: string; | ||
children: React.ReactNode; | ||
}) { | ||
const [show, setShow] = useState(false); | ||
if (show) return children; | ||
|
||
return ( | ||
<button | ||
className="border border-white p-2 rounded opacity-50" | ||
onClick={() => { | ||
setShow(true); | ||
}} | ||
> | ||
{label} | ||
</button> | ||
); | ||
} |
2 changes: 1 addition & 1 deletion
2
site/app/examples/clock/ClockExample.tsx → site/examples/clock/ClockExample.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createSynthAudioContext } from "@/app/audio-context"; | ||
import { useEffect, useState } from "react"; | ||
|
||
type Synth = { | ||
disconnect: () => void; | ||
}; | ||
|
||
export function useSynth<T extends Synth>( | ||
createSynth: (context: AudioContext) => T | ||
) { | ||
const [synth, setSynth] = useState<T | null>(null); | ||
|
||
useEffect(() => { | ||
console.log("connecting"); | ||
let bye = false; | ||
let synth: T; | ||
createSynthAudioContext().then((context) => { | ||
if (bye) return; | ||
synth = createSynth(context); | ||
console.log("connected", synth); | ||
setSynth(synth); | ||
}); | ||
return () => { | ||
bye = true; | ||
console.log("disconnecting", synth); | ||
synth?.disconnect(); | ||
}; | ||
}, []); | ||
|
||
return synth; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters