Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified js/packages/offline-renderer/elementary-wasm.wasm
Binary file not shown.
12 changes: 12 additions & 0 deletions js/packages/offline-renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,16 @@ export default class OfflineRenderer extends EventEmitter {
setCurrentTimeMs(t) {
this._native.setCurrentTimeMs(t);
}

setBeatTime(t: number) {
this._native.setBeatTime(t);
}

setBpm(bpm: number) {
this._native.setBpm(bpm);
}

setTimeSignature(numerator: number, denominator: number) {
this._native.setTimeSignature(numerator, denominator);
}
}
19 changes: 19 additions & 0 deletions js/packages/web-renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,25 @@ export default class WebRenderer extends EventEmitter {
});
}

async setBeatTime(t: number) {
return await this._sendWorkletRequest("setBeatTime", {
time: t,
});
}

async setBpm(bpm: number) {
return await this._sendWorkletRequest("setBpm", {
bpm: bpm,
});
}

async setTimeSignature(numerator: number, denominator: number) {
return await this._sendWorkletRequest("setTimeSignature", {
numerator: numerator,
denominator: denominator,
});
}

async pushMidiEvent(time, value) {
return await this._sendWorkletRequest("pushMidiEvent", {
time,
Expand Down
27 changes: 27 additions & 0 deletions js/packages/web-renderer/index.worklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,33 @@ class ElementaryAudioWorkletProcessor extends AudioWorkletProcessor {
result: this._native.setCurrentTimeMs(payload.time),
},
]);
case "setBeatTime":
return this.port.postMessage([
"reply",
{
requestId,
result: this._native.setBeatTime(payload.time),
},
]);
case "setBpm":
return this.port.postMessage([
"reply",
{
requestId,
result: this._native.setBpm(payload.bpm),
},
]);
case "setTimeSignature":
return this.port.postMessage([
"reply",
{
requestId,
result: this._native.setTimeSignature(
payload.numerator,
payload.denominator,
),
},
]);
case "pushMidiEvent":
let packedValue = 0 | 0;

Expand Down
Binary file modified js/packages/web-renderer/raw/elementary-wasm.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion runtime/elem/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ namespace elem
userData,
true,
emptyInputEvents,
emptyOutputEvents
emptyOutputEvents,
CurrentTime()
});
}

Expand Down
32 changes: 32 additions & 0 deletions runtime/elem/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@ namespace elem
}
};

//==============================================================================
// A struct representing the current global time in various units
struct CurrentTime {
int64_t sampleTime;
double beatTime;
double bpm;
double timeSignatureNumerator;
double timeSignatureDenominator;

CurrentTime()
: sampleTime(0)
, beatTime(0)
, bpm(120)
, timeSignatureNumerator(4)
, timeSignatureDenominator(4)
{}

explicit CurrentTime(
int64_t sampleTime,
double beatTime,
double bpm,
double timeSignatureNumerator,
double timeSignatureDenominator)
: sampleTime(sampleTime)
, beatTime(beatTime)
, bpm(bpm)
, timeSignatureNumerator(timeSignatureNumerator)
, timeSignatureDenominator(timeSignatureDenominator)
{}
};

//==============================================================================
// A simple struct representing the inputs to a given GraphNode during the realtime
// audio block processing step.
Expand All @@ -104,6 +135,7 @@ namespace elem
bool active;
BlockEvents const& inputEvents;
BlockEvents& outputEvents;
CurrentTime currentTime;
};

//==============================================================================
Expand Down
62 changes: 61 additions & 1 deletion wasm/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@
#include "FFT.h"
#include "Metro.h"
#include "SampleTime.h"
#include "elem/Types.h"


using namespace emscripten;

namespace {
inline double sampleTimeToBeatTime(int64_t sampleTime, double bpm, double sampleRate) {
if (sampleRate <= 0.0) return 0.0;
return (sampleTime / sampleRate) * (bpm / 60.0);
}

inline int64_t beatTimeToSampleTime(double beatTime, double bpm, double sampleRate) {
if (bpm <= 0.0) return 0;
return static_cast<int64_t>((beatTime * 60.0 / bpm) * sampleRate);
}
}

//==============================================================================
/** The main processor for the WASM DSP. */
class ElementaryAudioProcessor
Expand Down Expand Up @@ -212,6 +225,13 @@ class ElementaryAudioProcessor
}
}

auto const currentTime = elem::CurrentTime {
sampleTime,
beatTime,
bpm,
timeSignatureNumerator,
timeSignatureDenominator,
};
// We just operate on our scratch data. Expect the JavaScript caller to hit
// our getInputBufferData and getOutputBufferData to prepare and extract the actual
// data for this processor
Expand All @@ -225,9 +245,11 @@ class ElementaryAudioProcessor
true,
inputEvents,
outputEvents,
currentTime,
});

sampleTime += static_cast<int64_t>(numSamples);
beatTime += sampleTimeToBeatTime(numSamples, bpm, sampleRate);

// Prepare to receive new events before the next call
inputEvents.clear();
Expand All @@ -252,12 +274,42 @@ class ElementaryAudioProcessor
void setCurrentTime(int const timeInSamples)
{
sampleTime = timeInSamples;
beatTime = sampleTimeToBeatTime(sampleTime, bpm, sampleRate);
}

void setCurrentTimeMs(double const timeInMs)
{
double const timeInSeconds = timeInMs / 1000.0;
sampleTime = static_cast<int64_t>(timeInSeconds * sampleRate);
beatTime = sampleTimeToBeatTime(sampleTime, bpm, sampleRate);
}

void setBeatTime(double const timeInBeats)
{
if (sampleRate <= 0.0 || bpm <= 0.0)
return;

beatTime = timeInBeats;
sampleTime = beatTimeToSampleTime(timeInBeats, bpm, sampleRate);
}

void setBpm(double const beatsPerMinute)
{
if (beatsPerMinute <= 0.0)
return;

bpm = beatsPerMinute;
// Recalculate sampleTime from beatTime at new BPM
sampleTime = beatTimeToSampleTime(beatTime, bpm, sampleRate);
}

void setTimeSignature(double const numerator, double const denominator)
{
if (numerator <= 0.0 || denominator <= 0.0)
return;

timeSignatureNumerator = numerator;
timeSignatureDenominator = denominator;
}

private:
Expand Down Expand Up @@ -409,8 +461,13 @@ class ElementaryAudioProcessor
std::vector<double*> scratchPointers;

int64_t sampleTime = 0;
double beatTime = 0.0;
double sampleRate = 0;

double bpm = 120.0;
double timeSignatureNumerator = 4.0;
double timeSignatureDenominator = 4.0;

size_t numInputChannels = 0;
size_t numOutputChannels = 2;

Expand All @@ -435,5 +492,8 @@ EMSCRIPTEN_BINDINGS(Elementary) {
.function("process", &ElementaryAudioProcessor::process)
.function("processQueuedEvents", &ElementaryAudioProcessor::processQueuedEvents)
.function("setCurrentTime", &ElementaryAudioProcessor::setCurrentTime)
.function("setCurrentTimeMs", &ElementaryAudioProcessor::setCurrentTimeMs);
.function("setCurrentTimeMs", &ElementaryAudioProcessor::setCurrentTimeMs)
.function("setBeatTime", &ElementaryAudioProcessor::setBeatTime)
.function("setBpm", &ElementaryAudioProcessor::setBpm)
.function("setTimeSignature", &ElementaryAudioProcessor::setTimeSignature);
};