Skip to content

Commit

Permalink
backporting changes for tulip web into amy
Browse files Browse the repository at this point in the history
  • Loading branch information
bwhitman committed Nov 22, 2024
1 parent 66d3144 commit 8322abb
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 20 deletions.
15 changes: 6 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ EMSCRIPTEN_OPTIONS = -s WASM=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-sMODULARIZE -s 'EXPORT_NAME="amyModule"' \
-s EXPORTED_RUNTIME_METHODS="['cwrap','ccall']" \
-s EXPORTED_FUNCTIONS="['_amy_play_message', '_amy_reset_sysclock', '_amy_live_start', '_amy_start', '_malloc', '_free']"
-s EXPORTED_FUNCTIONS="['_amy_play_message', '_amy_reset_sysclock', '_amy_live_start', '_amy_start', '_sequencer_ticks', '_malloc', '_free']"

PYTHON = python3

Expand All @@ -43,12 +43,10 @@ all: default

SOURCES = src/algorithms.c src/amy.c src/envelope.c src/examples.c \
src/filters.c src/oscillators.c src/pcm.c src/partials.c src/custom.c \
src/delay.c src/log2_exp2.c src/patches.c src/transfer.c src/sequencer.c
src/delay.c src/log2_exp2.c src/patches.c src/transfer.c src/sequencer.c \
src/libminiaudio-audio.c

OBJECTS = $(patsubst %.c, %.o, src/algorithms.c src/amy.c src/envelope.c \
src/delay.c src/partials.c src/custom.c src/patches.c \
src/examples.c src/filters.c src/oscillators.c src/pcm.c src/log2_exp2.c \
src/libminiaudio-audio.c src/transfer.c src/sequencer.c)
OBJECTS = $(patsubst %.c, %.o, $(SOURCES))

HEADERS = $(wildcard src/*.h) src/amy_config.h
HEADERS_BUILD := $(filter-out src/patches.h,$(HEADERS))
Expand Down Expand Up @@ -85,9 +83,8 @@ timing: amy-module
cat /tmp/timings.txt | grep FILTER_PROCESS: | sed -e 's/us//' | sort -n | awk ' { a[i++]=$$4; } END { print a[int(i/2)]; }'
cat /tmp/timings.txt | grep PARAMETRIC_EQ_PROCESS: | sed -e 's/us//' | sort -n | awk ' { a[i++]=$$4; } END { print a[int(i/2)]; }'


web: $(TARGET)
emcc $(SOURCES) src/libminiaudio-audio.c $(EMSCRIPTEN_OPTIONS) -O3 -o docs/amy.js
docs/amy.js: $(TARGET)
emcc $(SOURCES) $(EMSCRIPTEN_OPTIONS) -O3 -o $@

clean:
-rm -f src/*.o
Expand Down
2 changes: 1 addition & 1 deletion docs/amy.js

Large diffs are not rendered by default.

Binary file modified docs/amy.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ if (TARGET tinyusb_device)
algorithms.c
amy.c
transfer.c
sequencer.c
delay.c
envelope.c
custom.c
Expand Down
4 changes: 2 additions & 2 deletions src/amy.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ int16_t amy_in_block[AMY_BLOCK_SIZE*AMY_NCHANS];
// Optional render hook that's called per oscillator during rendering
uint8_t (*amy_external_render_hook)(uint16_t, SAMPLE*, uint16_t len ) = NULL;

#ifndef malloc_caps
#ifndef MALLOC_CAPS_DEFINED
#define MALLOC_CAPS_DEFINED
void * malloc_caps(uint32_t size, uint32_t flags) {
#ifdef ESP_PLATFORM
//fprintf(stderr, "allocing size %ld flags %ld\n", size, flags);
Expand All @@ -139,7 +140,6 @@ void * malloc_caps(uint32_t size, uint32_t flags) {
#endif



// block -- what gets sent to the dac -- -32768...32767 (int16 le)
output_sample_type * block;
uint32_t total_samples;
Expand Down
9 changes: 4 additions & 5 deletions src/libminiaudio-audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ pthread_t amy_live_thread;

#ifdef __EMSCRIPTEN__
#include <emscripten.h>

extern void sequencer_check_and_fill();
void main_loop__em()
{
// We call repeatedly here to fill the sequencer, for webassembly (no threads)
sequencer_check_and_fill();
}
#endif


void amy_print_devices() {
ma_context context;
if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) {
Expand Down Expand Up @@ -71,9 +72,7 @@ void amy_print_devices() {

ma_context_uninit(&context);
}
#ifdef __EMSCRIPTEN__
//#include "console.h"
#endif


// I've seen frame counts as big as 1440, I think *8 is enough room (2048)
#define OUTPUT_RING_FRAMES (AMY_BLOCK_SIZE*8)
Expand Down
16 changes: 14 additions & 2 deletions src/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ esp_timer_handle_t periodic_timer;
#include <pthread.h>
#endif


#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

uint32_t sequencer_ticks() { return sequencer_tick_count; }

void sequencer_reset() {
// Remove all events
sequence_entry_ll_t **entry_ll_ptr = &sequence_entry_ll_start; // Start pointing to the root node.
Expand Down Expand Up @@ -83,7 +90,7 @@ uint8_t sequencer_add_event(struct event e, uint32_t tick, uint32_t period, uint
}


static void sequencer_check_and_fill() {
void sequencer_check_and_fill() {
// The while is in case the timer fires later than a tick; (on esp this would be due to SPI or wifi ops)
while(amy_sysclock() >= (next_amy_tick_us/1000)) {
sequencer_tick_count++;
Expand Down Expand Up @@ -119,7 +126,12 @@ static void sequencer_check_and_fill() {
entry_ll_ptr = &((*entry_ll_ptr)->next); // Update to point to the next field in the preceding list node.
}
}

// call the right hook:
#ifdef __EMSCRIPTEN__
EM_ASM({
amy_sequencer_js_hook($0);
}, sequencer_tick_count);
#endif
if(amy_external_sequencer_hook!=NULL) {
amy_external_sequencer_hook(sequencer_tick_count);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern uint32_t sequencer_tick_count ;
extern uint64_t next_amy_tick_us ;
extern uint32_t us_per_tick ;


uint32_t sequencer_ticks();
void sequencer_init();
void sequencer_recompute();
uint8_t sequencer_add_event(struct event e, uint32_t tick, uint32_t period, uint32_t tag);
Expand Down

0 comments on commit 8322abb

Please sign in to comment.