From e3264264bc61a9b5cc429ea3e16e3f12ff552ed2 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Mon, 13 Sep 2021 17:05:57 +0200 Subject: [PATCH] Turned IPlaybackRange into a tuple --- theatre/core/src/sequences/Sequence.ts | 49 ++++++++----------- .../AudioPlaybackController.ts | 16 +++--- .../DefaultPlaybackController.ts | 30 ++++++------ 3 files changed, 43 insertions(+), 52 deletions(-) diff --git a/theatre/core/src/sequences/Sequence.ts b/theatre/core/src/sequences/Sequence.ts index aa389cc..387be46 100644 --- a/theatre/core/src/sequences/Sequence.ts +++ b/theatre/core/src/sequences/Sequence.ts @@ -4,7 +4,6 @@ import type Sheet from '@theatre/core/sheets/Sheet' import type {SequenceAddress} from '@theatre/shared/utils/addresses' import didYouMean from '@theatre/shared/utils/didYouMean' import {InvalidArgumentError} from '@theatre/shared/utils/errors' -import type {IRange} from '@theatre/shared/utils/types' import type {IBox, IDerivation, Pointer} from '@theatre/dataverse' import {Box, prism, val, valueDerivation} from '@theatre/dataverse' import {padStart} from 'lodash-es' @@ -16,7 +15,7 @@ import DefaultPlaybackController from './playbackControllers/DefaultPlaybackCont import TheatreSequence from './TheatreSequence' import logger from '@theatre/shared/logger' -export type IPlaybackRange = IRange +export type IPlaybackRange = [from: number, to: number] export type IPlaybackDirection = | 'normal' @@ -142,10 +141,7 @@ export default class Sequence { _makeRangeFromSequenceTemplate(): IDerivation { return prism(() => { - return { - start: 0, - end: val(this._lengthD), - } + return [0, val(this._lengthD)] }) } @@ -158,49 +154,44 @@ export default class Sequence { }>, ): Promise { const sequenceDuration = this.length - const range = - conf && conf.range - ? conf.range - : { - start: 0, - end: sequenceDuration, - } + const range: IPlaybackRange = + conf && conf.range ? conf.range : [0, sequenceDuration] if (process.env.NODE_ENV !== 'production') { - if (typeof range.start !== 'number' || range.start < 0) { + if (typeof range[0] !== 'number' || range[0] < 0) { throw new InvalidArgumentError( - `Argument conf.range.start in sequence.play(conf) must be a positive number. ${JSON.stringify( - range.start, + `Argument conf.range[0] in sequence.play(conf) must be a positive number. ${JSON.stringify( + range[0], )} given.`, ) } - if (range.start >= sequenceDuration) { + if (range[0] >= sequenceDuration) { throw new InvalidArgumentError( - `Argument conf.range.start in sequence.play(conf) cannot be longer than the duration of the sequence, which is ${sequenceDuration}ms. ${JSON.stringify( - range.start, + `Argument conf.range[0] in sequence.play(conf) cannot be longer than the duration of the sequence, which is ${sequenceDuration}ms. ${JSON.stringify( + range[0], )} given.`, ) } - if (typeof range.end !== 'number' || range.end <= 0) { + if (typeof range[1] !== 'number' || range[1] <= 0) { throw new InvalidArgumentError( - `Argument conf.range.end in sequence.play(conf) must be a number larger than zero. ${JSON.stringify( - range.end, + `Argument conf.range[1] in sequence.play(conf) must be a number larger than zero. ${JSON.stringify( + range[1], )} given.`, ) } - if (range.end > sequenceDuration) { + if (range[1] > sequenceDuration) { logger.warn( - `Argument conf.range.end in sequence.play(conf) cannot be longer than the duration of the sequence, which is ${sequenceDuration}ms. ${JSON.stringify( - range.end, + `Argument conf.range[1] in sequence.play(conf) cannot be longer than the duration of the sequence, which is ${sequenceDuration}ms. ${JSON.stringify( + range[1], )} given.`, ) - range.end = sequenceDuration + range[1] = sequenceDuration } - if (range.end <= range.start) { + if (range[1] <= range[0]) { throw new InvalidArgumentError( - `Argument conf.range.end in sequence.play(conf) must be larger than conf.range.start. ${JSON.stringify( + `Argument conf.range[1] in sequence.play(conf) must be larger than conf.range[0]. ${JSON.stringify( range, )} given.`, ) @@ -259,7 +250,7 @@ export default class Sequence { return await this._play( iterationCount, - {start: range.start, end: range.end}, + [range[0], range[1]], rate, direction, ) diff --git a/theatre/core/src/sequences/playbackControllers/AudioPlaybackController.ts b/theatre/core/src/sequences/playbackControllers/AudioPlaybackController.ts index 4657b7c..cb9cb4c 100644 --- a/theatre/core/src/sequences/playbackControllers/AudioPlaybackController.ts +++ b/theatre/core/src/sequences/playbackControllers/AudioPlaybackController.ts @@ -65,7 +65,7 @@ export default class AudioPlaybackController implements IPlaybackController { const ticker = this._ticker const startPos = this.getCurrentPosition() - const iterationLength = range.end - range.start + const iterationLength = range[1] - range[0] if (direction !== 'normal') { throw new InvalidArgumentError( @@ -81,12 +81,12 @@ export default class AudioPlaybackController implements IPlaybackController { ) } - if (startPos < range.start || startPos > range.end) { + if (startPos < range[0] || startPos > range[1]) { // if we're currently out of the range - this._updatePositionInState(range.start) - } else if (startPos === range.end) { + this._updatePositionInState(range[0]) + } else if (startPos === range[1]) { // if we're currently at the very end of the range - this._updatePositionInState(range.start) + this._updatePositionInState(range[0]) } const deferred = defer() @@ -98,7 +98,7 @@ export default class AudioPlaybackController implements IPlaybackController { const audioStartTimeInSeconds = this._audioContext.currentTime const wait = 0 - const timeToRangeEnd = range.end - startPos + const timeToRangeEnd = range[1] - startPos currentSource.start( audioStartTimeInSeconds + wait, @@ -106,7 +106,7 @@ export default class AudioPlaybackController implements IPlaybackController { wait + timeToRangeEnd, ) const initialTickerTime = ticker.time - let initialElapsedPos = this.getCurrentPosition() - range.start + let initialElapsedPos = this.getCurrentPosition() - range[0] const totalPlaybackLength = iterationLength * iterationCount const tick = (currentTickerTime: number) => { @@ -128,7 +128,7 @@ export default class AudioPlaybackController implements IPlaybackController { this._updatePositionInState(currentIterationPos) requestNextTick() } else { - this._updatePositionInState(range.end) + this._updatePositionInState(range[1]) this.playing = false cleanup() deferred.resolve(true) diff --git a/theatre/core/src/sequences/playbackControllers/DefaultPlaybackController.ts b/theatre/core/src/sequences/playbackControllers/DefaultPlaybackController.ts index 7fec78e..130ad99 100644 --- a/theatre/core/src/sequences/playbackControllers/DefaultPlaybackController.ts +++ b/theatre/core/src/sequences/playbackControllers/DefaultPlaybackController.ts @@ -70,32 +70,32 @@ export default class DefaultPlaybackController implements IPlaybackController { this.playing = true const ticker = this._ticker - const iterationLength = range.end - range.start + const iterationLength = range[1] - range[0] { const startPos = this.getCurrentPosition() - if (startPos < range.start || startPos > range.end) { - this._updatePositionInState(range.start) + if (startPos < range[0] || startPos > range[1]) { + this._updatePositionInState(range[0]) } else if ( - startPos === range.end && + startPos === range[1] && (direction === 'normal' || direction === 'alternate') ) { - this._updatePositionInState(range.start) + this._updatePositionInState(range[0]) } else if ( - startPos === range.start && + startPos === range[0] && (direction === 'reverse' || direction === 'alternateReverse') ) { - this._updatePositionInState(range.end) + this._updatePositionInState(range[1]) } } const deferred = defer() const initialTickerTime = ticker.time const totalPlaybackLength = iterationLength * iterationCount - let initialElapsedPos = this.getCurrentPosition() - range.start + let initialElapsedPos = this.getCurrentPosition() - range[0] if (direction === 'reverse' || direction === 'alternateReverse') { - initialElapsedPos = range.end - initialElapsedPos + initialElapsedPos = range[1] - initialElapsedPos } const tick = (currentTickerTime: number) => { @@ -136,22 +136,22 @@ export default class DefaultPlaybackController implements IPlaybackController { requestNextTick() } else { if (direction === 'normal') { - this._updatePositionInState(range.end) + this._updatePositionInState(range[1]) } else if (direction === 'reverse') { - this._updatePositionInState(range.start) + this._updatePositionInState(range[0]) } else { const isLastIterationEven = (iterationCount - 1) % 2 === 0 if (direction === 'alternate') { if (isLastIterationEven) { - this._updatePositionInState(range.end) + this._updatePositionInState(range[1]) } else { - this._updatePositionInState(range.start) + this._updatePositionInState(range[0]) } } else { if (isLastIterationEven) { - this._updatePositionInState(range.start) + this._updatePositionInState(range[0]) } else { - this._updatePositionInState(range.end) + this._updatePositionInState(range[1]) } } }