This patch adds the low-level implementation of an LFO, with the DX7 waveforms, but doesn't yet contain the note wiring. It also adds a fast lookup table based exp function, which is mostly used for envelopes. There are also some build tweaks. It's possible some build files are out of sync with the source, but at least the Android app seems to build.master
parent
423408ec9f
commit
10392e8260
@ -0,0 +1,36 @@ |
||||
/*
|
||||
* Copyright 2013 Google Inc. |
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
#include <math.h> |
||||
|
||||
#include "synth.h" |
||||
#include "exp2.h" |
||||
|
||||
int32_t exp2tab[EXP2_N_SAMPLES << 1]; |
||||
|
||||
void Exp2::init() { |
||||
double inc = exp2(1.0 / EXP2_N_SAMPLES); |
||||
double y = 1 << 30; |
||||
for (int i = 0; i < EXP2_N_SAMPLES; i++) { |
||||
exp2tab[(i << 1) + 1] = (int32_t)floor(y + 0.5); |
||||
y *= inc; |
||||
} |
||||
for (int i = 0; i < EXP2_N_SAMPLES - 1; i++) { |
||||
exp2tab[i << 1] = exp2tab[(i << 1) + 3] - exp2tab[(i << 1) + 1]; |
||||
} |
||||
exp2tab[(EXP2_N_SAMPLES << 1) - 2] = (1U << 31) - exp2tab[(EXP2_N_SAMPLES << 1) - 1]; |
||||
} |
||||
|
@ -0,0 +1,46 @@ |
||||
/*
|
||||
* Copyright 2012 Google Inc. |
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
class Exp2 { |
||||
public: |
||||
Exp2(); |
||||
|
||||
static void init(); |
||||
|
||||
// Q24 in, Q24 out
|
||||
static int32_t lookup(int32_t x); |
||||
}; |
||||
|
||||
#define EXP2_LG_N_SAMPLES 10 |
||||
#define EXP2_N_SAMPLES (1 << EXP2_LG_N_SAMPLES) |
||||
|
||||
#define EXP2_INLINE |
||||
|
||||
extern int32_t exp2tab[EXP2_N_SAMPLES << 1]; |
||||
|
||||
#ifdef EXP2_INLINE |
||||
inline |
||||
int32_t Exp2::lookup(int32_t x) { |
||||
const int SHIFT = 24 - EXP2_LG_N_SAMPLES; |
||||
int lowbits = x & ((1 << SHIFT) - 1); |
||||
int x_int = (x >> (SHIFT - 1)) & ((EXP2_N_SAMPLES - 1) << 1); |
||||
int dy = exp2tab[x_int]; |
||||
int y0 = exp2tab[x_int + 1]; |
||||
|
||||
int y = y0 + (((int64_t)dy * (int64_t)lowbits) >> SHIFT); |
||||
return y >> (6 - (x >> 24)); |
||||
} |
||||
#endif |
@ -0,0 +1,69 @@ |
||||
/*
|
||||
* Copyright 2013 Google Inc. |
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
// Low frequency oscillator, compatible with DX7
|
||||
|
||||
#include "synth.h" |
||||
|
||||
#include "sin.h" |
||||
#include "lfo.h" |
||||
|
||||
void Lfo::init(double sample_rate) { |
||||
// constant is 1 << 32 / 15.5s / 11
|
||||
Lfo::unit_ = (int32_t)(N * 25190424 / sample_rate + 0.5); |
||||
} |
||||
|
||||
void Lfo::reset(const char params[6]) { |
||||
int rate = params[0]; // 0..99
|
||||
int sr = rate == 0 ? 1 : (165 * rate) >> 6; |
||||
sr *= sr < 160 ? 11 : (11 + ((sr - 160) >> 4)); |
||||
delta_ = unit_ * sr; |
||||
waveform_ = params[5]; |
||||
sync_ = params[4] != 0; |
||||
} |
||||
|
||||
int32_t Lfo::getsample() { |
||||
phase_ += delta_; |
||||
int32_t x; |
||||
switch (waveform_) { |
||||
case 0: // triangle
|
||||
x = phase_ >> 7; |
||||
x ^= -(phase_ >> 31); |
||||
x &= (1 << 24) - 1; |
||||
return x; |
||||
case 1: // sawtooth down
|
||||
return (~phase_ ^ (1U << 31)) >> 8; |
||||
case 2: // sawtooth up
|
||||
return (phase_ ^ (1U << 31)) >> 8; |
||||
case 3: // square
|
||||
return ((~phase_) >> 7) & (1 << 24); |
||||
case 4: // sine
|
||||
return (1 << 23) + (Sin::lookup(phase_ >> 8) >> 1); |
||||
case 5: // s&h
|
||||
if (phase_ < delta_) { |
||||
randstate_ = (randstate_ * 179 + 17) & 0xff; |
||||
} |
||||
x = randstate_ ^ 0x80; |
||||
return (x + 1) << 16; |
||||
} |
||||
return 1 << 23; |
||||
} |
||||
|
||||
void Lfo::keydown() { |
||||
if (sync_) { |
||||
phase_ = (1U << 31) - 1; |
||||
} |
||||
} |
@ -0,0 +1,36 @@ |
||||
/*
|
||||
* Copyright 2013 Google Inc. |
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
// Low frequency oscillator, compatible with DX7
|
||||
|
||||
class Lfo { |
||||
public: |
||||
static void init(double sample_rate); |
||||
void reset(const char params[6]); |
||||
|
||||
// result is 0..1 in Q24
|
||||
int32_t getsample(); |
||||
|
||||
void keydown(); |
||||
private: |
||||
static uint32_t unit_; |
||||
|
||||
uint32_t phase_; // Q32
|
||||
uint32_t delta_; |
||||
uint8_t waveform_; |
||||
uint8_t randstate_; |
||||
bool sync_; |
||||
}; |
Loading…
Reference in new issue