mirror of
https://github.com/exoticorn/microw8.git
synced 2026-11-13 23:17:19 +01:00
remove back-channel from audio thread for now
It needs some more thought before committing to it.
This commit is contained in:
1 parent
491bf88ade
commit
2bf8938183
5 files changed
+9
-35
No files matched your search
@@ -23,9 +23,7 @@ The memory has to be imported as `env` `memory` and has a maximum size of 256kb
|
|||||||
00070-00078: reserved
|
00070-00078: reserved
|
||||||
00078-12c78: frame buffer
|
00078-12c78: frame buffer
|
||||||
12c78-12c7c: sound registers/work area base address (for sndGes function)
|
12c78-12c7c: sound registers/work area base address (for sndGes function)
|
||||||
12c7c-12c80: reserved
|
12c7c-13000: reserved
|
||||||
12c80-12ca0: sound data (synced from sound thread)
|
|
||||||
12ca0-13000: reserved
|
|
||||||
13000-13400: palette
|
13000-13400: palette
|
||||||
13400-13c00: font
|
13400-13c00: font
|
||||||
13c00-14000: reserved
|
13c00-14000: reserved
|
||||||
@@ -283,8 +281,7 @@ MicroW8 actually runs two instances of your module. On the first instance, it ca
|
|||||||
second instance, it calls `snd` instead with an incrementing sample index and expects that function to return sound samples for the left and right
|
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.
|
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,
|
As the only means of communication, 32 bytes starting at address 0x00050 are copied from main to sound memory after `upd` returns.
|
||||||
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
|
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.
|
as sound registers. See the `sndGes` function for the meaning of those registers.
|
||||||
@@ -302,11 +299,12 @@ This implements a sound chip, generating sound based on 32 bytes of sound regist
|
|||||||
|
|
||||||
The spec of this sound chip are:
|
The spec of this sound chip are:
|
||||||
|
|
||||||
- 4 channels
|
- 4 channels with individual volume control (0-15)
|
||||||
- rect, saw, tri, noise wave forms selectable per channel
|
- rect, saw, tri, noise wave forms selectable per channel
|
||||||
- each wave form supports some kind of pulse width modulation
|
- 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 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.
|
- each channel can select between a narrow and a wide stereo positioning. The two stereo positions of each channel are fixed.
|
||||||
|
- optional ring modulation
|
||||||
|
|
||||||
This function requires 1024 bytes of working memory, the first 32 bytes of which are interpreted as the sound registers.
|
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.
|
The base address of its working memory can be configured by writing the address to 0x12c78. It defaults to 0x00050.
|
||||||
|
|||||||
+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 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
|
<!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),F=s.devkitMode,D=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?D|=t:D&=~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%7Bthis.memory%3FisNaN%28t.data%29%3Fe%28this.memory.buffer%2C80%2C32%29.set%28e%28t.data%29%29%3Athis.startTime%3Dt.data%3Athis.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%20n%3Dawait%20r%28t%29%3Bthis.memory%3Ds%2Cthis.snd%3Dn.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)},I=()=>{"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=I,I(),o=n;let b=window.location.pathname;if(g<=1024&&!i){let e="";for(let t of A(n))e+=String.fromCharCode(t);b+="#"+btoa(e),b!=window.location.pathname+window.location.hash&&history.pushState(null,null,b)}B.width=B.width;try{let t={initial:4};F||(t.maximum=4);let B,r=new WebAssembly.Memory(t),i=A(r.buffer),C={env:{memory:r}},Q=e=>{if(B&&(!F||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},I=async e=>(await WebAssembly.instantiate(e,C)).instance,b=async e=>Q(await(await fetch(e)).arrayBuffer());B=await I(await b(e("data:application/wasm;base64,AGFzbQEAAAABFQRgAX8Bf2ACf38Bf2ADf39%2FAGAAAAIPAQNlbnYGbWVtb3J5AgAEAwkIAAABAgEAAAMGEAN%2FAUEAC38BQQALfwFBAAsHGQIIbG9hZF91dzgAAAp1bmLine truncated
|
||||||
+2
-17
@@ -91,7 +91,6 @@ impl MicroW8 {
|
|||||||
struct Uw8Sound {
|
struct Uw8Sound {
|
||||||
stream: cpal::Stream,
|
stream: cpal::Stream,
|
||||||
tx: mpsc::SyncSender<[u8; 32]>,
|
tx: mpsc::SyncSender<[u8; 32]>,
|
||||||
rx: mpsc::Receiver<[u8; 32]>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl super::Runtime for MicroW8 {
|
impl super::Runtime for MicroW8 {
|
||||||
@@ -261,7 +260,6 @@ impl super::Runtime for MicroW8 {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let (tx, rx) = mpsc::sync_channel::<[u8; 32]>(1);
|
let (tx, rx) = mpsc::sync_channel::<[u8; 32]>(1);
|
||||||
let (back_tx, back_rx) = mpsc::sync_channel::<[u8; 32]>(2);
|
|
||||||
|
|
||||||
let start_time = Instant::now();
|
let start_time = Instant::now();
|
||||||
|
|
||||||
@@ -270,10 +268,8 @@ impl super::Runtime for MicroW8 {
|
|||||||
device.build_output_stream(
|
device.build_output_stream(
|
||||||
&config,
|
&config,
|
||||||
move |buffer: &mut [f32], _| {
|
move |buffer: &mut [f32], _| {
|
||||||
if let Ok(mut regs) = rx.try_recv() {
|
if let Ok(regs) = rx.try_recv() {
|
||||||
memory.write(&mut store, 80, ®s).unwrap();
|
memory.write(&mut store, 80, ®s).unwrap();
|
||||||
memory.read(&mut store, 0x12c80, &mut regs).unwrap();
|
|
||||||
back_tx.send(regs).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -293,11 +289,7 @@ impl super::Runtime for MicroW8 {
|
|||||||
)?
|
)?
|
||||||
};
|
};
|
||||||
|
|
||||||
Uw8Sound {
|
Uw8Sound { stream, tx }
|
||||||
stream,
|
|
||||||
tx,
|
|
||||||
rx: back_rx,
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
sound.stream.play()?;
|
sound.stream.play()?;
|
||||||
@@ -319,13 +311,6 @@ impl super::Runtime for MicroW8 {
|
|||||||
fn run_frame(&mut self) -> Result<()> {
|
fn run_frame(&mut self) -> Result<()> {
|
||||||
let mut result = Ok(());
|
let mut result = Ok(());
|
||||||
if let Some(mut instance) = self.instance.take() {
|
if let Some(mut instance) = self.instance.take() {
|
||||||
while let Ok(regs) = instance.sound.rx.try_recv() {
|
|
||||||
instance
|
|
||||||
.memory
|
|
||||||
.write(&mut instance.store, 0x12c80, ®s)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
let time = instance.start_time.elapsed().as_millis() as i32;
|
let time = instance.start_time.elapsed().as_millis() as i32;
|
||||||
let mut gamepad: u32 = 0;
|
let mut gamepad: u32 = 0;
|
||||||
|
|||||||
+1
-4
@@ -6,10 +6,7 @@ class APU extends AudioWorkletProcessor {
|
|||||||
this.port.onmessage = (ev) => {
|
this.port.onmessage = (ev) => {
|
||||||
if(this.memory) {
|
if(this.memory) {
|
||||||
if(isNaN(ev.data)) {
|
if(isNaN(ev.data)) {
|
||||||
let data = U8(ev.data);
|
U8(this.memory.buffer, 80, 32).set(U8(ev.data));
|
||||||
U8(this.memory.buffer, 80, 32).set(data);
|
|
||||||
data.set(U8(this.memory.buffer, 0x12c80, 32));
|
|
||||||
this.port.postMessage(ev.data);
|
|
||||||
} else {
|
} else {
|
||||||
this.startTime = ev.data;
|
this.startTime = ev.data;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-7
@@ -211,13 +211,7 @@ export default function MicroW8(screen, config = {}) {
|
|||||||
|
|
||||||
let platform_data = await loadModuleURL(platformUrl);
|
let platform_data = await loadModuleURL(platformUrl);
|
||||||
|
|
||||||
audioNode.port.onmessage = (e) => {
|
audioNode.port.onmessage = (e) => updateAudioReady(e.data);
|
||||||
if(isNaN(e.data)) {
|
|
||||||
U8(memory.buffer, 0x12c80, 32).set(U8(e.data));
|
|
||||||
} else {
|
|
||||||
updateAudioReady(e.data);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
audioNode.port.postMessage([platform_data, data]);
|
audioNode.port.postMessage([platform_data, data]);
|
||||||
|
|
||||||
let platform_instance = await instantiate(platform_data);
|
let platform_instance = await instantiate(platform_data);
|
||||||
|
|||||||
Reference in new issue
Block a user