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.
dexed/Source/PluginProcessor.cpp

605 lines
17 KiB

11 years ago
/**
*
* Copyright (c) 2013 Pascal Gauthier.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
11 years ago
*
* 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
11 years ago
*
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
#include "Dexed.h"
11 years ago
#include "msfa/synth.h"
#include "msfa/freqlut.h"
#include "msfa/sin.h"
#include "msfa/exp2.h"
#include "msfa/env.h"
11 years ago
#include "msfa/pitchenv.h"
#include "msfa/aligned_buf.h"
#include "msfa/fm_op_kernel.h"
11 years ago
10 years ago
File dexedWorkdir;
11 years ago
//==============================================================================
DexedAudioProcessor::DexedAudioProcessor() {
11 years ago
#ifdef DEBUG
11 years ago
Logger *tmp = Logger::getCurrentLogger();
11 years ago
if ( tmp == NULL ) {
Logger::setCurrentLogger(FileLogger::createDateStampedLogger("Dexed", "DebugSession-", "log", "DexedAudioProcessor Created"));
11 years ago
}
11 years ago
TRACE("Hi");
11 years ago
#endif
11 years ago
11 years ago
Exp2::init();
Tanh::init();
Sin::init();
lastStateSave = 0;
11 years ago
currentNote = -1;
11 years ago
vuSignal = 0;
10 years ago
monoMode = 0;
11 years ago
initCtrl();
11 years ago
sendSysexChange = true;
normalizeDxVelocity = false;
sysexComm.listener = this;
10 years ago
engineType = -1;
memset(&voiceStatus, 0, sizeof(VoiceStatus));
prefOptions.applicationName = String("Dexed");
prefOptions.filenameSuffix = String("xml");
prefOptions.folderName = String("DigitalSuburban");
prefOptions.osxLibrarySubFolder = String("Application Support");
10 years ago
dexedWorkdir = prefOptions.getDefaultFile().getParentDirectory();
controllers.values_[kControllerPitchRange] = 3;
controllers.values_[kControllerPitchStep] = 0;
loadPreference();
10 years ago
setEngineType(DEXED_ENGINE_MODERN);
for (int note = 0; note < MAX_ACTIVE_NOTES; ++note) {
voices[note].dx7_note = NULL;
}
10 years ago
setCurrentProgram(0);
nextMidi = NULL;
midiMsg = NULL;
clipboardContent = -1;
11 years ago
}
DexedAudioProcessor::~DexedAudioProcessor() {
11 years ago
TRACE("Bye");
11 years ago
}
//==============================================================================
void DexedAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlock) {
Freqlut::init(sampleRate);
Lfo::init(sampleRate);
PitchEnv::init(sampleRate);
Env::init_sr(sampleRate);
fx.init(sampleRate);
11 years ago
for (int note = 0; note < MAX_ACTIVE_NOTES; ++note) {
voices[note].dx7_note = new Dx7Note;
voices[note].keydown = false;
voices[note].sustained = false;
voices[note].live = false;
}
currentNote = 0;
controllers.values_[kControllerPitch] = 0x2000;
controllers.values_[kControllerModWheel] = 0;
11 years ago
sustain = false;
extra_buf_size = 0;
keyboardState.reset();
nextMidi = new MidiMessage(0xF0);
midiMsg = new MidiMessage(0xF0);
11 years ago
}
void DexedAudioProcessor::releaseResources() {
currentNote = -1;
for (int note = 0; note < MAX_ACTIVE_NOTES; ++note) {
if ( voices[note].dx7_note != NULL ) {
delete voices[note].dx7_note;
voices[note].dx7_note = NULL;
}
11 years ago
voices[note].keydown = false;
voices[note].sustained = false;
voices[note].live = false;
}
keyboardState.reset();
if ( nextMidi != NULL ) {
delete nextMidi;
nextMidi = NULL;
}
if ( midiMsg != NULL ) {
delete midiMsg;
midiMsg = NULL;
}
11 years ago
}
void DexedAudioProcessor::processBlock(AudioSampleBuffer& buffer, MidiBuffer& midiMessages) {
int numSamples = buffer.getNumSamples();
10 years ago
int i;
11 years ago
if ( refreshVoice ) {
10 years ago
for(i=0;i < MAX_ACTIVE_NOTES;i++) {
11 years ago
if ( voices[i].live )
voices[i].dx7_note->update(data, voices[i].midi_note);
}
lfo.reset(data + 137);
refreshVoice = false;
11 years ago
}
keyboardState.processNextMidiBuffer(midiMessages, 0, numSamples, true);
11 years ago
MidiBuffer::Iterator it(midiMessages);
hasMidiMessage = it.getNextEvent(*nextMidi,midiEventPos);
11 years ago
float *channelData = buffer.getSampleData(0);
11 years ago
// flush first events
10 years ago
for (i=0; i < numSamples && i < extra_buf_size; i++) {
11 years ago
channelData[i] = extra_buf[i];
}
11 years ago
// remaining buffer is still to be processed
if (extra_buf_size > numSamples) {
for (int j = 0; j < extra_buf_size - numSamples; j++) {
extra_buf[j] = extra_buf[j + numSamples];
}
extra_buf_size -= numSamples;
10 years ago
// flush the events, they will be process in the next cycle
11 years ago
while(getNextEvent(&it, numSamples)) {
processMidiMessage(midiMsg);
}
11 years ago
} else {
for (; i < numSamples; i += N) {
AlignedBuf<int32_t, N> audiobuf;
float sumbuf[N];
while(getNextEvent(&it, i)) {
processMidiMessage(midiMsg);
}
for (int j = 0; j < N; ++j) {
audiobuf.get()[j] = 0;
sumbuf[j] = 0;
}
int32_t lfovalue = lfo.getsample();
int32_t lfodelay = lfo.getdelay();
10 years ago
11 years ago
for (int note = 0; note < MAX_ACTIVE_NOTES; ++note) {
if (voices[note].live) {
voices[note].dx7_note->compute(audiobuf.get(), lfovalue, lfodelay, &controllers);
for (int j=0; j < N; ++j) {
10 years ago
int32_t val = audiobuf.get()[j]; //& 0xFFFFF000);
val = val >> 4;
11 years ago
int clip_val = val < -(1 << 24) ? 0x8000 : val >= (1 << 24) ? 0x7fff : val >> 9;
float f = ((float) clip_val) / (float) 32768;
if( f > 1 ) f = 1;
if( f < -1 ) f = -1;
sumbuf[j] += f;
audiobuf.get()[j] = 0;
}
}
}
int jmax = numSamples - i;
for (int j = 0; j < N; ++j) {
if (j < jmax) {
channelData[i + j] = sumbuf[j];
} else {
extra_buf[j - jmax] = sumbuf[j];
}
}
}
extra_buf_size = i - numSamples;
11 years ago
}
while(getNextEvent(&it, numSamples)) {
processMidiMessage(midiMsg);
}
11 years ago
fx.process(channelData, numSamples);
10 years ago
for(i=0; i<numSamples; i++) {
11 years ago
float s = std::abs(channelData[i]);
const double decayFactor = 0.99992;
if (s > vuSignal)
vuSignal = s;
else if (vuSignal > 0.001f)
vuSignal *= decayFactor;
else
vuSignal = 0;
}
11 years ago
// DX7 is a mono synth
buffer.copyFrom(1, 0, channelData, numSamples, 1);
11 years ago
}
//==============================================================================
// This creates new instances of the plugin..
AudioProcessor* JUCE_CALLTYPE createPluginFilter() {
return new DexedAudioProcessor();
}
bool DexedAudioProcessor::getNextEvent(MidiBuffer::Iterator* iter,const int samplePos) {
if (hasMidiMessage && midiEventPos <= samplePos) {
*midiMsg = *nextMidi;
hasMidiMessage = iter->getNextEvent(*nextMidi, midiEventPos);
return true;
}
return false;
}
void DexedAudioProcessor::processMidiMessage(const MidiMessage *msg) {
const uint8 *buf = msg->getRawData();
11 years ago
uint8_t cmd = buf[0];
switch(cmd & 0xf0) {
case 0x80 :
keyup(buf[1]);
return;
case 0x90 :
keydown(buf[1], buf[2]);
return;
case 0xb0 : {
int controller = buf[1];
int value = buf[2];
// mod wheel
if ( controller == 1 ) {
controllers.values_[kControllerModWheel] = value;
return;
}
// pedal
11 years ago
if (controller == 64) {
sustain = value != 0;
if (!sustain) {
for (int note = 0; note < MAX_ACTIVE_NOTES; note++) {
if (voices[note].sustained && !voices[note].keydown) {
voices[note].dx7_note->keyup();
voices[note].sustained = false;
}
}
}
return;
11 years ago
}
}
return;
case 0xc0 :
setCurrentProgram(buf[1]);
return;
}
switch (cmd) {
case 0xe0 :
controllers.values_[kControllerPitch] = buf[1] | (buf[2] << 7);
11 years ago
break;
}
}
void DexedAudioProcessor::keydown(uint8_t pitch, uint8_t velo) {
if ( velo == 0 ) {
keyup(pitch);
return;
}
pitch += data[144] - 24;
if ( normalizeDxVelocity ) {
velo = ((float)velo) * 0.7874015; // 100/127
}
11 years ago
int note = currentNote;
for (int i=0; i<MAX_ACTIVE_NOTES; i++) {
11 years ago
if (!voices[note].keydown) {
currentNote = (note + 1) % MAX_ACTIVE_NOTES;
lfo.keydown(); // TODO: should only do this if # keys down was 0
voices[note].midi_note = pitch;
voices[note].sustained = sustain;
voices[note].keydown = true;
11 years ago
voices[note].dx7_note->init(data, pitch, velo);
if ( data[136] )
voices[note].dx7_note->oscSync();
break;
11 years ago
}
note = (note + 1) % MAX_ACTIVE_NOTES;
}
if ( monoMode ) {
for(int i=0; i<MAX_ACTIVE_NOTES; i++) {
if ( voices[i].live ) {
10 years ago
// all keys are up, only transfert signal
if ( ! voices[i].keydown ) {
voices[i].live = false;
10 years ago
voices[note].dx7_note->transferSignal(*voices[i].dx7_note);
break;
}
if ( voices[i].midi_note < pitch ) {
voices[i].live = false;
10 years ago
voices[note].dx7_note->transferState(*voices[i].dx7_note);
break;
}
return;
}
}
}
10 years ago
voices[note].live = true;
11 years ago
}
void DexedAudioProcessor::keyup(uint8_t pitch) {
pitch += data[144] - 24;
int note;
for (note=0; note<MAX_ACTIVE_NOTES; ++note) {
if ( voices[note].midi_note == pitch && voices[note].keydown ) {
voices[note].keydown = false;
break;
}
}
// note not found ?
10 years ago
if ( note >= MAX_ACTIVE_NOTES ) {
TRACE("note-off not found???");
return;
}
if ( monoMode ) {
int highNote = -1;
int target = 0;
for (int i=0; i<MAX_ACTIVE_NOTES;i++) {
10 years ago
if ( voices[i].keydown && voices[i].midi_note > highNote ) {
target = i;
highNote = voices[i].midi_note;
11 years ago
}
}
if ( highNote != -1 ) {
voices[note].live = false;
voices[target].live = true;
10 years ago
voices[target].dx7_note->transferState(*voices[note].dx7_note);
}
}
if ( sustain ) {
voices[note].sustained = true;
} else {
voices[note].dx7_note->keyup();
11 years ago
}
}
void DexedAudioProcessor::panic() {
for(int i=0;i<MAX_ACTIVE_NOTES;i++) {
voices[i].keydown = false;
voices[i].live = false;
10 years ago
if ( voices[i].dx7_note != NULL ) {
voices[i].dx7_note->oscSync();
10 years ago
}
}
keyboardState.reset();
}
void DexedAudioProcessor::handleIncomingMidiMessage(MidiInput* source, const MidiMessage& message) {
if ( message.isActiveSense() )
return;
sysexComm.inActivity = true;
if ( ! message.isSysEx() )
return;
//const uint8 *buf = msg->getSysExData();
const uint8 *buf = message.getRawData();
int sz = message.getRawDataSize();
if ( sz < 3 )
return;
TRACE("SYSEX RECEIVED %d", sz);
// test if it is a Yamaha Sysex
if ( buf[1] != 0x43 ) {
TRACE("not a yamaha sysex %d", buf[0]);
return;
}
// single voice dump
if ( buf[3] == 0 ) {
if ( sz < 155 ) {
TRACE("wrong single voice datasize %d", sz);
return;
}
TRACE("program update sysex");
updateProgramFromSysex(buf+6);
String name = normalizeSysexName((const char *) buf+151);
packProgram((uint8_t *) sysex, (uint8_t *) data, currentProgram, name);
programNames.set(currentProgram, name);
}
// 32 voice dump
if ( buf[3] == 9 ) {
if ( sz < 4104 ) {
TRACE("wrong 32 voice datasize %d", sz);
return;
}
TRACE("update 32bulk voice");
importSysex((const char *)buf);
setCurrentProgram(0);
}
updateHostDisplay();
forceRefreshUI = true;
}
10 years ago
int DexedAudioProcessor::getEngineType() {
return engineType;
}
10 years ago
void DexedAudioProcessor::setEngineType(int tp) {
switch (tp) {
case DEXED_ENGINE_MODERN :
controllers.core = &engineMsfa;
break;
10 years ago
case DEXED_ENGINE_MARKI:
controllers.sinBitFilter = 0xFFFFC000; // 10 bit
controllers.dacBitFilter = 0xFFFFF000; // semi 14 bit
10 years ago
controllers.core = &engineMkI;
break;
10 years ago
case DEXED_ENGINE_OPL:
controllers.core = &engineOpl;
break;
}
10 years ago
engineType = tp;
}
void DexedAudioProcessor::setMonoMode(bool mode) {
panic();
monoMode = mode;
}
11 years ago
// ====================================================================
11 years ago
bool DexedAudioProcessor::peekVoiceStatus() {
11 years ago
if ( currentNote == -1 )
return false;
// we are trying to find the last "keydown" note
int note = currentNote;
for (int i = 0; i < MAX_ACTIVE_NOTES; i++) {
if (voices[note].keydown) {
11 years ago
voices[note].dx7_note->peekVoiceStatus(voiceStatus);
11 years ago
return true;
}
if ( --note < 0 )
note = MAX_ACTIVE_NOTES-1;
}
// not found; try a live note
note = currentNote;
for (int i = 0; i < MAX_ACTIVE_NOTES; i++) {
if (voices[note].live) {
11 years ago
voices[note].dx7_note->peekVoiceStatus(voiceStatus);
11 years ago
return true;
}
if ( --note < 0 )
note = MAX_ACTIVE_NOTES-1;
}
return true;
}
const String DexedAudioProcessor::getInputChannelName (int channelIndex) const {
return String (channelIndex + 1);
}
const String DexedAudioProcessor::getOutputChannelName (int channelIndex) const {
return String (channelIndex + 1);
}
bool DexedAudioProcessor::isInputChannelStereoPair (int index) const {
return true;
}
bool DexedAudioProcessor::isOutputChannelStereoPair (int index) const {
return true;
}
bool DexedAudioProcessor::acceptsMidi() const {
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool DexedAudioProcessor::producesMidi() const {
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
bool DexedAudioProcessor::silenceInProducesSilenceOut() const {
return false;
}
double DexedAudioProcessor::getTailLengthSeconds() const {
return 0.0;
}
const String DexedAudioProcessor::getName() const {
return JucePlugin_Name;
}
//==============================================================================
bool DexedAudioProcessor::hasEditor() const {
return true; // (change this to false if you choose to not supply an editor)
}
11 years ago
void DexedAudioProcessor::updateUI() {
// notify host something has changed
updateHostDisplay();
11 years ago
AudioProcessorEditor *editor = getActiveEditor();
if ( editor == NULL ) {
return;
}
DexedAudioProcessorEditor *dexedEditor = (DexedAudioProcessorEditor *) editor;
11 years ago
dexedEditor->updateUI();
11 years ago
}
11 years ago
AudioProcessorEditor* DexedAudioProcessor::createEditor() {
return new DexedAudioProcessorEditor (this);
}
void DexedAudioProcessor::handleAsyncUpdate() {
updateUI();
}
11 years ago
void dexed_trace(const char *source, const char *fmt, ...) {
11 years ago
char output[4096];
11 years ago
va_list argptr;
va_start(argptr, fmt);
vsnprintf(output, 4095, fmt, argptr);
va_end(argptr);
String dest;
dest << source << " " << output;
Logger::writeToLog(dest);
}