Skip to content

Instantly share code, notes, and snippets.

@wader
Created April 11, 2017 18:54
Show Gist options
  • Save wader/a672163a31f98106e6580fb056246230 to your computer and use it in GitHub Desktop.
Save wader/a672163a31f98106e6580fb056246230 to your computer and use it in GitHub Desktop.
libchromaprint.js
#include <emscripten/emscripten.h>
#include "chromaprint.h"
extern "C" {
EMSCRIPTEN_KEEPALIVE
uintptr_t js_chromaprint_new(int algorithm) {
return (uintptr_t)chromaprint_new(algorithm);
}
EMSCRIPTEN_KEEPALIVE
int js_chromaprint_start(uintptr_t ctx, int sample_rate, int num_channels) {
return chromaprint_start((ChromaprintContextPrivate *)ctx, sample_rate, num_channels);
}
EMSCRIPTEN_KEEPALIVE
void js_chromaprint_free(uintptr_t ctx) {
chromaprint_free((ChromaprintContextPrivate *)ctx);
}
EMSCRIPTEN_KEEPALIVE
void js_chromaprint_dealloc(uintptr_t ptr) {
chromaprint_dealloc((void *)ptr);
}
EMSCRIPTEN_KEEPALIVE
int js_chromaprint_feed(uintptr_t ctx, uintptr_t data, int size) {
return chromaprint_feed((ChromaprintContextPrivate *)ctx, (int16_t *)data, size);
}
EMSCRIPTEN_KEEPALIVE
int js_chromaprint_finish(uintptr_t ctx) {
return chromaprint_finish((ChromaprintContextPrivate *)ctx);
}
EMSCRIPTEN_KEEPALIVE
uintptr_t js_chromaprint_fingerprint(uintptr_t ctx) {
char *fp = NULL;
chromaprint_get_fingerprint((ChromaprintContextPrivate *)ctx, &fp);
return (uintptr_t)fp;
}
}
# make sure chromaprint source is in $PWD/chromaprint
# after run use $PWD/chromaprint/libchromaprint.min.js
# does not use --bind and set NO_DYNAMIC_EXECUTION to generate code
# that don't use eval
CC = emcc
CXX = em++
CFLAGS = -O2 -DUSE_KISSFFT=1 -Isrc -Ivendor/kissfft
CXXFLAGS = -O2 -std=c++11 -DUSE_KISSFFT=1 -Isrc -Ivendor/kissfft
LDFLAGS = \
-s MODULARIZE=1 \
-s EXPORT_NAME="'libchromaprint'" \
-s TOTAL_MEMORY=100000000 \
-s NO_DYNAMIC_EXECUTION=1 \
-s NO_EXIT_RUNTIME=1 \
-s NO_FILESYSTEM=1 \
-s ELIMINATE_DUPLICATE_FUNCTIONS=1
OBJS = \
src/chromaprint.o \
src/audio_processor.o \
src/chroma.o \
src/chroma_resampler.o \
src/chroma_filter.o \
src/spectrum.o \
src/fft_lib_kissfft.o \
src/fft.o \
src/fingerprinter.o \
src/image_builder.o \
src/simhash.o \
src/silence_remover.o \
src/fingerprint_calculator.o \
src/fingerprint_compressor.o \
src/fingerprint_decompressor.o \
src/fingerprinter_configuration.o \
src/fingerprint_matcher.o \
src/utils/base64.o \
src/avresample/resample2.o \
vendor/kissfft/kiss_fft.o \
vendor/kissfft/tools/kiss_fftr.o \
jsbindings.o
all: libchromaprint.js libchromaprint.min.js
libchromaprint.js: $(OBJS)
$(CC) $(LDFLAGS) $(OBJS) -o $@
%.min.js: %.js
java -jar /emscripten/third_party/closure-compiler/compiler.jar \
--language_in ECMASCRIPT5 \
--js_output_file $@ $<
docker:
docker run \
--rm -ti \
-v "$(PWD)/chromaprint:/src" \
-v "$(PWD)/Makefile:/src/Makefile" \
-v "$(PWD)/jsbindings.cpp:/src/jsbindings.cpp" \
apiaryio/emcc make clean all
clean:
rm -f libchromaprint.js libchromaprint.min.js $(OBJS)
@wader
Copy link
Author

wader commented Apr 11, 2017

Usage:

var chromaprint = libchromaprint();
chromaprint._js_chromaprint_start(1, 44100, 2);
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment