Skip to content

Commit

Permalink
Add setCurrentTime api for wasm renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed Feb 10, 2024
1 parent e9d9de6 commit a1b79b6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
33 changes: 33 additions & 0 deletions js/packages/offline-renderer/__tests__/time.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,36 @@ test('time node', async function() {
core.process(inps, outs);
expect(outs[0]).toMatchSnapshot();
});

test('setting time', async function() {
let core = new OfflineRenderer();

await core.initialize({
numInputChannels: 0,
numOutputChannels: 1,
});

// Graph
core.render(el.time());

// Ten blocks of data
let inps = [];
let outs = [new Float32Array(512 * 10)];

// Get past the fade-in
core.process(inps, outs);

// Set current time, then push another small block
outs = [new Float32Array(8)];

core.setCurrentTime(50);
core.process(inps, outs);
expect(Array.from(outs[0])).toMatchObject([50, 51, 52, 53, 54, 55, 56, 57]);

// Set current time, then push another small block
outs = [new Float32Array(8)];

core.setCurrentTimeMs(1000);
core.process(inps, outs);
expect(Array.from(outs[0])).toMatchObject([44100, 44101, 44102, 44103, 44104, 44105, 44106, 44107]);
});
2 changes: 1 addition & 1 deletion js/packages/offline-renderer/elementary-wasm.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions js/packages/offline-renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,12 @@ export default class OfflineRenderer extends EventEmitter {
reset() {
this._native.reset();
}

setCurrentTime(t) {
this._native.setCurrentTime(t);
}

setCurrentTimeMs(t) {
this._native.setCurrentTimeMs(t);
}
}
12 changes: 12 additions & 0 deletions js/packages/web-renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,16 @@ export default class WebAudioRenderer extends EventEmitter {
async reset() {
return await this._sendWorkletRequest('reset', {});
}

async setCurrentTime(t) {
return await this._sendWorkletRequest('setCurrentTime', {
time: t
});
}

async setCurrentTimeMs(t) {
return await this._sendWorkletRequest('setCurrentTimeMs', {
time: t
});
}
}
10 changes: 10 additions & 0 deletions js/packages/web-renderer/raw/WorkletProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ class ElementaryAudioWorkletProcessor extends AudioWorkletProcessor {
requestId,
result: this._native.listSharedResourceMap(),
}]);
case 'setCurrentTime':
return this.port.postMessage(['reply', {
requestId,
result: this._native.setCurrentTime(payload.time),
}]);
case 'setCurrentTimeMs':
return this.port.postMessage(['reply', {
requestId,
result: this._native.setCurrentTimeMs(payload.time),
}]);
default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion js/packages/web-renderer/raw/elementary-wasm.js

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions wasm/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ class ElementaryAudioProcessor

//==============================================================================
/** Called before processing starts. */
void prepare (double sampleRate, unsigned int maxBlockSize)
void prepare (double sr, unsigned int maxBlockSize)
{
sampleRate = sr;

scratchBuffers.clear();
scratchPointers.clear();

Expand Down Expand Up @@ -182,6 +184,17 @@ class ElementaryAudioProcessor
callback(valueToEmVal(batch));
}

void setCurrentTime(int const timeInSamples)
{
sampleTime = timeInSamples;
}

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

private:
//==============================================================================
std::optional<std::vector<float>> arrayToFloatVector (elem::js::Array const& ar)
Expand Down Expand Up @@ -321,6 +334,7 @@ class ElementaryAudioProcessor
std::vector<float*> scratchPointers;

int64_t sampleTime = 0;
double sampleRate = 0;

size_t numInputChannels = 0;
size_t numOutputChannels = 2;
Expand All @@ -338,5 +352,7 @@ EMSCRIPTEN_BINDINGS(Elementary) {
.function("pruneSharedResourceMap", &ElementaryAudioProcessor::pruneSharedResourceMap)
.function("listSharedResourceMap", &ElementaryAudioProcessor::listSharedResourceMap)
.function("process", &ElementaryAudioProcessor::process)
.function("processQueuedEvents", &ElementaryAudioProcessor::processQueuedEvents);
.function("processQueuedEvents", &ElementaryAudioProcessor::processQueuedEvents)
.function("setCurrentTime", &ElementaryAudioProcessor::setCurrentTime)
.function("setCurrentTimeMs", &ElementaryAudioProcessor::setCurrentTimeMs);
};

0 comments on commit a1b79b6

Please sign in to comment.