magic-glyphs 0.8.0 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -80,7 +80,7 @@ npx magic-glyphs add bell --format lottie --sound --out assets
80
80
 
81
81
  ## How the export was checked
82
82
 
83
- Every icon was converted and rendered at two mid-animation frames with an independent Lottie library, then compared pixel by pixel with the web engine. All 3,947 matched within a small tolerance. The files have **not** yet been played in lottie-ios, lottie-android or Lottie-Windows; try a handful on a device before relying on them.
83
+ Every icon was converted and rendered at two mid-animation frames with an independent Lottie library, then compared pixel by pixel with the web engine. All 3,947 icons the library had at the time matched within a small tolerance; icons added since use the same converter. The files have **not** yet been played in lottie-ios, lottie-android or Lottie-Windows; try a handful on a device before relying on them.
84
84
 
85
85
  ## After Effects, Webflow, Framer, LottieFiles
86
86
 
package/lib.js CHANGED
@@ -82,7 +82,9 @@ function checkIcon(ic,src){const bad=m=>{throw new Error(src+': '+m)};
82
82
  if(a.o!==undefined&&!(Array.isArray(a.o)&&a.o.length===2&&a.o.every(Number.isFinite)))bad('"'+ic.n+'" has a malformed motion origin');if(a.from!==undefined&&!Number.isFinite(a.from))bad('"'+ic.n+'" has a malformed motion start')}
83
83
  const out={n:ic.n,c:typeof ic.c==='string'&&NAME_OK.test(ic.c)?ic.c:'custom',t:typeof ic.t==='string'?ic.t.replace(/[<>"&]/g,'').slice(0,200):'',L:ic.L,A:ic.A||[]};
84
84
  if(typeof ic.s==='string'&&/^[a-z0-9]{1,20}$/.test(ic.s))out.s=ic.s;
85
- if(ic.S&&typeof ic.S==='object'){const S={};for(const k of ['sound','volume','pitch','speed','tone','pan','delay']){const v=ic.S[k];if(typeof v==='number'&&isFinite(v))S[k]=v;else if(k==='sound'&&typeof v==='string'&&/^[a-z0-9]{1,20}$/.test(v))S[k]=v;else if(k==='delay'&&v==='auto')S[k]=v}if(Object.keys(S).length)out.S=S}
85
+ if(ic.S&&typeof ic.S==='object'){const S={};for(const k of ['sound','volume','pitch','speed','tone','pan','delay']){const v=ic.S[k];if(typeof v==='number'&&isFinite(v))S[k]=v;else if(k==='sound'&&typeof v==='string'&&/^[a-z0-9]{1,20}$/.test(v))S[k]=v;else if(k==='delay'&&v==='auto')S[k]=v}
86
+ // the icon's own sound: a designed synth or an audio file (checked the same way the runtime checks it)
87
+ if(ic.S.custom!==undefined){const c=MG.sound.check(ic.S.custom);if(!c)bad('"'+ic.n+'" has a custom sound that is not valid');S.custom=c}if(Object.keys(S).length)out.S=S}
86
88
  return out}
87
89
  function addCustom(where){const files=[];
88
90
  for(const w of [].concat(where).filter(Boolean)){const p=path.resolve(w);if(!fs.existsSync(p))throw new Error('No such file or folder: '+w);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magic-glyphs",
3
- "version": "0.8.0",
3
+ "version": "0.8.5",
4
4
  "description": "Animated icon library with sound: spring-animated SVG icons for React, Next.js, Vue, Nuxt, Svelte, Astro, shadcn, HTML, Tailwind, Lottie (iOS, Android, Flutter, Windows) and a CLI and MCP server. 120 icons free.",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "author": {
@@ -110,10 +110,10 @@
110
110
  "types": "./web/runtime.d.ts",
111
111
  "repository": {
112
112
  "type": "git",
113
- "url": "git+https://github.com/lewisjohnvillamor/magic-glyphs.git"
113
+ "url": "git+https://github.com/lewisjohnvillamor/magicglyphs.git"
114
114
  },
115
115
  "bugs": {
116
- "url": "https://github.com/lewisjohnvillamor/magic-glyphs/issues",
116
+ "url": "https://github.com/lewisjohnvillamor/magicglyphs/issues",
117
117
  "email": "support@magicglyphs.com"
118
118
  }
119
119
  }
package/react/index.d.ts CHANGED
@@ -16,6 +16,8 @@ export interface GlyphSoundOptions {
16
16
  pan?: number;
17
17
  /** Milliseconds before the sound, or "auto" to line impact sounds up with the motion. */
18
18
  delay?: number | 'auto';
19
+ /** Your own audio file instead of a named sound: a URL or a data: URI. */
20
+ src?: string;
19
21
  }
20
22
  export interface MagicGlyphProps {
21
23
  name?: string;
package/react/index.js CHANGED
@@ -13,7 +13,7 @@ function MagicGlyph(props){
13
13
  },[html,trigger,name]);
14
14
  const play=()=>player.current&&player.current.play();
15
15
  React.useImperativeHandle(props.playerRef,()=>({play}),[]);
16
- const so={};if(soundOptions)for(const k of ['sound','volume','pitch','speed','tone','pan','delay'])if(soundOptions[k]!==undefined)so['sound-'+(k==='sound'?'name':k)]=String(soundOptions[k]);
16
+ const so={};if(soundOptions)for(const k of ['sound','volume','pitch','speed','tone','pan','delay','src'])if(soundOptions[k]!==undefined)so['sound-'+(k==='sound'?'name':k)]=String(soundOptions[k]);
17
17
  return React.createElement('span',{...so,ref,className,sound:sound?'':undefined,style:Object.assign({display:'inline-flex'},style),
18
18
  onMouseEnter:trigger==='hover'?play:undefined,onFocus:trigger==='hover'?play:undefined,onClick:trigger==='hover'||trigger==='click'?play:undefined,
19
19
  dangerouslySetInnerHTML:{__html:html}});
package/tools/audio.js CHANGED
@@ -16,9 +16,21 @@ class Osc extends Node{constructor(c){super(c);this.frequency=new Param(440);thi
16
16
  start(t){this.t0=t||0}stop(t){this.t1=t}
17
17
  run(i){const sr=this.c.sampleRate,t=i/sr;if(t<this.t0||t>=this.t1)return 0;const p=this.ph;this.ph=(p+this.frequency.at(t)/sr)%1;
18
18
  switch(this.type){case 'square':return p<.5?1:-1;case 'sawtooth':return 2*p-1;case 'triangle':return p<.5?4*p-1:3-4*p;default:return Math.sin(2*Math.PI*p)}}}
19
- class Src extends Node{constructor(c){super(c);this.buffer=null;this.loop=false;this.t0=Infinity;this.t1=Infinity}start(t){this.t0=t||0}stop(t){this.t1=t}
19
+ class Src extends Node{constructor(c){super(c);this.buffer=null;this.loop=false;this.playbackRate=new Param(1);this.t0=Infinity;this.t1=Infinity}start(t){this.t0=t||0}stop(t){this.t1=t}
20
20
  run(i){const sr=this.c.sampleRate,t=i/sr;if(!this.buffer||t<this.t0||t>=this.t1)return 0;const d=this.buffer.getChannelData(0),k=i-Math.round(this.t0*sr);
21
- return this.loop?d[k%d.length]:k<d.length?d[k]:0}}
21
+ if(this.loop)return d[k%d.length];
22
+ // audio files: follow the playback rate and the file's own sample rate, reading between samples
23
+ const x=k*this.playbackRate.value*(this.buffer.sampleRate||sr)/sr,j=Math.floor(x),f=x-j;if(j+1>=d.length)return j<d.length?d[j]:0;return d[j]*(1-f)+d[j+1]*f}}
24
+ // WAV files only (PCM 8/16/24/32-bit or 32-bit float), which is what the icon maker stores; other formats need a browser
25
+ function decodeWav(ab){const v=new DataView(ab),tag=o=>String.fromCharCode(v.getUint8(o),v.getUint8(o+1),v.getUint8(o+2),v.getUint8(o+3));
26
+ if(ab.byteLength<12||tag(0)!=='RIFF'||tag(8)!=='WAVE')throw new Error('Only WAV sound files can be rendered outside a browser. Convert it to .wav.');
27
+ let o=12,fmt=null,data=null;while(o+8<=ab.byteLength){const id=tag(o),n=v.getUint32(o+4,true);if(id==='fmt ')fmt={type:v.getUint16(o+8,true),ch:v.getUint16(o+10,true),sr:v.getUint32(o+12,true),bits:v.getUint16(o+22,true)};
28
+ else if(id==='data')data={at:o+8,n:Math.min(n,ab.byteLength-o-8)};o+=8+n+(n&1)}
29
+ if(!fmt||!data)throw new Error('That WAV file has no sound in it.');const bps=fmt.bits/8,frames=Math.floor(data.n/(bps*fmt.ch)),out=Array.from({length:fmt.ch},()=>new Float32Array(frames));
30
+ for(let i=0;i<frames;i++)for(let c=0;c<fmt.ch;c++){const p=data.at+(i*fmt.ch+c)*bps;let x;
31
+ if(fmt.type===3&&bps===4)x=v.getFloat32(p,true);else if(bps===1)x=(v.getUint8(p)-128)/128;else if(bps===2)x=v.getInt16(p,true)/32768;
32
+ else if(bps===3){let y=v.getUint8(p)|v.getUint8(p+1)<<8|v.getUint8(p+2)<<16;if(y&0x800000)y|=~0xffffff;x=y/8388608}else if(bps===4)x=v.getInt32(p,true)/2147483648;else throw new Error('Unsupported WAV sample size.');out[c][i]=x}
33
+ return{sampleRate:fmt.sr,length:frames,numberOfChannels:fmt.ch,duration:frames/fmt.sr,getChannelData:k=>out[k]}}
22
34
  class Biquad extends Node{constructor(c){super(c);this.type='lowpass';this.frequency=new Param(350);this.Q=new Param(1);this.gain=new Param(0);this.x1=this.x2=this.y1=this.y2=0;this.k=null}
23
35
  coef(t){const sr=this.c.sampleRate,f=Math.min(this.frequency.at(t),sr/2-1),w=2*Math.PI*f/sr,cs=Math.cos(w),sn=Math.sin(w),Q=this.Q.at(t),G=this.gain.at(t);let b0,b1,b2,a0,a1,a2;
24
36
  if(this.type==='bandpass'){const al=sn/(2*Math.max(Q,1e-4));b0=al;b1=0;b2=-al;a0=1+al;a1=-2*cs;a2=1-al}
@@ -32,6 +44,7 @@ class Dest extends Node{}
32
44
  class OfflineContext{
33
45
  constructor(ch,len,sr){this.length=len;this.sampleRate=sr;this.currentTime=0;this.destination=new Dest(this)}
34
46
  createGain(){return new Gain(this)}createOscillator(){return new Osc(this)}createBufferSource(){return new Src(this)}createBiquadFilter(){return new Biquad(this)}createStereoPanner(){return new Pan(this)}
47
+ decodeAudioData(ab){try{return Promise.resolve(decodeWav(ab instanceof ArrayBuffer?ab:new Uint8Array(ab).buffer))}catch(e){return Promise.reject(e)}}
35
48
  createBuffer(ch,len,sr){const d=Array.from({length:ch},()=>new Float32Array(len));return{sampleRate:sr,length:len,numberOfChannels:ch,getChannelData:k=>d[k]}}
36
49
  startRendering(){const n=this.length,L=new Float32Array(n),R=new Float32Array(n);
37
50
  for(let i=0;i<n;i++){let l=0,r=0;for(const s of this.destination.ins){const v=s.pull(i);if(Array.isArray(v)){l+=v[0];r+=v[1]}else{l+=v;r+=v}}L[i]=l;R[i]=r}
package/vue/index.d.ts CHANGED
@@ -10,8 +10,8 @@ export interface MagicGlyphProps {
10
10
  label?: string;
11
11
  /** Play the icon's sound with its animation. */
12
12
  sound?: boolean;
13
- /** Shape the sound: { sound, volume, pitch, speed, tone, pan, delay }. */
14
- soundOptions?: { sound?: string; volume?: number; pitch?: number; speed?: number; tone?: number; pan?: number; delay?: number | 'auto' };
13
+ /** Shape the sound: { sound, volume, pitch, speed, tone, pan, delay, src }. */
14
+ soundOptions?: { sound?: string; volume?: number; pitch?: number; speed?: number; tone?: number; pan?: number; delay?: number | 'auto'; src?: string };
15
15
  }
16
16
  export const MagicGlyph: DefineComponent<MagicGlyphProps>;
17
17
  export function register(icons: unknown): void;
package/vue/index.js CHANGED
@@ -13,7 +13,7 @@ const MagicGlyph=defineComponent({name:'MagicGlyph',
13
13
  onBeforeUnmount(()=>{if(io)io.disconnect()});
14
14
  watch(html,()=>Promise.resolve().then(bind));
15
15
  expose({play});
16
- return()=>{const so={};if(props.soundOptions)for(const k of ['sound','volume','pitch','speed','tone','pan','delay'])if(props.soundOptions[k]!==undefined)so['sound-'+(k==='sound'?'name':k)]=String(props.soundOptions[k]);return h('span',{...so,ref:el,sound:props.sound?'':undefined,style:{display:'inline-flex'},innerHTML:html.value,
16
+ return()=>{const so={};if(props.soundOptions)for(const k of ['sound','volume','pitch','speed','tone','pan','delay','src'])if(props.soundOptions[k]!==undefined)so['sound-'+(k==='sound'?'name':k)]=String(props.soundOptions[k]);return h('span',{...so,ref:el,sound:props.sound?'':undefined,style:{display:'inline-flex'},innerHTML:html.value,
17
17
  onMouseenter:props.trigger==='hover'?play:undefined,onFocusin:props.trigger==='hover'?play:undefined,onClick:props.trigger==='hover'||props.trigger==='click'?play:undefined})};
18
18
  }});
19
19
  // A speaker button visitors use to turn icon sounds on or off; their choice is remembered in the browser.
@@ -3,7 +3,7 @@
3
3
  'use strict';
4
4
  var REG={},uid=0;
5
5
  var STROKE='stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"';
6
- function register(data){(data.icons||(Array.isArray(data)?data:[data])).forEach(function(ic){REG[ic.n]=ic}); // a set, a list, or one icon
6
+ function register(data){(data.icons||(Array.isArray(data)?data:[data])).forEach(function(ic){REG[ic.n]=ic;var cu=ic.S&&ic.S.custom;if(cu&&typeof cu.src==='string'&&typeof window!=='undefined')setTimeout(function(){var d=cleanDef(cu);if(d&&d.src)load(d.src).catch(function(){})},0)}); // a set, a list, or one icon
7
7
  // elements already on the page were upgraded before their data arrived: draw them now
8
8
  if(typeof document!=='undefined')Array.prototype.forEach.call(document.querySelectorAll('magic-glyph'),function(e){if(e._render)e._render()});
9
9
  return api}
@@ -82,11 +82,11 @@
82
82
  function hook(){if(hooked||typeof document==='undefined')return;hooked=true;var un=function(){unlocked=true;ensureAudio()};
83
83
  document.addEventListener('pointerdown',un,true);document.addEventListener('keydown',un,true)}
84
84
  // tone and noise read the context, pitch, speed and start time of the sound being rendered from cur
85
- function tone(out,f,t,dur,g,type,f2){var C=cur.C,o=C.createOscillator(),a=C.createGain(),at=cur.t0+t/cur.sp,du=dur/cur.sp,pm=cur.pm;
85
+ function tone(out,f,t,dur,g,type,f2){if(cur.rec){cur.rec.push(['t',f,t,dur,g,type||'sine'].concat(f2?[f2]:[]));return}var C=cur.C,o=C.createOscillator(),a=C.createGain(),at=cur.t0+t/cur.sp,du=dur/cur.sp,pm=cur.pm;
86
86
  o.type=type||'sine';o.frequency.setValueAtTime(f*pm,at);if(f2)o.frequency.exponentialRampToValueAtTime(f2*pm,at+du);
87
87
  a.gain.setValueAtTime(.0001,at);a.gain.exponentialRampToValueAtTime(g,at+Math.min(.006,du/3));a.gain.exponentialRampToValueAtTime(.0001,at+du);
88
88
  o.connect(a);a.connect(out);o.start(at);o.stop(at+du+.05)}
89
- function noise(out,f,f2,t,dur,g,q){var C=cur.C,s=C.createBufferSource(),b=C.createBiquadFilter(),a=C.createGain(),at=cur.t0+t/cur.sp,du=dur/cur.sp,pm=cur.pm;
89
+ function noise(out,f,f2,t,dur,g,q){if(cur.rec){cur.rec.push(['n',f,f2,t,dur,g,q||1.2]);return}var C=cur.C,s=C.createBufferSource(),b=C.createBiquadFilter(),a=C.createGain(),at=cur.t0+t/cur.sp,du=dur/cur.sp,pm=cur.pm;
90
90
  s.buffer=noiseFor(C);s.loop=true;b.type='bandpass';b.Q.value=q||1.2;b.frequency.setValueAtTime(Math.min(f*pm,18000),at);b.frequency.exponentialRampToValueAtTime(Math.min(f2*pm,18000),at+du);
91
91
  a.gain.setValueAtTime(.0001,at);a.gain.exponentialRampToValueAtTime(g,at+du*.3);a.gain.exponentialRampToValueAtTime(.0001,at+du);
92
92
  s.connect(b);b.connect(a);a.connect(out);s.start(at);s.stop(at+du+.05)}
@@ -129,17 +129,116 @@
129
129
  buzz:o=>{tone(o,95,0,.12,.3,'sawtooth');tone(o,95,.17,.12,.3,'sawtooth')},
130
130
  coin:o=>{tone(o,1568,0,.08,.2,'square');tone(o,2093,.07,.35,.2,'square')},
131
131
  scribble:o=>{noise(o,2200,3200,0,.09,.3,4);noise(o,2800,2000,.1,.09,.3,4);noise(o,2200,3400,.2,.1,.3,4)},
132
- ticktock:o=>{tone(o,1900,0,.025,.22,'square');tone(o,1300,.3,.025,.2,'square');tone(o,1900,.6,.025,.16,'square')}
132
+ ticktock:o=>{tone(o,1900,0,.025,.22,'square');tone(o,1300,.3,.025,.2,'square');tone(o,1900,.6,.025,.16,'square')},
133
+ // ---- added in 0.8.5 ----
134
+ boop:o=>tone(o,520,0,.12,.35,'sine',260),
135
+ blip:o=>tone(o,1250,0,.05,.16,'square'),
136
+ hover:o=>tone(o,2200,0,.035,.08,'sine',2000),
137
+ switchon:o=>{noise(o,3000,1500,0,.02,.4,2);tone(o,700,.01,.07,.25,'sine',1050)},
138
+ switchoff:o=>{noise(o,2500,1200,0,.02,.4,2);tone(o,950,.01,.07,.25,'sine',620)},
139
+ typing:o=>{[0,.09,.15,.27,.33,.46].forEach((t,i)=>noise(o,2600+i%3*500,1800,t,.03,.4,3))},
140
+ flip:o=>{noise(o,1500,4000,0,.09,.4,1.2);noise(o,1800,4500,.1,.09,.3,1.2)},
141
+ plink:o=>{tone(o,2093,0,.35,.25,'triangle');tone(o,4186,0,.12,.06)},
142
+ pluck:o=>{tone(o,330,0,.4,.16,'sawtooth',326);tone(o,660,0,.18,.12,'triangle')},
143
+ marimba:o=>{tone(o,523,0,.45,.35);tone(o,2093,0,.08,.12)},
144
+ harp:o=>{[523,659,784,1047,1319].forEach((f,i)=>tone(o,f,i*.06,.5,.18,'triangle'))},
145
+ bell:o=>{tone(o,880,0,1.6,.25);tone(o,2429,0,.9,.1);tone(o,4752,0,.5,.05)},
146
+ gong:o=>{tone(o,98,0,2.2,.4);tone(o,147,.01,1.8,.2);tone(o,233,.02,1.4,.12);noise(o,400,200,0,.6,.08,1)},
147
+ magic:o=>{tone(o,523,0,.6,.1,'triangle',1047);[1568,2093,2637,3136].forEach((f,i)=>tone(o,f,.1+i*.07,.4,.08))},
148
+ sparkle:o=>{[2637,3136,2349,3520,2794,3951].forEach((f,i)=>tone(o,f,i*.05,.25,.09))},
149
+ ping:o=>{tone(o,1760,0,.8,.25);tone(o,3520,0,.3,.06)},
150
+ pong:o=>{tone(o,880,0,.25,.35);tone(o,1760,0,.08,.08)},
151
+ incoming:o=>{tone(o,1175,0,.18,.22);tone(o,1568,.12,.35,.22)},
152
+ bikebell:o=>{for(let i=0;i<2;i++){tone(o,2100,i*.16,.5,.14);tone(o,2700,i*.16,.4,.08)}},
153
+ trill:o=>{for(let i=0;i<10;i++)tone(o,i%2?1600:1300,i*.045,.045,.14,'triangle')},
154
+ alarm:o=>{for(let i=0;i<4;i++)tone(o,1050,i*.15,.09,.15,'square')},
155
+ countdown:o=>{tone(o,880,0,.08,.2);tone(o,880,.3,.08,.2);tone(o,1760,.6,.3,.22)},
156
+ tada:o=>{tone(o,523,0,.1,.2,'triangle');tone(o,659,.08,.1,.2,'triangle');[1047,1319,1568].forEach(f=>tone(o,f,.18,.7,.14,'triangle'))},
157
+ error:o=>{tone(o,196,0,.14,.2,'square');tone(o,185,.17,.22,.2,'square')},
158
+ warning:o=>{[0,1,2].forEach(i=>{tone(o,880,i*.22,.1,.16,'triangle');tone(o,660,i*.22+.11,.1,.16,'triangle')})},
159
+ fail:o=>{tone(o,494,0,.16,.22,'triangle');tone(o,392,.17,.16,.22,'triangle');tone(o,311,.34,.4,.22,'triangle',290)},
160
+ cancel:o=>{tone(o,660,0,.1,.22,'triangle');tone(o,440,.1,.18,.22,'triangle')},
161
+ unlock:o=>{noise(o,3200,1800,0,.03,.4,3);noise(o,2600,1400,.09,.04,.4,3);tone(o,1568,.16,.35,.16);tone(o,2093,.22,.45,.14)},
162
+ lock:o=>{noise(o,2400,1200,0,.04,.45,3);tone(o,220,0,.12,.35,'triangle',140)},
163
+ rise:o=>{tone(o,330,0,.5,.15,'triangle',660);tone(o,495,0,.5,.08,'sine',990)},
164
+ fall:o=>{tone(o,900,0,.45,.18,'triangle',300);tone(o,1350,0,.3,.06,'sine',450)},
165
+ swoop:o=>{tone(o,350,0,.22,.25,'sine',1500);noise(o,800,3500,0,.22,.2,1)},
166
+ whip:o=>{noise(o,1500,8000,0,.1,.5,1.5);noise(o,6000,3000,.1,.04,.4,2)},
167
+ zipper:o=>{for(let i=0;i<10;i++)noise(o,2000+i*350,2500+i*350,i*.025,.018,.7,6)},
168
+ boing:o=>{tone(o,140,0,.09,.35,'sine',420);tone(o,420,.09,.1,.3,'sine',230);tone(o,230,.19,.1,.25,'sine',340);tone(o,340,.29,.14,.18,'sine',260)},
169
+ wobble:o=>{for(let i=0;i<4;i++)tone(o,i%2?480:300,i*.09,.09,.2,'triangle',i%2?300:480)},
170
+ squeak:o=>{tone(o,1700,0,.08,.14,'sine',2600);tone(o,2500,.09,.1,.12,'sine',1700)},
171
+ gulp:o=>{tone(o,180,0,.08,.4,'sine',420);tone(o,420,.09,.07,.3,'sine',160)},
172
+ drip:o=>{tone(o,1400,0,.06,.35,'sine',500);tone(o,900,.07,.05,.12,'sine',1500)},
173
+ splash:o=>{noise(o,2200,400,0,.45,.5,.8);tone(o,500,.05,.08,.15,'sine',1100);tone(o,700,.15,.07,.12,'sine',1300)},
174
+ rain:o=>{[0,.07,.11,.2,.26,.31,.4,.47,.55,.6,.68].forEach((t,i)=>noise(o,4000+(i*1300)%4000,3000,t,.025,.6,5))},
175
+ thunder:o=>{noise(o,600,80,0,1.4,.6,.5);noise(o,300,60,.15,1.2,.5,.5);tone(o,45,0,1.2,.3,'sine',30)},
176
+ rumble:o=>{tone(o,55,0,.9,.3,'sawtooth',45);noise(o,180,90,0,.9,.35,.6)},
177
+ crackle:o=>{[0,.05,.13,.16,.28,.34,.45,.52].forEach((t,i)=>noise(o,1500+(i*900)%3000,800,t,.02,.4+(i%3)*.1,4))},
178
+ crumple:o=>{[0,.04,.09,.12,.19,.25,.28].forEach((t,i)=>noise(o,1200+(i*700)%2600,2000,t,.05,.35,2))},
179
+ clap:o=>{[0,.012,.024].forEach(t=>noise(o,1400,1100,t,.05,.45,1.5));noise(o,1300,900,.035,.18,.25,1)},
180
+ heartbeat:o=>{tone(o,90,0,.12,.6,'sine',45);tone(o,80,.17,.14,.45,'sine',40)},
181
+ woodblock:o=>{tone(o,820,0,.07,.5,'sine',760);tone(o,1650,0,.03,.15,'triangle')},
182
+ clang:o=>{tone(o,523,0,.9,.18,'triangle');tone(o,1347,0,.6,.12);tone(o,2273,0,.4,.08);noise(o,3000,1500,0,.05,.3,2)},
183
+ kick:o=>{tone(o,150,0,.3,.8,'sine',42);noise(o,2000,600,0,.02,.2,1)},
184
+ snare:o=>{noise(o,2200,1600,0,.18,.5,.7);tone(o,200,0,.1,.35,'triangle',160)},
185
+ hihat:o=>{noise(o,9000,8000,0,.05,.4,1);noise(o,9000,8000,.15,.05,.3,1)},
186
+ boom:o=>{tone(o,90,0,.8,.7,'sine',28);noise(o,700,80,0,.9,.6,.6)},
187
+ kaching:o=>{noise(o,2500,1200,0,.06,.4,3);tone(o,2637,.06,.6,.16,'triangle');tone(o,3520,.12,.8,.14)},
188
+ levelup:o=>{[523,659,784,1047].forEach((f,i)=>tone(o,f,i*.07,i<3?.08:.3,.16,'square'))},
189
+ powerup:o=>{tone(o,220,0,.45,.14,'square',1320);tone(o,330,0,.45,.08,'triangle',1980)},
190
+ powerdown:o=>{tone(o,1100,0,.5,.14,'square',140);tone(o,550,0,.5,.08,'triangle',70)},
191
+ jump:o=>tone(o,280,0,.16,.2,'square',760),
192
+ laser:o=>{tone(o,2400,0,.18,.18,'sawtooth',180);tone(o,1800,.02,.16,.1,'square',140)},
193
+ robot:o=>{[330,494,262,415].forEach((f,i)=>tone(o,f,i*.08,.07,.13,'square'))},
194
+ engine:o=>{for(let i=0;i<12;i++)tone(o,70+i*6,i*.05,.045,.35,'sawtooth',60+i*6)}
195
+ };
196
+ // families, for pickers and docs; every sound is in exactly one
197
+ var GROUPS={
198
+ interface:['tap','click','blink','hover','blip','boop','switchon','switchoff','tick2','ticktock','typing','flip','scribble','shutter'],
199
+ alerts:['ding','chime','ping','pong','beep','incoming','alert','success','error','warning','fail','cancel','alarm','countdown','bell','bikebell','trill','tada'],
200
+ motion:['whoosh','swish','swoop','breeze','rise','fall','whip','zipper','wobble','boing','bounce','drop','liftoff'],
201
+ impacts:['thump','knock','clank','clang','woodblock','kick','snare','hihat','clap','heartbeat','boom','crunch','crumple'],
202
+ nature:['bubble','drip','splash','rain','thunder','rumble','sizzle','fizz','crackle','chirp','whistle','gulp','squeak'],
203
+ musical:['plink','pluck','marimba','harp','gong','notes','glow','soft','sparkle','magic'],
204
+ game:['coin','kaching','levelup','powerup','powerdown','jump','laser','zap','pop','pops','robot'],
205
+ machines:['horn','siren','rotor','engine','ratchet','buzz','lock','unlock']
133
206
  };
134
207
  // loudness trims measured offline so every sound plays at about the same level and none clips (see tools/sound-audit.js)
135
- var NORM={"bounce":0.56,"siren":1.71,"bubble":0.87,"crunch":2.43,"sizzle":2.5,"fizz":2.06,"horn":0.92,"rotor":0.95,"chirp":1,"whistle":0.83,"ding":0.63,"thump":0.51,"liftoff":1.36,"whoosh":1.83,"swish":1.84,"breeze":2.5,"chime":0.73,"glow":1.16,"soft":1.1,"tick2":0.84,"blink":1.84,"ratchet":1.17,"clank":0.98,"click":0.62,"pop":0.6,"pops":0.78,"drop":0.73,"zap":1.07,"notes":0.92,"shutter":1.36,"knock":0.53,"success":1.02,"tap":0.95,"beep":1.08,"alert":1.1,"buzz":1.23,"coin":0.71,"scribble":2.5,"ticktock":0.9};
208
+ var NORM={"bounce":0.55,"siren":1.67,"bubble":0.85,"crunch":2.48,"sizzle":2.44,"fizz":2.04,"horn":0.9,"rotor":0.93,"chirp":0.98,"whistle":0.82,"ding":0.62,"thump":0.5,"liftoff":1.34,"whoosh":1.76,"swish":1.73,"breeze":2.5,"chime":0.71,"glow":1.14,"soft":1.08,"tick2":0.83,"blink":1.8,"ratchet":1.15,"clank":0.96,"click":0.63,"pop":0.58,"pops":0.76,"drop":0.71,"zap":1.04,"notes":0.9,"shutter":1.42,"knock":0.51,"success":1,"tap":0.93,"beep":1.05,"alert":1.07,"buzz":1.2,"coin":0.7,"scribble":2.5,"ticktock":0.88,"boop":0.68,"blip":1,"hover":1.88,"switchon":0.97,"switchoff":0.97,"typing":2.19,"flip":1.46,"plink":0.77,"pluck":1.74,"marimba":0.64,"harp":0.84,"bell":0.76,"gong":0.56,"magic":1.29,"sparkle":1.04,"ping":0.68,"pong":0.63,"incoming":0.85,"bikebell":0.88,"trill":1.74,"alarm":1.05,"countdown":0.85,"tada":1,"error":0.93,"warning":1.54,"fail":1.19,"cancel":1.17,"unlock":0.97,"lock":0.77,"rise":1.4,"fall":1.27,"swoop":0.9,"whip":1.15,"zipper":1.88,"boing":0.88,"wobble":1.37,"squeak":1.27,"gulp":0.69,"drip":0.82,"splash":1.11,"rain":1.3,"thunder":1.12,"rumble":1.2,"crackle":2.37,"crumple":1.85,"clap":1.96,"heartbeat":0.6,"woodblock":0.45,"clang":0.99,"kick":0.35,"snare":0.76,"hihat":0.9,"boom":0.43,"kaching":0.85,"levelup":1.03,"powerup":1.14,"powerdown":1.06,"jump":0.9,"laser":1.01,"robot":1.34,"engine":1.31};
136
209
  var num=function(v,d,lo,hi){v=parseFloat(v);return isFinite(v)?Math.max(lo,Math.min(hi,v)):d};
210
+ // ---- custom sounds: a designed synth (a list of steps) or an audio file, attached to an icon (S.custom) or named with sound.define ----
211
+ // a step is ['t', hz, start s, length s, level 0-1, wave, end hz?] for a tone or ['n', hz, end hz, start s, length s, level 0-1, q?] for filtered noise
212
+ var WAVES={sine:1,triangle:1,square:1,sawtooth:1},DEFS={},BUFS={},LOADING={};
213
+ var fin=function(v,lo,hi){return typeof v==='number'&&isFinite(v)&&v>=lo&&v<=hi};
214
+ function cleanSteps(a){if(!Array.isArray(a)||!a.length||a.length>48)return null;var out=[];
215
+ for(var i=0;i<a.length;i++){var x=a[i];if(!Array.isArray(x))return null;
216
+ if(x[0]==='t'&&fin(x[1],20,20000)&&fin(x[2],0,2.5)&&fin(x[3],.005,2.5)&&fin(x[4],.001,1)&&WAVES[x[5]||'sine']&&(x[6]===undefined||x[6]===null||fin(x[6],20,20000)))out.push(['t',x[1],x[2],x[3],x[4],x[5]||'sine'].concat(x[6]?[x[6]]:[]));
217
+ else if(x[0]==='n'&&fin(x[1],20,20000)&&fin(x[2],20,20000)&&fin(x[3],0,2.5)&&fin(x[4],.005,2.5)&&fin(x[5],.001,1)&&(x[6]===undefined||fin(x[6],.1,30)))out.push(['n',x[1],x[2],x[3],x[4],x[5],x[6]||1.2]);
218
+ else return null}return out}
219
+ // audio may be a data: URI (what the icon maker stores) or a URL your page can fetch
220
+ function cleanDef(d){if(!d||typeof d!=='object')return null;if(d.synth){var st=cleanSteps(d.synth);return st?{synth:st}:null}
221
+ if(typeof d.src==='string'&&d.src.length<=1500000&&(/^data:audio\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/=]+$/.test(d.src)||/^(https?:)?\/\/|^\.{0,2}\//.test(d.src)))return{src:d.src};return null}
222
+ function bytes(src){if(src.slice(0,5)==='data:'){var b=atob(src.slice(src.indexOf(',')+1)),u=new Uint8Array(b.length);for(var i=0;i<b.length;i++)u[i]=b.charCodeAt(i);return Promise.resolve(u.buffer)}
223
+ return fetch(src).then(function(r){if(!r.ok)throw new Error('Could not load the sound '+src+' ('+r.status+').');return r.arrayBuffer()})}
224
+ // decode once and keep it; level it so a quiet or loud recording plays at about the same level as the built-in sounds
225
+ function load(src,c){if(BUFS[src])return Promise.resolve(BUFS[src]);if(LOADING[src])return LOADING[src];
226
+ var dc=c||(typeof window!=='undefined'&&(window.OfflineAudioContext||window.webkitOfflineAudioContext)?new (window.OfflineAudioContext||window.webkitOfflineAudioContext)(1,1,44100):ensureAudio());
227
+ if(!dc)return Promise.reject(new Error('Audio is not available here.'));
228
+ return LOADING[src]=bytes(src).then(function(ab){return new Promise(function(ok,no){var pr=dc.decodeAudioData(ab,ok,no);if(pr&&pr.then)pr.then(ok,no)})}).then(function(buf){
229
+ var pk=0;for(var ch=0;ch<buf.numberOfChannels;ch++){var d=buf.getChannelData(ch);for(var i=0;i<d.length;i+=2){var a=d[i]<0?-d[i]:d[i];if(a>pk)pk=a}}
230
+ delete LOADING[src];return BUFS[src]={buf:buf,gain:pk>1e-4?Math.min(6,.45/pk):1}},function(e){delete LOADING[src];throw e})}
231
+ function sample(out,b){var C=cur.C,s=C.createBufferSource(),a=C.createGain();s.buffer=b.buf;if(s.playbackRate)s.playbackRate.value=cur.pm*cur.sp;a.gain.value=b.gain;s.connect(a);a.connect(out);s.start(cur.t0)}
232
+ // the definition behind a sound name, if it is not built in
233
+ function defOf(o){return o.sound==='custom'?o.custom:DEFS[o.sound]}
234
+ function synthFn(st){return function(o){st.forEach(function(x){x[0]==='t'?tone(o,x[1],x[2],x[3],x[4],x[5],x[6]):noise(o,x[1],x[2],x[3],x[4],x[5],x[6])})}}
137
235
  // settings for one play: defaults < sound.set(icon) < the icon's own data (ic.S) < attributes on the element
138
236
  function settings(ic,el,extra){var o={},k;for(k in DEF)o[k]=DEF[k];
139
237
  var add=function(x){if(x)for(var k in x)if(x[k]!==undefined&&x[k]!==null&&x[k]!=='')o[k]=x[k]};
140
238
  if(ic){add(PER[ic.n]);add(ic.S)}
141
- if(el&&el.getAttribute)add({sound:el.getAttribute('sound-name'),volume:el.getAttribute('sound-volume'),pitch:el.getAttribute('sound-pitch'),speed:el.getAttribute('sound-speed'),tone:el.getAttribute('sound-tone'),pan:el.getAttribute('sound-pan'),delay:el.getAttribute('sound-delay')});
239
+ if(el&&el.getAttribute)add({sound:el.getAttribute('sound-name'),volume:el.getAttribute('sound-volume'),pitch:el.getAttribute('sound-pitch'),speed:el.getAttribute('sound-speed'),tone:el.getAttribute('sound-tone'),pan:el.getAttribute('sound-pan'),delay:el.getAttribute('sound-delay'),src:el.getAttribute('sound-src')});
142
240
  add(extra);o.sound=o.sound||(ic&&ic.s)||null;
241
+ if(o.src){o.custom=cleanDef({src:o.src});o.sound='custom';delete o.src}else if(o.custom)o.custom=cleanDef(o.custom);
143
242
  o.volume=num(o.volume,1,0,2);o.pitch=num(o.pitch,0,-12,12);o.speed=num(o.speed,1,.5,2);o.tone=num(o.tone,0,-1,1);o.pan=num(o.pan,1,0,1);
144
243
  o.delay=o.delay==='auto'?(ic?beat(ic,o.sound):0):num(o.delay,0,0,2000);return o}
145
244
  // impact sounds wait for the moment of impact. The animations mark it: a reaction (a squash, a tray dipping, a lid jumping)
@@ -149,8 +248,8 @@
149
248
  ic.A.forEach(function(a){var t=a.k&&a.k[0]&&a.k[0][0];if(t>40&&t<600&&(!hit||t<hit))hit=t});
150
249
  return ic._beat=hit}
151
250
  // build the sound graph into any audio context: volume and tone, then position
152
- function render(c,dest,o,pan){var fn=SOUNDS[o.sound];if(!fn)return false;
153
- var g=c.createGain();g.gain.value=(NORM[o.sound]||1)*o.volume;var node=g;
251
+ function render(c,dest,o,pan){var def=defOf(o),fn=def?(def.synth?synthFn(def.synth):def.src&&BUFS[def.src]?function(g){sample(g,BUFS[def.src])}:null):SOUNDS[o.sound];if(!fn)return false;
252
+ var g=c.createGain();g.gain.value=(def?1:NORM[o.sound]||1)*o.volume;var node=g;
154
253
  if(o.tone<0){var lp=c.createBiquadFilter();lp.type='lowpass';lp.frequency.value=20000*Math.pow(2,o.tone*5);lp.Q.value=.5;g.connect(lp);node=lp}
155
254
  else if(o.tone>0){var hs=c.createBiquadFilter();hs.type='highshelf';hs.frequency.value=2500;hs.gain.value=o.tone*9;g.connect(hs);node=hs}
156
255
  if(c.createStereoPanner&&pan){var p=c.createStereoPanner();p.pan.setValueAtTime(Math.max(-1,Math.min(1,pan)),c.currentTime);node.connect(p);p.connect(dest)}else node.connect(dest);
@@ -158,6 +257,7 @@
158
257
  // sound="off" on an element always wins, so decorative or autoplaying icons stay silent even when sound is on for the page
159
258
  function wantsSound(el){if(el&&el.getAttribute&&el.getAttribute('sound')==='off')return false;return soundAll||!!(el&&el.hasAttribute&&el.hasAttribute('sound'))}
160
259
  function playFor(ic,el,extra){if(muted||!unlocked||!ensureAudio())return false;var o=settings(ic,el,extra);if(!o.sound||!o.volume)return false;
260
+ var def=defOf(o);if(def&&def.src&&!BUFS[def.src]){var asked=Date.now();load(def.src).then(function(){if(Date.now()-asked<700)playFor(ic,el,extra)},function(e){if(typeof console!=='undefined')console.warn('magic-glyphs: '+e.message)});return true}
161
261
  var pan=0;if(o.pan&&el&&el.getBoundingClientRect&&typeof innerWidth==='number'&&innerWidth>0){var r=el.getBoundingClientRect();pan=((r.left+r.width/2)/innerWidth*2-1)*.85*o.pan}
162
262
  try{return render(ctx,master,o,pan)}catch(e){return false}}
163
263
  function playSound(name,el,extra){var x={sound:name};if(extra)for(var k in extra)x[k]=extra[k];return playFor(null,el,x)}
@@ -182,7 +282,8 @@
182
282
  // render to an AudioBuffer offline (no click needed), for previews, tests and exporting audio
183
283
  render:function(name,opts,Offline){var OAC=Offline||typeof window!=='undefined'&&(window.OfflineAudioContext||window.webkitOfflineAudioContext);if(!OAC)return Promise.reject(new Error('Offline audio is not available here.'));
184
284
  var o=REG[name]?settings(REG[name],null,opts):settings(null,null,Object.assign({sound:name},opts||{}));var c=new OAC(2,Math.ceil(44100*(2.6+o.delay/1000)),44100);
185
- if(!render(c,c.destination,o,(opts&&opts.panTo)||0))return Promise.reject(new Error('No sound named "'+o.sound+'".'));return c.startRendering()},
285
+ var def=defOf(o);return (def&&def.src?load(def.src,c):Promise.resolve()).then(function(){
286
+ if(!render(c,c.destination,o,(opts&&opts.panTo)||0))throw new Error(o.sound==='custom'?'This icon\'s own sound is missing or not valid.':'No sound named "'+o.sound+'".');return c.startRendering()})},
186
287
  // encode a rendered buffer as a 16-bit stereo WAV file (ArrayBuffer), trimming the silence at the end
187
288
  wav:function(buf){var L=buf.getChannelData(0),R=buf.numberOfChannels>1?buf.getChannelData(1):L,n=L.length;
188
289
  while(n>1&&Math.abs(L[n-1])<1e-4&&Math.abs(R[n-1])<1e-4)n--;n=Math.min(L.length,n+Math.round(buf.sampleRate*.02));
@@ -190,7 +291,19 @@
190
291
  w(0,'RIFF');v.setUint32(4,36+n*4,true);w(8,'WAVE');w(12,'fmt ');v.setUint32(16,16,true);v.setUint16(20,1,true);v.setUint16(22,2,true);
191
292
  v.setUint32(24,buf.sampleRate,true);v.setUint32(28,buf.sampleRate*4,true);v.setUint16(32,4,true);v.setUint16(34,16,true);w(36,'data');v.setUint32(40,n*4,true);
192
293
  for(var i=0,o=44;i<n;i++,o+=4){v.setInt16(o,Math.max(-1,Math.min(1,L[i]))*32767,true);v.setInt16(o+2,Math.max(-1,Math.min(1,R[i]))*32767,true)}return ab},
193
- names:function(){return Object.keys(SOUNDS)},unlocked:function(){return unlocked}};
294
+ names:function(){return Object.keys(SOUNDS).concat(Object.keys(DEFS))},
295
+ // the built-in sounds by family: {interface:[...], alerts:[...], ...}
296
+ groups:function(){var g={};for(var k in GROUPS)g[k]=GROUPS[k].slice();var mine=Object.keys(DEFS);if(mine.length)g.yours=mine;return g},
297
+ // your own sound under a name: sound.define('pling', {src:'/sounds/pling.mp3'}) or {synth:[['t',880,0,.2,.3,'sine']]}; null removes it
298
+ define:function(name,d){if(!/^[a-z0-9-]{1,40}$/.test(name)||SOUNDS[name])throw new Error('Pick a sound name of lowercase letters, digits and dashes that is not built in.');
299
+ if(d===null){delete DEFS[name];return api}var c=cleanDef(d);if(!c)throw new Error('A sound needs {src: a URL or data: URI} or {synth: a list of steps}.');DEFS[name]=c;return api},
300
+ // start loading an audio file ahead of the first play
301
+ preload:function(src){return load(src).then(function(){})},
302
+ // the steps a built-in sound is made of, to start a design from: [['t',hz,start,len,level,wave,endHz], ['n',hz,endHz,start,len,level,q]]
303
+ steps:function(name){var d=DEFS[name];if(d&&d.synth)return JSON.parse(JSON.stringify(d.synth));var fn=SOUNDS[name];if(!fn)return null;var rec=[],was=cur;cur={rec:rec};try{fn(null)}finally{cur=was}
304
+ return rec.map(function(x){return x.map(function(v){return typeof v==='number'?+v.toFixed(4):v})})},
305
+ // check a custom sound definition: the cleaned copy, or null
306
+ check:cleanDef,unlocked:function(){return unlocked}};
194
307
  var api={sound:soundApi,register:register,svg:svg,attach:attach,state:state,origins:origins,transform:transform,has:function(n){return !!REG[n]},names:function(){return Object.keys(REG)},icon:function(n){return REG[n]}};
195
308
  // <magic-glyph name="heart" variant="duotone" trigger="hover" size="32" label="Like"></magic-glyph>
196
309
  if(typeof customElements!=='undefined'&&typeof HTMLElement!=='undefined'&&!customElements.get('magic-glyph')){
package/web/runtime.d.ts CHANGED
@@ -15,7 +15,13 @@ export interface GlyphSoundOptions {
15
15
  pan?: number;
16
16
  /** Milliseconds, or "auto". */
17
17
  delay?: number | 'auto';
18
+ /** Play your own audio file instead: a URL or a data: URI (the sound-src attribute). */
19
+ src?: string;
18
20
  }
21
+ /** One layer of a designed sound: a tone ['t', hz, start s, length s, level 0-1, wave, end hz?] or filtered noise ['n', hz, end hz, start s, length s, level 0-1, focus?]. */
22
+ export type GlyphSoundStep = ['t', number, number, number, number, 'sine' | 'triangle' | 'square' | 'sawtooth', number?] | ['n', number, number, number, number, number, number?];
23
+ /** Your own sound: an audio file, or a design made of steps. */
24
+ export type GlyphCustomSound = { src: string } | { synth: GlyphSoundStep[] };
19
25
  export interface GlyphSound {
20
26
  /** Turn sound on or off for every icon. Off by default. */
21
27
  enable(on?: boolean): void;
@@ -34,7 +40,18 @@ export interface GlyphSound {
34
40
  defaults(options?: GlyphSoundOptions): Required<GlyphSoundOptions>;
35
41
  /** Render a sound offline to an AudioBuffer, without needing a click. */
36
42
  render(name: string, options?: GlyphSoundOptions & { panTo?: number }, offlineContext?: unknown): Promise<AudioBuffer>;
43
+ /** Every sound name: the built-in ones and any you defined. */
37
44
  names(): string[];
45
+ /** The built-in sounds by family (interface, alerts, motion, impacts, nature, musical, game, machines), plus yours. */
46
+ groups(): Record<string, string[]>;
47
+ /** Add your own named sound; null removes it. Built-in names cannot be replaced. */
48
+ define(name: string, sound: GlyphCustomSound | null): MagicGlyphsRuntime;
49
+ /** Load an audio file ahead of its first play. */
50
+ preload(src: string): Promise<void>;
51
+ /** The steps a built-in or defined sound is made of, to start a design from. */
52
+ steps(name: string): GlyphSoundStep[] | null;
53
+ /** The cleaned copy of a custom sound, or null when it is not valid. */
54
+ check(sound: unknown): GlyphCustomSound | null;
38
55
  unlocked(): boolean;
39
56
  /** Turn sound on or off for every icon and remember the choice in this browser (a visitor's mute switch). */
40
57
  toggle(on?: boolean): boolean;
package/web/runtime.js CHANGED
@@ -3,7 +3,7 @@
3
3
  'use strict';
4
4
  var REG={},uid=0;
5
5
  var STROKE='stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"';
6
- function register(data){(data.icons||(Array.isArray(data)?data:[data])).forEach(function(ic){REG[ic.n]=ic}); // a set, a list, or one icon
6
+ function register(data){(data.icons||(Array.isArray(data)?data:[data])).forEach(function(ic){REG[ic.n]=ic;var cu=ic.S&&ic.S.custom;if(cu&&typeof cu.src==='string'&&typeof window!=='undefined')setTimeout(function(){var d=cleanDef(cu);if(d&&d.src)load(d.src).catch(function(){})},0)}); // a set, a list, or one icon
7
7
  // elements already on the page were upgraded before their data arrived: draw them now
8
8
  if(typeof document!=='undefined')Array.prototype.forEach.call(document.querySelectorAll('magic-glyph'),function(e){if(e._render)e._render()});
9
9
  return api}
@@ -82,11 +82,11 @@
82
82
  function hook(){if(hooked||typeof document==='undefined')return;hooked=true;var un=function(){unlocked=true;ensureAudio()};
83
83
  document.addEventListener('pointerdown',un,true);document.addEventListener('keydown',un,true)}
84
84
  // tone and noise read the context, pitch, speed and start time of the sound being rendered from cur
85
- function tone(out,f,t,dur,g,type,f2){var C=cur.C,o=C.createOscillator(),a=C.createGain(),at=cur.t0+t/cur.sp,du=dur/cur.sp,pm=cur.pm;
85
+ function tone(out,f,t,dur,g,type,f2){if(cur.rec){cur.rec.push(['t',f,t,dur,g,type||'sine'].concat(f2?[f2]:[]));return}var C=cur.C,o=C.createOscillator(),a=C.createGain(),at=cur.t0+t/cur.sp,du=dur/cur.sp,pm=cur.pm;
86
86
  o.type=type||'sine';o.frequency.setValueAtTime(f*pm,at);if(f2)o.frequency.exponentialRampToValueAtTime(f2*pm,at+du);
87
87
  a.gain.setValueAtTime(.0001,at);a.gain.exponentialRampToValueAtTime(g,at+Math.min(.006,du/3));a.gain.exponentialRampToValueAtTime(.0001,at+du);
88
88
  o.connect(a);a.connect(out);o.start(at);o.stop(at+du+.05)}
89
- function noise(out,f,f2,t,dur,g,q){var C=cur.C,s=C.createBufferSource(),b=C.createBiquadFilter(),a=C.createGain(),at=cur.t0+t/cur.sp,du=dur/cur.sp,pm=cur.pm;
89
+ function noise(out,f,f2,t,dur,g,q){if(cur.rec){cur.rec.push(['n',f,f2,t,dur,g,q||1.2]);return}var C=cur.C,s=C.createBufferSource(),b=C.createBiquadFilter(),a=C.createGain(),at=cur.t0+t/cur.sp,du=dur/cur.sp,pm=cur.pm;
90
90
  s.buffer=noiseFor(C);s.loop=true;b.type='bandpass';b.Q.value=q||1.2;b.frequency.setValueAtTime(Math.min(f*pm,18000),at);b.frequency.exponentialRampToValueAtTime(Math.min(f2*pm,18000),at+du);
91
91
  a.gain.setValueAtTime(.0001,at);a.gain.exponentialRampToValueAtTime(g,at+du*.3);a.gain.exponentialRampToValueAtTime(.0001,at+du);
92
92
  s.connect(b);b.connect(a);a.connect(out);s.start(at);s.stop(at+du+.05)}
@@ -129,17 +129,116 @@
129
129
  buzz:o=>{tone(o,95,0,.12,.3,'sawtooth');tone(o,95,.17,.12,.3,'sawtooth')},
130
130
  coin:o=>{tone(o,1568,0,.08,.2,'square');tone(o,2093,.07,.35,.2,'square')},
131
131
  scribble:o=>{noise(o,2200,3200,0,.09,.3,4);noise(o,2800,2000,.1,.09,.3,4);noise(o,2200,3400,.2,.1,.3,4)},
132
- ticktock:o=>{tone(o,1900,0,.025,.22,'square');tone(o,1300,.3,.025,.2,'square');tone(o,1900,.6,.025,.16,'square')}
132
+ ticktock:o=>{tone(o,1900,0,.025,.22,'square');tone(o,1300,.3,.025,.2,'square');tone(o,1900,.6,.025,.16,'square')},
133
+ // ---- added in 0.8.5 ----
134
+ boop:o=>tone(o,520,0,.12,.35,'sine',260),
135
+ blip:o=>tone(o,1250,0,.05,.16,'square'),
136
+ hover:o=>tone(o,2200,0,.035,.08,'sine',2000),
137
+ switchon:o=>{noise(o,3000,1500,0,.02,.4,2);tone(o,700,.01,.07,.25,'sine',1050)},
138
+ switchoff:o=>{noise(o,2500,1200,0,.02,.4,2);tone(o,950,.01,.07,.25,'sine',620)},
139
+ typing:o=>{[0,.09,.15,.27,.33,.46].forEach((t,i)=>noise(o,2600+i%3*500,1800,t,.03,.4,3))},
140
+ flip:o=>{noise(o,1500,4000,0,.09,.4,1.2);noise(o,1800,4500,.1,.09,.3,1.2)},
141
+ plink:o=>{tone(o,2093,0,.35,.25,'triangle');tone(o,4186,0,.12,.06)},
142
+ pluck:o=>{tone(o,330,0,.4,.16,'sawtooth',326);tone(o,660,0,.18,.12,'triangle')},
143
+ marimba:o=>{tone(o,523,0,.45,.35);tone(o,2093,0,.08,.12)},
144
+ harp:o=>{[523,659,784,1047,1319].forEach((f,i)=>tone(o,f,i*.06,.5,.18,'triangle'))},
145
+ bell:o=>{tone(o,880,0,1.6,.25);tone(o,2429,0,.9,.1);tone(o,4752,0,.5,.05)},
146
+ gong:o=>{tone(o,98,0,2.2,.4);tone(o,147,.01,1.8,.2);tone(o,233,.02,1.4,.12);noise(o,400,200,0,.6,.08,1)},
147
+ magic:o=>{tone(o,523,0,.6,.1,'triangle',1047);[1568,2093,2637,3136].forEach((f,i)=>tone(o,f,.1+i*.07,.4,.08))},
148
+ sparkle:o=>{[2637,3136,2349,3520,2794,3951].forEach((f,i)=>tone(o,f,i*.05,.25,.09))},
149
+ ping:o=>{tone(o,1760,0,.8,.25);tone(o,3520,0,.3,.06)},
150
+ pong:o=>{tone(o,880,0,.25,.35);tone(o,1760,0,.08,.08)},
151
+ incoming:o=>{tone(o,1175,0,.18,.22);tone(o,1568,.12,.35,.22)},
152
+ bikebell:o=>{for(let i=0;i<2;i++){tone(o,2100,i*.16,.5,.14);tone(o,2700,i*.16,.4,.08)}},
153
+ trill:o=>{for(let i=0;i<10;i++)tone(o,i%2?1600:1300,i*.045,.045,.14,'triangle')},
154
+ alarm:o=>{for(let i=0;i<4;i++)tone(o,1050,i*.15,.09,.15,'square')},
155
+ countdown:o=>{tone(o,880,0,.08,.2);tone(o,880,.3,.08,.2);tone(o,1760,.6,.3,.22)},
156
+ tada:o=>{tone(o,523,0,.1,.2,'triangle');tone(o,659,.08,.1,.2,'triangle');[1047,1319,1568].forEach(f=>tone(o,f,.18,.7,.14,'triangle'))},
157
+ error:o=>{tone(o,196,0,.14,.2,'square');tone(o,185,.17,.22,.2,'square')},
158
+ warning:o=>{[0,1,2].forEach(i=>{tone(o,880,i*.22,.1,.16,'triangle');tone(o,660,i*.22+.11,.1,.16,'triangle')})},
159
+ fail:o=>{tone(o,494,0,.16,.22,'triangle');tone(o,392,.17,.16,.22,'triangle');tone(o,311,.34,.4,.22,'triangle',290)},
160
+ cancel:o=>{tone(o,660,0,.1,.22,'triangle');tone(o,440,.1,.18,.22,'triangle')},
161
+ unlock:o=>{noise(o,3200,1800,0,.03,.4,3);noise(o,2600,1400,.09,.04,.4,3);tone(o,1568,.16,.35,.16);tone(o,2093,.22,.45,.14)},
162
+ lock:o=>{noise(o,2400,1200,0,.04,.45,3);tone(o,220,0,.12,.35,'triangle',140)},
163
+ rise:o=>{tone(o,330,0,.5,.15,'triangle',660);tone(o,495,0,.5,.08,'sine',990)},
164
+ fall:o=>{tone(o,900,0,.45,.18,'triangle',300);tone(o,1350,0,.3,.06,'sine',450)},
165
+ swoop:o=>{tone(o,350,0,.22,.25,'sine',1500);noise(o,800,3500,0,.22,.2,1)},
166
+ whip:o=>{noise(o,1500,8000,0,.1,.5,1.5);noise(o,6000,3000,.1,.04,.4,2)},
167
+ zipper:o=>{for(let i=0;i<10;i++)noise(o,2000+i*350,2500+i*350,i*.025,.018,.7,6)},
168
+ boing:o=>{tone(o,140,0,.09,.35,'sine',420);tone(o,420,.09,.1,.3,'sine',230);tone(o,230,.19,.1,.25,'sine',340);tone(o,340,.29,.14,.18,'sine',260)},
169
+ wobble:o=>{for(let i=0;i<4;i++)tone(o,i%2?480:300,i*.09,.09,.2,'triangle',i%2?300:480)},
170
+ squeak:o=>{tone(o,1700,0,.08,.14,'sine',2600);tone(o,2500,.09,.1,.12,'sine',1700)},
171
+ gulp:o=>{tone(o,180,0,.08,.4,'sine',420);tone(o,420,.09,.07,.3,'sine',160)},
172
+ drip:o=>{tone(o,1400,0,.06,.35,'sine',500);tone(o,900,.07,.05,.12,'sine',1500)},
173
+ splash:o=>{noise(o,2200,400,0,.45,.5,.8);tone(o,500,.05,.08,.15,'sine',1100);tone(o,700,.15,.07,.12,'sine',1300)},
174
+ rain:o=>{[0,.07,.11,.2,.26,.31,.4,.47,.55,.6,.68].forEach((t,i)=>noise(o,4000+(i*1300)%4000,3000,t,.025,.6,5))},
175
+ thunder:o=>{noise(o,600,80,0,1.4,.6,.5);noise(o,300,60,.15,1.2,.5,.5);tone(o,45,0,1.2,.3,'sine',30)},
176
+ rumble:o=>{tone(o,55,0,.9,.3,'sawtooth',45);noise(o,180,90,0,.9,.35,.6)},
177
+ crackle:o=>{[0,.05,.13,.16,.28,.34,.45,.52].forEach((t,i)=>noise(o,1500+(i*900)%3000,800,t,.02,.4+(i%3)*.1,4))},
178
+ crumple:o=>{[0,.04,.09,.12,.19,.25,.28].forEach((t,i)=>noise(o,1200+(i*700)%2600,2000,t,.05,.35,2))},
179
+ clap:o=>{[0,.012,.024].forEach(t=>noise(o,1400,1100,t,.05,.45,1.5));noise(o,1300,900,.035,.18,.25,1)},
180
+ heartbeat:o=>{tone(o,90,0,.12,.6,'sine',45);tone(o,80,.17,.14,.45,'sine',40)},
181
+ woodblock:o=>{tone(o,820,0,.07,.5,'sine',760);tone(o,1650,0,.03,.15,'triangle')},
182
+ clang:o=>{tone(o,523,0,.9,.18,'triangle');tone(o,1347,0,.6,.12);tone(o,2273,0,.4,.08);noise(o,3000,1500,0,.05,.3,2)},
183
+ kick:o=>{tone(o,150,0,.3,.8,'sine',42);noise(o,2000,600,0,.02,.2,1)},
184
+ snare:o=>{noise(o,2200,1600,0,.18,.5,.7);tone(o,200,0,.1,.35,'triangle',160)},
185
+ hihat:o=>{noise(o,9000,8000,0,.05,.4,1);noise(o,9000,8000,.15,.05,.3,1)},
186
+ boom:o=>{tone(o,90,0,.8,.7,'sine',28);noise(o,700,80,0,.9,.6,.6)},
187
+ kaching:o=>{noise(o,2500,1200,0,.06,.4,3);tone(o,2637,.06,.6,.16,'triangle');tone(o,3520,.12,.8,.14)},
188
+ levelup:o=>{[523,659,784,1047].forEach((f,i)=>tone(o,f,i*.07,i<3?.08:.3,.16,'square'))},
189
+ powerup:o=>{tone(o,220,0,.45,.14,'square',1320);tone(o,330,0,.45,.08,'triangle',1980)},
190
+ powerdown:o=>{tone(o,1100,0,.5,.14,'square',140);tone(o,550,0,.5,.08,'triangle',70)},
191
+ jump:o=>tone(o,280,0,.16,.2,'square',760),
192
+ laser:o=>{tone(o,2400,0,.18,.18,'sawtooth',180);tone(o,1800,.02,.16,.1,'square',140)},
193
+ robot:o=>{[330,494,262,415].forEach((f,i)=>tone(o,f,i*.08,.07,.13,'square'))},
194
+ engine:o=>{for(let i=0;i<12;i++)tone(o,70+i*6,i*.05,.045,.35,'sawtooth',60+i*6)}
195
+ };
196
+ // families, for pickers and docs; every sound is in exactly one
197
+ var GROUPS={
198
+ interface:['tap','click','blink','hover','blip','boop','switchon','switchoff','tick2','ticktock','typing','flip','scribble','shutter'],
199
+ alerts:['ding','chime','ping','pong','beep','incoming','alert','success','error','warning','fail','cancel','alarm','countdown','bell','bikebell','trill','tada'],
200
+ motion:['whoosh','swish','swoop','breeze','rise','fall','whip','zipper','wobble','boing','bounce','drop','liftoff'],
201
+ impacts:['thump','knock','clank','clang','woodblock','kick','snare','hihat','clap','heartbeat','boom','crunch','crumple'],
202
+ nature:['bubble','drip','splash','rain','thunder','rumble','sizzle','fizz','crackle','chirp','whistle','gulp','squeak'],
203
+ musical:['plink','pluck','marimba','harp','gong','notes','glow','soft','sparkle','magic'],
204
+ game:['coin','kaching','levelup','powerup','powerdown','jump','laser','zap','pop','pops','robot'],
205
+ machines:['horn','siren','rotor','engine','ratchet','buzz','lock','unlock']
133
206
  };
134
207
  // loudness trims measured offline so every sound plays at about the same level and none clips (see tools/sound-audit.js)
135
- var NORM={"bounce":0.56,"siren":1.71,"bubble":0.87,"crunch":2.43,"sizzle":2.5,"fizz":2.06,"horn":0.92,"rotor":0.95,"chirp":1,"whistle":0.83,"ding":0.63,"thump":0.51,"liftoff":1.36,"whoosh":1.83,"swish":1.84,"breeze":2.5,"chime":0.73,"glow":1.16,"soft":1.1,"tick2":0.84,"blink":1.84,"ratchet":1.17,"clank":0.98,"click":0.62,"pop":0.6,"pops":0.78,"drop":0.73,"zap":1.07,"notes":0.92,"shutter":1.36,"knock":0.53,"success":1.02,"tap":0.95,"beep":1.08,"alert":1.1,"buzz":1.23,"coin":0.71,"scribble":2.5,"ticktock":0.9};
208
+ var NORM={"bounce":0.55,"siren":1.67,"bubble":0.85,"crunch":2.48,"sizzle":2.44,"fizz":2.04,"horn":0.9,"rotor":0.93,"chirp":0.98,"whistle":0.82,"ding":0.62,"thump":0.5,"liftoff":1.34,"whoosh":1.76,"swish":1.73,"breeze":2.5,"chime":0.71,"glow":1.14,"soft":1.08,"tick2":0.83,"blink":1.8,"ratchet":1.15,"clank":0.96,"click":0.63,"pop":0.58,"pops":0.76,"drop":0.71,"zap":1.04,"notes":0.9,"shutter":1.42,"knock":0.51,"success":1,"tap":0.93,"beep":1.05,"alert":1.07,"buzz":1.2,"coin":0.7,"scribble":2.5,"ticktock":0.88,"boop":0.68,"blip":1,"hover":1.88,"switchon":0.97,"switchoff":0.97,"typing":2.19,"flip":1.46,"plink":0.77,"pluck":1.74,"marimba":0.64,"harp":0.84,"bell":0.76,"gong":0.56,"magic":1.29,"sparkle":1.04,"ping":0.68,"pong":0.63,"incoming":0.85,"bikebell":0.88,"trill":1.74,"alarm":1.05,"countdown":0.85,"tada":1,"error":0.93,"warning":1.54,"fail":1.19,"cancel":1.17,"unlock":0.97,"lock":0.77,"rise":1.4,"fall":1.27,"swoop":0.9,"whip":1.15,"zipper":1.88,"boing":0.88,"wobble":1.37,"squeak":1.27,"gulp":0.69,"drip":0.82,"splash":1.11,"rain":1.3,"thunder":1.12,"rumble":1.2,"crackle":2.37,"crumple":1.85,"clap":1.96,"heartbeat":0.6,"woodblock":0.45,"clang":0.99,"kick":0.35,"snare":0.76,"hihat":0.9,"boom":0.43,"kaching":0.85,"levelup":1.03,"powerup":1.14,"powerdown":1.06,"jump":0.9,"laser":1.01,"robot":1.34,"engine":1.31};
136
209
  var num=function(v,d,lo,hi){v=parseFloat(v);return isFinite(v)?Math.max(lo,Math.min(hi,v)):d};
210
+ // ---- custom sounds: a designed synth (a list of steps) or an audio file, attached to an icon (S.custom) or named with sound.define ----
211
+ // a step is ['t', hz, start s, length s, level 0-1, wave, end hz?] for a tone or ['n', hz, end hz, start s, length s, level 0-1, q?] for filtered noise
212
+ var WAVES={sine:1,triangle:1,square:1,sawtooth:1},DEFS={},BUFS={},LOADING={};
213
+ var fin=function(v,lo,hi){return typeof v==='number'&&isFinite(v)&&v>=lo&&v<=hi};
214
+ function cleanSteps(a){if(!Array.isArray(a)||!a.length||a.length>48)return null;var out=[];
215
+ for(var i=0;i<a.length;i++){var x=a[i];if(!Array.isArray(x))return null;
216
+ if(x[0]==='t'&&fin(x[1],20,20000)&&fin(x[2],0,2.5)&&fin(x[3],.005,2.5)&&fin(x[4],.001,1)&&WAVES[x[5]||'sine']&&(x[6]===undefined||x[6]===null||fin(x[6],20,20000)))out.push(['t',x[1],x[2],x[3],x[4],x[5]||'sine'].concat(x[6]?[x[6]]:[]));
217
+ else if(x[0]==='n'&&fin(x[1],20,20000)&&fin(x[2],20,20000)&&fin(x[3],0,2.5)&&fin(x[4],.005,2.5)&&fin(x[5],.001,1)&&(x[6]===undefined||fin(x[6],.1,30)))out.push(['n',x[1],x[2],x[3],x[4],x[5],x[6]||1.2]);
218
+ else return null}return out}
219
+ // audio may be a data: URI (what the icon maker stores) or a URL your page can fetch
220
+ function cleanDef(d){if(!d||typeof d!=='object')return null;if(d.synth){var st=cleanSteps(d.synth);return st?{synth:st}:null}
221
+ if(typeof d.src==='string'&&d.src.length<=1500000&&(/^data:audio\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/=]+$/.test(d.src)||/^(https?:)?\/\/|^\.{0,2}\//.test(d.src)))return{src:d.src};return null}
222
+ function bytes(src){if(src.slice(0,5)==='data:'){var b=atob(src.slice(src.indexOf(',')+1)),u=new Uint8Array(b.length);for(var i=0;i<b.length;i++)u[i]=b.charCodeAt(i);return Promise.resolve(u.buffer)}
223
+ return fetch(src).then(function(r){if(!r.ok)throw new Error('Could not load the sound '+src+' ('+r.status+').');return r.arrayBuffer()})}
224
+ // decode once and keep it; level it so a quiet or loud recording plays at about the same level as the built-in sounds
225
+ function load(src,c){if(BUFS[src])return Promise.resolve(BUFS[src]);if(LOADING[src])return LOADING[src];
226
+ var dc=c||(typeof window!=='undefined'&&(window.OfflineAudioContext||window.webkitOfflineAudioContext)?new (window.OfflineAudioContext||window.webkitOfflineAudioContext)(1,1,44100):ensureAudio());
227
+ if(!dc)return Promise.reject(new Error('Audio is not available here.'));
228
+ return LOADING[src]=bytes(src).then(function(ab){return new Promise(function(ok,no){var pr=dc.decodeAudioData(ab,ok,no);if(pr&&pr.then)pr.then(ok,no)})}).then(function(buf){
229
+ var pk=0;for(var ch=0;ch<buf.numberOfChannels;ch++){var d=buf.getChannelData(ch);for(var i=0;i<d.length;i+=2){var a=d[i]<0?-d[i]:d[i];if(a>pk)pk=a}}
230
+ delete LOADING[src];return BUFS[src]={buf:buf,gain:pk>1e-4?Math.min(6,.45/pk):1}},function(e){delete LOADING[src];throw e})}
231
+ function sample(out,b){var C=cur.C,s=C.createBufferSource(),a=C.createGain();s.buffer=b.buf;if(s.playbackRate)s.playbackRate.value=cur.pm*cur.sp;a.gain.value=b.gain;s.connect(a);a.connect(out);s.start(cur.t0)}
232
+ // the definition behind a sound name, if it is not built in
233
+ function defOf(o){return o.sound==='custom'?o.custom:DEFS[o.sound]}
234
+ function synthFn(st){return function(o){st.forEach(function(x){x[0]==='t'?tone(o,x[1],x[2],x[3],x[4],x[5],x[6]):noise(o,x[1],x[2],x[3],x[4],x[5],x[6])})}}
137
235
  // settings for one play: defaults < sound.set(icon) < the icon's own data (ic.S) < attributes on the element
138
236
  function settings(ic,el,extra){var o={},k;for(k in DEF)o[k]=DEF[k];
139
237
  var add=function(x){if(x)for(var k in x)if(x[k]!==undefined&&x[k]!==null&&x[k]!=='')o[k]=x[k]};
140
238
  if(ic){add(PER[ic.n]);add(ic.S)}
141
- if(el&&el.getAttribute)add({sound:el.getAttribute('sound-name'),volume:el.getAttribute('sound-volume'),pitch:el.getAttribute('sound-pitch'),speed:el.getAttribute('sound-speed'),tone:el.getAttribute('sound-tone'),pan:el.getAttribute('sound-pan'),delay:el.getAttribute('sound-delay')});
239
+ if(el&&el.getAttribute)add({sound:el.getAttribute('sound-name'),volume:el.getAttribute('sound-volume'),pitch:el.getAttribute('sound-pitch'),speed:el.getAttribute('sound-speed'),tone:el.getAttribute('sound-tone'),pan:el.getAttribute('sound-pan'),delay:el.getAttribute('sound-delay'),src:el.getAttribute('sound-src')});
142
240
  add(extra);o.sound=o.sound||(ic&&ic.s)||null;
241
+ if(o.src){o.custom=cleanDef({src:o.src});o.sound='custom';delete o.src}else if(o.custom)o.custom=cleanDef(o.custom);
143
242
  o.volume=num(o.volume,1,0,2);o.pitch=num(o.pitch,0,-12,12);o.speed=num(o.speed,1,.5,2);o.tone=num(o.tone,0,-1,1);o.pan=num(o.pan,1,0,1);
144
243
  o.delay=o.delay==='auto'?(ic?beat(ic,o.sound):0):num(o.delay,0,0,2000);return o}
145
244
  // impact sounds wait for the moment of impact. The animations mark it: a reaction (a squash, a tray dipping, a lid jumping)
@@ -149,8 +248,8 @@
149
248
  ic.A.forEach(function(a){var t=a.k&&a.k[0]&&a.k[0][0];if(t>40&&t<600&&(!hit||t<hit))hit=t});
150
249
  return ic._beat=hit}
151
250
  // build the sound graph into any audio context: volume and tone, then position
152
- function render(c,dest,o,pan){var fn=SOUNDS[o.sound];if(!fn)return false;
153
- var g=c.createGain();g.gain.value=(NORM[o.sound]||1)*o.volume;var node=g;
251
+ function render(c,dest,o,pan){var def=defOf(o),fn=def?(def.synth?synthFn(def.synth):def.src&&BUFS[def.src]?function(g){sample(g,BUFS[def.src])}:null):SOUNDS[o.sound];if(!fn)return false;
252
+ var g=c.createGain();g.gain.value=(def?1:NORM[o.sound]||1)*o.volume;var node=g;
154
253
  if(o.tone<0){var lp=c.createBiquadFilter();lp.type='lowpass';lp.frequency.value=20000*Math.pow(2,o.tone*5);lp.Q.value=.5;g.connect(lp);node=lp}
155
254
  else if(o.tone>0){var hs=c.createBiquadFilter();hs.type='highshelf';hs.frequency.value=2500;hs.gain.value=o.tone*9;g.connect(hs);node=hs}
156
255
  if(c.createStereoPanner&&pan){var p=c.createStereoPanner();p.pan.setValueAtTime(Math.max(-1,Math.min(1,pan)),c.currentTime);node.connect(p);p.connect(dest)}else node.connect(dest);
@@ -158,6 +257,7 @@
158
257
  // sound="off" on an element always wins, so decorative or autoplaying icons stay silent even when sound is on for the page
159
258
  function wantsSound(el){if(el&&el.getAttribute&&el.getAttribute('sound')==='off')return false;return soundAll||!!(el&&el.hasAttribute&&el.hasAttribute('sound'))}
160
259
  function playFor(ic,el,extra){if(muted||!unlocked||!ensureAudio())return false;var o=settings(ic,el,extra);if(!o.sound||!o.volume)return false;
260
+ var def=defOf(o);if(def&&def.src&&!BUFS[def.src]){var asked=Date.now();load(def.src).then(function(){if(Date.now()-asked<700)playFor(ic,el,extra)},function(e){if(typeof console!=='undefined')console.warn('magic-glyphs: '+e.message)});return true}
161
261
  var pan=0;if(o.pan&&el&&el.getBoundingClientRect&&typeof innerWidth==='number'&&innerWidth>0){var r=el.getBoundingClientRect();pan=((r.left+r.width/2)/innerWidth*2-1)*.85*o.pan}
162
262
  try{return render(ctx,master,o,pan)}catch(e){return false}}
163
263
  function playSound(name,el,extra){var x={sound:name};if(extra)for(var k in extra)x[k]=extra[k];return playFor(null,el,x)}
@@ -182,7 +282,8 @@
182
282
  // render to an AudioBuffer offline (no click needed), for previews, tests and exporting audio
183
283
  render:function(name,opts,Offline){var OAC=Offline||typeof window!=='undefined'&&(window.OfflineAudioContext||window.webkitOfflineAudioContext);if(!OAC)return Promise.reject(new Error('Offline audio is not available here.'));
184
284
  var o=REG[name]?settings(REG[name],null,opts):settings(null,null,Object.assign({sound:name},opts||{}));var c=new OAC(2,Math.ceil(44100*(2.6+o.delay/1000)),44100);
185
- if(!render(c,c.destination,o,(opts&&opts.panTo)||0))return Promise.reject(new Error('No sound named "'+o.sound+'".'));return c.startRendering()},
285
+ var def=defOf(o);return (def&&def.src?load(def.src,c):Promise.resolve()).then(function(){
286
+ if(!render(c,c.destination,o,(opts&&opts.panTo)||0))throw new Error(o.sound==='custom'?'This icon\'s own sound is missing or not valid.':'No sound named "'+o.sound+'".');return c.startRendering()})},
186
287
  // encode a rendered buffer as a 16-bit stereo WAV file (ArrayBuffer), trimming the silence at the end
187
288
  wav:function(buf){var L=buf.getChannelData(0),R=buf.numberOfChannels>1?buf.getChannelData(1):L,n=L.length;
188
289
  while(n>1&&Math.abs(L[n-1])<1e-4&&Math.abs(R[n-1])<1e-4)n--;n=Math.min(L.length,n+Math.round(buf.sampleRate*.02));
@@ -190,7 +291,19 @@
190
291
  w(0,'RIFF');v.setUint32(4,36+n*4,true);w(8,'WAVE');w(12,'fmt ');v.setUint32(16,16,true);v.setUint16(20,1,true);v.setUint16(22,2,true);
191
292
  v.setUint32(24,buf.sampleRate,true);v.setUint32(28,buf.sampleRate*4,true);v.setUint16(32,4,true);v.setUint16(34,16,true);w(36,'data');v.setUint32(40,n*4,true);
192
293
  for(var i=0,o=44;i<n;i++,o+=4){v.setInt16(o,Math.max(-1,Math.min(1,L[i]))*32767,true);v.setInt16(o+2,Math.max(-1,Math.min(1,R[i]))*32767,true)}return ab},
193
- names:function(){return Object.keys(SOUNDS)},unlocked:function(){return unlocked}};
294
+ names:function(){return Object.keys(SOUNDS).concat(Object.keys(DEFS))},
295
+ // the built-in sounds by family: {interface:[...], alerts:[...], ...}
296
+ groups:function(){var g={};for(var k in GROUPS)g[k]=GROUPS[k].slice();var mine=Object.keys(DEFS);if(mine.length)g.yours=mine;return g},
297
+ // your own sound under a name: sound.define('pling', {src:'/sounds/pling.mp3'}) or {synth:[['t',880,0,.2,.3,'sine']]}; null removes it
298
+ define:function(name,d){if(!/^[a-z0-9-]{1,40}$/.test(name)||SOUNDS[name])throw new Error('Pick a sound name of lowercase letters, digits and dashes that is not built in.');
299
+ if(d===null){delete DEFS[name];return api}var c=cleanDef(d);if(!c)throw new Error('A sound needs {src: a URL or data: URI} or {synth: a list of steps}.');DEFS[name]=c;return api},
300
+ // start loading an audio file ahead of the first play
301
+ preload:function(src){return load(src).then(function(){})},
302
+ // the steps a built-in sound is made of, to start a design from: [['t',hz,start,len,level,wave,endHz], ['n',hz,endHz,start,len,level,q]]
303
+ steps:function(name){var d=DEFS[name];if(d&&d.synth)return JSON.parse(JSON.stringify(d.synth));var fn=SOUNDS[name];if(!fn)return null;var rec=[],was=cur;cur={rec:rec};try{fn(null)}finally{cur=was}
304
+ return rec.map(function(x){return x.map(function(v){return typeof v==='number'?+v.toFixed(4):v})})},
305
+ // check a custom sound definition: the cleaned copy, or null
306
+ check:cleanDef,unlocked:function(){return unlocked}};
194
307
  var api={sound:soundApi,register:register,svg:svg,attach:attach,state:state,origins:origins,transform:transform,has:function(n){return !!REG[n]},names:function(){return Object.keys(REG)},icon:function(n){return REG[n]}};
195
308
  // <magic-glyph name="heart" variant="duotone" trigger="hover" size="32" label="Like"></magic-glyph>
196
309
  if(typeof customElements!=='undefined'&&typeof HTMLElement!=='undefined'&&!customElements.get('magic-glyph')){