mark I 9-bit engine

pull/1/head
asb2m10 9 years ago
parent ce3db6b2b1
commit 627d7b8015
  1. BIN
      Builds/MacOSX/Dexed.xcodeproj/project.xcworkspace/xcuserdata/asb2m10.xcuserdatad/UserInterfaceState.xcuserstate
  2. 2
      README.md
  3. 310
      Source/EngineMkI.cpp
  4. 4
      Source/ParamDialog.cpp
  5. 4
      Source/PluginParam.cpp
  6. 5
      Source/PluginProcessor.cpp

@ -66,7 +66,7 @@ Credits & thanks
* DX Synth engine : Raph Levien and the [msfa](https://code.google.com/p/music-synthesizer-for-android) team * DX Synth engine : Raph Levien and the [msfa](https://code.google.com/p/music-synthesizer-for-android) team
* Graphical design : [AZur Studio](http://bji.yukihotaru.com/) * Graphical design : [AZur Studio](http://bji.yukihotaru.com/)
* LP Filter : Filatov Vadim (2DaT); taken from the excellent [Obxd](https://obxd.wordpress.com) project * LP Filter : Filatov Vadim (2DaT); taken from the excellent [Obxd](https://obxd.wordpress.com) project
* PPPlay : Great [OPL3](http://sourceforge.net/projects/peepeeplayer) implementation, with documented code :D * PPPlay : Great [OPL3](https://github.com/stohrendorf/ppplay) implementation, with documented code :D
* DX7 program compilation : Jean-Marc Desprez (author of [SynprezFM](http://www.synprez.com/SynprezFM)) * DX7 program compilation : Jean-Marc Desprez (author of [SynprezFM](http://www.synprez.com/SynprezFM))
* DX7 programs : Dave Benson, Frank Carvalho, Tim Conrardy, Jack Deckard, Chris Dodunski, Tim Garrett, Hitaye, Stephan Ibsen, Christian Jezreel, Narfman, Godric Wilkie * DX7 programs : Dave Benson, Frank Carvalho, Tim Conrardy, Jack Deckard, Chris Dodunski, Tim Garrett, Hitaye, Stephan Ibsen, Christian Jezreel, Narfman, Godric Wilkie
* falkTX [distrho](http://distrho.sourceforge.net/) * falkTX [distrho](http://distrho.sourceforge.net/)

@ -1,18 +1,24 @@
/* /*
* Copyright 2014 Pascal Gauthier. * Copyright (C) 2015 Pascal Gauthier
* Copyright 2012 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * This program is free software; you can redistribute it and/or modify
* you may not use this file except in compliance with the License. * it under the terms of the GNU General Public License as published by
* You may obtain a copy of the License at * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* *
* http://www.apache.org/licenses/LICENSE-2.0 * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* The code is based on ppplay https://github.com/stohrendorf/ppplay and opl3
* math documentation :
* https://github.com/gtaylormb/opl3_fpga/blob/master/docs/opl3math/opl3math.pdf
* *
* 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 "EngineMkI.h" #include "EngineMkI.h"
@ -23,10 +29,10 @@
#include "msfa/sin.h" #include "msfa/sin.h"
#include "msfa/exp2.h" #include "msfa/exp2.h"
static const uint16_t MKI_BITDEPTH = 8; #ifdef DEBUG
static const uint16_t MKI_TABLESIZE = 1<<MKI_BITDEPTH; #include "time.h"
static const uint16_t MKI_TABLEFILTER = MKI_TABLESIZE-1; //#define MKIDEBUG
static const uint16_t NEGATIVE_BIT = 0x8000; #endif
#ifdef _WIN32 #ifdef _WIN32
__declspec(align(16)) int zeros[N] = {0}; __declspec(align(16)) int zeros[N] = {0};
@ -34,63 +40,103 @@ static const uint16_t NEGATIVE_BIT = 0x8000;
const int32_t __attribute__ ((aligned(16))) zeros[N] = {0}; const int32_t __attribute__ ((aligned(16))) zeros[N] = {0};
#endif #endif
static uint16_t sinLogTable[MKI_TABLESIZE]; static const uint16_t NEGATIVE_BIT = 0x8000;
static uint16_t sinExpTable[MKI_TABLESIZE]; static const uint16_t ENV_BITDEPTH = 14;
static const uint16_t SINLOG_BITDEPTH = 10;
static const uint16_t SINLOG_TABLESIZE = 1<<SINLOG_BITDEPTH;
static uint16_t sinLogTable[SINLOG_TABLESIZE];
static const uint16_t SINEXP_BITDEPTH = 10;
static const uint16_t SINEXP_TABLESIZE = 1<<SINEXP_BITDEPTH;
static uint16_t sinExpTable[SINEXP_TABLESIZE];
static inline uint16_t sinLog(uint16_t phi) {
const uint16_t SINLOG_TABLEFILTER = SINLOG_TABLESIZE-1;
const uint16_t index = (phi & SINLOG_TABLEFILTER);
switch( ( phi & (SINLOG_TABLESIZE * 3) ) ) {
case 0:
return sinLogTable[index];
case SINLOG_TABLESIZE:
return sinLogTable[index ^ SINLOG_TABLEFILTER];
case SINLOG_TABLESIZE * 2 :
return sinLogTable[index] | NEGATIVE_BIT;
default:
return sinLogTable[index ^ SINLOG_TABLEFILTER] | NEGATIVE_BIT;
}
}
EngineMkI::EngineMkI() { EngineMkI::EngineMkI() {
float bitReso = MKI_TABLESIZE; float bitReso = SINLOG_TABLESIZE;
for(int i=0;i<MKI_TABLESIZE;i++) { for(int i=0;i<SINLOG_TABLESIZE;i++) {
float x1 = sin(((0.5+i)/bitReso) * M_PI/2.0); float x1 = sin(((0.5+i)/bitReso) * M_PI/2.0);
sinLogTable[i] = round(-bitReso * log2(x1)); sinLogTable[i] = round(-1024 * log2(x1));
} }
bitReso = MKI_TABLESIZE; bitReso = SINEXP_TABLESIZE;
for(int i=0;i<MKI_TABLESIZE;i++) { for(int i=0;i<SINEXP_TABLESIZE;i++) {
float x1 = (pow(2, float(i)/bitReso)-1)*1024; float x1 = (pow(2, float(i)/bitReso)-1) * 4096;
sinExpTable[i] = round(x1); sinExpTable[i] = round(x1);
} }
}
static inline uint16_t sinLog(uint16_t phi) {
const uint16_t index = (phi & MKI_TABLEFILTER);
switch( ( phi & (MKI_TABLESIZE*3) ) ) { #ifdef MKIDEBUG
case 0x0000: char buffer[4096];
// rising quarter wave Shape A int pos = 0;
return sinLogTable[index];
case MKI_TABLESIZE: TRACE("****************************************");
// falling quarter wave Shape B for(int i=0;i<SINLOG_TABLESIZE;i++) {
return sinLogTable[index ^ MKI_TABLEFILTER]; pos += sprintf(buffer+pos, "%d ", sinLogTable[i]);
case (MKI_TABLESIZE*2): if ( pos > 90 ) {
// rising quarter wave -ve Shape C TRACE("SINLOGTABLE: %s" ,buffer);
return sinLogTable[index] | NEGATIVE_BIT; buffer[0] = 0;
default: pos = 0;
// falling quarter wave -ve Shape D }
return sinLogTable[index ^ MKI_TABLEFILTER] | NEGATIVE_BIT;
} }
TRACE("SINLOGTABLE: %s", buffer);
buffer[0] = 0;
pos = 0;
TRACE("----------------------------------------");
for(int i=0;i<SINEXP_TABLESIZE;i++) {
pos += sprintf(buffer+pos, "%d ", sinExpTable[i]);
if ( pos > 90 ) {
TRACE("SINEXTTABLE: %s" ,buffer);
buffer[0] = 0;
pos = 0;
}
}
TRACE("SINEXTTABLE: %s", buffer);
TRACE("****************************************");
#endif
} }
inline int32_t mkiSin(int32_t phase, uint16_t env) { inline int32_t mkiSin(int32_t phase, uint16_t env) {
const uint16_t shift = 22 - MKI_BITDEPTH; uint16_t expVal = sinLog(phase >> (22 - SINLOG_BITDEPTH)) + (env);
uint16_t expVal = sinLog(phase >> shift) + (env << 3); //int16_t expValShow = expVal;
const bool isSigned = expVal & NEGATIVE_BIT;
const bool isSigned = expVal & NEGATIVE_BIT;
expVal &= ~NEGATIVE_BIT; expVal &= ~NEGATIVE_BIT;
// expVal: 0..2137+511*8 = 0..6225
// result: 0..1018+1024
uint16_t result = 0x0400 + sinExpTable[( expVal & MKI_TABLEFILTER ) ^ MKI_TABLEFILTER];
result <<= 1;
result >>= ( expVal >> 8 ); // exp
uint32_t ret; const uint16_t SINEXP_FILTER = 0x3FF;
if( isSigned ) { uint16_t result = 4096 + sinExpTable[( expVal & SINEXP_FILTER ) ^ SINEXP_FILTER];
// -1 for one's complement
ret = -result - 1; //uint16_t resultB4 = result;
} else { result >>= ( expVal >> 10 ); // exp
ret = result;
#ifdef MKIDEBUG
if ( ( time(NULL) % 5 ) == 0 ) {
if ( expValShow < 0 ) {
expValShow = (expValShow + 0x7FFF) * -1;
}
//TRACE(",%d,%d,%d,%d,%d,%d", phase >> (22 - SINLOG_BITDEPTH), env, expValShow, ( expVal & SINEXP_FILTER ) ^ SINEXP_FILTER, resultB4, result);
} }
return ret << shift; #endif
if( isSigned )
return (-result - 1) << 13;
else
return result << 13;
} }
void EngineMkI::compute(int32_t *output, const int32_t *input, void EngineMkI::compute(int32_t *output, const int32_t *input,
@ -148,44 +194,50 @@ void EngineMkI::compute_fb(int32_t *output, int32_t phase0, int32_t freq,
fb_buf[1] = y; fb_buf[1] = y;
} }
void EngineMkI::render(int32_t *output, FmOpParams *params, int algorithm,
// exclusively used for ALGO 6 with feedback int32_t *fb_buf, int feedback_shift) {
void EngineMkI::compute_fb2(int32_t *output, FmOpParams *parms, int32_t gain01, int32_t gain02, int32_t *fb_buf, int fb_shift) { const uint16_t ENV_MAX = 1<<ENV_BITDEPTH;
int32_t dgain[2]; const uint16_t kLevelThresh = ENV_MAX-100; // really ???? uhuhuh
int32_t gain[2]; const FmAlgorithm alg = algorithms[algorithm];
int32_t phase[2]; bool has_contents[3] = { true, false, false };
int32_t y0 = fb_buf[0]; for (int op = 0; op < 6; op++) {
int32_t y = fb_buf[1]; int flags = alg.ops[op];
bool add = (flags & OUT_BUS_ADD) != 0;
phase[0] = parms[0].phase; FmOpParams &param = params[op];
phase[1] = parms[1].phase; int inbus = (flags >> 4) & 3;
int outbus = flags & 3;
gain[0] = gain01; int32_t *outptr = (outbus == 0) ? output : buf_[outbus - 1].get();
gain[1] = parms[1].gain_out; int32_t gain1 = param.gain_out == 0 ? (ENV_MAX-1) : param.gain_out;
int32_t gain2 = ENV_MAX-(param.level_in >> (28-ENV_BITDEPTH));
dgain[0] = (gain02 - gain01 + (N >> 1)) >> LG_N; param.gain_out = gain2;
parms[1].gain_out = Exp2::lookup(parms[1].level_in - (14 * (1 << 24)));
dgain[1] = (parms[1].gain_out - gain[1] + (N >> 1)) >> LG_N;
for (int i = 0; i < N; i++) {
// op 0
gain[0] += dgain[0];
int32_t scaled_fb = (y0 + y) >> (fb_shift + 2); // tsk tsk tsk: this needs some tuning
y0 = y;
y = Sin::lookup(phase[0] + scaled_fb);
y = ((int64_t)y * (int64_t)gain[0]) >> 24;
phase[0] += parms[0].freq;
// op 1 if (gain1 <= kLevelThresh || gain2 <= kLevelThresh) {
gain[1] += dgain[1]; if (!has_contents[outbus]) {
y = Sin::lookup(phase[1] + y); add = false;
y = ((int64_t)y * (int64_t)gain[1]) >> 24; }
output[i] = y; if (inbus == 0 || !has_contents[inbus]) {
phase[1] += parms[1].freq; // todo: more than one op in a feedback loop
if ((flags & 0xc0) == 0xc0 && feedback_shift < 16) {
// cout << op << " fb " << inbus << outbus << add << endl;
compute_fb(outptr, param.phase, param.freq,
gain1, gain2,
fb_buf, feedback_shift, add);
} else {
// cout << op << " pure " << inbus << outbus << add << endl;
compute_pure(outptr, param.phase, param.freq,
gain1, gain2, add);
}
} else {
// cout << op << " normal " << inbus << outbus << " " << param.freq << add << endl;
compute(outptr, buf_[inbus - 1].get(),
param.phase, param.freq, gain1, gain2, add);
}
has_contents[outbus] = true;
} else if (!add) {
has_contents[outbus] = false;
}
param.phase += param.freq << LG_N;
} }
fb_buf[0] = y0;
fb_buf[1] = y;
} }
const FmAlgorithm EngineMkI::algo2[32] = { const FmAlgorithm EngineMkI::algo2[32] = {
@ -272,49 +324,43 @@ void EngineMkI::compute_fb3(int32_t *output, FmOpParams *parms, int32_t gain01,
fb_buf[1] = y; fb_buf[1] = y;
} }
void EngineMkI::render(int32_t *output, FmOpParams *params, int algorithm, // exclusively used for ALGO 6 with feedback
int32_t *fb_buf, int feedback_shift) { void EngineMkI::compute_fb2(int32_t *output, FmOpParams *parms, int32_t gain01, int32_t gain02, int32_t *fb_buf, int fb_shift) {
const int kLevelThresh = 507; // really ???? int32_t dgain[2];
const FmAlgorithm alg = algorithms[algorithm]; int32_t gain[2];
bool has_contents[3] = { true, false, false }; int32_t phase[2];
for (int op = 0; op < 6; op++) { int32_t y0 = fb_buf[0];
int flags = alg.ops[op]; int32_t y = fb_buf[1];
bool add = (flags & OUT_BUS_ADD) != 0;
FmOpParams &param = params[op]; phase[0] = parms[0].phase;
int inbus = (flags >> 4) & 3; phase[1] = parms[1].phase;
int outbus = flags & 3;
int32_t *outptr = (outbus == 0) ? output : buf_[outbus - 1].get(); gain[0] = gain01;
int32_t gain1 = param.gain_out == 0 ? 511 : param.gain_out; gain[1] = parms[1].gain_out;
int32_t gain2 = 512-(param.level_in >> 19);
param.gain_out = gain2; dgain[0] = (gain02 - gain01 + (N >> 1)) >> LG_N;
parms[1].gain_out = Exp2::lookup(parms[1].level_in - (14 * (1 << 24)));
dgain[1] = (parms[1].gain_out - gain[1] + (N >> 1)) >> LG_N;
for (int i = 0; i < N; i++) {
// op 0
gain[0] += dgain[0];
int32_t scaled_fb = (y0 + y) >> (fb_shift + 2); // tsk tsk tsk: this needs some tuning
y0 = y;
y = Sin::lookup(phase[0] + scaled_fb);
y = ((int64_t)y * (int64_t)gain[0]) >> 24;
phase[0] += parms[0].freq;
if (gain1 <= kLevelThresh || gain2 <= kLevelThresh) { // op 1
if (!has_contents[outbus]) { gain[1] += dgain[1];
add = false; y = Sin::lookup(phase[1] + y);
} y = ((int64_t)y * (int64_t)gain[1]) >> 24;
if (inbus == 0 || !has_contents[inbus]) { output[i] = y;
// todo: more than one op in a feedback loop phase[1] += parms[1].freq;
if ((flags & 0xc0) == 0xc0 && feedback_shift < 16) {
// cout << op << " fb " << inbus << outbus << add << endl;
compute_fb(outptr, param.phase, param.freq,
gain1, gain2,
fb_buf, feedback_shift, add);
} else {
// cout << op << " pure " << inbus << outbus << add << endl;
compute_pure(outptr, param.phase, param.freq,
gain1, gain2, add);
}
} else {
// cout << op << " normal " << inbus << outbus << " " << param.freq << add << endl;
compute(outptr, buf_[inbus - 1].get(),
param.phase, param.freq, gain1, gain2, add);
}
has_contents[outbus] = true;
} else if (!add) {
has_contents[outbus] = false;
}
param.phase += param.freq << LG_N;
} }
fb_buf[0] = y0;
fb_buf[1] = y;
} }
/* /*

@ -70,7 +70,7 @@ ParamDialog::ParamDialog ()
engineReso->setJustificationType (Justification::centredLeft); engineReso->setJustificationType (Justification::centredLeft);
engineReso->setTextWhenNothingSelected (String::empty); engineReso->setTextWhenNothingSelected (String::empty);
engineReso->setTextWhenNoChoicesAvailable (TRANS("(no choices)")); engineReso->setTextWhenNoChoicesAvailable (TRANS("(no choices)"));
engineReso->addItem (TRANS("Modern (Direct)"), 1); engineReso->addItem (TRANS("Modern (24-bit)"), 1);
engineReso->addItem (TRANS("Mark I"), 2); engineReso->addItem (TRANS("Mark I"), 2);
engineReso->addItem (TRANS("OPL Series"), 3); engineReso->addItem (TRANS("OPL Series"), 3);
engineReso->addListener (this); engineReso->addListener (this);
@ -635,7 +635,7 @@ BEGIN_JUCER_METADATA
textBoxWidth="80" textBoxHeight="20" skewFactor="1"/> textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
<COMBOBOX name="new combo box" id="4087ff978c3d9e8d" memberName="engineReso" <COMBOBOX name="new combo box" id="4087ff978c3d9e8d" memberName="engineReso"
virtualName="" explicitFocusOrder="0" pos="160 156 168 24" editable="0" virtualName="" explicitFocusOrder="0" pos="160 156 168 24" editable="0"
layout="33" items="Modern (Direct)&#10;Mark I&#10;OPL Series" layout="33" items="Modern (24-bit)&#10;Mark I&#10;OPL Series"
textWhenNonSelected="" textWhenNoItems="(no choices)"/> textWhenNonSelected="" textWhenNoItems="(no choices)"/>
<TOGGLEBUTTON name="showKeyboard" id="c963d2cb8e49ffd7" memberName="showKeyboard" <TOGGLEBUTTON name="showKeyboard" id="c963d2cb8e49ffd7" memberName="showKeyboard"
virtualName="" explicitFocusOrder="0" pos="264 96 56 24" buttonText="" virtualName="" explicitFocusOrder="0" pos="264 96 56 24" buttonText=""

@ -578,7 +578,7 @@ void DexedAudioProcessor::loadPreference() {
} }
if ( prop.containsKey( String("engineType") ) ) { if ( prop.containsKey( String("engineType") ) ) {
engineType = prop.getIntValue( String("engineType") ); setEngineType(prop.getIntValue(String("engineType")));
} }
if ( prop.containsKey( String("showKeyboard") ) ) { if ( prop.containsKey( String("showKeyboard") ) ) {
@ -627,7 +627,7 @@ void DexedAudioProcessor::savePreference() {
controllers.at.setConfig(mod_cfg); controllers.at.setConfig(mod_cfg);
prop.setValue(String("aftertouchMod"), mod_cfg); prop.setValue(String("aftertouchMod"), mod_cfg);
//prop.setValue(String("engineResolution"), engineResolution); prop.setValue(String("engineType"), (int) engineType);
prop.save(); prop.save();
} }

@ -61,12 +61,11 @@ DexedAudioProcessor::DexedAudioProcessor() {
showKeyboard = true; showKeyboard = true;
memset(&voiceStatus, 0, sizeof(VoiceStatus)); memset(&voiceStatus, 0, sizeof(VoiceStatus));
setEngineType(DEXED_ENGINE_MODERN);
controllers.values_[kControllerPitchRange] = 3; controllers.values_[kControllerPitchRange] = 3;
controllers.values_[kControllerPitchStep] = 0; controllers.values_[kControllerPitchStep] = 0;
loadPreference(); loadPreference();
setEngineType(DEXED_ENGINE_MODERN);
for (int note = 0; note < MAX_ACTIVE_NOTES; ++note) { for (int note = 0; note < MAX_ACTIVE_NOTES; ++note) {
voices[note].dx7_note = NULL; voices[note].dx7_note = NULL;
@ -475,6 +474,8 @@ int DexedAudioProcessor::getEngineType() {
} }
void DexedAudioProcessor::setEngineType(int tp) { void DexedAudioProcessor::setEngineType(int tp) {
TRACE("settings engine %d", tp);
switch (tp) { switch (tp) {
case DEXED_ENGINE_MODERN : case DEXED_ENGINE_MODERN :
controllers.core = &engineMsfa; controllers.core = &engineMsfa;

Loading…
Cancel
Save