Turned IPlaybackRange into a tuple

This commit is contained in:
Aria Minaei 2021-09-13 17:05:57 +02:00
parent e8d32089ca
commit e3264264bc
3 changed files with 43 additions and 52 deletions

View file

@ -4,7 +4,6 @@ import type Sheet from '@theatre/core/sheets/Sheet'
import type {SequenceAddress} from '@theatre/shared/utils/addresses' import type {SequenceAddress} from '@theatre/shared/utils/addresses'
import didYouMean from '@theatre/shared/utils/didYouMean' import didYouMean from '@theatre/shared/utils/didYouMean'
import {InvalidArgumentError} from '@theatre/shared/utils/errors' import {InvalidArgumentError} from '@theatre/shared/utils/errors'
import type {IRange} from '@theatre/shared/utils/types'
import type {IBox, IDerivation, Pointer} from '@theatre/dataverse' import type {IBox, IDerivation, Pointer} from '@theatre/dataverse'
import {Box, prism, val, valueDerivation} from '@theatre/dataverse' import {Box, prism, val, valueDerivation} from '@theatre/dataverse'
import {padStart} from 'lodash-es' import {padStart} from 'lodash-es'
@ -16,7 +15,7 @@ import DefaultPlaybackController from './playbackControllers/DefaultPlaybackCont
import TheatreSequence from './TheatreSequence' import TheatreSequence from './TheatreSequence'
import logger from '@theatre/shared/logger' import logger from '@theatre/shared/logger'
export type IPlaybackRange = IRange export type IPlaybackRange = [from: number, to: number]
export type IPlaybackDirection = export type IPlaybackDirection =
| 'normal' | 'normal'
@ -142,10 +141,7 @@ export default class Sequence {
_makeRangeFromSequenceTemplate(): IDerivation<IPlaybackRange> { _makeRangeFromSequenceTemplate(): IDerivation<IPlaybackRange> {
return prism(() => { return prism(() => {
return { return [0, val(this._lengthD)]
start: 0,
end: val(this._lengthD),
}
}) })
} }
@ -158,49 +154,44 @@ export default class Sequence {
}>, }>,
): Promise<boolean> { ): Promise<boolean> {
const sequenceDuration = this.length const sequenceDuration = this.length
const range = const range: IPlaybackRange =
conf && conf.range conf && conf.range ? conf.range : [0, sequenceDuration]
? conf.range
: {
start: 0,
end: sequenceDuration,
}
if (process.env.NODE_ENV !== 'production') { 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( throw new InvalidArgumentError(
`Argument conf.range.start in sequence.play(conf) must be a positive number. ${JSON.stringify( `Argument conf.range[0] in sequence.play(conf) must be a positive number. ${JSON.stringify(
range.start, range[0],
)} given.`, )} given.`,
) )
} }
if (range.start >= sequenceDuration) { if (range[0] >= sequenceDuration) {
throw new InvalidArgumentError( 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( `Argument conf.range[0] in sequence.play(conf) cannot be longer than the duration of the sequence, which is ${sequenceDuration}ms. ${JSON.stringify(
range.start, range[0],
)} given.`, )} given.`,
) )
} }
if (typeof range.end !== 'number' || range.end <= 0) { if (typeof range[1] !== 'number' || range[1] <= 0) {
throw new InvalidArgumentError( throw new InvalidArgumentError(
`Argument conf.range.end in sequence.play(conf) must be a number larger than zero. ${JSON.stringify( `Argument conf.range[1] in sequence.play(conf) must be a number larger than zero. ${JSON.stringify(
range.end, range[1],
)} given.`, )} given.`,
) )
} }
if (range.end > sequenceDuration) { if (range[1] > sequenceDuration) {
logger.warn( 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( `Argument conf.range[1] in sequence.play(conf) cannot be longer than the duration of the sequence, which is ${sequenceDuration}ms. ${JSON.stringify(
range.end, range[1],
)} given.`, )} given.`,
) )
range.end = sequenceDuration range[1] = sequenceDuration
} }
if (range.end <= range.start) { if (range[1] <= range[0]) {
throw new InvalidArgumentError( 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, range,
)} given.`, )} given.`,
) )
@ -259,7 +250,7 @@ export default class Sequence {
return await this._play( return await this._play(
iterationCount, iterationCount,
{start: range.start, end: range.end}, [range[0], range[1]],
rate, rate,
direction, direction,
) )

View file

@ -65,7 +65,7 @@ export default class AudioPlaybackController implements IPlaybackController {
const ticker = this._ticker const ticker = this._ticker
const startPos = this.getCurrentPosition() const startPos = this.getCurrentPosition()
const iterationLength = range.end - range.start const iterationLength = range[1] - range[0]
if (direction !== 'normal') { if (direction !== 'normal') {
throw new InvalidArgumentError( 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 // if we're currently out of the range
this._updatePositionInState(range.start) this._updatePositionInState(range[0])
} else if (startPos === range.end) { } else if (startPos === range[1]) {
// if we're currently at the very end of the range // if we're currently at the very end of the range
this._updatePositionInState(range.start) this._updatePositionInState(range[0])
} }
const deferred = defer<boolean>() const deferred = defer<boolean>()
@ -98,7 +98,7 @@ export default class AudioPlaybackController implements IPlaybackController {
const audioStartTimeInSeconds = this._audioContext.currentTime const audioStartTimeInSeconds = this._audioContext.currentTime
const wait = 0 const wait = 0
const timeToRangeEnd = range.end - startPos const timeToRangeEnd = range[1] - startPos
currentSource.start( currentSource.start(
audioStartTimeInSeconds + wait, audioStartTimeInSeconds + wait,
@ -106,7 +106,7 @@ export default class AudioPlaybackController implements IPlaybackController {
wait + timeToRangeEnd, wait + timeToRangeEnd,
) )
const initialTickerTime = ticker.time const initialTickerTime = ticker.time
let initialElapsedPos = this.getCurrentPosition() - range.start let initialElapsedPos = this.getCurrentPosition() - range[0]
const totalPlaybackLength = iterationLength * iterationCount const totalPlaybackLength = iterationLength * iterationCount
const tick = (currentTickerTime: number) => { const tick = (currentTickerTime: number) => {
@ -128,7 +128,7 @@ export default class AudioPlaybackController implements IPlaybackController {
this._updatePositionInState(currentIterationPos) this._updatePositionInState(currentIterationPos)
requestNextTick() requestNextTick()
} else { } else {
this._updatePositionInState(range.end) this._updatePositionInState(range[1])
this.playing = false this.playing = false
cleanup() cleanup()
deferred.resolve(true) deferred.resolve(true)

View file

@ -70,32 +70,32 @@ export default class DefaultPlaybackController implements IPlaybackController {
this.playing = true this.playing = true
const ticker = this._ticker const ticker = this._ticker
const iterationLength = range.end - range.start const iterationLength = range[1] - range[0]
{ {
const startPos = this.getCurrentPosition() const startPos = this.getCurrentPosition()
if (startPos < range.start || startPos > range.end) { if (startPos < range[0] || startPos > range[1]) {
this._updatePositionInState(range.start) this._updatePositionInState(range[0])
} else if ( } else if (
startPos === range.end && startPos === range[1] &&
(direction === 'normal' || direction === 'alternate') (direction === 'normal' || direction === 'alternate')
) { ) {
this._updatePositionInState(range.start) this._updatePositionInState(range[0])
} else if ( } else if (
startPos === range.start && startPos === range[0] &&
(direction === 'reverse' || direction === 'alternateReverse') (direction === 'reverse' || direction === 'alternateReverse')
) { ) {
this._updatePositionInState(range.end) this._updatePositionInState(range[1])
} }
} }
const deferred = defer<boolean>() const deferred = defer<boolean>()
const initialTickerTime = ticker.time const initialTickerTime = ticker.time
const totalPlaybackLength = iterationLength * iterationCount const totalPlaybackLength = iterationLength * iterationCount
let initialElapsedPos = this.getCurrentPosition() - range.start let initialElapsedPos = this.getCurrentPosition() - range[0]
if (direction === 'reverse' || direction === 'alternateReverse') { if (direction === 'reverse' || direction === 'alternateReverse') {
initialElapsedPos = range.end - initialElapsedPos initialElapsedPos = range[1] - initialElapsedPos
} }
const tick = (currentTickerTime: number) => { const tick = (currentTickerTime: number) => {
@ -136,22 +136,22 @@ export default class DefaultPlaybackController implements IPlaybackController {
requestNextTick() requestNextTick()
} else { } else {
if (direction === 'normal') { if (direction === 'normal') {
this._updatePositionInState(range.end) this._updatePositionInState(range[1])
} else if (direction === 'reverse') { } else if (direction === 'reverse') {
this._updatePositionInState(range.start) this._updatePositionInState(range[0])
} else { } else {
const isLastIterationEven = (iterationCount - 1) % 2 === 0 const isLastIterationEven = (iterationCount - 1) % 2 === 0
if (direction === 'alternate') { if (direction === 'alternate') {
if (isLastIterationEven) { if (isLastIterationEven) {
this._updatePositionInState(range.end) this._updatePositionInState(range[1])
} else { } else {
this._updatePositionInState(range.start) this._updatePositionInState(range[0])
} }
} else { } else {
if (isLastIterationEven) { if (isLastIterationEven) {
this._updatePositionInState(range.start) this._updatePositionInState(range[0])
} else { } else {
this._updatePositionInState(range.end) this._updatePositionInState(range[1])
} }
} }
} }