Skip to content

Commit 23284b7

Browse files
author
ByteSizedFox
committed
minor WASM improvements
1 parent fffc7bb commit 23284b7

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

wasm/compile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
emcc main.c ../src/*.c -I ../include -o DECtalkMini.js \
22
-s WASM=1 \
3-
-s EXPORTED_FUNCTIONS='["_tts_init","_tts_speak","_tts_get_buffer","_tts_get_buffer_length","_tts_reset","_tts_get_last_phone","_malloc","_free"]' \
3+
-s EXPORTED_FUNCTIONS='["_tts_init","_tts_speak","_tts_get_buffer","_tts_get_buffer_length","_tts_reset","_malloc","_free"]' \
44
-s EXPORTED_RUNTIME_METHODS='["cwrap","ccall","getValue","setValue","UTF8ToString","stringToUTF8","HEAP16"]' \
55
-s ALLOW_MEMORY_GROWTH=1 \
66
-s INITIAL_MEMORY=33554432 \

wasm/index.html

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ <h1>DECtalkMini</h1>
8686

8787
<script>
8888
let Module = null;
89-
let tts_init, tts_speak, tts_get_buffer, tts_get_buffer_length, tts_reset, tts_get_last_phone;
89+
let tts_init, tts_speak, tts_get_buffer, tts_get_buffer_length, tts_reset;
9090
let audioContext = null;
9191
let currentSource = null;
9292
let currentAudioBuffer = null;
@@ -142,13 +142,13 @@ <h1>DECtalkMini</h1>
142142
});
143143
}
144144

145-
window.onPhoneCallback = function(phone) {
146-
const currentSampleCount = Module.cwrap('tts_get_buffer_length', 'number', [])();
145+
window.onPhoneCallback = function(phoneme) {
146+
const currentSampleCount = tts_get_buffer_length();
147147
const timeInSeconds = currentSampleCount / currentSampleRate;
148-
const phonemeName = phonemes[phone] || 'UNKNOWN';
148+
const phonemeName = phonemes[phoneme] || 'UNKNOWN';
149149

150150
phonemeTimeline.push({
151-
phone: phone,
151+
phone: phoneme,
152152
phonemeName: phonemeName,
153153
time: timeInSeconds
154154
});
@@ -172,7 +172,6 @@ <h1>DECtalkMini</h1>
172172
tts_get_buffer = Module.cwrap('tts_get_buffer', 'number', []);
173173
tts_get_buffer_length = Module.cwrap('tts_get_buffer_length', 'number', []);
174174
tts_reset = Module.cwrap('tts_reset', 'number', []);
175-
tts_get_last_phone = Module.cwrap('tts_get_last_phone', 'number', []);
176175

177176
tts_init();
178177
audioContext = new (window.AudioContext || window.webkitAudioContext)();

wasm/main.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "epsonapi.h"
33
#include <emscripten.h>
44
#include <string.h>
5-
#include <stdio.h>
65

76
#define MAX_BUFFER_SIZE 65536
87
static short audio_buffer[MAX_BUFFER_SIZE];
@@ -11,20 +10,22 @@ static int sample_rate = 11025;
1110

1211
extern int last_phoneme;
1312

14-
// Declare JS function that will be called from C
15-
EM_JS(void, js_phone_callback, (int phone), {
13+
// JavaScript callback that we'll call
14+
EM_JS(void, js_audio_callback, (short* data, int length, int phoneme, int buffer_position), {
1615
if (window.onPhoneCallback) {
17-
window.onPhoneCallback(phone);
16+
window.onPhoneCallback(phoneme);
1817
}
1918
});
2019

2120
EMSCRIPTEN_KEEPALIVE
2221
short* audio_callback(short *data, long length) {
23-
//printf("last_phone value: %d\n", last_phoneme); // Should be 0-48
24-
// Call JavaScript callback with current phone value
25-
js_phone_callback(last_phoneme & 0x00FF);
22+
// Get current phoneme and pass it to JavaScript along with audio data
23+
int current_phoneme = last_phoneme & 0x00FF;
2624

27-
// Copy samples from callback into our buffer
25+
// Call JavaScript callback with phoneme info
26+
js_audio_callback(data, length, current_phoneme, buffer_index);
27+
28+
// Copy samples to buffer
2829
for (int i = 0; i < length && buffer_index < MAX_BUFFER_SIZE; i++) {
2930
audio_buffer[buffer_index++] = data[i];
3031
}
@@ -58,11 +59,6 @@ int tts_get_buffer_length() {
5859
return buffer_index;
5960
}
6061

61-
EMSCRIPTEN_KEEPALIVE
62-
int tts_get_last_phone() {
63-
return last_phoneme;
64-
}
65-
6662
EMSCRIPTEN_KEEPALIVE
6763
int tts_reset() {
6864
buffer_index = 0;

0 commit comments

Comments
 (0)