jjhoffstein commited on
Commit
b910de7
·
verified ·
1 Parent(s): 1055d4f

Upload 2 files

Browse files
Files changed (2) hide show
  1. file.py +308 -0
  2. requirements.txt +0 -0
file.py ADDED
@@ -0,0 +1,308 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fasthtml.common import *
2
+ # MonsterUI shadows fasthtml components with the same name
3
+ from monsterui.all import *
4
+ # If you don't want shadowing behavior, you use import monsterui.core as ... style instead
5
+
6
+ # Get frankenui and tailwind headers via CDN using Theme.blue.headers()
7
+ _hdrs = Theme.blue.headers()
8
+
9
+ # fast_app is shadowed by MonsterUI to make it default to no Pico, and add body classes
10
+ # needed for frankenui theme styling
11
+ app, rt = fast_app(hdrs=_hdrs)
12
+
13
+
14
+ def TimerCard():
15
+ return Card(
16
+ Div(
17
+ Div(
18
+ H1("Two-Phase Bird Timer"),
19
+ ThemePicker(color=False, radii=False, shadows=False, font=False, mode=True, cls="ml-auto"),
20
+ cls="flex items-center justify-between"
21
+ ),
22
+ H4("Set phase durations and press Start.", cls="text-muted-foreground mb-2"),
23
+ cls="space-y-2"
24
+ ),
25
+ Grid(
26
+ # Controls
27
+ Div(
28
+ Div(
29
+ LabelInput("First phase seconds", id="phase1", type="number", value="60", min="1"),
30
+ LabelInput("Second phase seconds", id="phase2", type="number", value="15", min="1"),
31
+ Div(
32
+ Button("Start", id="startBtn", cls=(ButtonT.primary, "mr-2")),
33
+ Button("Reset", id="resetBtn", cls=ButtonT.secondary),
34
+ cls="flex items-center"
35
+ ),
36
+ cls="space-y-3"
37
+ ),
38
+ cls="p-2"
39
+ ),
40
+ # Display
41
+ Div(
42
+ Div(
43
+ H2("00:00", id="timerDisplay", cls="font-mono text-5xl"),
44
+ H4("Phase: none", id="phaseLabel", cls="text-muted-foreground"),
45
+ cls="text-center space-y-1"
46
+ ),
47
+ Div(
48
+ Img(
49
+ src="https://cdn.pixabay.com/photo/2025/05/04/18/04/robin-9578746_1280.jpg",
50
+ alt="Bird",
51
+ id="imgPhase1",
52
+ cls="rounded-lg border hidden"
53
+ ),
54
+ Img(
55
+ src="https://cdn.pixabay.com/photo/2025/04/17/08/33/ring-necked-parakeet-9539733_1280.jpg",
56
+ alt="Parakeet",
57
+ id="imgPhase2",
58
+ cls="rounded-lg border hidden"
59
+ ),
60
+ cls="mt-4 flex justify-center gap-4"
61
+ ),
62
+ cls="p-2"
63
+ ),
64
+ cols_lg=2
65
+ ),
66
+ # Test sound buttons
67
+ Div(
68
+ H3("Test Sounds", cls="text-lg font-semibold mb-3"),
69
+ Div(
70
+ Button("🐦 Test Bird Chirp", id="testBirdBtn", cls=(ButtonT.secondary, "mr-3")),
71
+ Button("🦜 Test Parakeet Call", id="testParakeetBtn", cls=ButtonT.secondary),
72
+ cls="flex gap-3"
73
+ ),
74
+ cls="mt-6 p-4 border-t"
75
+ ),
76
+
77
+ # Inline script: timer logic, Web Audio, and dark mode toggle
78
+ Script("""
79
+ (() => {
80
+ const display = document.getElementById('timerDisplay');
81
+ const phaseLabel = document.getElementById('phaseLabel');
82
+ const startBtn = document.getElementById('startBtn');
83
+ const resetBtn = document.getElementById('resetBtn');
84
+ const img1 = document.getElementById('imgPhase1');
85
+ const img2 = document.getElementById('imgPhase2');
86
+ const phase1Input = document.getElementById('phase1');
87
+ const phase2Input = document.getElementById('phase2');
88
+ const testBirdBtn = document.getElementById('testBirdBtn');
89
+ const testParakeetBtn = document.getElementById('testParakeetBtn');
90
+
91
+
92
+ let intervalId = null;
93
+ let startEpochMs = null;
94
+ let phase1Ms = 60000; // default 60s
95
+ let phase2Ms = 15000; // default 15s
96
+ let phase1Fired = false;
97
+
98
+ const fmt = (ms) => {
99
+ const total = Math.max(0, Math.floor(ms / 1000));
100
+ const m = String(Math.floor(total / 60)).padStart(2, '0');
101
+ const s = String(total % 60).padStart(2, '0');
102
+ return `${m}:${s}`;
103
+ };
104
+
105
+ const showPhase = (phase) => {
106
+ if (phase === 1) {
107
+ phaseLabel.textContent = 'Phase: bird';
108
+ img1.classList.remove('hidden');
109
+ img2.classList.add('hidden');
110
+ } else if (phase === 2) {
111
+ phaseLabel.textContent = 'Phase: parakeet';
112
+ img2.classList.remove('hidden');
113
+ img1.classList.add('hidden');
114
+ } else {
115
+ phaseLabel.textContent = 'Phase: none';
116
+ img1.classList.add('hidden');
117
+ img2.classList.add('hidden');
118
+ }
119
+ };
120
+
121
+ // Web Audio setup per user gesture
122
+ let audioCtx = null;
123
+ const ensureAudio = () => {
124
+ if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
125
+ };
126
+
127
+ const chirp = () => {
128
+ ensureAudio();
129
+ const now = audioCtx.currentTime;
130
+
131
+ // First chirp
132
+ const o1 = audioCtx.createOscillator();
133
+ const g1 = audioCtx.createGain();
134
+ o1.type = 'triangle';
135
+ o1.frequency.setValueAtTime(1800, now);
136
+ o1.frequency.exponentialRampToValueAtTime(2600, now + 0.12);
137
+ g1.gain.setValueAtTime(0, now);
138
+ g1.gain.linearRampToValueAtTime(0.4, now + 0.02);
139
+ g1.gain.exponentialRampToValueAtTime(0.0001, now + 0.25);
140
+ o1.connect(g1).connect(audioCtx.destination);
141
+ o1.start(now);
142
+ o1.stop(now + 0.28);
143
+
144
+ // Second chirp (slightly delayed)
145
+ const o2 = audioCtx.createOscillator();
146
+ const g2 = audioCtx.createGain();
147
+ o2.type = 'triangle';
148
+ o2.frequency.setValueAtTime(1900, now + 0.35); // Slightly higher pitch
149
+ o2.frequency.exponentialRampToValueAtTime(2700, now + 0.47);
150
+ g2.gain.setValueAtTime(0, now + 0.35);
151
+ g2.gain.linearRampToValueAtTime(0.4, now + 0.37);
152
+ g2.gain.exponentialRampToValueAtTime(0.0001, now + 0.60);
153
+ o2.connect(g2).connect(audioCtx.destination);
154
+ o2.start(now + 0.35);
155
+ o2.stop(now + 0.63);
156
+ };
157
+
158
+ const parakeetCall = () => {
159
+ ensureAudio();
160
+ const now = audioCtx.currentTime;
161
+ const duration = 2.2; // Shorter, more natural duration
162
+
163
+ // --- Two oscillators for natural parakeet sound ---
164
+ const osc1 = audioCtx.createOscillator();
165
+ osc1.type = 'sine'; // Pure tone for natural sound
166
+ const osc2 = audioCtx.createOscillator();
167
+ osc2.type = 'triangle'; // Adds warmth and harmonics
168
+
169
+ // --- Natural filtering for bird-like resonance ---
170
+ const filter = audioCtx.createBiquadFilter();
171
+ filter.type = 'bandpass';
172
+ filter.Q.value = 8; // Moderate Q for natural resonance
173
+ filter.frequency.value = 1200; // Parakeet frequency range
174
+
175
+ // --- Light saturation for warmth ---
176
+ const waveShaper = audioCtx.createWaveShaper();
177
+ const n_samples = 256;
178
+ const curve = new Float32Array(n_samples);
179
+ for (let i = 0; i < n_samples; ++i) {
180
+ const x = (i * 2) / n_samples - 1;
181
+ // Gentle saturation curve
182
+ curve[i] = Math.tanh(x * 2);
183
+ }
184
+ waveShaper.curve = curve;
185
+ waveShaper.oversample = '2x';
186
+
187
+ const gain = audioCtx.createGain();
188
+
189
+ // --- Single LFO for natural vibrato ---
190
+ const lfo = audioCtx.createOscillator();
191
+ lfo.type = 'sine';
192
+ lfo.frequency.value = 6; // Natural vibrato rate
193
+ const lfoGain = audioCtx.createGain();
194
+ lfoGain.gain.value = 80; // Subtle modulation
195
+
196
+ // --- Natural frequency sweeps mimicking parakeet calls ---
197
+ osc1.frequency.setValueAtTime(1000, now);
198
+ osc1.frequency.exponentialRampToValueAtTime(1800, now + 0.3);
199
+ osc1.frequency.exponentialRampToValueAtTime(1200, now + 0.8);
200
+ osc1.frequency.exponentialRampToValueAtTime(2000, now + 1.2);
201
+ osc1.frequency.exponentialRampToValueAtTime(1100, now + 1.8);
202
+ osc1.frequency.exponentialRampToValueAtTime(1600, now + duration);
203
+
204
+ osc2.frequency.setValueAtTime(1200, now);
205
+ osc2.frequency.exponentialRampToValueAtTime(2000, now + 0.4);
206
+ osc2.frequency.exponentialRampToValueAtTime(1400, now + 0.9);
207
+ osc2.frequency.exponentialRampToValueAtTime(2200, now + 1.3);
208
+ osc2.frequency.exponentialRampToValueAtTime(1300, now + 1.9);
209
+ osc2.frequency.exponentialRampToValueAtTime(1800, now + duration);
210
+
211
+ // --- Connect the audio graph ---
212
+ lfo.connect(lfoGain);
213
+ lfoGain.connect(osc1.frequency);
214
+ osc1.connect(filter);
215
+ osc2.connect(filter);
216
+ filter.connect(waveShaper);
217
+ waveShaper.connect(gain);
218
+ gain.connect(audioCtx.destination);
219
+
220
+ // --- Natural envelope with bird-like peaks ---
221
+ gain.gain.setValueAtTime(0, now);
222
+ gain.gain.linearRampToValueAtTime(0.7, now + 0.05); // Louder and stronger
223
+ gain.gain.linearRampToValueAtTime(0.5, now + 0.4);
224
+ gain.gain.linearRampToValueAtTime(0.8, now + 0.8); // Second call - stronger
225
+ gain.gain.linearRampToValueAtTime(0.4, now + 1.3);
226
+ gain.gain.linearRampToValueAtTime(0.9, now + 1.7); // Final call - loudest and strongest
227
+ gain.gain.exponentialRampToValueAtTime(0.0001, now + duration);
228
+
229
+ // --- Start and stop ---
230
+ lfo.start(now);
231
+ osc1.start(now);
232
+ osc2.start(now);
233
+
234
+ lfo.stop(now + duration);
235
+ osc1.stop(now + duration);
236
+ osc2.stop(now + duration);
237
+ };
238
+
239
+ const start = () => {
240
+ // Freeze inputs during run
241
+ startBtn.disabled = true;
242
+ phase1Input.disabled = true;
243
+ phase2Input.disabled = true;
244
+
245
+ const p1 = Math.max(1, parseInt(phase1Input.value || '60', 10));
246
+ const p2 = Math.max(1, parseInt(phase2Input.value || '15', 10));
247
+ phase1Ms = p1 * 1000;
248
+ phase2Ms = p2 * 1000;
249
+ phase1Fired = false;
250
+ showPhase(null);
251
+
252
+ startEpochMs = Date.now();
253
+ display.textContent = '00:00';
254
+
255
+ intervalId = setInterval(() => {
256
+ const elapsed = Date.now() - startEpochMs;
257
+ display.textContent = fmt(elapsed);
258
+ if (!phase1Fired && elapsed >= phase1Ms) {
259
+ phase1Fired = true;
260
+ showPhase(1);
261
+ chirp();
262
+ }
263
+ if (elapsed >= (phase1Ms + phase2Ms)) {
264
+ showPhase(2);
265
+ parakeetCall();
266
+ stop();
267
+ }
268
+ }, 100);
269
+ };
270
+
271
+ const stop = () => {
272
+ if (intervalId) {
273
+ clearInterval(intervalId);
274
+ intervalId = null;
275
+ }
276
+ startBtn.disabled = false;
277
+ phase1Input.disabled = false;
278
+ phase2Input.disabled = false;
279
+ };
280
+
281
+ const reset = () => {
282
+ stop();
283
+ startEpochMs = null;
284
+ display.textContent = '00:00';
285
+ showPhase(null);
286
+ };
287
+
288
+ startBtn.addEventListener('click', (e) => { e.preventDefault(); start(); });
289
+ resetBtn.addEventListener('click', (e) => { e.preventDefault(); reset(); });
290
+ testBirdBtn.addEventListener('click', (e) => { e.preventDefault(); chirp(); });
291
+ testParakeetBtn.addEventListener('click', (e) => { e.preventDefault(); parakeetCall(); });
292
+ })();
293
+ """),
294
+ cls="max-w-3xl mx-auto"
295
+ )
296
+
297
+
298
+ @rt
299
+ def index():
300
+ return Titled(
301
+ Container(
302
+ TimerCard(),
303
+ cls="py-6"
304
+ )
305
+ )
306
+
307
+
308
+ serve()
requirements.txt ADDED
File without changes