|
| 1 | +import { nextPowerOfTwo } from '../../util' |
| 2 | +import { ANTIALIAS_WAVETABLE_OVERSAMPLING, WAVETABLE_SIZE } from './constants' |
| 3 | +import { fft } from './fft' |
| 4 | + |
| 5 | +class Real { |
| 6 | + @inline static saw(real: StaticArray<f32>, i: u32, j: u32): void { |
| 7 | + const temp: f32 = -1.0 / f32(i) |
| 8 | + real[i] = temp |
| 9 | + real[j] = -temp |
| 10 | + } |
| 11 | + |
| 12 | + @inline static ramp(real: StaticArray<f32>, i: u32, j: u32): void { |
| 13 | + const temp: f32 = -1.0 / f32(i) |
| 14 | + real[i] = -temp |
| 15 | + real[j] = temp |
| 16 | + } |
| 17 | + |
| 18 | + @inline static sqr(real: StaticArray<f32>, i: u32, j: u32): void { |
| 19 | + const temp: f32 = i & 0x01 ? 1.0 / f32(i) : 0.0 |
| 20 | + real[i] = -temp |
| 21 | + real[j] = temp |
| 22 | + } |
| 23 | + |
| 24 | + static sign: f32 = 1.0 |
| 25 | + @inline static tri(real: StaticArray<f32>, i: u32, j: u32): void { |
| 26 | + const temp: f32 = i & 0x01 ? 1.0 / f32(i * i) * (this.sign = -this.sign) : 0.0 |
| 27 | + real[i] = temp |
| 28 | + real[j] = -temp |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export class AntialiasWavetable { |
| 33 | + real: StaticArray<f32> |
| 34 | + imag: StaticArray<f32> |
| 35 | + freqs: StaticArray<f32> |
| 36 | + topFreq: f64 |
| 37 | + maxHarms: u32 |
| 38 | + numOfTables: u32 |
| 39 | + tableLength: u32 |
| 40 | + tableMask: u32 |
| 41 | + tableIndex: u32 = 0 |
| 42 | + stepShift: i32 = 0 |
| 43 | + sampleRate: u32 |
| 44 | + |
| 45 | + saw: StaticArray<StaticArray<f32>> |
| 46 | + ramp: StaticArray<StaticArray<f32>> |
| 47 | + sqr: StaticArray<StaticArray<f32>> |
| 48 | + tri: StaticArray<StaticArray<f32>> |
| 49 | + |
| 50 | + constructor(sampleRate: u32) { |
| 51 | + let topFreq: f64 = 10 |
| 52 | + let maxHarms: u32 = u32(f64(sampleRate) / (3.0 * topFreq) + 0.5) |
| 53 | + const tableLength: u32 = nextPowerOfTwo(maxHarms) * 2 * ANTIALIAS_WAVETABLE_OVERSAMPLING |
| 54 | + const tableMask: u32 = (tableLength - 1) << 2 |
| 55 | + const numOfTables: u32 = u32(Math.log2(f64(maxHarms)) + 1) |
| 56 | + |
| 57 | + // logi(tableLength) |
| 58 | + const saw = new StaticArray<StaticArray<f32>>(numOfTables) |
| 59 | + const ramp = new StaticArray<StaticArray<f32>>(numOfTables) |
| 60 | + const sqr = new StaticArray<StaticArray<f32>>(numOfTables) |
| 61 | + const tri = new StaticArray<StaticArray<f32>>(numOfTables) |
| 62 | + for (let i: u32 = 0; i < numOfTables; i++) { |
| 63 | + saw[i] = new StaticArray<f32>(tableLength) |
| 64 | + ramp[i] = new StaticArray<f32>(tableLength) |
| 65 | + sqr[i] = new StaticArray<f32>(tableLength) |
| 66 | + tri[i] = new StaticArray<f32>(tableLength) |
| 67 | + } |
| 68 | + |
| 69 | + const freqs = new StaticArray<f32>(numOfTables) |
| 70 | + const real = new StaticArray<f32>(tableLength) |
| 71 | + const imag = new StaticArray<f32>(tableLength) |
| 72 | + |
| 73 | + this.real = real |
| 74 | + this.imag = imag |
| 75 | + this.freqs = freqs |
| 76 | + |
| 77 | + this.saw = saw |
| 78 | + this.ramp = ramp |
| 79 | + this.sqr = sqr |
| 80 | + this.tri = tri |
| 81 | + |
| 82 | + this.sampleRate = sampleRate |
| 83 | + this.topFreq = topFreq |
| 84 | + this.maxHarms = maxHarms |
| 85 | + this.numOfTables = numOfTables |
| 86 | + this.tableLength = tableLength |
| 87 | + this.tableMask = tableMask |
| 88 | + this.stepShift = i32(Math.log2(f64(WAVETABLE_SIZE))) - i32(Math.log2(f64(this.tableLength))) |
| 89 | + |
| 90 | + this.makeTables(this.saw, Real.saw) |
| 91 | + this.makeTables(this.ramp, Real.ramp) |
| 92 | + this.makeTables(this.sqr, Real.sqr) |
| 93 | + this.makeTables(this.tri, Real.tri) |
| 94 | + } |
| 95 | + |
| 96 | + getTableIndex(hz: f32): u32 { |
| 97 | + let tableIndex: u32 = 0 |
| 98 | + while ( |
| 99 | + hz >= this.freqs[tableIndex] |
| 100 | + && tableIndex < this.numOfTables - 1 |
| 101 | + ) { |
| 102 | + tableIndex = tableIndex + 1 |
| 103 | + } |
| 104 | + return tableIndex |
| 105 | + } |
| 106 | + |
| 107 | + makeTables(target: StaticArray<StaticArray<f32>>, fn: (real: StaticArray<f32>, i: u32, j: u32) => void): void { |
| 108 | + let topFreq: f64 = this.topFreq |
| 109 | + let i: u32 = 0 |
| 110 | + for (let harms: u32 = this.maxHarms; harms >= 1; harms >>= 1) { |
| 111 | + this.defineWaveform(harms, fn) |
| 112 | + this.makeWavetable(target[i]) |
| 113 | + this.freqs[i] = f32(topFreq) |
| 114 | + topFreq = topFreq * 2 |
| 115 | + i = i + 1 |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + defineWaveform(harms: u32, fn: (real: StaticArray<f32>, i: u32, j: u32) => void): void { |
| 120 | + if (harms > (this.tableLength >> 1)) { |
| 121 | + harms = (this.tableLength >> 1) |
| 122 | + } |
| 123 | + |
| 124 | + this.imag.fill(0) |
| 125 | + this.real.fill(0) |
| 126 | + |
| 127 | + Real.sign = 1.0 |
| 128 | + for (let i: u32 = 1, j: u32 = this.tableLength - 1; i <= harms; i++, j--) { |
| 129 | + fn(this.real, i, j) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + writeSaw(i: u32, j: u32): void { |
| 134 | + const temp: f32 = -1.0 / f32(i) |
| 135 | + this.real[i] = temp |
| 136 | + this.real[j] = -temp |
| 137 | + } |
| 138 | + |
| 139 | + makeWavetable(wave: StaticArray<f32>): void { |
| 140 | + fft(this.tableLength, this.real, this.imag) |
| 141 | + |
| 142 | + // calc normal |
| 143 | + let scale: f32 |
| 144 | + let max: f32 = 0.0 |
| 145 | + for (let i: u32 = 0; i < this.tableLength; i++) { |
| 146 | + let temp: f32 = Mathf.abs(this.imag[i]) |
| 147 | + if (max < temp) max = temp |
| 148 | + } |
| 149 | + scale = 1.0 / max * 0.999 |
| 150 | + |
| 151 | + for (let idx: u32 = 0; idx < this.tableLength; idx++) { |
| 152 | + wave[idx] = this.imag[idx] * scale |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments