mirror of
https://github.com/exoticorn/microw8.git
synced 2026-11-13 23:17:19 +01:00
add first version of sound doku
This commit is contained in:
1 parent
e05701300c
commit
491bf88ade
4 files changed
+113
-26
No files matched your search
Binary file not shown.
@@ -16,7 +16,7 @@ export fn gesSnd(t: i32) -> f32 {
|
||||
let i: i32;
|
||||
loop clearLoop {
|
||||
(baseAddr + i)!GesBufferOffset = 0;
|
||||
branch_if (i := i + 4) < 128*8: clearLoop;
|
||||
branch_if (i := i + 4) < 128*4: clearLoop;
|
||||
}
|
||||
|
||||
let ch: i32;
|
||||
|
||||
+111
-24
@@ -234,7 +234,8 @@ Avoid the reserved control chars, they are currently NOPs but their behavior can
|
||||
| 2-3 | - | Reserved |
|
||||
| 4 | - | Switch to normal mode |
|
||||
| 5 | - | Switch to graphics mode |
|
||||
| 6-7 | - | Reserved |
|
||||
| 6 | - | Reserved |
|
||||
| 7 | - | Bell / trigger sound channel 0 |
|
||||
| 8 | - | Move cursor left |
|
||||
| 9 | - | Move cursor right |
|
||||
| 10 | - | Move cursor down |
|
||||
@@ -276,34 +277,120 @@ Sets the cursor position. In normal mode `x` and `y` are multiplied by 8 to get
|
||||
|
||||
## Sound
|
||||
|
||||
### Low level operation
|
||||
|
||||
MicroW8 actually runs two instances of your module. On the first instance, it calls `upd` and displays the framebuffer found in its memory. On the
|
||||
second instance, it calls `snd` instead with an incrementing sample index and expects that function to return sound samples for the left and right
|
||||
channel at 44100 Hz. If your module does not export a `snd` function, it calls the api function `sndGes` instead.
|
||||
|
||||
As the only means of communication, 32 bytes starting at address 0x00050 are copied from main to sound memory after `upd` returns. At the same time,
|
||||
32 bytes starting at 0x12c80 are copied from sound to main memory.
|
||||
|
||||
By default, the `sndGes` function generates sound based on the 32 bytes at 0x00050. This means that in the default configuration those 32 bytes act
|
||||
as sound registers. See the `sndGes` function for the meaning of those registers.
|
||||
|
||||
### fn playNote(channel: i32, note: i32)
|
||||
|
||||
Triggers a note (1-127) on the given channel (0-3). Notes are semitones with 69 being A4 (same as MIDI). A note value of 0 stops the
|
||||
sound playing on that channel. A note value 128-255 will trigger note-128 and immediately stop it (playing attack+release parts of envelope).
|
||||
|
||||
This function assumes the default setup, with the `sndGes` registers located at 0x00050.
|
||||
|
||||
### fn sndGes(sampleIndex: i32) -> f32
|
||||
|
||||
This implements a sound chip, generating sound based on 32 bytes of sound registers.
|
||||
|
||||
The spec of this sound chip are:
|
||||
|
||||
- 4 channels
|
||||
- rect, saw, tri, noise wave forms selectable per channel
|
||||
- each wave form supports some kind of pulse width modulation
|
||||
- each channel has an optional automatic low pass filter, or can be sent to one of two manually controllable filters
|
||||
- each channel can select between a narrow and a wide stereo positioning. The two stereo positions of each channel are fixed.
|
||||
|
||||
This function requires 1024 bytes of working memory, the first 32 bytes of which are interpreted as the sound registers.
|
||||
The base address of its working memory can be configured by writing the address to 0x12c78. It defaults to 0x00050.
|
||||
|
||||
Here is a short description of the 32 sound registers.
|
||||
|
||||
```
|
||||
Per channel:
|
||||
00 - CTRL0
|
||||
06 - CTRL1
|
||||
0c - CTRL2
|
||||
12 - CTRL3
|
||||
| 7 6 | 5 | 4 | 3 2 | 1 | 0 |
|
||||
| wave | ring | wide | filter | trigger | note on |
|
||||
|
||||
00 : CTRL - wave form, ring, sync, filter send, trigger
|
||||
bit 0: note on flag
|
||||
bit 1: note trigger
|
||||
bit 2,3: filter 0,1 send
|
||||
bit 6,7: wave form (rect, saw, tri, noise)
|
||||
01 : PULS - pulse width
|
||||
02 : FINE - fine tuning
|
||||
03 : NOTE - note
|
||||
04 : ENVA - attack, decay
|
||||
05 : ENVR - sustain, release
|
||||
note on: stay in decay/sustain part of envelope
|
||||
trigger: the attack part of the envlope is triggered when either this changes
|
||||
or note on is changed from 0 to 1.
|
||||
filter : 0 - no filter
|
||||
1 - fixed 6db 1-pole filter with cutoff two octaves above note
|
||||
2 - programmable filter 0
|
||||
3 - programmable filter 1
|
||||
wide : use wide stereo panning
|
||||
ring : ring modulate with triangle wave at frequency of previous channel
|
||||
wave : 0 - rectangle
|
||||
1 - saw
|
||||
2 - triangle
|
||||
3 - noise
|
||||
|
||||
50-56: channel 0
|
||||
56-5b: channel 1
|
||||
5c-61: channel 2
|
||||
62-67: channel 3
|
||||
01 - PULS0
|
||||
07 - PULS1
|
||||
0d - PULS2
|
||||
13 - PULS3
|
||||
Pulse width 0-255, with 0 being the plain version of each wave form.
|
||||
rectangle - 50%-100% pulse width
|
||||
saw - inverts 0%-100% of the saw wave form around the center
|
||||
triangle - morphs into an octave up triangle wave
|
||||
noise - blends into a decimated saw wave (just try it out)
|
||||
|
||||
68: VO01 - volumes channel 0&1
|
||||
69: VO23 - volumes channel 2&3
|
||||
02 - FINE0
|
||||
08 - FINE1
|
||||
0e - FINE2
|
||||
14 - FINE3
|
||||
Fractional note
|
||||
|
||||
6a : FCTR 0 - type, resonance
|
||||
6b : FCTR 1 - type, resonance
|
||||
6c : FFIN 0 - cutoff fine tuning
|
||||
6d : FNOT 0 - cutoff note
|
||||
6e : FFIN 1 - cutoff fine tuning
|
||||
6f : FNOT 1 - cutoff note
|
||||
03 - NOTE0
|
||||
09 - NOTE1
|
||||
0f - NOTE2
|
||||
15 - NOTE3
|
||||
Note, 69 = A4
|
||||
|
||||
04 - ENVA0
|
||||
0a - ENVA1
|
||||
10 - ENVA2
|
||||
16 - ENVA3
|
||||
| 7 6 5 4 | 3 2 1 0 |
|
||||
| decay | attack |
|
||||
|
||||
05 - ENVB0
|
||||
0b - ENVB1
|
||||
11 - ENVB2
|
||||
17 - ENVB3
|
||||
| 7 6 5 4 | 3 2 1 0 |
|
||||
| release | sustain |
|
||||
|
||||
18 - VO01
|
||||
| 7 6 5 4 | 3 2 1 0 |
|
||||
| volume 1 | volume 0 |
|
||||
|
||||
19 - VO23
|
||||
| 7 6 5 4 | 3 2 1 0 |
|
||||
| volume 3 | volume 2 |
|
||||
|
||||
1a - FCTR0
|
||||
1b - FCTR1
|
||||
| 7 6 5 4 | 3 | 2 | 1 | 0 |
|
||||
| resonance | 0 | band | high | low |
|
||||
|
||||
1c - FFIN0
|
||||
1e - FFIN1
|
||||
cutoff frequency - fractional note
|
||||
|
||||
1d - FNOT0
|
||||
1f - FNOT1
|
||||
cutoff frequency - note
|
||||
```
|
||||
|
||||
# The `uw8` tool
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
<!doctype html><html><head><meta charset="utf8"><title>uw8-run</title><style>body,canvas,html{background-color:#202024;margin:0;padding:0}body,html{height:100%}body{display:grid;grid-template-rows:1fr}#screen{align-self:center;border:4px solid #303040;image-rendering:pixelated;justify-self:center}#message{background-color:rgba(0,0,0,.4);color:#c64;font:700 12pt sans-serif;padding:8px;position:absolute;width:calc(100% - 16px);z-index:2}@media (min-width:648px) and (min-height:488px){#screen{height:480px;width:640px}}@media (min-width:968px) and (min-height:728px){#screen{height:720px;width:960px}}</style></head><body> <canvas id="screen" width="320" height="240" tabindex="1"> </canvas> <div id="message"></div> </body><script type="module">function e(e){return e&&e.__esModule?e.default:e}class t extends AudioWorkletNode{constructor(e){super(e,"apu",{outputChannelCount:[2]})}}let A=(...e)=>new Uint8Array(...e),a=(...e)=>new Uint32Array(...e);let s=function(s,B={}){B.setMessage||(B.setMessage=(e,t)=>{t&&console.log("error: "+t)});let r,o,n,i,l=s.getContext("2d"),d=l.createImageData(320,240),u=B.devkitMode,F=0,g=null==B.keyboardElement?s:B.keyboardElement;if(g){let e=e=>{let t,A="keydown"==e.type;switch(e.code){case"ArrowUp":t=1;break;case"ArrowDown":t=2;break;case"ArrowLeft":t=4;break;case"ArrowRight":t=8;break;case"KeyZ":t=16;break;case"KeyX":t=32;break;case"KeyA":t=64;break;case"KeyS":t=128;break;case"KeyR":A&&c(o,!0);break;case"F9":A&&s.toBlob((e=>{f(e,".png")})),e.preventDefault();break;case"F10":A&&function(){if(n)return n.stop(),void(n=null);n=new MediaRecorder(s.captureStream(),{mimeType:"video/webm",videoBitsPerSecond:25e6});let e=[];n.ondataavailable=t=>{e.push(t.data)};let t=B.timerElement;t&&(t.hidden=!1,t.innerText="00:00");function A(){if(i){if(t){let e=Math.floor((Date.now()-i)/1e3);t.innerText=Math.floor(e/60).toString().padStart(2,"0")+":"+(e%60).toString().padStart(2,"0")}setTimeout(A,1e3)}}n.onstop=()=>{t&&(t.hidden=!0),f(new Blob(e,{type:"video/webm"}),".webm")},n.start(),i=Date.now(),setTimeout(A,1e3)}(),e.preventDefault()}A?F|=t:F&=~t};g.onkeydown=e,g.onkeyup=e}async function c(n,i){r&&(r(),r=null);let g=new AudioContext({sampleRate:44100}),f=!0,Q=new AbortController;r=()=>{g.close(),f=!1,Q.abort()};let D=n.byteLength;if(B.setMessage(D),0==D)return;await g.audioWorklet.addModule(e("data:application/javascript,let%20e%3D%28...e%29%3D%3Enew%20Uint8Array%28...e%29%3Bclass%20t%20extends%20AudioWorkletProcessor%7Bconstructor%28%29%7Bsuper%28%29%2Cthis.sampleIndex%3D0%2Cthis.port.onmessage%3Dt%3D%3E%7Bif%28this.memory%29if%28isNaN%28t.data%29%29%7Blet%20s%3De%28t.data%29%3Be%28this.memory.buffer%2C80%2C32%29.set%28s%29%2Cs.set%28e%28this.memory.buffer%2C76928%2C32%29%29%2Cthis.port.postMessage%28t.data%29%7Delse%20this.startTime%3Dt.data%3Belse%20this.load%28t.data%5B0%5D%2Ct.data%5B1%5D%29%7D%7Dasync%20load%28e%2Ct%29%7Blet%20s%3Dnew%20WebAssembly.Memory%28%7Binitial%3A4%2Cmaximum%3A4%7D%29%2Ca%3D%7Benv%3A%7Bmemory%3As%7D%7D%3Bfor%28let%20e%20of%5B%22acos%22%2C%22asin%22%2C%22atan%22%2C%22atan2%22%2C%22cos%22%2C%22exp%22%2C%22log%22%2C%22sin%22%2C%22tan%22%2C%22pow%22%5D%29a.env%5Be%5D%3DMath%5Be%5D%3Bfor%28let%20e%3D9%3Be%3C64%3B%2B%2Be%29a.env%5B%22reserved%22%2Be%5D%3D%28%29%3D%3E%7B%7D%3Bfor%28let%20e%3D0%3Be%3C16%3B%2B%2Be%29a.env%5B%22g_reserved%22%2Be%5D%3D0%3Blet%20r%3Dasync%20e%3D%3E%28await%20WebAssembly.instantiate%28e%2Ca%29%29.instance%2Ci%3Dawait%20r%28e%29%3Bfor%28let%20e%20in%20i.exports%29a.env%5Be%5D%3Di.exports%5Be%5D%3Blet%20o%3Dawait%20r%28t%29%3Bthis.memory%3Ds%2Cthis.snd%3Do.exports.snd%7C%7Ci.exports.gesSnd%2Cthis.port.postMessage%282%29%7Dprocess%28e%2Ct%2Cs%29%7Bif%28this.snd%26%26this.startTime%29%7Bnew%20Uint32Array%28this.memory.buffer%29%5B16%5D%3DDate.now%28%29-this.startTime%3Blet%20e%3Dt%5B0%5D%2Cs%3Dthis.sampleIndex%2Ca%3De%5B0%5D.length%3Bfor%28let%20t%3D0%3Bt%3Ca%3B%2B%2Bt%29e%5B0%5D%5Bt%5D%3Dthis.snd%28s%2B%2B%29%2Ce%5B1%5D%5Bt%5D%3Dthis.snd%28s%2B%2B%29%3Bthis.sampleIndex%3D4294967295%26s%7Dreturn%210%7D%7DregisterProcessor%28%22apu%22%2Ct%29%3B"));let w,C=new t(g),E=0,I=new Promise((e=>w=e)),y=e=>{E|=e,3==E&&w&&(w(!0),w=null)},m=()=>{"suspended"==g.state?(B.startButton&&(B.startButton.style="",s.style="display:none"),(B.startButton||s).onclick=()=>{g.resume()}):(B.startButton&&(B.startButton.style="display:none",s.style=""),y(1))};g.onstatechange=m,m(),o=n;let h=window.location.pathname;if(D<=1024&&!i){let e="";for(let t of A(n))e+=String.fromCharCode(t);h+="#"+btoa(e),h!=window.location.pathname+window.location.hash&&history.pushState(null,null,h)}s.width=s.width;try{let t={initial:4};u||(t.maximum=4);let s,r=new WebAssembly.Memory(t),i=A(r.buffer),w={env:{memory:r}},E=e=>{if(s&&(!u||0!=A(e)[0])){i.set(A(e));let t=s.exports.load_uw8(e.byteLength);e=new ArrayBuffer(t),A(e).set(i.slice(0,t))}return e},m=async e=>(await WebAssembly.instantiate(e,w)).instance,h=async e=>E(await(await fetch(e)).arrayBuffer());s=await m(await h(e("data:application/wasm;base64,ALine truncated
|
||||
<!doctype html><html><head><meta charset="utf8"><title>uw8-run</title><style>body,canvas,html{background-color:#202024;margin:0;padding:0}body,html{height:100%}body{display:grid;grid-template-rows:1fr}#screen{align-self:center;border:4px solid #303040;image-rendering:pixelated;justify-self:center}#message{background-color:rgba(0,0,0,.4);color:#c64;font:700 12pt sans-serif;padding:8px;position:absolute;width:calc(100% - 16px);z-index:2}@media (min-width:648px) and (min-height:488px){#screen{height:480px;width:640px}}@media (min-width:968px) and (min-height:728px){#screen{height:720px;width:960px}}</style></head><body> <canvas id="screen" width="320" height="240" tabindex="1"> </canvas> <div id="message"></div> </body><script type="module">function e(e){return e&&e.__esModule?e.default:e}class t extends AudioWorkletNode{constructor(e){super(e,"apu",{outputChannelCount:[2]})}}let A=(...e)=>new Uint8Array(...e),a=(...e)=>new Uint32Array(...e);let B=function(B,s={}){s.setMessage||(s.setMessage=(e,t)=>{t&&console.log("error: "+t)});let r,o,n,i,l=B.getContext("2d"),d=l.createImageData(320,240),D=s.devkitMode,F=0,f=null==s.keyboardElement?B:s.keyboardElement;if(f){let e=e=>{let t,A="keydown"==e.type;switch(e.code){case"ArrowUp":t=1;break;case"ArrowDown":t=2;break;case"ArrowLeft":t=4;break;case"ArrowRight":t=8;break;case"KeyZ":t=16;break;case"KeyX":t=32;break;case"KeyA":t=64;break;case"KeyS":t=128;break;case"KeyR":A&&u(o,!0);break;case"F9":A&&B.toBlob((e=>{w(e,".png")})),e.preventDefault();break;case"F10":A&&function(){if(n)return n.stop(),void(n=null);n=new MediaRecorder(B.captureStream(),{mimeType:"video/webm",videoBitsPerSecond:25e6});let e=[];n.ondataavailable=t=>{e.push(t.data)};let t=s.timerElement;t&&(t.hidden=!1,t.innerText="00:00");function A(){if(i){if(t){let e=Math.floor((Date.now()-i)/1e3);t.innerText=Math.floor(e/60).toString().padStart(2,"0")+":"+(e%60).toString().padStart(2,"0")}setTimeout(A,1e3)}}n.onstop=()=>{t&&(t.hidden=!0),w(new Blob(e,{type:"video/webm"}),".webm")},n.start(),i=Date.now(),setTimeout(A,1e3)}(),e.preventDefault()}A?F|=t:F&=~t};f.onkeydown=e,f.onkeyup=e}async function u(n,i){r&&(r(),r=null);let f=new AudioContext({sampleRate:44100}),w=!0,c=new AbortController;r=()=>{f.close(),w=!1,c.abort()};let g=n.byteLength;if(s.setMessage(g),0==g)return;await f.audioWorklet.addModule(e("data:application/javascript,let%20e%3D%28...e%29%3D%3Enew%20Uint8Array%28...e%29%3Bclass%20t%20extends%20AudioWorkletProcessor%7Bconstructor%28%29%7Bsuper%28%29%2Cthis.sampleIndex%3D0%2Cthis.port.onmessage%3Dt%3D%3E%7Bif%28this.memory%29if%28isNaN%28t.data%29%29%7Blet%20s%3De%28t.data%29%3Be%28this.memory.buffer%2C80%2C32%29.set%28s%29%2Cs.set%28e%28this.memory.buffer%2C76928%2C32%29%29%2Cthis.port.postMessage%28t.data%29%7Delse%20this.startTime%3Dt.data%3Belse%20this.load%28t.data%5B0%5D%2Ct.data%5B1%5D%29%7D%7Dasync%20load%28e%2Ct%29%7Blet%20s%3Dnew%20WebAssembly.Memory%28%7Binitial%3A4%2Cmaximum%3A4%7D%29%2Ca%3D%7Benv%3A%7Bmemory%3As%7D%7D%3Bfor%28let%20e%20of%5B%22acos%22%2C%22asin%22%2C%22atan%22%2C%22atan2%22%2C%22cos%22%2C%22exp%22%2C%22log%22%2C%22sin%22%2C%22tan%22%2C%22pow%22%5D%29a.env%5Be%5D%3DMath%5Be%5D%3Bfor%28let%20e%3D9%3Be%3C64%3B%2B%2Be%29a.env%5B%22reserved%22%2Be%5D%3D%28%29%3D%3E%7B%7D%3Bfor%28let%20e%3D0%3Be%3C16%3B%2B%2Be%29a.env%5B%22g_reserved%22%2Be%5D%3D0%3Blet%20r%3Dasync%20e%3D%3E%28await%20WebAssembly.instantiate%28e%2Ca%29%29.instance%2Ci%3Dawait%20r%28e%29%3Bfor%28let%20e%20in%20i.exports%29a.env%5Be%5D%3Di.exports%5Be%5D%3Blet%20o%3Dawait%20r%28t%29%3Bthis.memory%3Ds%2Cthis.snd%3Do.exports.snd%7C%7Ci.exports.gesSnd%2Cthis.port.postMessage%282%29%7Dprocess%28e%2Ct%2Cs%29%7Bif%28this.snd%26%26this.startTime%29%7Bnew%20Uint32Array%28this.memory.buffer%29%5B16%5D%3DDate.now%28%29-this.startTime%3Blet%20e%3Dt%5B0%5D%2Cs%3Dthis.sampleIndex%2Ca%3De%5B0%5D.length%3Bfor%28let%20t%3D0%3Bt%3Ca%3B%2B%2Bt%29e%5B0%5D%5Bt%5D%3Dthis.snd%28s%2B%2B%29%2Ce%5B1%5D%5Bt%5D%3Dthis.snd%28s%2B%2B%29%3Bthis.sampleIndex%3D4294967295%26s%7Dreturn%210%7D%7DregisterProcessor%28%22apu%22%2Ct%29%3B"));let C,m=new t(f),Q=0,y=new Promise((e=>C=e)),E=e=>{Q|=e,3==Q&&C&&(C(!0),C=null)},b=()=>{"suspended"==f.state?(s.startButton&&(s.startButton.style="",B.style="display:none"),(s.startButton||B).onclick=()=>{f.resume()}):(s.startButton&&(s.startButton.style="display:none",B.style=""),E(1))};f.onstatechange=b,b(),o=n;let I=window.location.pathname;if(g<=1024&&!i){let e="";for(let t of A(n))e+=String.fromCharCode(t);I+="#"+btoa(e),I!=window.location.pathname+window.location.hash&&history.pushState(null,null,I)}B.width=B.width;try{let t={initial:4};D||(t.maximum=4);let B,r=new WebAssembly.Memory(t),i=A(r.buffer),C={env:{memory:r}},Q=e=>{if(B&&(!D||0!=A(e)[0])){i.set(A(e));let t=B.exports.load_uw8(e.byteLength);e=new ArrayBuffer(t),A(e).set(i.slice(0,t))}return e},b=async e=>(await WebAssembly.instantiate(e,C)).instance,I=async e=>Q(await(await fetch(e)).arrayBuffer());B=await b(await I(e("data:application/wasm;base64,ALine truncated
|
||||
Reference in new issue
Block a user