Skip to content

Commit c498a84

Browse files
authored
Merge pull request #93 from yuchanns/audio-patch
feat(audio): use ScriptNodeProcessor for WASM
2 parents 92585fe + 76de66b commit c498a84

7 files changed

Lines changed: 45 additions & 32 deletions

File tree

.github/workflows/pages.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ jobs:
3838
soluna_path: "."
3939
debug: ${{ github.event.inputs.debug }}
4040

41-
# - name: Build WASM side module (sample)
42-
# uses: ./.github/actions/sample
43-
# id: sample
44-
# with:
45-
# soluna_path: "."
41+
- name: Build WASM side module (sample)
42+
uses: ./.github/actions/sample
43+
id: sample
44+
with:
45+
soluna_path: "."
4646

4747
- name: Install web build tools
4848
run: |
@@ -60,9 +60,9 @@ jobs:
6060
cp "${{ steps.build.outputs.SOLUNA_WASM_MAP_PATH }}" web/static/runtime/soluna.wasm.map
6161
fi
6262
63-
# - name: Copy WASM side module into runtime
64-
# run: |
65-
# cp "${{ steps.sample.outputs.SAMPLE_WASM_PATH }}" web/static/runtime/sample.wasm
63+
- name: Copy WASM side module into runtime
64+
run: |
65+
cp "${{ steps.sample.outputs.SAMPLE_WASM_PATH }}" web/static/runtime/sample.wasm
6666
6767
- name: Setup Hugo
6868
uses: peaceiris/actions-hugo@v3

make.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ lm:conf {
8585
flags = {
8686
"-Wall",
8787
"-pthread",
88+
"-fPIC",
8889
"--use-port=emdawnwebgpu",
89-
lm.mode == "debug" and "-fwasm-exceptions",
90+
"-fwasm-exceptions",
9091
},
9192
links = {
9293
"idbfs.js",
@@ -101,10 +102,8 @@ lm:conf {
101102
"-s USE_PTHREADS=1",
102103
"-s PTHREAD_POOL_SIZE='Math.max(2,navigator.hardwareConcurrency)'",
103104
"-s PTHREAD_POOL_SIZE_STRICT=2",
104-
"-s AUDIO_WORKLET=1",
105-
"-s WASM_WORKERS=1",
106-
"-s ASYNCIFY=1",
107-
lm.mode == "debug" and "-fwasm-exceptions",
105+
"-fwasm-exceptions",
106+
108107
lm.mode == "debug" and "-gsource-map",
109108
lm.mode == "debug" and "-s EXCEPTION_STACK_TRACES=1",
110109
lm.mode == "debug" and "-s ASSERTIONS=2",
@@ -115,7 +114,6 @@ lm:conf {
115114
defines = {
116115
"_POSIX_C_SOURCE=200809L",
117116
"_GNU_SOURCE",
118-
"MA_ENABLE_AUDIO_WORKLETS",
119117
},
120118
},
121119
defines = {
@@ -139,6 +137,13 @@ lm:import "clibs/soluna/make.lua"
139137

140138
lm:exe "soluna" {
141139
deps = deps,
140+
emcc = {
141+
ldflags = {
142+
"-s MAIN_MODULE=2",
143+
"-Wl,-u,emscripten_builtin_memalign",
144+
"-Wl,--export=emscripten_builtin_memalign",
145+
},
146+
},
142147
}
143148

144149
lm:dll "sample" {

script/build_web.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ local example_paths = exec_lines("find " .. shell_quote(soluna_dir .. "/test") .
310310
table.sort(example_paths)
311311
for _, path in ipairs(example_paths) do
312312
local name = path:match("([^/]+)%.lua$")
313-
if name and name ~= "extlua" then
313+
if name then
314314
examples[#examples + 1] = {
315315
id = name,
316316
title = titleize(name),

src/audio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ laudio_init(lua_State *L) {
168168
if (r != MA_SUCCESS) {
169169
return luaL_error(L, "ma_engine_init() error : %s", ma_result_description(r));
170170
}
171+
e->rm.config.decodedFormat = ma_format_f32;
172+
e->rm.config.decodedSampleRate = ma_engine_get_sample_rate(&e->engine);
171173
lua_pushlightuserdata(L, (void *)e);
172174

173175
return 2;

src/lualib/main.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ local init_func_temp = [=[
5151
return load(file.load(filename), "@"..filename)
5252
]=]
5353

54+
local api = {}
55+
5456
local function start(config)
5557
local boot = require "ltask.bootstrap"
5658
local mqueue = require "ltask.mqueue"
@@ -124,6 +126,7 @@ local function start(config)
124126
send_log_ud = logger_ud,
125127
mqueue = appmsg_queue,
126128
cleanup = function()
129+
api.deinit()
127130
while not send_message "cleanup" do end
128131
bootstrap.wait(ctx)
129132
mqueue.delete(appmsg_queue)
@@ -158,7 +161,6 @@ if args.cpath then
158161
package.cpath = args.cpath
159162
end
160163

161-
local api = {}
162164
local audio_device
163165

164166
function api.start(app)
@@ -226,5 +228,12 @@ function api.init(desc)
226228
audio.device, audio_device = audio.init()
227229
end
228230

229-
return api
231+
function api.deinit()
232+
if audio_device then
233+
local audio = require "soluna.audio"
234+
audio.deinit(audio_device)
235+
audio_device = nil
236+
end
237+
end
230238

239+
return api

src/service/audio.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ function S.fetch()
6262
end
6363

6464
function S.quit()
65-
if DEVICE then
66-
audio.deinit(DEVICE)
67-
end
6865
DEVICE = nil
6966
end
7067

web/assets/playframe.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@
261261
return;
262262
}
263263

264-
// let sampleWasmBuffer = null;
265-
// try {
266-
// sampleWasmBuffer = await loadBuffer(`${base}/runtime/sample.wasm`);
267-
// } catch (err) {
268-
// setStatus("Failed to load external module sample.wasm.");
269-
// setNote(err.message);
270-
// return;
271-
// }
264+
let sampleWasmBuffer = null;
265+
try {
266+
sampleWasmBuffer = await loadBuffer(`${base}/runtime/sample.wasm`);
267+
} catch (err) {
268+
setStatus("Failed to load external module sample.wasm.");
269+
setNote(err.message);
270+
return;
271+
}
272272

273273
setStatus("Preparing fonts...");
274274
let fontZip;
@@ -280,7 +280,7 @@
280280
return;
281281
}
282282

283-
const mainGame = "entry : main.lua\nhigh_dpi : true\ntext_sampler :\n min_filter : linear\n mag_filter : linear\n";
283+
const mainGame = "entry : main.lua\nhigh_dpi : true\ntext_sampler :\n min_filter : linear\n mag_filter : linear\nextlua_entry : extlua_init\nextlua_preload : sample\n";
284284
const mainLuaBytes = new TextEncoder().encode(sourceText);
285285
const mainGameBytes = new TextEncoder().encode(mainGame);
286286
const mainZip = createZip([
@@ -315,15 +315,15 @@
315315
Module.addRunDependency("asset-zip");
316316
Module.addRunDependency("main-zip");
317317
Module.addRunDependency("font-zip");
318-
// Module.addRunDependency("sample-wasm");
318+
Module.addRunDependency("sample-wasm");
319319
Module.FS.writeFile("/data/asset.zip", new Uint8Array(assetBuffer), { canOwn: true });
320320
Module.FS.writeFile("/data/main.zip", mainZip, { canOwn: true });
321321
Module.FS.writeFile("/data/font.zip", fontZip, { canOwn: true });
322-
// Module.FS.writeFile("/data/sample.wasm", new Uint8Array(sampleWasmBuffer), { canOwn: true });
322+
Module.FS.writeFile("/data/sample.wasm", new Uint8Array(sampleWasmBuffer), { canOwn: true });
323323
Module.removeRunDependency("asset-zip");
324324
Module.removeRunDependency("main-zip");
325325
Module.removeRunDependency("font-zip");
326-
// Module.removeRunDependency("sample-wasm");
326+
Module.removeRunDependency("sample-wasm");
327327
},
328328
],
329329
onAbort(reason) {

0 commit comments

Comments
 (0)