Compare commits

..

2 commits

4 changed files with 23 additions and 16 deletions

View file

@ -40,21 +40,23 @@ export const TrafficLight = memo(function TrafficLight({state, speed, raiseUIEve
// preloadAudio(sndAtmosphere); // preloadAudio(sndAtmosphere);
// the traffic light makes sound too: // play wind
useEffect(() => { useEffect(() => {
const stopPlaying = playURL(sndAtmosphere, true); const stopPlaying = playURL(sndAtmosphere, true);
return () => stopPlaying(); return () => stopPlaying();
}, []); }, []);
useEffect(() => { // for added realism, every light color has its own buzzing noise volume
if (redOn || yellowOn || greenOn) { for (const [color, gain] of [[redOn, 0.5], [yellowOn, 1], [greenOn, 0.3]] as [boolean, number][]) {
const stopPlaying = playURL(sndBuzz, true); useEffect(() => {
return () => { if (color) {
stopPlaying(); const stopPlaying = playURL(sndBuzz, true, gain);
}; return () => {
} stopPlaying();
else return () => {}; };
}, [redOn || yellowOn || greenOn]) }
}, [color]);
}
return <> return <>
<style>{` <style>{`

View file

@ -28,12 +28,15 @@ export function useAudioContext(speed: number) {
.then(buf => ref.current!.ctx.decodeAudioData(buf)); .then(buf => ref.current!.ctx.decodeAudioData(buf));
}), [ref.current]); }), [ref.current]);
function play(url: string, loop: boolean) { function play(url: string, loop: boolean, gain: number = 1) {
const srcPromise = url2AudioBuf(url) const srcPromise = url2AudioBuf(url)
.then(audioBuf => { .then(audioBuf => {
const src = ref.current!.ctx.createBufferSource(); const src = ref.current!.ctx.createBufferSource();
const gainNode = ref.current!.ctx.createGain();
gainNode.gain.value = gain;
gainNode.connect(ref.current!.hipass);
src.buffer = audioBuf; src.buffer = audioBuf;
src.connect(ref.current!.hipass); src.connect(gainNode);
src.playbackRate.value = speed; src.playbackRate.value = speed;
src.loop = loop; src.loop = loop;
src.start(); src.start();
@ -59,5 +62,5 @@ export function useAudioContext(speed: number) {
} }
}, [speed]); }, [speed]);
return [play, url2AudioBuf] as [(url: string, loop: boolean) => ()=>void, (url:string)=>void]; return [play, url2AudioBuf] as [(url: string, loop: boolean, gain?: number) => ()=>void, (url:string)=>void];
} }

View file

@ -394,7 +394,6 @@ export function fire(simtime: number, t: Transition, ts: Map<string, Transition[
// transition actions // transition actions
environment = addEventParam(environment.enterScope("<transition>"), label); environment = addEventParam(environment.enterScope("<transition>"), label);
for (const action of label.actions) { for (const action of label.actions) {
console.log('environment after adding event param:', environment);
({environment, history, ...rest} = execAction(action, {environment, history, ...rest}, [t.uid])); ({environment, history, ...rest} = execAction(action, {environment, history, ...rest}, [t.uid]));
} }
environment = environment.dropScope(); environment = environment.dropScope();

View file

@ -115,6 +115,7 @@ export function coupledExecution<T extends {[name: string]: any}>(models: {[name
if (events.length > 0) { if (events.length > 0) {
const [event, ...rest] = events; const [event, ...rest] = events;
const destination = conns[model+'.'+event.name]; const destination = conns[model+'.'+event.name];
console.log(model, '.', event, destination);
if (destination === undefined) { if (destination === undefined) {
// ignore // ignore
console.log(`${model}.${event.name} goes nowhere`); console.log(`${model}.${event.name} goes nowhere`);
@ -160,11 +161,13 @@ export function coupledExecution<T extends {[name: string]: any}>(models: {[name
// @ts-ignore // @ts-ignore
state[modelName] = modelState; state[modelName] = modelState;
} }
console.log('all outputs:', allOutputs);
// 2. handle all output events (models' outputs may be inputs for each other) // 2. handle all output events (models' outputs may be inputs for each other)
let finalOutputs = []; let finalOutputs = [];
for (const [modelName, outputEvents] of allOutputs) { for (const [modelName, outputEvent] of allOutputs) {
console.log('what about', modelName, outputEvent);
let newOutputs; let newOutputs;
[newOutputs, state] = processOutputs(0, outputEvents, modelName, state); [newOutputs, state] = processOutputs(0, [outputEvent], modelName, state);
finalOutputs.push(...newOutputs); finalOutputs.push(...newOutputs);
} }
return [finalOutputs, state]; return [finalOutputs, state];