Turned IPlaybackRange into a tuple
This commit is contained in:
parent
e8d32089ca
commit
e3264264bc
3 changed files with 43 additions and 52 deletions
|
@ -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<IPlaybackRange> {
|
||||
return prism(() => {
|
||||
return {
|
||||
start: 0,
|
||||
end: val(this._lengthD),
|
||||
}
|
||||
return [0, val(this._lengthD)]
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -158,49 +154,44 @@ export default class Sequence {
|
|||
}>,
|
||||
): Promise<boolean> {
|
||||
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,
|
||||
)
|
||||
|
|
|
@ -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<boolean>()
|
||||
|
@ -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)
|
||||
|
|
|
@ -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<boolean>()
|
||||
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue