Skip to content

Commit

Permalink
Fix time remaining treatment
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliastik committed Mar 3, 2024
1 parent e927e33 commit 33e53bb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dist/cjs/SimpleSoundStudioLibrary.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cjs/SimpleSoundStudioLibrary.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/SimpleSoundStudioLibrary.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/SimpleSoundStudioLibrary.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/AudioEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export default class AudioEditor extends AbstractAudioElement {

if (this.eventEmitter) {
this.eventEmitter.emit(EventType.UPDATE_AUDIO_TREATMENT_PERCENT, 0);
this.eventEmitter.emit(EventType.UPDATE_REMAINING_TIME_ESTIMATED, -1);
}
} else {
throw new Error("Audio Context is not ready!");
Expand Down Expand Up @@ -503,6 +504,7 @@ export default class AudioEditor extends AbstractAudioElement {

if (this.eventEmitter) {
this.eventEmitter.emit(EventType.UPDATE_AUDIO_TREATMENT_PERCENT, 0);
this.eventEmitter.emit(EventType.UPDATE_REMAINING_TIME_ESTIMATED, -1);
}

const passthroughFilter = this.filters.find(f => f.id === Constants.FILTERS_NAMES.PASSTHROUGH);
Expand Down
36 changes: 19 additions & 17 deletions lib/filters/PassThroughFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,13 @@ export default class PassThroughFilter extends AbstractAudioFilterWorklet<PassTh
}

receiveEvent(message: MessageEvent<PassThroughWorkletEvent>): void {
if (!this.eventEmitter) {
return;
}

const currentTime = performance.now();
const samplesProcessed = message.data.samplesCount;

if (this.currentTimeSamplesPerSecond === 0) {
this.currentTimeSamplesPerSecond = currentTime;
}

const timeDifferenceSamplePerSecond = currentTime - this.currentTimeSamplesPerSecond;

if (timeDifferenceSamplePerSecond >= 1000) {
console.log(samplesProcessed, this.lastSampleCount);
this.samplePerSecond = (samplesProcessed - this.lastSampleCount) / (timeDifferenceSamplePerSecond / 1000);
this.currentTimeSamplesPerSecond = currentTime;
this.lastSampleCount = samplesProcessed;
}

if (this.currentTime === 0) {
this.currentTime = currentTime;
}
Expand All @@ -43,16 +34,27 @@ export default class PassThroughFilter extends AbstractAudioFilterWorklet<PassTh
const remainingSamples = this._totalSamples - samplesProcessed;
const remainingTimeSeconds = remainingSamples / this.samplePerSecond;

if (this.eventEmitter && message.data.command === "update" && timeDifference >= Constants.TREATMENT_TIME_COUNTING_THROTTLE_INTERVAL) {
if (message.data.command === "update" && timeDifference >= Constants.TREATMENT_TIME_COUNTING_THROTTLE_INTERVAL) {
this.eventEmitter.emit(EventType.UPDATE_AUDIO_TREATMENT_PERCENT, percentageProcessed * 100);
this.currentTime = currentTime;
}

if (this.currentTimeSamplesPerSecond === 0) {
this.currentTimeSamplesPerSecond = currentTime;
}

const timeDifferenceSamplePerSecond = currentTime - this.currentTimeSamplesPerSecond;

if (timeDifferenceSamplePerSecond >= 1000) {
this.samplePerSecond = (samplesProcessed - this.lastSampleCount) / (timeDifferenceSamplePerSecond / 1000);
this.currentTimeSamplesPerSecond = currentTime;
this.lastSampleCount = samplesProcessed;

if (isNaN(remainingTimeSeconds) || !isFinite(remainingTimeSeconds)) {
this.eventEmitter.emit(EventType.UPDATE_REMAINING_TIME_ESTIMATED, 0);
this.eventEmitter.emit(EventType.UPDATE_REMAINING_TIME_ESTIMATED, -1);
} else {
this.eventEmitter.emit(EventType.UPDATE_REMAINING_TIME_ESTIMATED, remainingTimeSeconds);
}

this.currentTime = currentTime;
}
}

Expand Down

0 comments on commit 33e53bb

Please sign in to comment.