Skip to content

Commit

Permalink
Merge pull request #30 from JohnDog3112/dev2
Browse files Browse the repository at this point in the history
Audio Library
  • Loading branch information
twiddlingbits authored Sep 20, 2024
2 parents 6a0d49f + 5cc81fc commit 5ee8bf9
Show file tree
Hide file tree
Showing 19 changed files with 1,690 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/buildall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,9 @@ cd ../lib
$make clean
$make

cd ../tests-audio
$make clean
$make



2 changes: 2 additions & 0 deletions examples/buildbundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ cp multi-io/*.wasm dist/multi-io/
cp tests-d2d/*.wasm dist/tests-d2d
cp tests-d2d/*.jpg dist/tests-d2d
cp lib/*.wasm dist/lib/
cp tests-audio/*.wasm dist/tests-audio
cp tests-audio/*.mp3 dist/tests-audio
3 changes: 3 additions & 0 deletions examples/cleanall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ $make clean
cd ../tests-d2d
$make clean

cd ../tests-audio
$make clean

cd ..


Expand Down
5 changes: 5 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ <h1>WebAssembly C/C++ Examples<br>Using twr-wasm</h1>
<td>Unit tests for Draw 2D canvas console</td>
<td><a href="tests-d2d/index.html">run</a><br><a href="tests-d2d/index.html#async">run async</a></td>
</tr>
<tr>
<td>tests-audio</td>
<td>Unit tests for Audio Library. WARNING: Turn volume all the way down before running as some of the test sounds are very loud.</td>
<td><a href="tests-audio/index.html">run</a><br><a href="tests-audio/index.html#async">run async</a></td>
</tr>
</table>


Expand Down
32 changes: 32 additions & 0 deletions examples/tests-audio/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CC := clang
TWRCFLAGS := --target=wasm32 -nostdinc -nostdlib -isystem ../../include
CPPLIB := ../twr-cpp
CFLAGS := -c -Wall -O3 $(TWRCFLAGS) -I $(CPPLIB)
CFLAGS_DEBUG := -c -Wall -g -O0 $(TWRCFLAGS) -I $(CPPLIB)



.PHONY: default

default: tests-audio.wasm tests-audio-a.wasm clearIODiv.js

clearIODiv.js: index.html clearIODiv.ts
tsc

tests-audio.o: tests-audio.c
$(CC) $(CFLAGS) $< -o $@

tests-audio-a.o: tests-audio.c
$(CC) $(CFLAGS) $< -o $@ -DASYNC

tests-audio.wasm: tests-audio.o
wasm-ld tests-audio.o ../../lib-c/twr.a -o tests-audio.wasm \
--no-entry --initial-memory=4063232 --max-memory=4063232 \

tests-audio-a.wasm: tests-audio-a.o
wasm-ld tests-audio-a.o ../../lib-c/twr.a -o tests-audio-a.wasm \
--no-entry --shared-memory --no-check-features --initial-memory=4063232 --max-memory=4063232 \

clean:
rm -f *.o
rm -f *.wasm
29 changes: 29 additions & 0 deletions examples/tests-audio/clearIODiv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {IWasmModule, IWasmModuleAsync, twrLibrary, keyEventToCodePoint, TLibImports, twrLibraryInstanceRegistry} from "twr-wasm"

// Libraries use default export
export default class clearIODivLib extends twrLibrary {
id: number;

imports:TLibImports = {
clearIODiv: {},
};

// every library should have this line
libSourcePath = new URL(import.meta.url).pathname;

constructor() {
// all library constructors should start with these two lines
super();
this.id=twrLibraryInstanceRegistry.register(this);
}

clearIODiv(mod: IWasmModule|IWasmModuleAsync) {
const ioDiv = document.getElementById("twr_iodiv");
if (!ioDiv) throw new Error("clearIODiv couldn't find twr_iodiv!");

ioDiv.innerText = "";
}

}


Binary file added examples/tests-audio/croud.mp3
Binary file not shown.
81 changes: 81 additions & 0 deletions examples/tests-audio/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<!DOCTYPE html>
<html>
<head>
<title>Audio Tests</title>

<script type="importmap">
{
"imports": {
"twr-wasm": "../../lib-js/index.js"
}
}
</script>
</head>
<body>
<select id="test_select"></select>
<button id="test_button">Test</button>
<button id="test_all_button">Test All</button>
<div id="twr_iodiv"></div>
<canvas id="twr_d2dcanvas" width="600" height="600"></canvas>

<script type="module">
"use strict";
let test_select = document.getElementById("test_select");
let test_button = document.getElementById("test_button");
let test_all_button = document.getElementById("test_all_button");
let io = document.getElementById("twr_iodiv");

import {twrWasmModule, twrWasmModuleAsync} from "twr-wasm";
import clearIODivLib from "./out/clearIODiv.js";

// const timerLibInit = new timerLib();
const clearIODivLibInit = new clearIODivLib;

const is_async = window.location.hash=="#async";

const mod = is_async ? new twrWasmModuleAsync() : new twrWasmModule();
await mod.loadWasm(is_async ? "./tests-audio-a.wasm" : "./tests-audio.wasm");

const numTests = await mod.callC(["getNumTests"]);
console.log("tests: ", numTests);
for (let i = 0; i < numTests; i++) {
let option = document.createElement("OPTION");
const testName = mod.getString(await mod.callC(["getTestName", i]));
option.innerText = testName;
option.value = testName;
test_select.appendChild(option);
}

test_all_button.onclick = async () => {
await mod.callC(["testAll"]);
};

test_button.onclick = async () => {
await mod.callC(["testCase", test_select.selectedIndex]);
}

// const numTests = await mod.callC(["get_num_tests"])
// for (let i = 0; i < numTests; i++) {
// let option = document.createElement("OPTION");
// const testName = mod.getString(await mod.callC(["get_test_name", i]));
// option.innerText = testName;
// option.value = testName;
// test_select.appendChild(option);
// }

// await mod.callC(["test_all"]);

// test_all_button.onclick = async () => {
// io.innerHTML = "";
// await mod.callC(["test_all"]);
// };

// test_button.onclick = async () => {
// io.innerHTML = "";
// await mod.callC(["test_specific", test_select.selectedIndex]);
// }


</script>
</body>
</html>
9 changes: 9 additions & 0 deletions examples/tests-audio/out/clearIODiv.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IWasmModule, IWasmModuleAsync, twrLibrary, TLibImports } from "twr-wasm";
export default class clearIODivLib extends twrLibrary {
id: number;
imports: TLibImports;
libSourcePath: string;
constructor();
clearIODiv(mod: IWasmModule | IWasmModuleAsync): void;
}
//# sourceMappingURL=clearIODiv.d.ts.map
6 changes: 6 additions & 0 deletions examples/tests-audio/out/timerLib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IWasmModule, IWasmModuleAsync, twrLibrary, TLibImports } from "twr-wasm";
export default class timerLib extends twrLibrary {
imports: TLibImports;
setTimeout(mod: IWasmModule | IWasmModuleAsync, eventID: number, time: number, args: number): void;
}
//# sourceMappingURL=timerLib.d.ts.map
11 changes: 11 additions & 0 deletions examples/tests-audio/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"@parcel/resolver-default": {
"packageExports": true
},
"alias": {
"twr-wasm": "../../lib-js/index.js"
},
"dependencies": {
"twr-wasm": "^2.0.0"
}
}
Binary file added examples/tests-audio/ping.mp3
Binary file not shown.
Loading

0 comments on commit 5ee8bf9

Please sign in to comment.