Commit 2cafb6fe authored by Daniel Clarke's avatar Daniel Clarke

changed how maximJs is extracted from Module. Added maxiArray class as way to...

changed how maximJs is extracted from Module. Added maxiArray class as way to avoid using vector.get function
parent 1231b4c2
# change '../../em++' to path to em++ in emscriten folder file
# change '../../em++' to path to em++ in emscriten folder
CC=../../em++
# POST_JS is all the external js stuff that handles web audio etc
......@@ -10,9 +10,10 @@ SOURCE=src/cpp/maxi_emscr.cpp
# destination .js file
OUTPUT=maxiLib/maxiLib.js
EXP_FUNCS=-s EXPORTED_FUNCTIONS="['_sumArray']"
# https://kripken.github.io/emscripten-site/docs/tools_reference/emcc.html
CFLAGS=-O2 -s DISABLE_EXCEPTION_CATCHING=0 -s ALLOW_MEMORY_GROWTH=1 --memory-init-file 0 --profiling
CFLAGS=-O2 $(EXP_FUNCS) -s DISABLE_EXCEPTION_CATCHING=0 -s ALLOW_MEMORY_GROWTH=1 --memory-init-file 0 --profiling
all:
......
This diff is collapsed.
......@@ -52,6 +52,9 @@ var myOscOutput,myCurrentVolume;//
// var myEnvelopeData = new VectorDouble();//this data will be used to make an envelope. Value and time to value in ms.
var myEnvelope = new maximJs.maxiEnvelope();
// unfortunately, '.get(n)' must be used to access vector data
// or the data can be used as an array and the GetArray... function used when required
var myEnvelopeData = GetArrayAsVectorDbl([1,0,0,500]);//this data will be used to make an envelope. Value and time to value in ms.
function setup(){
......@@ -59,6 +62,7 @@ function setup(){
}
function play(){
// pass vector to myEnvelope.line
myCurrentVolume = myEnvelope.line(myEnvelopeData.size(), myEnvelopeData);
CurrentCount = Math.floor(myCounter.phasor(1, 1, 9));//phasor can take three arguments; frequency, start value and end value
......@@ -88,6 +92,6 @@ if (CurrentCount === 1) {
<h1> Envelope Example </h1>
<p>
</p>
</body></html>
......@@ -7,9 +7,28 @@
#include <emscripten.h>
#include <emscripten/bind.h>
extern "C" {
// class arrayTest{
// int int_sqrt(int x) {
// return sqrt(x);
// }
// };
//
// arrayTest* a(){
// return new arrayTest();
// }
int sumArray(double* arr, int size){
double valOut = 0;
for(int i = 0; i < size; i++){
valOut += arr[i];
}
return valOut;
}
}
class vectorTools {
public:
//template <typename T>
static void clearVectorDbl(vector<double>& vecIn) {
vecIn.clear();
}
......@@ -27,6 +46,30 @@ EMSCRIPTEN_BINDINGS(my_module) {
.class_function("clearVectorDbl", &vectorTools::clearVectorDbl)
;
// class_<testVectorHolder>("testVectorHolder")
// .constructor<>()
/*
Using a smart_ptr_constructor ensures lifetime management on the js side
by returning a smart_ptr when a constructor is used
*/
// .smart_ptr_constructor("shared_ptr<testVectorHolder>",&std::make_shared<testVectorHolder>)
// .function("at", &testVectorHolder::at)
// .function("coswave", &maxiOsc::coswave)
// ;
// -----------------------------------------
class_<maxiTest>("maxiTest")
// .constructor<>()
/*
Using a smart_ptr_constructor ensures lifetime management on the js side
by returning a smart_ptr when a constructor is used
*/
.smart_ptr_constructor("shared_ptr<maxiOsc>",&std::make_shared<maxiTest>)
.function("sumArray", &maxiTest::sumArray, allow_raw_pointers())
;
// maxi stuff
class_<maxiSettings>("maxiSettings")
.constructor<>()
.class_function("setup", &maxiSettings::setup)
......@@ -88,6 +131,7 @@ EMSCRIPTEN_BINDINGS(my_module) {
.function("hipass", &maxiFilter::hipass)
;
// MAXI MIX
class_<maxiMix>("maxiMix")
// .constructor<>()
......
......@@ -226,6 +226,8 @@ double maxiOsc::triangle(double frequency) {
// --------------------------------------------------------------------------------
// MAXI ENVELOPE
//double maxiEnvelope::line(int numberofsegments, double segments[]) {
double maxiEnvelope::line(int numberofsegments, std::vector<double>& segments) {
//This is a basic multi-segment ramp generator that you can use for more or less anything.
......
......@@ -55,6 +55,23 @@ using namespace std;
#endif
#define TWOPI 6.283185307179586476925286766559
class maxiTest{
public:
int sumArray(uintptr_t input, size_t len) {
const int* ptr = reinterpret_cast<int*>(input);
// double n = 0;
// for(int i = 0; i < len; i++){
// n+=ptr[i];
// }
return ptr[0];
}
};
class maxiSettings {
public:
static int sampleRate;
......@@ -110,7 +127,7 @@ class maxiEnvelope {
public:
// maxiEnvelope(){}
double line(int numberofsegments, std::vector<double>& segments);
// double line(int numberofsegments , double segments[1000]);
// double line(int numberofsegments , double segments[1000]);
void trigger(int index,double amp);
int valindex;
......@@ -182,11 +199,12 @@ public:
// double x;
// double y;
// double z;
/*
currently using non-pointer version for embind, need to sort out pointer stuff though
embind has no support for pointers to primitives!
*/
// double *stereo(double input,double two[2],double x);
// double *stereo(double input,double two[2],double x);
// double *quad(double input,double four[4], double x,double y);
// double *ambisonic(double input,double eight[8],double x,double y, double z);
......
// give Module more appropriate name?
var maximJs = Module;
// reassign to remove neew for Module - REMOVE THIS!
// var maxiSettings = Module.maxiSettings;
// var maxiOsc = Module.maxiOsc;
// var maxiEnvelope = Module.maxiEnvelope;
// var maxiDelayline = Module.maxiDelayline;
// might adopt some strange functions from Module?
// var maximJs = Module;
// alternatively...
// probably better this way
// no inheritance of Module stuff...
var maximJs = {};
maximJs.maxiSettings = Module.maxiSettings;
maximJs.maxiOsc = Module.maxiOsc;
maximJs.maxiEnvelope = Module.maxiEnvelope;
maximJs.maxiDelayline = Module.maxiDelayline;
maximJs.maxiFilter = Module.maxiFilter;
maximJs.maxiMix = Module.maxiMix;
maximJs.maxiLagExp = Module.maxiLagExp;
maximJs.maxiSample = Module.maxiSample;
maximJs.maxiMap = Module.maxiMap;
maximJs.maxiDyn = Module.maxiDyn;
maximJs.maxiEnv = Module.maxiEnv;
maximJs.convert = Module.convert;
maximJs.maxiDistortion = Module.maxiDistortion;
maximJs.maxiFlanger = Module.maxiFlanger;
maximJs.maxiChorus = Module.maxiChorus;
maximJs.maxiEnvelopeFollower = Module.maxiEnvelopeFollower;
// alternative array method to avoid mixing vector terminology with arrays
// seems like bad practice though and needs some testing...
// how to stop people messing with vec and its effects on object
maximJs.maxiArray = function maxiArray(){
this.length = 0;
this.vec = new Module.VectorDouble();
this.set = function(arrayIn){
this.clear();
this.vec = GetArrayAsVectorDbl(arrayIn);
this.length = this.vec.size();
};
// var maxiFilter = Module.maxiFilter;
// var maxiMix = Module.maxiMix;
// var maxiLagExp = Module.maxiLagExp;
// var maxiSample = Module.maxiSample;
// set object properties to mimic array
this.SetSqBrackets = function(useSq){
for(var i = 0; i < this.length; i++){
if(useSq){
this[i] = this.vec.get(i);
} else{
delete this[i];
}
}
};
// var maxiMap = Module.maxiMap;
// var maxiDyn = Module.maxiDyn;
// var maxiEnv = Module.maxiEnv;
// var convert = Module.convert;
this.clear = function(useSq){
for(var i = 0; i < this.length; i++){
delete this[i];
}
Module.vectorTools.clearVectorDbl(this.vec);
this.length = 0;
};
this.push = function(num){
this.vec.push_back(num);
this.length++;
};
// this.update = function(){
// var lengthsMatch = this.length !== this.vec.size();
// if(!lengthsMatch){
// if(this.length < this.vec.size()){
// for(var i = this.length; i < this.vec.size(); i++){
// this[i] = this.vec.get(i);
// }
// } else{
// for(var i = this.length; i < this.vec.size(); i++){
// delete this[i];
// }
// }
// // reset length var
// this.length = this.vec.size();
// }
// };
};
// var maxiDistortion = Module.maxiDistortion;
// var maxiFlanger = Module.maxiFlanger;
// var maxiChorus = Module.maxiChorus;
function GetArrayAsVectorDbl(arrayIn){
var vecOut = new Module.VectorDouble();
for(var i = 0; i < arrayIn.length; i++){
vecOut.push_back(arrayIn[i]);
}
// var maxiEnvelopeFollower = Module.maxiEnvelopeFollower;
return vecOut;
}
// would be nice to put this (all below) in function/class of some sort
// Temporary patch until all browsers support unprefixed context.
AudioContext = AudioContext || webkitAudioContext;
......@@ -55,15 +126,6 @@ function play(){
// console.log("non-overrided play happening");
}
function GetArrayAsVectorDbl(arrayIn){
var vecOut = new Module.VectorDouble();
for(var i = 0; i < arrayIn.length; i++){
vecOut.push_back(arrayIn[i]);
}
return vecOut;
}
// set num channels and set output as an array
// use this if you want to change number of channels
function setNumChannels(isArray, numChannelsOut_){
......@@ -113,7 +175,7 @@ function process(event) {
}
else
{
for (var channel = 0; channel < numChannels; channel++) {
for (var channel = 0; channel < numChannels; channel++) {
event.outputBuffer.getChannelData(channel)[i] = output;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment