Skip to content

Commit 44572c6

Browse files
Merge pull request #41 from JohnDog3112/dev
Fixes for milestone 2.5.0
2 parents a8192d8 + 91d4940 commit 44572c6

File tree

19 files changed

+440
-186
lines changed

19 files changed

+440
-186
lines changed

docs/api/api-c-audio.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void play() {
3939

4040
//creates a mon-audio channel buffer at our given SAMPLE_RATE
4141
// and square-wave data we generated
42-
long node_id = twr_audio_from_samples(1, SAMPLE_RATE, wave, length);
42+
long node_id = twr_audio_from_float_pcm(1, SAMPLE_RATE, wave, length);
4343

4444
//plays the square wave
4545
// Can be played multiple times, and is only freed on twr_audio_free
@@ -50,7 +50,7 @@ void play() {
5050
## Overview
5151
The Audio API is part a twr-wasm library that can be accessed via `#include "twr-audio.h"`. It has two main methods to play audio: from raw PCM data and from a URL.
5252

53-
Raw PCM data can be initialized via `twr_audio_from_samples` or `twr_audio_load`. `twr_audio_from_samples` takes in the PCM data as an array of floats between -1.0 and 1.0. `twr_audio_load`, on the other hand, reads the PCM data from an audio file that is specified by a URL. This method does not stream the audio, so it might take some time to read the file (depending how long the file is). Each function returns an integer `node_id` that identifies the loaded PCM data. Once the PCM data is initialized, it can be played via functions like `twr_audio_play`, `twr_audio_play_range`, `twr_audio_play_sync`, and `twr_audio_play_range_sync`. The play functions can be called multiple times for each audio ID.
53+
Raw PCM data can be initialized via `twr_audio_from_<type>_pcm` or `twr_audio_load`. There are multiple types for `twr_audio_from_<type>_pcm`. These types include Float, 8bit, 16bit, and 32bit. Float takes in values between -1.0 and 1.0. Meanwhile, the 8bit, 16bit, and 32bit versions take them in as signed numbers between their minimum and maximum values. `twr_audio_load`, on the other hand, reads the PCM data from an audio file that is specified by a URL. This method does not stream the audio, so it might take some time to read the file (depending how long the file is). Each function returns an integer `node_id` that identifies the loaded PCM data. Once the PCM data is initialized, it can be played via functions like `twr_audio_play`, `twr_audio_play_range`, `twr_audio_play_sync`, and `twr_audio_play_range_sync`. The play functions can be called multiple times for each audio ID.
5454

5555
You can also play audio directly from a URL. Unlike `twr_audio_load`, the url is initialized directly into an `HTMLAudioElement` which streams the audio and starts playback immediately.
5656

@@ -65,7 +65,15 @@ Functions that end in `_sync` are for use with `twrWamModuleAsync`, and are sync
6565
These are the current Audio APIs available in C/C++:
6666

6767
~~~c
68-
long twr_audio_from_samples(long num_channels, long sample_rate, float* data, long singleChannelDataLen);
68+
long twr_audio_from_float_pcm(long num_channels, long sample_rate, float* data, long singleChannelDataLen);
69+
long twr_audio_from_8bit_pcm(long number_channels, long sample_rate, char* data, long singleChannelDataLen);
70+
long twr_audio_from_16bit_pcm(long number_channels, long sample_rate, short* data, long singleChannelDataLen);
71+
long twr_audio_from_32bit_pcm(long number_channels, long sample_rate, int* data, long singleChannelDataLen);
72+
73+
float* twr_audio_get_float_pcm(long node_id, long* singleChannelDataLenPtr, long* numChannelsPtr);
74+
char* twr_audio_get_8bit_pcm(long node_id, long* singleChannelDataLenPtr, long* numChannelsPtr);
75+
short* twr_audio_get_16bit_pcm(long node_id, long* singleChannelDataLenPtr, long* numChannelsPtr);
76+
int* twr_audio_get_32bit_pcm(long node_id, long* singleChannelDataLenPtr, long* numChannelsPtr);
6977

7078
long twr_audio_play(long node_id);
7179
long twr_audio_play_volume(long node_id, double volume, double pan);
@@ -97,14 +105,13 @@ long twr_audio_play_range_sync_ex(long node_id, long start_sample, long end_samp
97105
long twr_audio_load_sync(char* url);
98106
long twr_audio_load(int event_id, char* url);
99107
long twr_audio_query_playback_position(long playback_id);
100-
float* twr_audio_get_samples(long node_id, long* singleChannelDataLenPtr, long* channelPtr);
101108
void twr_audio_free_id(long node_id);
102109

103110
void twr_audio_stop_playback(long playback_id);
104111

105-
void twr_audio_modify_playback_volume(long node_id, double volume);
106-
void twr_audio_modify_playback_pan(long node_id, double pan);
107-
void twr_audio_modify_playback_rate(long node_id, double sample_rate);
112+
void twr_audio_modify_playback_volume(long playback_id, double volume);
113+
void twr_audio_modify_playback_pan(long playback_id, double pan);
114+
void twr_audio_modify_playback_rate(long playback_id, double sample_rate);
108115

109116
long twr_audio_play_file(char* file_url);
110117
long twr_audio_play_file_ex(char* file_url, double volume, double playback_rate, int loop);
@@ -116,9 +123,5 @@ struct AudioMetadata {
116123
};
117124

118125
void twr_audio_get_metadata(long node_id, struct AudioMetadata* metadata);
119-
120-
float* twr_convert_8_bit_pcm(char* pcm, long pcm_len);
121-
float* twr_convert_16_bit_pcm(short* pcm, long pcm_len);
122-
float* twr_convert_32_bit_pcm(long* pcm, long pcm_len);
123126
~~~
124127

docs/api/api-c-d2d.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ void d2d_resettransform(struct d2d_draw_seq* ds);
197197
bool d2d_load_image(const char* url, long id);
198198
bool d2d_load_image_with_con(const char* url, long id, twr_ioconsole_t * con);
199199
void d2d_drawimage(struct d2d_draw_seq* ds, long id, double dx, double dy);
200+
void d2d_drawimage_ex(struct d2d_draw_seq* ds, long id, double sx, double sy, double sWidth, double sHeight, double dx, double dy, double dWidth, double dHeight);
200201
void d2d_getimagedata(struct d2d_draw_seq* ds, long id, double x, double y, double width, double height);
201202
unsigned long d2d_getimagedatasize(double width, double height);
202203
void d2d_imagedatatoc(struct d2d_draw_seq* ds, long id, void* buffer, unsigned long buffer_len);

docs/api/api-c-stdlib.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ void assert(int expression);
108108
double acos(double arg);
109109
double asin(double arg);
110110
double atan(double arg);
111+
double atan2(double y, double x);
111112
double ceil(double arg);
112113
double cos(double arg);
113114
double exp(double arg);

examples/pong/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jsEventsLib.js: jsEventsLib.ts
3636

3737
entry-point.wasm: $(OBJECTS)
3838
wasm-ld $(OBJECTS) ../../lib-c/twr.a -o entry-point.wasm \
39-
--no-entry --shared-memory --no-check-features --initial-memory=2031616 --max-memory=2031616
39+
--no-entry --initial-memory=2031616 --max-memory=2031616
4040

4141
entry-point-a.wasm: $(OBJECTS_ASYNC)
4242
wasm-ld $(OBJECTS_ASYNC) ../../lib-c/twr.a -o entry-point-a.wasm \

examples/pong/extra.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ float* generate_square_wave(double frequency, double duration, long sample_rate)
1515

1616
long load_square_wave(double frequency, double duration, long sample_rate) {
1717
float* wave = generate_square_wave(frequency, duration, sample_rate);
18-
long node_id = twr_audio_from_samples(1, sample_rate, wave, (long)ceil(duration * sample_rate));
18+
long node_id = twr_audio_from_float_pcm(1, sample_rate, wave, (long)ceil(duration * sample_rate));
1919
free(wave);
2020
return node_id;
2121
}

examples/pong/pong.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void Pong::render() {
8383
}
8484
void Pong::renderBackground() {
8585
#ifdef ASYNC
86-
this->canvas.drawImage(background_image_id, 0, 0, 0, 0, this->width, this->height);
86+
this->canvas.drawImage(background_image_id, 0, 0, this->width, this->height, 0, 0, 0, 0);
8787
#endif
8888

8989

@@ -261,6 +261,7 @@ void Pong::tick(long time) {
261261

262262
this->run_time += delta;
263263
}
264+
const double BALL_BOUNCE_VOL = 2.0;
264265
void Pong::tickBall(long delta) {
265266
double prev_y = this->ball_y;
266267
this->ball_x += this->ball_velocity_x * delta;
@@ -270,18 +271,18 @@ void Pong::tickBall(long delta) {
270271
if (this->ball_x <= this->border_width) { //left wall
271272
this->ball_x = this->border_width;
272273
this->ball_velocity_x *= -1;
273-
twr_audio_play(this->bounce_noise);
274+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
274275
} else if (this->ball_x >= this->width - this->ball_size - this->border_width) { //right wall
275276
this->ball_x = this->width - this->ball_size - this->border_width;
276277
this->ball_velocity_x *= -1;
277-
twr_audio_play(this->bounce_noise);
278+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
278279
}
279280

280281
//x and y are seperate checks for the corner case
281282
if (this->ball_y <= border_width) { //top wall
282283
this->ball_y = this->border_width;
283284
this->ball_velocity_y *= -1;
284-
twr_audio_play(this->bounce_noise);
285+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
285286
} else if (this->ball_y >= this->height - this->ball_size - this->border_width) { //bottom wall, lost game
286287
this->ball_y = this->height - this->ball_size - this->border_width - 1.0;
287288
twr_audio_play(this->lose_noise);
@@ -324,7 +325,7 @@ void Pong::tickBall(long delta) {
324325
//set score time
325326
this->score_time = this->last_timestamp;
326327
}
327-
twr_audio_play(this->bounce_noise);
328+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
328329
}
329330
}
330331
void Pong::tickPaddle(long delta) {

examples/pong/two-player-pong.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ T better_abs(T val) {
336336
}
337337

338338

339+
const double BALL_BOUNCE_VOL = 2.0;
339340
void paddleCollision(Ball& ball, double& n_x, double& n_y, Paddle& paddle, double paddle_x, long bounce_noise){
340341

341342
double paddle_middle = paddle.y + PADDLE_HEIGHT/2.0;
@@ -364,7 +365,7 @@ void paddleCollision(Ball& ball, double& n_x, double& n_y, Paddle& paddle, doubl
364365
ball.v_y = better_abs(ball.v_y);
365366
}
366367

367-
twr_audio_play(bounce_noise);
368+
twr_audio_play_volume(bounce_noise, BALL_BOUNCE_VOL, 0.0);
368369

369370
//add paddle velocity to ball
370371
double paddle_vel = get_paddle_vel(paddle);
@@ -396,15 +397,15 @@ void TwoPlayerPong::updateBall(double delta) {
396397
//interpolate to add the extra y back in th eopposite direction
397398
n_y = 0 - n_y;
398399
this->ball.v_y *= -1;
399-
twr_audio_play(this->bounce_noise);
400+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
400401
} else if (n_y > max_y) {
401402
//bounce off bottom wall by flipping y direction
402403
//interpolate to add the extra y back in the opposite direction
403404

404405
//max_y - (n_y - max_y) = max_y + max_y - n_y = 2*max_y - n_y
405406
n_y = 2*max_y - n_y;
406407
this->ball.v_y *= -1;
407-
twr_audio_play(this->bounce_noise);
408+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
408409
}
409410

410411
if (n_x < 0) { //hit left

examples/server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ def send_my_headers(self):
1010
self.send_header("Access-Control-Allow-Origin", "*")
1111
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
1212
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
13+
# Turn off caching
14+
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
15+
self.send_header("Pragma", "no-cache")
16+
self.send_header("Expires", "0")
1317

1418
if __name__ == '__main__':
1519
server.test(HandlerClass=MyHTTPRequestHandler)
16-
20+

0 commit comments

Comments
 (0)