nish 0.46.0 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/std/synth.ts CHANGED
@@ -101,11 +101,19 @@ export const synthTone = (
101
101
  }
102
102
  const from: i32 = toI32(start * rate)
103
103
  const n: i32 = toI32(length * rate)
104
+ // Never true: `start` is not negative here. The test is what proves `from + i`
105
+ // stays a sample index.
106
+ if (from < 0) {
107
+ return
108
+ }
109
+ const size: i32 = toI32(out.length)
104
110
  const ratio: f64 = Math.log(f1 / f0)
105
111
  let phase: f64 = 0.0
106
112
  let noise: i32 = SYNTH_NOISE_SEED
107
113
  let held: f64 = 0.0
108
- for (let i: i32 = 0; i < n && from + i < toI32(out.length); i++) {
114
+ // `at` is `from + i`, counted on its own so the test against `size` bounds it.
115
+ let at: i32 = from
116
+ for (let i: i32 = 0; i < n && at < size; i++) {
109
117
  const t: f64 = toF64(i) / rate
110
118
  const f: f64 = f0 * Math.exp((ratio * toF64(i)) / toF64(n))
111
119
  const before: f64 = phase
@@ -128,7 +136,8 @@ export const synthTone = (
128
136
  }
129
137
  const attack: f64 = Math.min(t / 0.002, 1.0)
130
138
  const release: f64 = Math.min((length - t) / 0.005, 1.0)
131
- out[from + i] = out[from + i] + v * volume * attack * release * Math.exp(-t * decay)
139
+ out[at] = out[at] + v * volume * attack * release * Math.exp(-t * decay)
140
+ at = at + 1
132
141
  }
133
142
  }
134
143