Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
58 changes: 57 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,15 @@ class ElementaryAudioProcessor
}
}

auto const beatTime = sampleTimeToBeatTime(sampleTime, bpm, sampleRate);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only thought is here: should we hold separate state for beatTime and sampleTime? My thought is that if bpm is strictly constant, then this mapping from sampleTime<>beatTime holds as expected. But if the tempo ever changes, this calculation will cause a (potentially large) jump in beat time that isn't quite accurate– like a tempo change at the start of bar 5 should still report the beatTime as start of bar 5 right?

So maybe at the bottom of this process call we have like

sampleTime += numSamples;
beatTime += sampleTimeToBeatTime(numSamples, bpm, sampleRate);

Then the question becomes, what do we do in setCurrentTime in the ms or beattime case.... I guess at that point its' probably fine to just snap both values according to the current bpm.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nick-thompson yes you're right! I just pushed this change.

For reference, Ableton Live maintains beat position, but snaps the sample position according to the new BPM when tempo changes. I think it seems intuitive to use that behavior here as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Thank you, let's roll


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,6 +247,7 @@ class ElementaryAudioProcessor
true,
inputEvents,
outputEvents,
currentTime,
});

sampleTime += static_cast<int64_t>(numSamples);
Expand Down Expand Up @@ -260,6 +283,32 @@ class ElementaryAudioProcessor
sampleTime = static_cast<int64_t>(timeInSeconds * sampleRate);
}

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

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

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

bpm = beatsPerMinute;
// sampleTime stays fixed, beatTime recalculated in next process()
}

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

timeSignatureNumerator = numerator;
timeSignatureDenominator = denominator;
}

private:
//==============================================================================
elem::js::Value emValToValue (val const& v)
Expand Down Expand Up @@ -411,6 +460,10 @@ class ElementaryAudioProcessor
int64_t sampleTime = 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 +488,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);
};