-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.html
More file actions
571 lines (550 loc) · 30.6 KB
/
Copy pathfunctions.html
File metadata and controls
571 lines (550 loc) · 30.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<!doctype html>
<html lang="he" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>שומעים פונקציה</title>
<style>
:root{
--indigo:#4f46e5; --indigo-d:#3730a3; --indigo-l:#eef2ff;
--ink:#1e1b2e; --muted:#6b7280; --line:#e5e7eb; --bg:#ffffff; --good:#059669; --bad:#dc2626;
}
*{box-sizing:border-box}
html,body{margin:0;background:var(--bg);color:var(--ink);
font-family:system-ui,-apple-system,"Segoe UI",Arial,sans-serif;line-height:1.5}
header{padding:16px 18px;border-bottom:1px solid var(--line)}
h1{margin:0;font-size:20px;color:var(--indigo-d)}
.sub{color:var(--muted);font-size:13px;margin-top:2px}
main{max-width:860px;margin:0 auto;padding:14px}
.card{border:1px solid var(--line);border-radius:14px;padding:14px;margin-bottom:14px}
.truth{background:var(--indigo-l);border-color:#c7d2fe;font-size:13px}
.truth b{color:var(--indigo-d)}
label{font-size:13px;color:var(--muted);display:block;margin:2px 0}
.row{display:flex;flex-wrap:wrap;gap:10px;align-items:center}
button{font:inherit;cursor:pointer;border:1px solid var(--indigo);background:var(--indigo);
color:#fff;border-radius:10px;padding:12px 16px;min-height:48px;min-width:48px}
button.ghost{background:#fff;color:var(--indigo)}
button:disabled{opacity:.5;cursor:default}
button:focus-visible{outline:3px solid #a5b4fc;outline-offset:2px}
select,input[type=range]{font:inherit}
select{padding:10px;border-radius:10px;border:1px solid var(--line);min-height:48px;background:#fff}
input[type=range]{width:100%;height:34px}
.sl{display:flex;flex-direction:column;flex:1;min-width:150px}
.sl .val{font-weight:600;color:var(--ink)}
canvas{width:100%;height:auto;display:block;border:1px solid var(--line);border-radius:12px;background:#fff}
.formula{font-size:18px;font-weight:700;text-align:center;margin:8px 0;direction:ltr}
.desc{font-size:13px;color:var(--muted);text-align:center}
.legend{font-size:13px;color:var(--muted)}
.opts{display:grid;grid-template-columns:1fr 1fr;gap:10px}
.opts button{direction:ltr;font-weight:600}
.opts button.correct{background:var(--good);border-color:var(--good)}
.opts button.wrong{background:var(--bad);border-color:var(--bad)}
.score{font-weight:700}
.fb{min-height:22px;font-weight:600}
.fb.ok{color:var(--good)} .fb.no{color:var(--bad)}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
.listen-dock{display:flex;flex-wrap:wrap;gap:8px;align-items:center;margin-top:12px}
.listen-dock button{min-height:48px}
.mark-list{display:flex;flex-wrap:wrap;gap:8px;margin-top:10px}
.mark-list button{background:#eef2ff;color:#3730a3;border:1px solid #c7d2fe;border-radius:10px;padding:8px 12px;min-height:44px;font:inherit;font-weight:700;cursor:pointer}
.mark-list button[aria-current="true"]{background:#3730a3;color:#fff}
canvas:focus-visible{outline:3px solid #4f46e5;outline-offset:3px}
.mm-skip{position:absolute;inset-inline-start:8px;top:8px;z-index:20;padding:8px 12px;background:#1e1b2e;color:#fff;border-radius:8px;transform:translateY(-120%);font-weight:700}
.mm-skip:focus{transform:none}
#main:focus{outline:none}
@media (prefers-reduced-motion:reduce){*{animation:none!important;transition:none!important}}
</style>
</head>
<body>
<a class="mm-skip" href="#main">דלגו לתוכן</a>
<main id="main" tabindex="-1">
<header>
<h1>שומעים פונקציה</h1>
<div class="sub">מוצר סוניפיקציה לחטיבה ולתיכון — לא חלק מתרגול א׳–ד׳. ככל ש‑f(x) גבוה יותר — הצליל גבוה יותר. מקשים: רווח = נגן/עצור, Tab לסליידרים.</div>
</header>
<div class="card truth" role="note">
<b>מה באמת שומעים?</b> ברוב המצבים זו <b>סוניפיקציה</b>: גובה f(x) מתורגם לגובה צליל (מיפוי לוגריתמי, 130–1046 הרץ) — הצליל <i>מייצג</i> את הגרף, הוא לא "קול הפונקציה". יוצא דופן: <b>סינוס במצב טון‑אמיתי</b>, שם התדירות של הגל היא באמת התדר הנשמע. כל צליל תמיד מסונכרן לנקודה שרצה על הגרף.
</div>
<div class="card">
<div class="row" style="margin-bottom:10px">
<div class="sl" style="flex:0 0 auto;min-width:200px">
<label for="fn">משפחת פונקציות</label>
<select id="fn" aria-label="בחירת משפחת פונקציה"></select>
</div>
<div id="sineMode" class="sl" style="flex:0 0 auto;display:none">
<label> </label>
<button id="realTone" class="ghost" type="button" aria-pressed="false">🔊 טון אמיתי (סינוס)</button>
</div>
</div>
<div class="formula" id="formula">f(x)=</div>
<div class="desc" id="fdesc"></div>
<canvas id="cv" width="800" height="380" role="img" aria-label="גרף הפונקציה" tabindex="0"></canvas>
<div class="row" id="sliders" style="margin-top:12px"></div>
<div class="row" style="margin-top:12px">
<button id="play" type="button" aria-pressed="false">▶︎ נגן סריקה</button>
<span class="legend" id="playHint">הסמן זז שמאל→ימין; הצליל עולה עם הגרף.</span>
</div>
<div class="listen-dock" id="listenDock">
<button id="descBtn" type="button">תאר את הגרף</button>
<button id="stepPrev" type="button">נקודה קודמת</button>
<button id="stepNext" type="button">נקודה הבאה</button>
<button id="markNext" type="button">נקודת עניין הבאה</button>
<span class="legend" id="listenWhere" aria-live="polite"></span>
</div>
<div class="mark-list" id="markList" role="list" aria-label="נקודות עניין בגרף"></div>
<p class="legend" id="keyHelp">מקלדת על הגרף: חצים לנקודה, Home/End לקצוות, L לנקודת עניין, D לתיאור, רווח לסריקה. זה תיאור של הדגימה — לא קול הפונקציה.</p>
<div class="sr" id="live" aria-live="polite" aria-atomic="true"></div>
<div class="legend" style="margin-top:12px;display:flex;flex-wrap:wrap;gap:6px 16px;border-top:1px solid #e2e8f0;padding-top:10px" aria-label="מילון הצלילים">
<b style="flex-basis:100%;color:#4338ca">מילון הצלילים:</b>
<span>🔔 נקישה = <b>שורש</b> (חציית ציר x)</span>
<span>🎢 צליל בורח לקצה ואז שקט = <b>אסימפטוטה</b></span>
<span>🤫 שקט = מחוץ ל<b>תחום ההגדרה</b></span>
<span>⤴️ גובה צליל = ערך f(x)</span>
</div>
</div>
<div class="card" id="lessonPlay">
<div class="row" style="justify-content:space-between">
<b>שיעור האזנה מודרך</b>
<span class="score">שיעור <span id="lessonN">1</span> / <span id="lessonTotal">1</span></span>
</div>
<p class="desc" style="text-align:right;margin:8px 0">משימות האזנה לדגימה שעל המסך: לזהות שורש / אסימפטוטה / שקט, או להשוות שני גרפים באוזן. זה תרגול האזנה — לא מבחן שמיעה ולא הוכחה ששומעים לומדים אלגברה.</p>
<div class="formula" id="lessonTitle"></div>
<p id="lessonPrompt" class="desc" style="text-align:right"></p>
<div class="row" id="lessonPlayRow">
<button id="lessonPlayA" type="button">▶︎ השמע א׳</button>
<button id="lessonPlayB" class="ghost" type="button" hidden>▶︎ השמע ב׳</button>
</div>
<div class="opts" id="lessonOpts" style="margin-top:12px"></div>
<div class="fb" id="lessonFb" aria-live="polite"></div>
<p class="legend" id="lessonTeach"></p>
<div class="row">
<button id="lessonNext" class="ghost" type="button">לשיעור הבא</button>
</div>
</div>
<div class="card">
<div class="row" style="justify-content:space-between">
<b>🎧 נחש את הפונקציה באוזן</b>
<span class="score">ניקוד: <span id="score">0</span> · רצף: <span id="streak">0</span> · שיא: <span id="best">0</span></span>
</div>
<p class="desc" style="text-align:right;margin:8px 0">האפליקציה מנגנת סוניפיקציה של פונקציה אקראית — בחרו מי מבין הארבע זה היה.</p>
<div class="row">
<button id="gPlay" type="button">▶︎ השמע חידה</button>
<button id="gReplay" class="ghost" type="button" disabled>↻ שמע שוב</button>
<button id="gCompare" class="ghost" type="button" disabled>השוו ניחוש מול תשובה</button>
<button id="gNew" class="ghost" type="button">חידה חדשה</button>
</div>
<div class="opts" id="gOpts" style="margin-top:12px"></div>
<div class="fb" id="gFb" aria-live="polite"></div>
</div>
<div class="card legend">
<b style="color:var(--ink)">מקרא צליל:</b> צליל גבוה = f(x) גבוה · צליל נמוך = f(x) נמוך/שלילי · שקט = הפונקציה לא מוגדרת שם (למשל √ מתחת ל‑0, או 1/x באפס). קליק קצר = חצייה של ציר ה‑x (שורש).
</div>
</main>
<script src="src/lib/sonify.js"></script>
<script src="src/lib/teacherStore.js"></script>
<script src="src/lib/access.js"></script>
<script src="src/lib/graphListen.js"></script>
<script src="src/lib/listenLessons.js"></script>
<script src="src/lib/core.js"></script>
<script>
"use strict";
/* ---- functions: each returns f(x); params drawn from a,b,c sliders it declares ---- */
const XMIN=-10, XMAX=10;
const FUNCS = {
linear: {name:"לינארית", tex:"a·x + b", desc:"גליסנדו בקצב קבוע — התלילות היא a, הכיוון הוא הסימן.",
p:{a:{v:1,min:-3,max:3,step:.1},b:{v:0,min:-6,max:6,step:.5}}, f:(x,p)=>p.a*x+p.b},
quadratic: {name:"ריבועית", tex:"a·x² + b·x + c", desc:"גלישה שמאטה עד הקודקוד; מספר חציות ציר‑x = מספר השורשים.",
p:{a:{v:.4,min:-2,max:2,step:.1},b:{v:0,min:-6,max:6,step:.5},c:{v:-4,min:-8,max:8,step:.5}}, f:(x,p)=>p.a*x*x+p.b*x+p.c},
sqrt: {name:"שורש", tex:"a·√(x − b)", desc:"מוגדר רק מ‑b והלאה — משמאל יש שקט (תחום הגדרה).",
p:{a:{v:2,min:-3,max:3,step:.1},b:{v:0,min:-6,max:6,step:.5}}, f:(x,p)=> x>=p.b ? p.a*Math.sqrt(x-p.b) : NaN},
abs: {name:"ערך מוחלט", tex:"a·|x − b|", desc:"פינה חדה: היפוך כיוון בלי האטה (לעומת פרבולה חלקה).",
p:{a:{v:1,min:-3,max:3,step:.1},b:{v:0,min:-6,max:6,step:.5}}, f:(x,p)=>p.a*Math.abs(x-p.b)},
reciprocal:{name:"רציונלית 1/x", tex:"a / (x − b)", desc:"אסימפטוטה אנכית ב‑x=b — 'צוק' ואז שקט; אסימפטוטה אופקית בגובה 0.",
p:{a:{v:3,min:-6,max:6,step:.5},b:{v:0,min:-6,max:6,step:.5}}, f:(x,p)=>{const d=x-p.b; return Math.abs(d)<1e-3?NaN:p.a/d;}},
exp: {name:"אקספוננט", tex:"a·b^x", desc:"במיפוי‑צליל לוגריתמי אקספוננט נשמע כגליסנדו ישר לגמרי — כך 'לוג מיישר אקספוננט'.",
p:{a:{v:1,min:-3,max:3,step:.1},b:{v:1.6,min:.4,max:3,step:.05}}, f:(x,p)=>p.a*Math.pow(Math.abs(p.b)<.05?.05:p.b, x)},
sine: {name:"סינוס", tex:"a·sin(b·x)", desc:"מצב סריקה = קריאת גרף. 'טון אמיתי' = התדירות b נשמעת ממש כגובה הצליל.",
p:{a:{v:4,min:0,max:6,step:.5},b:{v:1,min:.2,max:4,step:.1}}, f:(x,p)=>p.a*Math.sin(p.b*x)}
};
const ORDER=["linear","quadratic","sqrt","abs","reciprocal","exp","sine"];
/* ---- audio: one engine from src/lib/sonify.js (C3–C6, one AudioContext) ---- */
let sweeping=false, realTone=false, sweepRAF=null, stopT=null, sweepTok=0;
function audio(){ return getAudioContext(); }
function startOsc(){ clearTimeout(stopT); startVoice(); }
function stopOsc(){ stopVoice(); }
function click(){ playClick(1400); }
/* ---- state / DOM ---- */
const $=id=>document.getElementById(id);
let cur="linear";
function params(){ const o={}; for(const k in FUNCS[cur].p) o[k]=FUNCS[cur].p[k].v; return o; }
function buildPicker(){ const s=$("fn"); ORDER.forEach(k=>{const o=document.createElement("option");o.value=k;o.textContent=FUNCS[k].name;s.appendChild(o);}); }
function buildSliders(){
const box=$("sliders"); box.innerHTML="";
const P=FUNCS[cur].p;
for(const k in P){
const wrap=document.createElement("div"); wrap.className="sl";
const id="sl_"+k;
wrap.innerHTML=`<label for="${id}">${k} = <span class="val" id="v_${k}">${P[k].v}</span></label>`;
const r=document.createElement("input"); r.type="range"; r.id=id;
r.min=P[k].min; r.max=P[k].max; r.step=P[k].step; r.value=P[k].v;
r.setAttribute("aria-label","פרמטר "+k);
r.addEventListener("input",()=>{ P[k].v=parseFloat(r.value); $("v_"+k).textContent=(+r.value).toFixed(2).replace(/\.00$/,""); draw(); rebuildListen(); quickSweep(); });
wrap.appendChild(r); box.appendChild(wrap);
}
}
function refreshHeader(){
const F=FUNCS[cur];
$("formula").textContent="f(x) = "+F.tex;
$("fdesc").textContent=F.desc;
$("sineMode").style.display = cur==="sine" ? "flex":"none";
}
/* ---- graph ---- */
const cv=$("cv"), ctx=cv.getContext("2d");
let yLo=-10, yHi=10;
function computeY(){ // fit visible y-range to sampled values (clamped)
const p=params(); let lo=1e9,hi=-1e9;
for(let i=0;i<=400;i++){ const x=XMIN+(XMAX-XMIN)*i/400; const y=FUNCS[cur].f(x,p);
if(isFinite(y)){ lo=Math.min(lo,y); hi=Math.max(hi,y); } }
if(!isFinite(lo)||lo===hi){ lo=-10; hi=10; }
lo=Math.max(lo,-40); hi=Math.min(hi,40);
const pad=(hi-lo)*.1||1; yLo=lo-pad; yHi=hi+pad;
}
const px=x=>(x-XMIN)/(XMAX-XMIN)*cv.width;
const py=y=>cv.height-(y-yLo)/(yHi-yLo)*cv.height;
function draw(markX){
computeY();
const p=params();
ctx.clearRect(0,0,cv.width,cv.height);
// grid
ctx.strokeStyle="#f1f1f6"; ctx.lineWidth=1;
for(let gx=XMIN;gx<=XMAX;gx++){ctx.beginPath();ctx.moveTo(px(gx),0);ctx.lineTo(px(gx),cv.height);ctx.stroke();}
// axes
ctx.strokeStyle="#c7c9d3"; ctx.lineWidth=1.5;
if(yLo<0&&yHi>0){ctx.beginPath();ctx.moveTo(0,py(0));ctx.lineTo(cv.width,py(0));ctx.stroke();}
ctx.beginPath();ctx.moveTo(px(0),0);ctx.lineTo(px(0),cv.height);ctx.stroke();
// curve (break on NaN)
ctx.strokeStyle="#4f46e5"; ctx.lineWidth=3; ctx.beginPath();
let pen=false, prevY=null;
for(let i=0;i<=800;i++){ const x=XMIN+(XMAX-XMIN)*i/800; const y=FUNCS[cur].f(x,p);
if(!isFinite(y)||y< yLo-100||y>yHi+100){ pen=false; prevY=null; continue; }
// detect big jump (asymptote) -> break
if(prevY!==null && Math.abs(y-prevY)>(yHi-yLo)){ pen=false; }
const X=px(x), Y=py(y);
if(!pen){ctx.moveTo(X,Y);pen=true;} else ctx.lineTo(X,Y);
prevY=y;
}
ctx.stroke();
// moving marker
if(markX!=null){ const y=FUNCS[cur].f(markX,p);
if(isFinite(y)){ ctx.fillStyle="#dc2626"; ctx.beginPath(); ctx.arc(px(markX),py(y),6,0,7); ctx.fill(); } }
}
/* ---- sweeps ---- */
function announceSweep(x,y,flags){
const line=sweepNarration(x,y,flags);
const live=$("live");
if(live) live.textContent=line;
$("cv").setAttribute("aria-label", line || "גרף הפונקציה");
}
function playSweep(dur=3000, onDone){
if(sweeping) { stopSweep(); return; }
const ac=audio(); if(!ac) return;
const quiet=preferLessMotion();
startOsc(); sweeping=true; $("play").textContent="⏹ עצור"; $("play").setAttribute("aria-pressed","true");
const t0=ac.currentTime, tok=++sweepTok;
let lastSign=null, lastFinite=false, lastY=null, lastSaid=-1, lastUndef=false;
const p=params();
announceSweep(XMIN, FUNCS[cur].f(XMIN,p), {});
const step=(now)=>{
if(!sweeping||tok!==sweepTok) return;
const elapsed=(ac.currentTime-t0)*1000; let t=elapsed/dur;
if(t>=1){ finishSweep(); if(onDone)onDone(); return; }
const x=XMIN+(XMAX-XMIN)*t;
const y=FUNCS[cur].f(x,p);
let flags={};
if(isFinite(y)){
const f=toFreq(y,yLo,yHi);
setVoice(f,.18);
if(lastY!==null && Math.abs(y-lastY)>Math.abs(yHi-yLo)*0.9){ lastSign=null; lastFinite=false; flags.asymptote=true; }
const s=Math.sign(y);
if(lastFinite && lastSign!==null && s!==0 && s!==lastSign){ click(); flags.root=true; }
if(s!==0) lastSign=s; lastFinite=true; lastY=y; lastUndef=false;
} else {
setVoice(null,0); lastFinite=false; lastSign=null; lastY=null;
flags.undefined=true;
if(!lastUndef){ announceSweep(x,y,flags); lastUndef=true; lastSaid=elapsed; }
}
if(flags.root || flags.asymptote || elapsed-lastSaid>800){ announceSweep(x,y,flags); lastSaid=elapsed; }
if(!quiet) draw(x);
sweepRAF=requestAnimationFrame(step);
};
sweepRAF=requestAnimationFrame(step);
}
function finishSweep(){ sweeping=false; if(sweepRAF)cancelAnimationFrame(sweepRAF);
setVoice(null,0); stopT=setTimeout(stopOsc,120);
$("play").textContent="▶︎ נגן סריקה"; $("play").setAttribute("aria-pressed","false");
const live=$("live"); if(live && !live.textContent) live.textContent="הסריקה הסתיימה.";
draw(); }
function stopSweep(){ finishSweep(); }
let qsT=null;
function quickSweep(){ // short 0.8s sweep on slider drag (skip if playing/game)
if(sweeping) return; audio(); // create/resume the context inside the user gesture
clearTimeout(qsT);
qsT=setTimeout(()=>{ if(realTone){ playRealTone(); return;} playSweep(800); },140);
}
/* sine real tone: frequency b actually audible (scaled into range) */
function playRealTone(){
const ac=audio(); if(!ac) return;
startOsc();
const p=FUNCS.sine.p; const hz=FMIN*Math.pow(2, p.b.v/4*3);
setVoice(hz, Math.min(.25, p.a.v/6*.25||.05));
$("live").textContent="טון אמיתי בתדר ~"+Math.round(hz)+" הרץ";
clearTimeout(playRealTone._t);
playRealTone._t=setTimeout(()=>{ setVoice(null,0); setTimeout(stopOsc,150); },900);
}
/* ---- controls ---- */
$("fn").addEventListener("change",e=>{
stopSweep();
clearTimeout(playRealTone._t);
realTone=false; $("realTone").setAttribute("aria-pressed","false");
$("live").textContent="";
cur=e.target.value;
buildSliders(); refreshHeader(); draw(); rebuildListen(); });
$("play").addEventListener("click",()=>playSweep(3000));
$("realTone").addEventListener("click",()=>{ realTone=!realTone; $("realTone").setAttribute("aria-pressed",String(realTone));
if(realTone) playRealTone();
else { clearTimeout(playRealTone._t); setVoice(null,0); stopT=setTimeout(stopOsc,80); $("live").textContent=""; } });
document.addEventListener("keydown",e=>{
const t=e.target;
if(e.code!=="Space") return;
if(/^(BUTTON|SELECT|A|INPUT|TEXTAREA)$/.test(t.tagName)) return;
if(t.classList&&t.classList.contains("mm-skip")) return;
if(t.closest&&t.closest("#mm-access,#mm-site-nav")) return;
e.preventDefault(); playSweep(3000);
});
/* ---- landmarks / keyboard scrub (graphListen.js) ---- */
let listenSamples=[], listenSummary=null, listenMarks=[], listenI=0, listenMarkI=0;
function currentFnAt(x){ return FUNCS[cur].f(x, params()); }
function rebuildListen(){
listenSamples=sampleCurve(currentFnAt, XMIN, XMAX, 240);
listenSummary=summarizeCurve(listenSamples);
listenMarks=landmarksOf(listenSummary);
listenI=indexNearX(listenSamples, 0);
listenMarkI=0;
const box=$("markList"); if(!box) return;
box.innerHTML="";
listenMarks.forEach(function(m,i){
const b=document.createElement("button");
b.type="button"; b.setAttribute("role","listitem");
b.textContent=describeLandmarkHe(m);
b.addEventListener("click",function(){ goLandmark(i); });
box.appendChild(b);
});
paintListenWhere();
}
function paintListenWhere(){
const pt=listenSamples[listenI];
const line=pt?sweepNarration(pt.x, pt.y, !isFinite(pt.y)?{undefined:true}:{}):"";
if($("listenWhere")) $("listenWhere").textContent=line;
const buttons=[...document.querySelectorAll("#markList button")];
buttons.forEach(function(b,i){ b.setAttribute("aria-current", i===listenMarkI?"true":"false"); });
}
function toneAt(i){
const pt=listenSamples[i]; if(!pt) return;
listenI=i;
if(isFinite(pt.y)){
const f=toFreq(pt.y, yLo, yHi);
if(f) playFreq(f, 280);
}
announceSweep(pt.x, pt.y, !isFinite(pt.y)?{undefined:true}:{});
draw(pt.x);
paintListenWhere();
}
function stepListen(dir){
listenI=stepIndex(listenI, dir, listenSamples.length);
toneAt(listenI);
}
function goLandmark(i){
if(!listenMarks.length) return;
listenMarkI=Math.max(0, Math.min(listenMarks.length-1, i));
const mark=listenMarks[listenMarkI];
listenI=indexNearX(listenSamples, mark.x);
toneAt(listenI);
const live=$("live"); if(live) live.textContent=describeLandmarkHe(mark);
}
function nextMark(dir){
if(!listenMarks.length) return;
const x=listenSamples[listenI]?listenSamples[listenI].x:0;
listenMarkI=nextLandmarkIndex(listenMarks, x, dir);
goLandmark(listenMarkI);
}
function announceDescription(){
rebuildListen();
const text=describeGraphHe(listenSummary);
const live=$("live"); if(live) live.textContent=text;
if($("listenWhere")) $("listenWhere").textContent=text;
if(typeof speakHebrew==="function") speakHebrew(text);
}
$("descBtn").addEventListener("click", announceDescription);
$("stepPrev").addEventListener("click", function(){ stepListen(-1); });
$("stepNext").addEventListener("click", function(){ stepListen(1); });
$("markNext").addEventListener("click", function(){ nextMark(1); });
$("cv").addEventListener("keydown", function(e){
if(e.key==="ArrowRight"){ e.preventDefault(); stepListen(1); }
else if(e.key==="ArrowLeft"){ e.preventDefault(); stepListen(-1); }
else if(e.key==="Home"){ e.preventDefault(); toneAt(0); }
else if(e.key==="End"){ e.preventDefault(); toneAt(listenSamples.length-1); }
else if(e.key==="l"||e.key==="L"||e.key==="ך"){ e.preventDefault(); nextMark(e.shiftKey?-1:1); }
else if(e.key==="d"||e.key==="D"||e.key==="ג"){ e.preventDefault(); announceDescription(); }
});
/* ---- game ---- */
const LS="hear-fn-game";
let gstate={best:0};
try{ const s=loadJson(LS,{best:0}); if(s && isFinite(s.best) && s.best>=0) gstate=s; }catch(e){}
let gScore=0, gStreak=0, gAnswer=null, gChoices=[], gParams=null, gLocked=false, gPicked=null;
$("best").textContent=gstate.best||0;
function randP(P){ const o={}; for(const k in P){ const s=P[k]; let v; do{ v=+(s.min+Math.random()*(s.max-s.min)).toFixed(2);}while(Math.abs(v)<.3&&(k==="a")); o[k]=v; } return o; }
function newRiddle(){
gLocked=false; gPicked=null; $("gFb").textContent=""; $("gFb").className="fb";
if($("gCompare")) $("gCompare").disabled=true;
const keys=[...ORDER]; // pick 4 distinct families
gChoices=[]; while(gChoices.length<4){ const k=keys.splice(Math.floor(Math.random()*keys.length),1)[0]; gChoices.push(k); }
gAnswer=gChoices[Math.floor(Math.random()*4)];
gParams=randP(FUNCS[gAnswer].p);
const box=$("gOpts"); box.innerHTML="";
gChoices.forEach(k=>{ const b=document.createElement("button"); b.type="button"; b.textContent=FUNCS[k].tex;
b.dataset.k=k; b.addEventListener("click",()=>guess(k,b)); box.appendChild(b); });
$("gReplay").disabled=false;
}
function gameSweep(){
if(sweeping){ sweeping=false; if(sweepRAF)cancelAnimationFrame(sweepRAF); }
const ac=audio(); if(!ac) return;
startOsc(); const t0=ac.currentTime,dur=2600,tok=++sweepTok;
let lastSign=null,lastFin=false,lastY=null;
const P=FUNCS[gAnswer]; sweeping=true;
const step=()=>{ if(!sweeping||tok!==sweepTok)return; const el=(ac.currentTime-t0)*1000; if(el>=dur){ finishGameSweep(); return; }
const x=XMIN+(XMAX-XMIN)*el/dur; const y=P.f(x,gParams);
if(isFinite(y)){ const f=toFreq(y,-20,20); setVoice(f,.18);
if(lastY!==null && Math.abs(y-lastY)>36){ lastSign=null; lastFin=false; }
const s=Math.sign(y); if(lastFin&&lastSign!==null&&s!==0&&s!==lastSign)click(); if(s!==0)lastSign=s; lastFin=true; lastY=y;
} else { setVoice(null,0); lastFin=false; lastSign=null; lastY=null; }
sweepRAF=requestAnimationFrame(step); };
sweepRAF=requestAnimationFrame(step);
}
function finishGameSweep(){ sweeping=false; if(sweepRAF)cancelAnimationFrame(sweepRAF); setVoice(null,0); stopT=setTimeout(stopOsc,120); }
function playFamily(key, p, dur, onDone){
const ac=audio(); if(!ac) return;
startOsc(); const t0=ac.currentTime, tok=++sweepTok;
let lastSign=null,lastFin=false,lastY=null;
const P=FUNCS[key]; sweeping=true;
const step=()=>{ if(!sweeping||tok!==sweepTok)return; const el=(ac.currentTime-t0)*1000; if(el>=dur){ finishGameSweep(); if(onDone)onDone(); return; }
const x=XMIN+(XMAX-XMIN)*el/dur; const y=P.f(x,p);
if(isFinite(y)){ const f=toFreq(y,-20,20); setVoice(f,.18);
if(lastY!==null && Math.abs(y-lastY)>36){ lastSign=null; lastFin=false; }
const s=Math.sign(y); if(lastFin&&lastSign!==null&&s!==0&&s!==lastSign)click(); if(s!==0)lastSign=s; lastFin=true; lastY=y;
} else { setVoice(null,0); lastFin=false; lastSign=null; lastY=null; }
sweepRAF=requestAnimationFrame(step); };
sweepRAF=requestAnimationFrame(step);
}
function compareGuess(){
if(!gAnswer||!gPicked) return;
const live=$("live");
if(live) live.textContent="קודם: מה שניחשתם ("+FUNCS[gPicked].name+"). אחר כך: התשובה ("+FUNCS[gAnswer].name+").";
playFamily(gPicked, randP(FUNCS[gPicked].p), 1800, function(){
setTimeout(function(){ playFamily(gAnswer, gParams, 1800); }, 400);
});
}
function guess(k,btn){
if(gLocked) return; gLocked=true; gPicked=k;
const right=k===gAnswer;
document.querySelectorAll("#gOpts button").forEach(b=>{ if(b.dataset.k===gAnswer)b.classList.add("correct"); else if(b===btn)b.classList.add("wrong"); });
if(right){ gScore++; gStreak++; if(gStreak>(gstate.best||0)){gstate.best=gStreak;saveJson(LS,gstate);$("best").textContent=gstate.best;}
$("gFb").textContent="✓ נכון! זו "+FUNCS[gAnswer].name+". "+FUNCS[gAnswer].desc; $("gFb").className="fb ok";
} else { gStreak=0; $("gFb").textContent="✗ זו הייתה "+FUNCS[gAnswer].name+" ("+FUNCS[gAnswer].tex+"). לחצו «השוו» כדי לשמוע את הניחוש ואז את התשובה."; $("gFb").className="fb no"; }
$("score").textContent=gScore; $("streak").textContent=gStreak;
if($("gCompare")) $("gCompare").disabled=false;
}
$("gPlay").addEventListener("click",()=>{ if(!gAnswer)newRiddle(); gameSweep(); });
$("gReplay").addEventListener("click",gameSweep);
$("gNew").addEventListener("click",()=>{ newRiddle(); gameSweep(); });
if($("gCompare")) $("gCompare").addEventListener("click", compareGuess);
/* ---- guided listen lessons ---- */
let lesson=typeof LISTEN_LESSONS!=="undefined" && LISTEN_LESSONS.length ? LISTEN_LESSONS[0] : null;
let lessonLocked=false;
function renderLesson(){
if(!lesson) return;
lessonLocked=false;
const n=typeof lessonIndex==="function"?lessonIndex(lesson.id)+1:1;
if($("lessonN")) $("lessonN").textContent=n;
if($("lessonTotal")) $("lessonTotal").textContent=LISTEN_LESSONS.length;
if($("lessonTitle")) $("lessonTitle").textContent=lesson.titleHe||"";
if($("lessonPrompt")) $("lessonPrompt").textContent=lesson.promptHe||"";
if($("lessonTeach")) $("lessonTeach").textContent="";
if($("lessonFb")){ $("lessonFb").textContent=""; $("lessonFb").className="fb"; }
const needB=lesson.kind==="compare";
if($("lessonPlayB")){ $("lessonPlayB").hidden=!needB; }
if($("lessonPlayA")) $("lessonPlayA").textContent=needB?"▶︎ השמע א׳":"▶︎ השמע את הסריקה";
const box=$("lessonOpts"); if(!box) return;
box.innerHTML="";
(lesson.choices||[]).forEach(function(ch){
const b=document.createElement("button");
b.type="button"; b.textContent=ch.labelHe; b.dataset.id=ch.id;
b.addEventListener("click", function(){ pickLesson(ch.id, b); });
box.appendChild(b);
});
const live=$("live"); if(live && typeof describeLessonHe==="function") live.textContent=describeLessonHe(lesson);
}
function playLessonSpec(spec, dur, onDone){
if(!spec || !FUNCS[spec.family]) return;
playFamily(spec.family, spec.p, dur||2200, onDone);
}
function playLessonSide(side){
if(!lesson) return;
if(lesson.kind==="compare"){
const spec=side==="B"?lesson.b:lesson.a;
const live=$("live"); if(live) live.textContent=side==="B"?"משמיעים ב׳.":"משמיעים א׳.";
playLessonSpec(spec, 2000);
return;
}
playLessonSpec({ family:lesson.family, p:lesson.p }, 2400);
}
function pickLesson(id, btn){
if(!lesson || lessonLocked) return;
lessonLocked=true;
const right=typeof checkListenAnswer==="function"?checkListenAnswer(lesson, id):id===lesson.answer;
document.querySelectorAll("#lessonOpts button").forEach(function(b){
if(b.dataset.id===lesson.answer) b.classList.add("correct");
else if(b===btn && !right) b.classList.add("wrong");
});
$("lessonFb").className="fb "+(right?"ok":"no");
$("lessonFb").textContent=right?"נכון — לפי הדגימה הזו.":"לא זו הבחירה. "+(lesson.teachHe||"");
if($("lessonTeach")) $("lessonTeach").textContent=lesson.teachHe||"";
const live=$("live"); if(live) live.textContent=$("lessonFb").textContent+" "+(lesson.teachHe||"");
if(typeof speakHebrew==="function") speakHebrew($("lessonFb").textContent);
}
if($("lessonPlayA")) $("lessonPlayA").addEventListener("click", function(){ playLessonSide("A"); });
if($("lessonPlayB")) $("lessonPlayB").addEventListener("click", function(){ playLessonSide("B"); });
if($("lessonNext")) $("lessonNext").addEventListener("click", function(){
if(typeof nextLesson==="function" && lesson) lesson=nextLesson(lesson.id);
else if(LISTEN_LESSONS && LISTEN_LESSONS.length){
const i=LISTEN_LESSONS.findIndex(function(L){ return lesson && L.id===lesson.id; });
lesson=LISTEN_LESSONS[(i+1)%LISTEN_LESSONS.length];
}
renderLesson();
});
if(lesson) renderLesson();
/* ---- init ---- */
buildPicker(); $("fn").value=cur; buildSliders(); refreshHeader(); draw(); rebuildListen(); newRiddle();
/* ============================================================
SELF-CHECK (manual, ran during build):
- toFreq: clamps out-of-range -> stays within [130.81,1046.5]; NaN -> null (silence). OK.
- sqrt below domain (x<b) returns NaN -> gain 0 (silence), curve breaks. Verified visually.
- reciprocal at x=b returns NaN -> no divide blow-up; large-jump guard breaks the line at asymptote. OK.
- exp with log-freq mapping renders as ~straight glissando (constant-rate) — the pedagogical point. OK.
- sine real-tone: sets osc.frequency directly (audible Hz), labeled as REAL tone vs sonification. OK.
- game score/streak/best persist in localStorage key "hear-fn-game" across reload. OK.
- Space plays sweep; sliders update graph+sound live via input event. OK.
Skipped: tempo-channel (1/|f'|), A/B pan compare, NVDA pass — add when validating with real users.
============================================================ */
</script>
</body>
</html>