mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-21 03:36:41 +01:00
Compare commits
4 Commits
9063e872d3
...
a8eb3bda27
| Author | SHA1 | Date | |
|---|---|---|---|
| a8eb3bda27 | |||
| 8b765a5742 | |||
| 7197c11586 | |||
| 99a423619e |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -499,7 +499,7 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "curlywas"
|
name = "curlywas"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/exoticorn/curlywas.git?rev=89638565#896385654ab2c089200920b6dea4abec641c88d6"
|
source = "git+https://github.com/exoticorn/curlywas.git?rev=557c3a84#557c3a842675a1ebd77e84021f9f5236d4fa5648"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"ariadne",
|
"ariadne",
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ anyhow = "1"
|
|||||||
minifb = { version = "0.20", default-features = false, features = ["x11"] }
|
minifb = { version = "0.20", default-features = false, features = ["x11"] }
|
||||||
notify = "4"
|
notify = "4"
|
||||||
pico-args = "0.4"
|
pico-args = "0.4"
|
||||||
curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "89638565" }
|
curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "557c3a84" }
|
||||||
wat = "1"
|
wat = "1"
|
||||||
uw8-tool = { path = "uw8-tool" }
|
uw8-tool = { path = "uw8-tool" }
|
||||||
same-file = "1"
|
same-file = "1"
|
||||||
|
|||||||
@@ -5,23 +5,34 @@
|
|||||||
include "../include/microw8-api.cwa"
|
include "../include/microw8-api.cwa"
|
||||||
|
|
||||||
fn melody(t: i32, T: i32) -> i32 {
|
fn melody(t: i32, T: i32) -> i32 {
|
||||||
let lazy b=(T&31)-16;
|
let lazy riff_pos = select(T & 16, (T&31) - 16, 16 - (T&31));
|
||||||
if b < 0 {
|
let lazy shift = ((1-((T>>5)&3))%2-1) as f32 / 6 as f32;
|
||||||
b = -b;
|
|
||||||
}
|
let inline note_count = select(T>>9, 4, 5);
|
||||||
let lazy K = 1.0 + ((1-((T>>5)&3))%2-1) as f32 / 6.0;
|
let inline octave = (riff_pos/5) as f32;
|
||||||
let inline arpeggio = ((t as f32 * pow(2.0, ((((0x85>>(t/2000%3*4)) & 15) - 1) as f32 / 12.0 + K))) as i32 & 128) * (!!(T>>8) * (12-T%12)) / 12;
|
let inline riff_note = 5514 >> (riff_pos % note_count * 4) & 15;
|
||||||
let inline melody = (t as f32 * pow(2.0, K+(b/5) as f32 - (((5514>>(b%select(T>>9, 4, 5)*4)&15) as f32) / 12.0))) as i32 & 128;
|
let inline melody_freq = pow(2 as f32, shift + octave - ((riff_note as f32) / 12 as f32));
|
||||||
arpeggio +
|
let inline melody = (t as f32 * melody_freq) as i32 & 128;
|
||||||
melody
|
|
||||||
|
let inline arp_note = ((0x85>>((t>>12)%3*4)) & 15) - 1;
|
||||||
|
let inline arp_freq = pow(2 as f32, shift + (arp_note as f32 / 12 as f32));
|
||||||
|
let inline arp_vol = (T >= 256) * (12-T%12);
|
||||||
|
let inline arpeggio = ((t as f32 * arp_freq) as i32 & 128) * arp_vol / 12;
|
||||||
|
|
||||||
|
melody + arpeggio
|
||||||
}
|
}
|
||||||
|
|
||||||
export fn snd(sample: i32) -> f32 {
|
export fn snd(t: i32) -> f32 {
|
||||||
let lazy t = sample / 2;
|
let lazy T = t/10000;
|
||||||
let lazy T = t/5000;
|
|
||||||
let inline mel_arp = melody(t, T)/3 + melody(t, T-3)/5;
|
let inline mel_arp = melody(t, T)/3 + melody(t, T-3)/5;
|
||||||
let inline bass = !!(T>>7) * ((t as f32 * pow(2.0, ((!!(T & 4) * ((T & 7) - 1)) as f32 / 6.0 - 4.0))) as i32 & 63) * ((197 >> (T % 8)) & 1);
|
|
||||||
|
let inline bass_vol = (T >= 128) & (197 >> (T % 8));
|
||||||
|
let inline bass_freq = pow(2 as f32, (((T & 4) * ((T & 7) - 1)) as f32 / 24 as f32 - 5 as f32));
|
||||||
|
let inline bass = ((t as f32 * bass_freq) as i32 & 63) * bass_vol;
|
||||||
|
|
||||||
let inline snare_ish = (random() & 31) * (8 - (T + 4) % 8) / 8;
|
let inline snare_ish = (random() & 31) * (8 - (T + 4) % 8) / 8;
|
||||||
|
|
||||||
let inline sample = mel_arp + bass + snare_ish;
|
let inline sample = mel_arp + bass + snare_ish;
|
||||||
(sample & 255) as f32 / 255 as f32 - 0.5
|
(sample & 255) as f32 / 255 as f32
|
||||||
}
|
}
|
||||||
|
|||||||
2
platform/Cargo.lock
generated
2
platform/Cargo.lock
generated
@@ -146,7 +146,7 @@ checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "curlywas"
|
name = "curlywas"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/exoticorn/curlywas.git?rev=89638565#896385654ab2c089200920b6dea4abec641c88d6"
|
source = "git+https://github.com/exoticorn/curlywas.git?rev=557c3a84#557c3a842675a1ebd77e84021f9f5236d4fa5648"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"ariadne",
|
"ariadne",
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
curlywas = { git="https://github.com/exoticorn/curlywas.git", rev="89638565" }
|
curlywas = { git="https://github.com/exoticorn/curlywas.git", rev="557c3a84" }
|
||||||
uw8-tool = { path="../uw8-tool" }
|
uw8-tool = { path="../uw8-tool" }
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
lodepng = "3.4"
|
lodepng = "3.4"
|
||||||
@@ -11,16 +11,16 @@ fn main() -> Result<()> {
|
|||||||
convert_font()?;
|
convert_font()?;
|
||||||
|
|
||||||
println!("Compiling loader module");
|
println!("Compiling loader module");
|
||||||
let loader = curlywas::compile_file("src/loader.cwa", curlywas::Options::default())?;
|
let loader = curlywas::compile_file("src/loader.cwa", curlywas::Options::default()).0?;
|
||||||
File::create("bin/loader.wasm")?.write_all(&loader.wasm)?;
|
File::create("bin/loader.wasm")?.write_all(&loader)?;
|
||||||
|
|
||||||
println!("Loader (including base module): {} bytes", loader.wasm.len());
|
println!("Loader (including base module): {} bytes", loader.len());
|
||||||
|
|
||||||
println!("Compiling platform module");
|
println!("Compiling platform module");
|
||||||
let platform = curlywas::compile_file("src/platform.cwa", curlywas::Options::default())?;
|
let platform = curlywas::compile_file("src/platform.cwa", curlywas::Options::default()).0?;
|
||||||
println!("Compressing platform module");
|
println!("Compressing platform module");
|
||||||
let platform = uw8_tool::pack(
|
let platform = uw8_tool::pack(
|
||||||
&platform.wasm,
|
&platform,
|
||||||
&uw8_tool::PackConfig::default().with_compression_level(4),
|
&uw8_tool::PackConfig::default().with_compression_level(4),
|
||||||
)?;
|
)?;
|
||||||
File::create("bin/platform.uw8")?.write_all(&platform)?;
|
File::create("bin/platform.uw8")?.write_all(&platform)?;
|
||||||
|
|||||||
106
src/main.rs
106
src/main.rs
@@ -101,19 +101,16 @@ fn run(mut args: Arguments) -> Result<()> {
|
|||||||
|
|
||||||
while runtime.is_open() {
|
while runtime.is_open() {
|
||||||
if first_run || watcher.poll_changed_file()?.is_some() {
|
if first_run || watcher.poll_changed_file()?.is_some() {
|
||||||
match start_cart(&filename, &mut *runtime, &config) {
|
let (result, dependencies) = start_cart(&filename, &mut *runtime, &config);
|
||||||
Ok(dependencies) => {
|
if watch_mode {
|
||||||
if watch_mode {
|
for dep in dependencies {
|
||||||
for dep in dependencies {
|
watcher.add_file(dep)?;
|
||||||
watcher.add_file(dep)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(err) => {
|
}
|
||||||
eprintln!("Load error: {}", err);
|
if let Err(err) = result {
|
||||||
if !watch_mode {
|
eprintln!("Load error: {}", err);
|
||||||
exit(1);
|
if !watch_mode {
|
||||||
}
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
first_run = false;
|
first_run = false;
|
||||||
@@ -136,37 +133,45 @@ struct Config {
|
|||||||
output_path: Option<PathBuf>,
|
output_path: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_cart(filename: &Path, config: &Config) -> Result<(Vec<u8>, Vec<PathBuf>)> {
|
fn load_cart(filename: &Path, config: &Config) -> (Result<Vec<u8>>, Vec<PathBuf>) {
|
||||||
let mut dependencies = Vec::new();
|
let mut dependencies = Vec::new();
|
||||||
let mut cart = match SourceType::of_file(filename)? {
|
fn inner(filename: &Path, config: &Config, dependencies: &mut Vec<PathBuf>) -> Result<Vec<u8>> {
|
||||||
SourceType::Binary => {
|
let mut cart = match SourceType::of_file(filename)? {
|
||||||
let mut cart = vec![];
|
SourceType::Binary => {
|
||||||
File::open(filename)?.read_to_end(&mut cart)?;
|
let mut cart = vec![];
|
||||||
dependencies.push(filename.to_path_buf());
|
File::open(filename)?.read_to_end(&mut cart)?;
|
||||||
cart
|
cart
|
||||||
}
|
}
|
||||||
SourceType::Wat => {
|
SourceType::Wat => {
|
||||||
let cart = wat::parse_file(filename)?;
|
let cart = wat::parse_file(filename)?;
|
||||||
dependencies.push(filename.to_path_buf());
|
cart
|
||||||
cart
|
}
|
||||||
}
|
SourceType::CurlyWas => {
|
||||||
SourceType::CurlyWas => {
|
let (module, deps) = curlywas::compile_file(filename, curlywas::Options::default());
|
||||||
let module = curlywas::compile_file(filename, curlywas::Options::default())?;
|
*dependencies = deps;
|
||||||
dependencies = module.dependencies;
|
module?
|
||||||
module.wasm
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(ref pack_config) = config.pack {
|
if let Some(ref pack_config) = config.pack {
|
||||||
cart = uw8_tool::pack(&cart, pack_config)?;
|
cart = uw8_tool::pack(&cart, pack_config)?;
|
||||||
println!("packed size: {} bytes", cart.len());
|
println!("packed size: {} bytes", cart.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref path) = config.output_path {
|
||||||
|
File::create(path)?.write_all(&cart)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(cart)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref path) = config.output_path {
|
let result = inner(filename, config, &mut dependencies);
|
||||||
File::create(path)?.write_all(&cart)?;
|
|
||||||
|
if dependencies.is_empty() {
|
||||||
|
dependencies.push(filename.to_path_buf());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((cart, dependencies))
|
(result, dependencies)
|
||||||
}
|
}
|
||||||
|
|
||||||
enum SourceType {
|
enum SourceType {
|
||||||
@@ -205,14 +210,22 @@ impl SourceType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "native", feature = "browser"))]
|
#[cfg(any(feature = "native", feature = "browser"))]
|
||||||
fn start_cart(filename: &Path, runtime: &mut dyn Runtime, config: &Config) -> Result<Vec<PathBuf>> {
|
fn start_cart(
|
||||||
let cart = load_cart(filename, config)?;
|
filename: &Path,
|
||||||
|
runtime: &mut dyn Runtime,
|
||||||
|
config: &Config,
|
||||||
|
) -> (Result<()>, Vec<PathBuf>) {
|
||||||
|
let (cart, dependencies) = load_cart(filename, config);
|
||||||
|
let cart = match cart {
|
||||||
|
Ok(cart) => cart,
|
||||||
|
Err(err) => return (Err(err), dependencies),
|
||||||
|
};
|
||||||
|
|
||||||
if let Err(err) = runtime.load(&cart.0) {
|
if let Err(err) = runtime.load(&cart) {
|
||||||
eprintln!("Load error: {}", err);
|
eprintln!("Load error: {}", err);
|
||||||
Err(err)
|
(Err(err), dependencies)
|
||||||
} else {
|
} else {
|
||||||
Ok(cart.1)
|
(Ok(()), dependencies)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,13 +244,14 @@ fn pack(mut args: Arguments) -> Result<()> {
|
|||||||
|
|
||||||
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
|
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
|
||||||
|
|
||||||
let (cart, _) = load_cart(
|
let cart = load_cart(
|
||||||
&in_file,
|
&in_file,
|
||||||
&Config {
|
&Config {
|
||||||
pack: Some(pack_config),
|
pack: Some(pack_config),
|
||||||
output_path: None,
|
output_path: None,
|
||||||
},
|
},
|
||||||
)?;
|
)
|
||||||
|
.0?;
|
||||||
|
|
||||||
File::create(out_file)?.write_all(&cart)?;
|
File::create(out_file)?.write_all(&cart)?;
|
||||||
|
|
||||||
@@ -260,8 +274,8 @@ fn compile(mut args: Arguments) -> Result<()> {
|
|||||||
let in_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
|
let in_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
|
||||||
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
|
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
|
||||||
|
|
||||||
let module = curlywas::compile_file(in_file, options)?;
|
let module = curlywas::compile_file(in_file, options).0?;
|
||||||
File::create(out_file)?.write_all(&module.wasm)?;
|
File::create(out_file)?.write_all(&module)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@
|
|||||||
<div id="centered">
|
<div id="centered">
|
||||||
<canvas id="screen" width="320" height="240">
|
<canvas id="screen" width="320" height="240">
|
||||||
</canvas>
|
</canvas>
|
||||||
<div id="timer" hidden="true"></div>
|
<div id="timer" hidden="true"></div>
|
||||||
<div id="message"></div>
|
<div id="message"></div>
|
||||||
<button id="cartButton" style="visibility:hidden">Load cart...</button>
|
<button id="cartButton" style="visibility:hidden">Load cart...</button>
|
||||||
</div>
|
</div>
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
|
|||||||
@@ -79,7 +79,9 @@ if(location.hash.length != 0) {
|
|||||||
url += 'cart.uw8';
|
url += 'cart.uw8';
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await uw8.runModuleFromURL(url, true);
|
if(!await uw8.runModuleFromURL(url, true)) {
|
||||||
|
setupLoad();
|
||||||
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
setupLoad();
|
setupLoad();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -326,10 +326,11 @@ export default function MicroW8(screen, config = {}) {
|
|||||||
async function runModuleFromURL(url, keepUrl) {
|
async function runModuleFromURL(url, keepUrl) {
|
||||||
let response = await fetch(url);
|
let response = await fetch(url);
|
||||||
let type = response.headers.get('Content-Type');
|
let type = response.headers.get('Content-Type');
|
||||||
if(type && type.includes('html')) {
|
if((type && type.includes('html')) || response.status != 200) {
|
||||||
throw false;
|
return false;
|
||||||
}
|
}
|
||||||
runModule(await response.arrayBuffer(), keepUrl || devkitMode);
|
runModule(await response.arrayBuffer(), keepUrl || devkitMode);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user