Skip to content

Commit 0a987bc

Browse files
fix: clamping aligned with spec, nits (#773)
1 parent b04e9d2 commit 0a987bc

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

packages/audiodocs/docs/core/audio-param.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ as they are more efficient for continuous changes. For more specific use cases,
4141
| Parameter | Type | Description |
4242
| :---: | :---: | :---- |
4343
| `value` | `number` | A float representing the value the `AudioParam` will be set at given time |
44-
| `startTime` | `number` | The time, in seconds, at which the change in value is going to happen. |
44+
| `startTime` | `number` | The time, in seconds, at which the change in value is going to happen. If it's smaller than [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties), it will be clamped to [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties). |
4545

4646
#### Errors:
4747

@@ -61,7 +61,7 @@ The change begins at the time designated for the previous event. It follows a li
6161
| Parameter | Type | Description |
6262
| :---: | :---: | :---- |
6363
| `value` | `number` | A float representing the value, the `AudioParam` will ramp to by given time. |
64-
| `endTime` | `number` | The time, in seconds, at which the value ramp will end. |
64+
| `endTime` | `number` | The time, in seconds, at which the value ramp will end. If it's smaller than [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties), it will be clamped to [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties). |
6565

6666
#### Errors
6767

@@ -81,7 +81,7 @@ The change begins at the time designated for the previous event. It follows an e
8181
| Parameter | Type | Description |
8282
| :---: | :---: | :---- |
8383
| `value` | `number` | A float representing the value the `AudioParam` will ramp to by given time. |
84-
| `endTime` | `number` | The time, in seconds, at which the value ramp will end. |
84+
| `endTime` | `number` | The time, in seconds, at which the value ramp will end. If it's smaller than [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties), it will be clamped to [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties).|
8585

8686
#### Errors
8787

@@ -101,7 +101,7 @@ This method is useful for decay or release portions of [ADSR envelopes](/docs/ef
101101
| Parameter | Type | Description |
102102
| :---: | :---: | :---- |
103103
| `target` | `number` | A float representing the value to which the `AudioParam` will start transitioning. |
104-
| `startTime` | `number` | The time, in seconds, at which exponential transition will begin. |
104+
| `startTime` | `number` | The time, in seconds, at which exponential transition will begin. If it's smaller than [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties), it will be clamped to [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties). |
105105
| `timeConstant` | `number` | A double representing the time-constant value of an exponential approach to the `target`. |
106106

107107
#### Errors
@@ -122,7 +122,7 @@ Schedules the parameters's value change following a curve defined by given array
122122
| Parameter | Type | Description |
123123
| :---: | :---: | :---- |
124124
| `values` | `Float32Array` | The array of values defining a curve, which change will follow. |
125-
| `startTime` | `number` | The time, in seconds, at which change will begin. |
125+
| `startTime` | `number` | The time, in seconds, at which change will begin. If it's smaller than [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties), it will be clamped to [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties). |
126126
| `duration` | `number` | A double representing total time over which the change will happen. |
127127

128128
#### Errors
@@ -139,7 +139,7 @@ Cancels all scheduled changes after given cancel time.
139139

140140
| Parameter | Type | Description |
141141
| :---: | :---: | :---- |
142-
| `cancelTime` | `number` | The time, in seconds, after which all scheduled changes will be cancelled. |
142+
| `cancelTime` | `number` | The time, in seconds, after which all scheduled changes will be cancelled. If it's smaller than [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties), it will be clamped to [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties). |
143143

144144
#### Errors
145145

@@ -155,7 +155,7 @@ Cancels all scheduled changes after given cancel time, but holds its value at gi
155155

156156
| Parameter | Type | Description |
157157
| :---: | :---: | :---- |
158-
| `cancelTime` | `number` | The time, in seconds, after which all scheduled changes will be cancelled. |
158+
| `cancelTime` | `number` | The time, in seconds, after which all scheduled changes will be cancelled. If it's smaller than [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties), it will be clamped to [`currentTime`](https://docs.swmansion.com/react-native-audio-api/docs/core/base-audio-context#properties).|
159159

160160
#### Errors
161161

packages/react-native-audio-api/src/core/AudioParam.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export default class AudioParam {
3333
);
3434
}
3535

36-
this.audioParam.setValueAtTime(value, startTime);
36+
const clampedTime = Math.max(startTime, this.context.currentTime);
37+
this.audioParam.setValueAtTime(value, clampedTime);
3738

3839
return this;
3940
}
@@ -45,7 +46,8 @@ export default class AudioParam {
4546
);
4647
}
4748

48-
this.audioParam.linearRampToValueAtTime(value, endTime);
49+
const clampedTime = Math.max(endTime, this.context.currentTime);
50+
this.audioParam.linearRampToValueAtTime(value, clampedTime);
4951

5052
return this;
5153
}
@@ -54,13 +56,14 @@ export default class AudioParam {
5456
value: number,
5557
endTime: number
5658
): AudioParam {
57-
if (endTime < 0) {
59+
if (endTime <= 0) {
5860
throw new RangeError(
5961
`endTime must be a finite non-negative number: ${endTime}`
6062
);
6163
}
6264

63-
this.audioParam.exponentialRampToValueAtTime(value, endTime);
65+
const clampedTime = Math.max(endTime, this.context.currentTime);
66+
this.audioParam.exponentialRampToValueAtTime(value, clampedTime);
6467

6568
return this;
6669
}
@@ -78,11 +81,12 @@ export default class AudioParam {
7881

7982
if (timeConstant < 0) {
8083
throw new RangeError(
81-
`timeConstant must be a finite non-negative number: ${startTime}`
84+
`timeConstant must be a finite non-negative number: ${timeConstant}`
8285
);
8386
}
8487

85-
this.audioParam.setTargetAtTime(target, startTime, timeConstant);
88+
const clampedTime = Math.max(startTime, this.context.currentTime);
89+
this.audioParam.setTargetAtTime(target, clampedTime, timeConstant);
8690

8791
return this;
8892
}
@@ -98,17 +102,18 @@ export default class AudioParam {
98102
);
99103
}
100104

101-
if (duration < 0) {
105+
if (duration <= 0) {
102106
throw new RangeError(
103-
`duration must be a finite non-negative number: ${startTime}`
107+
`duration must be a finite strictly-positive number: ${duration}`
104108
);
105109
}
106110

107111
if (values.length < 2) {
108112
throw new InvalidStateError(`values must contain at least two values`);
109113
}
110114

111-
this.audioParam.setValueCurveAtTime(values, startTime, duration);
115+
const clampedTime = Math.max(startTime, this.context.currentTime);
116+
this.audioParam.setValueCurveAtTime(values, clampedTime, duration);
112117

113118
return this;
114119
}
@@ -120,7 +125,8 @@ export default class AudioParam {
120125
);
121126
}
122127

123-
this.audioParam.cancelScheduledValues(cancelTime);
128+
const clampedTime = Math.max(cancelTime, this.context.currentTime);
129+
this.audioParam.cancelScheduledValues(clampedTime);
124130

125131
return this;
126132
}
@@ -132,7 +138,8 @@ export default class AudioParam {
132138
);
133139
}
134140

135-
this.audioParam.cancelAndHoldAtTime(cancelTime);
141+
const clampedTime = Math.max(cancelTime, this.context.currentTime);
142+
this.audioParam.cancelAndHoldAtTime(clampedTime);
136143

137144
return this;
138145
}

0 commit comments

Comments
 (0)