You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MicroDexed/third-party/Synth_Dexed/env.h

82 lines
2.4 KiB

/*
Copyright 2017 Pascal Gauthier.
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.
*/
#ifndef __ENV_H
#define __ENV_H
#include "synth.h"
// DX7 envelope generation
#define ACCURATE_ENVELOPE
class Env {
public:
// The rates and levels arrays are calibrated to match the Dx7 parameters
// (ie, value 0..99). The outlevel parameter is calibrated in microsteps
// (ie units of approx .023 dB), with 99 * 32 = nominal full scale. The
// rate_scaling parameter is in qRate units (ie 0..63).
void init(const int32_t rates[4], const int32_t levels[4], int32_t outlevel,
int32_t rate_scaling);
void update(const int32_t rates[4], const int32_t levels[4], int32_t outlevel,
int32_t rate_scaling);
// Result is in Q24/doubling log format. Also, result is subsampled
// for every N samples.
// A couple more things need to happen for this to be used as a gain
// value. First, the # of outputs scaling needs to be applied. Also,
// modulation.
// Then, of course, log to linear.
int32_t getsample();
void keydown(bool down);
static int32_t scaleoutlevel(int32_t outlevel);
void getPosition(char *step);
static void init_sr(double sample_rate);
void transfer(Env &src);
private:
// PG: This code is normalized to 44100, need to put a multiplier
// if we are not using 44100.
static uint32_t sr_multiplier;
int32_t rates_[4];
int32_t levels_[4];
int32_t outlevel_;
int32_t rate_scaling_;
// Level is stored so that 2^24 is one doubling, ie 16 more bits than
// the DX7 itself (fraction is stored in level rather than separate
// counter)
int32_t level_;
int32_t targetlevel_;
bool rising_;
int32_t ix_;
int32_t inc_;
#ifdef ACCURATE_ENVELOPE
int32_t staticcount_;
#endif
bool down_;
void advance(int newix);
};
#endif