Skip to content

Commit fa98888

Browse files
committed
Separated shared code
The code that doesn't undergo a single change is now in its own file
1 parent a453ad5 commit fa98888

File tree

5 files changed

+103
-347
lines changed

5 files changed

+103
-347
lines changed

test/webaudio/audioworklet_2x_in_hard_pan.c

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,8 @@
1111
// This needs to be big enough for the stereo output, 2x mono inputs and the worker stack
1212
#define AUDIO_STACK_SIZE 3072
1313

14-
// Helper for MEMORY64 to cast an audio context or type to a void*
15-
#define WA_2_VOIDP(ctx) ((void*) (intptr_t) ctx)
16-
// Helper for MEMORY64 to cast a void* to an audio context or type
17-
#define VOIDP_2_WA(ptr) ((EMSCRIPTEN_WEBAUDIO_T) (intptr_t) ptr)
18-
19-
// Count the audio callbacks and return after 375 frames (1 second with the default 128 size)
20-
//
21-
// *** Remove this in your own code ***
22-
//
23-
volatile int audioProcessedCount = 0;
24-
bool playedAndMixed(double time, void* data) {
25-
if (audioProcessedCount >= 375) {
26-
emscripten_force_exit(0);
27-
return false;
28-
}
29-
return true;
30-
}
31-
32-
// ID to the beat and bass loops
33-
EMSCRIPTEN_WEBAUDIO_T beatID = 0;
34-
EMSCRIPTEN_WEBAUDIO_T bassID = 0;
35-
36-
// Creates a MediaElementAudioSourceNode with the supplied URL (which is
37-
// registered as an internal audio object and the ID returned).
38-
EM_JS(EMSCRIPTEN_WEBAUDIO_T, createTrack, (EMSCRIPTEN_WEBAUDIO_T ctxID, const char* url, bool looping), {
39-
var context = emscriptenGetAudioObject(ctxID);
40-
if (context) {
41-
var audio = document.createElement('audio');
42-
audio.src = UTF8ToString(url);
43-
audio.loop = looping;
44-
var track = context.createMediaElementSource(audio);
45-
return emscriptenRegisterAudioObject(track);
46-
}
47-
return 0;
48-
});
49-
50-
// Toggles the play/pause of a MediaElementAudioSourceNode given its ID
51-
EM_JS(void, toggleTrack, (EMSCRIPTEN_WEBAUDIO_T srcID), {
52-
var source = emscriptenGetAudioObject(srcID);
53-
if (source) {
54-
var audio = source.mediaElement;
55-
if (audio) {
56-
if (audio.paused) {
57-
audio.currentTime = 0;
58-
audio.play();
59-
} else {
60-
audio.pause();
61-
}
62-
}
63-
}
64-
});
14+
// Shared file playback and bootstrap
15+
#include "audioworklet_test_shared.inl"
6516

6617
// Callback to process and copy the audio tracks
6718
bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, AudioSampleFrame* outputs, int numParams, const AudioParamFrame* params, void* data) {
@@ -81,19 +32,6 @@ bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, Audi
8132
return true;
8233
}
8334

84-
// Registered click even to (1) enable audio playback and (2) toggle playing the tracks
85-
bool onClick(int type, const EmscriptenMouseEvent* e, void* data) {
86-
EMSCRIPTEN_WEBAUDIO_T ctx = VOIDP_2_WA(data);
87-
if (emscripten_audio_context_state(ctx) != AUDIO_CONTEXT_STATE_RUNNING) {
88-
printf("Resuming playback\n");
89-
emscripten_resume_audio_context_sync(ctx);
90-
}
91-
printf("Toggling audio playback\n");
92-
toggleTrack(beatID);
93-
toggleTrack(bassID);
94-
return false;
95-
}
96-
9735
// Audio processor created, now register the audio callback
9836
void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
9937
if (!success) {
@@ -130,25 +68,3 @@ void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
13068
// Register the counter that exits the test after one second of mixing
13169
emscripten_set_timeout_loop(&playedAndMixed, 16, NULL);
13270
}
133-
134-
// Worklet thread inited, now create the audio processor
135-
void initialised(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
136-
if (!success) {
137-
printf("Audio worklet failed to initialise\n");
138-
return;
139-
}
140-
printf("Audio worklet initialised\n");
141-
142-
WebAudioWorkletProcessorCreateOptions opts = {
143-
.name = "mixer",
144-
};
145-
emscripten_create_wasm_audio_worklet_processor_async(context, &opts, &processorCreated, NULL);
146-
}
147-
148-
int main() {
149-
static char workletStack[AUDIO_STACK_SIZE];
150-
EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(NULL);
151-
emscripten_start_wasm_audio_worklet_thread_async(context, workletStack, sizeof workletStack, &initialised, NULL);
152-
emscripten_runtime_keepalive_push();
153-
return 0;
154-
}

test/webaudio/audioworklet_2x_in_out_stereo.c

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,8 @@
1010
// This needs to be big enough for the 2x stereo outputs, 2x inputs and the worker stack
1111
#define AUDIO_STACK_SIZE 6144
1212

13-
// Helper for MEMORY64 to cast an audio context or type to a void*
14-
#define WA_2_VOIDP(ctx) ((void*) (intptr_t) ctx)
15-
// Helper for MEMORY64 to cast a void* to an audio context or type
16-
#define VOIDP_2_WA(ptr) ((EMSCRIPTEN_WEBAUDIO_T) (intptr_t) ptr)
17-
18-
// Count the audio callbacks and return after 375 frames (1 second with the default 128 size)
19-
//
20-
// *** Remove this in your own code ***
21-
//
22-
volatile int audioProcessedCount = 0;
23-
bool playedAndMixed(double time, void* data) {
24-
if (audioProcessedCount >= 375) {
25-
emscripten_force_exit(0);
26-
return false;
27-
}
28-
return true;
29-
}
30-
31-
// ID to the beat and bass loops
32-
EMSCRIPTEN_WEBAUDIO_T beatID = 0;
33-
EMSCRIPTEN_WEBAUDIO_T bassID = 0;
34-
35-
// Creates a MediaElementAudioSourceNode with the supplied URL (which is
36-
// registered as an internal audio object and the ID returned).
37-
EM_JS(EMSCRIPTEN_WEBAUDIO_T, createTrack, (EMSCRIPTEN_WEBAUDIO_T ctxID, const char* url, bool looping), {
38-
var context = emscriptenGetAudioObject(ctxID);
39-
if (context) {
40-
var audio = document.createElement('audio');
41-
audio.src = UTF8ToString(url);
42-
audio.loop = looping;
43-
var track = context.createMediaElementSource(audio);
44-
return emscriptenRegisterAudioObject(track);
45-
}
46-
return 0;
47-
});
48-
49-
// Toggles the play/pause of a MediaElementAudioSourceNode given its ID
50-
EM_JS(void, toggleTrack, (EMSCRIPTEN_WEBAUDIO_T srcID), {
51-
var source = emscriptenGetAudioObject(srcID);
52-
if (source) {
53-
var audio = source.mediaElement;
54-
if (audio) {
55-
if (audio.paused) {
56-
audio.currentTime = 0;
57-
audio.play();
58-
} else {
59-
audio.pause();
60-
}
61-
}
62-
}
63-
});
13+
// Shared file playback and bootstrap
14+
#include "audioworklet_test_shared.inl"
6415

6516
// Callback to process and copy the audio tracks
6617
bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, AudioSampleFrame* outputs, int numParams, const AudioParamFrame* params, void* data) {
@@ -81,19 +32,6 @@ bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, Audi
8132
return true;
8233
}
8334

84-
// Registered click even to (1) enable audio playback and (2) toggle playing the tracks
85-
bool onClick(int type, const EmscriptenMouseEvent* e, void* data) {
86-
EMSCRIPTEN_WEBAUDIO_T ctx = VOIDP_2_WA(data);
87-
if (emscripten_audio_context_state(ctx) != AUDIO_CONTEXT_STATE_RUNNING) {
88-
printf("Resuming playback\n");
89-
emscripten_resume_audio_context_sync(ctx);
90-
}
91-
printf("Toggling audio playback\n");
92-
toggleTrack(beatID);
93-
toggleTrack(bassID);
94-
return false;
95-
}
96-
9735
// Audio processor created, now register the audio callback
9836
void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
9937
if (!success) {
@@ -132,25 +70,3 @@ void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
13270
// Register the counter that exits the test after one second of mixing
13371
emscripten_set_timeout_loop(&playedAndMixed, 16, NULL);
13472
}
135-
136-
// Worklet thread inited, now create the audio processor
137-
void initialised(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
138-
if (!success) {
139-
printf("Audio worklet failed to initialise\n");
140-
return;
141-
}
142-
printf("Audio worklet initialised\n");
143-
144-
WebAudioWorkletProcessorCreateOptions opts = {
145-
.name = "mixer",
146-
};
147-
emscripten_create_wasm_audio_worklet_processor_async(context, &opts, &processorCreated, NULL);
148-
}
149-
150-
int main() {
151-
static char workletStack[AUDIO_STACK_SIZE];
152-
EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(NULL);
153-
emscripten_start_wasm_audio_worklet_thread_async(context, workletStack, sizeof workletStack, &initialised, NULL);
154-
emscripten_runtime_keepalive_push();
155-
return 0;
156-
}

test/webaudio/audioworklet_in_out_mono.c

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,8 @@
1111
// This needs to be big enough for the mono output, 2x inputs and the worker stack
1212
#define AUDIO_STACK_SIZE 2048
1313

14-
// Helper for MEMORY64 to cast an audio context or type to a void*
15-
#define WA_2_VOIDP(ctx) ((void*) (intptr_t) ctx)
16-
// Helper for MEMORY64 to cast a void* to an audio context or type
17-
#define VOIDP_2_WA(ptr) ((EMSCRIPTEN_WEBAUDIO_T) (intptr_t) ptr)
18-
19-
// Count the audio callbacks and return after 375 frames (1 second with the default 128 size)
20-
//
21-
// *** Remove this in your own code ***
22-
//
23-
volatile int audioProcessedCount = 0;
24-
bool playedAndMixed(double time, void* data) {
25-
if (audioProcessedCount >= 375) {
26-
emscripten_force_exit(0);
27-
return false;
28-
}
29-
return true;
30-
}
31-
32-
// ID to the beat and bass loops
33-
EMSCRIPTEN_WEBAUDIO_T beatID = 0;
34-
EMSCRIPTEN_WEBAUDIO_T bassID = 0;
35-
36-
// Creates a MediaElementAudioSourceNode with the supplied URL (which is
37-
// registered as an internal audio object and the ID returned).
38-
EM_JS(EMSCRIPTEN_WEBAUDIO_T, createTrack, (EMSCRIPTEN_WEBAUDIO_T ctxID, const char* url, bool looping), {
39-
var context = emscriptenGetAudioObject(ctxID);
40-
if (context) {
41-
var audio = document.createElement('audio');
42-
audio.src = UTF8ToString(url);
43-
audio.loop = looping;
44-
var track = context.createMediaElementSource(audio);
45-
return emscriptenRegisterAudioObject(track);
46-
}
47-
return 0;
48-
});
49-
50-
// Toggles the play/pause of a MediaElementAudioSourceNode given its ID
51-
EM_JS(void, toggleTrack, (EMSCRIPTEN_WEBAUDIO_T srcID), {
52-
var source = emscriptenGetAudioObject(srcID);
53-
if (source) {
54-
var audio = source.mediaElement;
55-
if (audio) {
56-
if (audio.paused) {
57-
audio.currentTime = 0;
58-
audio.play();
59-
} else {
60-
audio.pause();
61-
}
62-
}
63-
}
64-
});
14+
// Shared file playback and bootstrap
15+
#include "audioworklet_test_shared.inl"
6516

6617
// Callback to process and mix the audio tracks
6718
bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, AudioSampleFrame* outputs, int numParams, const AudioParamFrame* params, void* data) {
@@ -93,19 +44,6 @@ bool process(int numInputs, const AudioSampleFrame* inputs, int numOutputs, Audi
9344
return true;
9445
}
9546

96-
// Registered click even to (1) enable audio playback and (2) toggle playing the tracks
97-
bool onClick(int type, const EmscriptenMouseEvent* e, void* data) {
98-
EMSCRIPTEN_WEBAUDIO_T ctx = VOIDP_2_WA(data);
99-
if (emscripten_audio_context_state(ctx) != AUDIO_CONTEXT_STATE_RUNNING) {
100-
printf("Resuming playback\n");
101-
emscripten_resume_audio_context_sync(ctx);
102-
}
103-
printf("Toggling audio playback\n");
104-
toggleTrack(beatID);
105-
toggleTrack(bassID);
106-
return false;
107-
}
108-
10947
// Audio processor created, now register the audio callback
11048
void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
11149
if (!success) {
@@ -142,25 +80,3 @@ void processorCreated(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
14280
// Register the counter that exits the test after one second of mixing
14381
emscripten_set_timeout_loop(&playedAndMixed, 16, NULL);
14482
}
145-
146-
// Worklet thread inited, now create the audio processor
147-
void initialised(EMSCRIPTEN_WEBAUDIO_T context, bool success, void* data) {
148-
if (!success) {
149-
printf("Audio worklet failed to initialise\n");
150-
return;
151-
}
152-
printf("Audio worklet initialised\n");
153-
154-
WebAudioWorkletProcessorCreateOptions opts = {
155-
.name = "mixer",
156-
};
157-
emscripten_create_wasm_audio_worklet_processor_async(context, &opts, &processorCreated, NULL);
158-
}
159-
160-
int main() {
161-
static char workletStack[AUDIO_STACK_SIZE];
162-
EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(NULL);
163-
emscripten_start_wasm_audio_worklet_thread_async(context, workletStack, sizeof workletStack, &initialised, NULL);
164-
emscripten_runtime_keepalive_push();
165-
return 0;
166-
}

0 commit comments

Comments
 (0)