Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliastik committed Jan 12, 2025
1 parent 58d1432 commit c50b1c7
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 20 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.

10 changes: 10 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ interface FilterManagerInterface {
* Initialize worklets filters
*/
initializeWorklets(context: BaseAudioContext): Promise<void>;
/**
* Clear old worklets
*/
clearWorklets(): void;
/**
* Get the total time the filters add to the audio duration
*/
Expand Down Expand Up @@ -528,6 +532,10 @@ interface AudioProcessorInterface {
* Cancel the audio rendering
*/
cancelAudioRendering(): void;
/**
* Clear and remove rendered buffer to free memory
*/
clearRenderedBuffer(): void;
/**
* Get the rendered audio buffer
*/
Expand Down Expand Up @@ -615,6 +623,7 @@ declare class AudioEditor extends AbstractAudioElement implements AudioEditorInt
resetFilterSettings(filterId: string): Promise<void>;
resetAllFiltersState(): void;
exit(): void;
private clearBuffers;
cancelAudioRendering(): void;
on(event: string, callback: EventEmitterCallback): void;
off(event: string, callback: EventEmitterCallback): void;
Expand Down Expand Up @@ -1215,6 +1224,7 @@ declare const utilFunctions: {
*/
resetAudioRenderingProgress(eventEmitter: EventEmitterInterface | null): void;
forceDownload(blob: Blob, filename: string): void;
clearAudioBuffer(buffer: AudioBuffer | null): void;
};

declare enum EventType {
Expand Down
13 changes: 4 additions & 9 deletions lib/audioEditor/AudioContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export default class AudioContextManager implements AudioContextManagerInterface
/** The current audio context */
private _currentContext: AudioContext | null | undefined;

/** The old audio context */
private oldAudioContext: AudioContext | null | undefined;

/** The previous sample rate setting */
private previousSampleRate = Constants.DEFAULT_SAMPLE_RATE;

Expand Down Expand Up @@ -81,8 +78,7 @@ export default class AudioContextManager implements AudioContextManagerInterface
*/
createNewContext(sampleRate: number) {
if (this._currentContext) {
this.oldAudioContext = this._currentContext;
this.destroyOldContext();
this.destroyOldContext(this._currentContext);
}

const options: AudioContextOptions = {
Expand All @@ -105,10 +101,9 @@ export default class AudioContextManager implements AudioContextManagerInterface
/**
* Destroy previous AudioContext
*/
private destroyOldContext() {
if (this.oldAudioContext) {
this.oldAudioContext.close();
this.oldAudioContext = null;
private destroyOldContext(oldAudioContext: AudioContext) {
if(oldAudioContext) {
oldAudioContext.close();
}
}

Expand Down
21 changes: 15 additions & 6 deletions lib/audioEditor/AudioEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import AbstractAudioFilter from "../filters/interfaces/AbstractAudioFilter";
import AbstractAudioRenderer from "../filters/interfaces/AbstractAudioRenderer";
import utils from "../utils/Functions";
import { EventType } from "../model/EventTypeEnum";
import utilFunctions from "../utils/Functions";
import { FilterSettings } from "../model/filtersSettings/FilterSettings";
import { EventEmitterCallback } from "../model/EventEmitterCallback";
import { FilterState } from "../model/FilterState";
Expand Down Expand Up @@ -144,7 +143,7 @@ export default class AudioEditor extends AbstractAudioElement implements AudioEd
}

async loadBufferFromFile(file: File) {
this.principalBuffer = null;
this.clearBuffers();
this.loadingAudio = true;

if (this.audioProcessor) {
Expand All @@ -162,7 +161,7 @@ export default class AudioEditor extends AbstractAudioElement implements AudioEd
throw new Error("Error decoding audio file");
}

utilFunctions.resetAudioRenderingProgress(this.eventEmitter);
utils.resetAudioRenderingProgress(this.eventEmitter);

// If switching between a list of audio to one audio, reset the loop audio playing
if (this.bufferPlayer && this.bufferPlayer.loopAll && this.totalFileList <= 1) {
Expand Down Expand Up @@ -310,7 +309,7 @@ export default class AudioEditor extends AbstractAudioElement implements AudioEd

isAudioWorkletAvailable(): boolean {
if (this.contextManager && this.contextManager.currentContext) {
return utilFunctions.isAudioWorkletCompatible(this.contextManager.currentContext);
return utils.isAudioWorkletCompatible(this.contextManager.currentContext);
}

return false;
Expand Down Expand Up @@ -345,7 +344,7 @@ export default class AudioEditor extends AbstractAudioElement implements AudioEd

const speedAudio = this.filterManager.entrypointFilter.getSpeed();
this.bufferPlayer.speedAudio = speedAudio;
this.bufferPlayer.duration = utilFunctions.calculateAudioDuration(this.principalBuffer, this.filterManager, speedAudio) * speedAudio;
this.bufferPlayer.duration = utils.calculateAudioDuration(this.principalBuffer, this.filterManager, speedAudio) * speedAudio;
}
}

Expand Down Expand Up @@ -416,11 +415,21 @@ export default class AudioEditor extends AbstractAudioElement implements AudioEd
}

this.cancelAudioRendering();
this.principalBuffer = null;
this.clearBuffers();

this.fileList = null;
this.fileListCurrIndex = 0;
}

private clearBuffers() {
utils.clearAudioBuffer(this.principalBuffer);
this.principalBuffer = null;

if(this.audioProcessor) {
this.audioProcessor.clearRenderedBuffer();
}
}

cancelAudioRendering() {
if (this.audioProcessor) {
this.audioProcessor.cancelAudioRendering();
Expand Down
7 changes: 7 additions & 0 deletions lib/audioEditor/AudioProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export default class AudioProcessor extends AbstractAudioElement implements Audi

const renderedBuffer = await offlineContext.startRendering();

this.filterManager.clearWorklets();

if (this.contextManager && !this.loadRenderedAudio(inputBuffer, renderedBuffer)) {
return await this.setupOutput(inputBuffer, this.contextManager.currentContext!, durationAudio);
}
Expand Down Expand Up @@ -229,6 +231,11 @@ export default class AudioProcessor extends AbstractAudioElement implements Audi
}
}

clearRenderedBuffer() {
utils.clearAudioBuffer(this._renderedBuffer);
this._renderedBuffer = null;
}

/**
* Set compatibility/direct audio rendering mode already checked for auto enabling (if an error occurs rendering in offline context)
* @param checked boolean
Expand Down
9 changes: 9 additions & 0 deletions lib/audioEditor/FilterManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,20 @@ export default class FilterManager extends AbstractAudioElement implements Filte
async initializeWorklets(context: BaseAudioContext) {
for (const filter of this.filters) {
if (filter.isWorklet()) {
this.clearWorklets();
await (filter as AbstractAudioFilterWorklet<object>).initializeWorklet(context);
}
}
}

clearWorklets() {
for (const filter of this.filters) {
if (filter.isWorklet()) {
(filter as AbstractAudioFilterWorklet<object>).stop();
}
}
}

getAddingTime() {
let duration = 0;

Expand Down
5 changes: 5 additions & 0 deletions lib/audioEditor/interfaces/AudioProcessorInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export default interface AudioProcessorInterface {
*/
cancelAudioRendering(): void;

/**
* Clear and remove rendered buffer to free memory
*/
clearRenderedBuffer(): void;

/**
* Get the rendered audio buffer
*/
Expand Down
5 changes: 5 additions & 0 deletions lib/audioEditor/interfaces/FilterManagerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export default interface FilterManagerInterface {
*/
initializeWorklets(context: BaseAudioContext): Promise<void>;

/**
* Clear old worklets
*/
clearWorklets(): void;

/**
* Get the total time the filters add to the audio duration
*/
Expand Down
6 changes: 5 additions & 1 deletion lib/bufferPlayer/BufferPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ export default class BufferPlayer extends AbstractAudioElement implements Buffer
this._contextManager.currentContext.resume();

if (!this.compatibilityMode && this.buffer) {
if (this.source != null && !direct) this.source.disconnect();
if (this.source != null && !direct) {
this.source.buffer = null;
this.source.disconnect();
}

this.source = this._contextManager.currentContext.createBufferSource();
this.source.buffer = this.buffer;
this.duration = this.buffer.duration * this.speedAudio;
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/Functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ const utilFunctions = {
link.click();
URL.revokeObjectURL(url);
window.document.body.removeChild(link);
},
clearAudioBuffer(buffer: AudioBuffer | null) {
if(buffer) {
for(let channel = 0; channel < buffer.numberOfChannels; channel++) {
const channelData = buffer.getChannelData(channel);

for(let i = 0; i < channelData.length; i++) {
channelData[i] = 0;
}
}
}
}
};

Expand Down

0 comments on commit c50b1c7

Please sign in to comment.