summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Zammit <damien@zamaudio.com>2014-04-28 21:03:50 +1000
committerDamien Zammit <damien@zamaudio.com>2014-04-28 21:03:50 +1000
commit25ecc420afc5e71ccc2c6d53f065f81d7e13f6a1 (patch)
tree12247ebb0d33083a7e3155584974271abc852bef
parent787cbe95ef61506af875cfe2ec35d935f8d537e8 (diff)
Fixed envelopes, need to fix defaults
Signed-off-by: Damien Zammit <damien@zamaudio.com>
-rw-r--r--plugins/ZamSynth/ZamSynthPlugin.cpp99
-rw-r--r--plugins/ZamSynth/ZamSynthPlugin.hpp273
2 files changed, 161 insertions, 211 deletions
diff --git a/plugins/ZamSynth/ZamSynthPlugin.cpp b/plugins/ZamSynth/ZamSynthPlugin.cpp
index 376849c..2fdbfb6 100644
--- a/plugins/ZamSynth/ZamSynthPlugin.cpp
+++ b/plugins/ZamSynth/ZamSynthPlugin.cpp
@@ -115,7 +115,8 @@ void ZamSynthPlugin::d_setProgram(uint32_t index)
for (int i = 0; i < 127; i++) {
voice[i].playing = false;
voice[i].notenum = 0;
- voice[i].envpos = 0.f;
+ voice[i].envpos = 0;
+ voice[i].slowcount = 0;
voice[i].curamp = 0.f;
voice[i].vi = 0.f;
voice[i].rampstate = 0.f;
@@ -193,10 +194,9 @@ void ZamSynthPlugin::d_run(float**, float** outputs, uint32_t frames,
const MidiEvent* midievent, uint32_t midicount)
{
float srate = d_getSampleRate();
- int slowdown = (int) srate / 2000;
- uint32_t i,j;
+ int slowfactor = (int) srate / 6000; // 8
+ uint32_t i;
float RD_0;
- int vn;
for (i = 0; i < midicount; i++) {
int type = midievent[i].buf[0] & 0xF0;
@@ -206,37 +206,11 @@ void ZamSynthPlugin::d_run(float**, float** outputs, uint32_t frames,
if (type == 0x90 && chan == 0x0) {
// NOTE ON
//printf("ON: Note\n");
- //find voice with current notenum
- vn = -1;
- nvoices = 0;
- for (int k = 0; k < 128; k++) {
- if (voice[k].playing) {
+ //printf("ON: begin attack\n");
+ for (int k = 0; k < 128; k++)
+ if (voice[k].playing)
nvoices++;
- if (voice[k].notenum == num) {
- vn = k;
- }
- }
- }
- if (vn != -1) {
- //printf("note already playing\n");
- //begin attack
- voice[vn].playing = true;
- voice[vn].envpos = 1;
- voice[vn].notenum = num;
- voice[vn].vi = vel / 127.f;
- voice[vn].curamp = vel / 127.f;
- voice[vn].rampstate = 0;
- continue; // restart note
- }
- nvoices++;
curvoice = &voice[nvoices];
- if (nvoices > MAX_VOICES) {
- //printf("steal first voice\n");
- curvoice = voice; // steal first voice
- nvoices--;
- }
- //printf("ON: nvoices = %d\n", nvoices);
- //printf("ON: begin attack\n");
curvoice->envpos = 1; // begin attack
curvoice->playing = true;
curvoice->notenum = num;
@@ -248,34 +222,15 @@ void ZamSynthPlugin::d_run(float**, float** outputs, uint32_t frames,
// NOTE OFF
//printf("OFF: Note\n");
//find voice with current notenum
- vn = -1;
- int v2 = -1;
nvoices = 0;
for (int k = 0; k < 128; k++) {
- if (voice[k].playing) {
- nvoices++;
- if (voice[k].notenum == num) {
- vn = k;
- }
+ if (voice[k].playing && voice[k].notenum == num) {
+ voice[k].envpos = MAX_ENV / 2 + 1; // begin release;
}
if (!voice[k].playing && voice[k].notenum == num) {
- v2 = k;
+ voice[k].notenum = -1;
}
}
- if (vn != -1) {
- voice[vn].envpos = MAX_ENV / 2 + 1; // begin release;
- //printf("begin release\n");
- continue;
- }
- if (v2 != -1) {
- voice[v2].notenum = -1;
- //printf("note already off, do nothing\n");
- // //voice[v2].envpos = 0;
- //voice[v2].curamp = 0.f;
- //voice[v2].vi = 0.f;
- //voice[v2].playing = false;
- //printf("OFF: nvoices = %d\n", nvoices);
- }
}
}
@@ -294,38 +249,32 @@ void ZamSynthPlugin::d_run(float**, float** outputs, uint32_t frames,
for (k = 0; k < 128; k++) {
j = &voice[k];
if (j->playing) {
- if ((int) j->envpos <= 0) {
+ if (j->envpos <= 0) {
//silence
j->curamp = 0.f;
j->playing = false;
+ j->slowcount = 0;
j->envpos = 0;
- } else if ((int) j->envpos > 0 && (int) j->envpos < MAX_ENV / 2) {
+ } else if (j->envpos > 0 && (int) j->envpos < MAX_ENV / 2) {
//attack
- j->curamp = j->vi * env_y[(int)(j->envpos)];
+ j->curamp = j->vi * env_y[j->envpos];
//printf("att: %d %d curamp=%.2f\n",k,j->envpos, j->curamp);
- j->envpos += 1. / slowdown;
- } else if ((int) j->envpos > MAX_ENV / 2) {
+ j->slowcount++;
+ j->envpos += ((j->slowcount % slowfactor) == 0) ? 1 : 0;
+ } else if (j->envpos > MAX_ENV / 2) {
//release
- j->curamp = j->vi * env_y[(int)(j->envpos)];
+ j->curamp = j->vi * env_y[j->envpos];
//printf("rel: %d %d curamp=%.2f\n",k,j->envpos, j->curamp);
- j->envpos += 1. / slowdown;
- if ((int) j->envpos == MAX_ENV) {
+ j->slowcount++;
+ j->envpos += ((j->slowcount % slowfactor) == 0) ? 1 : 0;
+ if (j->envpos == MAX_ENV) {
//end of release
- j->envpos = 0.f;
+ j->envpos = 0;
+ j->slowcount = 0;
j->curamp = 0.f;
j->vi = 0.f;
j->playing = false;
- curvoice--;
- nvoices = 0;
- for (int k = 0; k < 128; k++)
- if (voice[k].playing)
- nvoices++;
- nvoices--;
- if (nvoices < 0) {
- curvoice = voice + MAX_VOICES-1;
- nvoices++;
- }
- //printf("killed, nvoices=%d\n",nvoices);
+ //printf("killed, n=%d\n",k);
}
} else {
//sustain
diff --git a/plugins/ZamSynth/ZamSynthPlugin.hpp b/plugins/ZamSynth/ZamSynthPlugin.hpp
index 27eb23c..c9c48bf 100644
--- a/plugins/ZamSynth/ZamSynthPlugin.hpp
+++ b/plugins/ZamSynth/ZamSynthPlugin.hpp
@@ -1,136 +1,137 @@
-/*
- * ZamSynth polyphonic synthesiser
- * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com>
- *
- * 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 2 of
- * the License, or any later version.
- *
- * 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.
- *
- * For a full copy of the GNU General Public License see the doc/GPL.txt file.
- */
-
-#ifndef ZAMSYNTHPLUGIN_HPP_INCLUDED
-#define ZAMSYNTHPLUGIN_HPP_INCLUDED
-
-#include <string.h>
-#include "DistrhoPlugin.hpp"
-#define MAX_VOICES 64
-#define AREAHEIGHT 250
-#define MAX_ENV AREAHEIGHT
-
-START_NAMESPACE_DISTRHO
-
-// -----------------------------------------------------------------------
-
-class ZamSynthPlugin : public Plugin
-{
-public:
- enum Parameters
- {
- paramGain,
- paramGraph,
- paramCount
- };
-
- ZamSynthPlugin();
- ~ZamSynthPlugin() override;
-
-protected:
- // -------------------------------------------------------------------
- // Information
-
- const char* d_getLabel() const noexcept override
- {
- return "ZamSynth";
- }
-
- const char* d_getMaker() const noexcept override
- {
- return "Damien Zammit";
- }
-
- const char* d_getLicense() const noexcept override
- {
- return "GPL v2+";
- }
-
- uint32_t d_getVersion() const noexcept override
- {
- return 0x1000;
- }
-
- long d_getUniqueId() const noexcept override
- {
- return d_cconst('Z', 'S', 'T', 'H');
- }
-
- // -------------------------------------------------------------------
- // Init
-
- void d_initParameter(uint32_t index, Parameter& parameter) ;
- void d_initProgramName(uint32_t index, d_string& programName) ;
-
- // -------------------------------------------------------------------
- // Internal data
-
- float d_getParameterValue(uint32_t index) const override;
- void d_setParameterValue(uint32_t index, float value) override;
- void d_setProgram(uint32_t index) ;
-
- // -------------------------------------------------------------------
- // Process
-
- static inline float
- sanitize_denormal(float v) {
- if(!std::isnormal(v))
- return 0.f;
- return v;
- }
-
- static inline float
- from_dB(float gdb) {
- return (exp(gdb/20.f*log(10.f)));
- }
-
- static inline float
- to_dB(float g) {
- return (20.f*log10(g));
- }
-
- float wavetable(float in);
- void d_activate() override;
- void d_deactivate() override;
- void d_run(float** inputs, float** outputs, uint32_t frames,
- const MidiEvent* midievent, uint32_t midicount) override;
- void d_setState(const char* key, const char* value) override;
- void d_initStateKey(unsigned int key, d_string& val) override;
- // -------------------------------------------------------------------
-
-private:
- float gain, graph;
- int nvoices;
- float wave_y[AREAHEIGHT];
- float env_y[MAX_ENV];
- typedef struct v {
- bool playing;
- int notenum;
- float envpos;
- float curamp;
- float vi;
- float rampstate;
- } Voice;
- Voice voice[128];
- Voice* curvoice;
-};
-
-// -----------------------------------------------------------------------
-
-END_NAMESPACE_DISTRHO
-
-#endif // ZAMSYNTH_HPP_INCLUDED
+/*
+ * ZamSynth polyphonic synthesiser
+ * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com>
+ *
+ * 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 2 of
+ * the License, or any later version.
+ *
+ * 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.
+ *
+ * For a full copy of the GNU General Public License see the doc/GPL.txt file.
+ */
+
+#ifndef ZAMSYNTHPLUGIN_HPP_INCLUDED
+#define ZAMSYNTHPLUGIN_HPP_INCLUDED
+
+#include <string.h>
+#include "DistrhoPlugin.hpp"
+#define MAX_VOICES 128
+#define AREAHEIGHT 250
+#define MAX_ENV AREAHEIGHT
+
+START_NAMESPACE_DISTRHO
+
+// -----------------------------------------------------------------------
+
+class ZamSynthPlugin : public Plugin
+{
+public:
+ enum Parameters
+ {
+ paramGain,
+ paramGraph,
+ paramCount
+ };
+
+ ZamSynthPlugin();
+ ~ZamSynthPlugin() override;
+
+protected:
+ // -------------------------------------------------------------------
+ // Information
+
+ const char* d_getLabel() const noexcept override
+ {
+ return "ZamSynth";
+ }
+
+ const char* d_getMaker() const noexcept override
+ {
+ return "Damien Zammit";
+ }
+
+ const char* d_getLicense() const noexcept override
+ {
+ return "GPL v2+";
+ }
+
+ uint32_t d_getVersion() const noexcept override
+ {
+ return 0x1000;
+ }
+
+ long d_getUniqueId() const noexcept override
+ {
+ return d_cconst('Z', 'S', 'T', 'H');
+ }
+
+ // -------------------------------------------------------------------
+ // Init
+
+ void d_initParameter(uint32_t index, Parameter& parameter) ;
+ void d_initProgramName(uint32_t index, d_string& programName) ;
+
+ // -------------------------------------------------------------------
+ // Internal data
+
+ float d_getParameterValue(uint32_t index) const override;
+ void d_setParameterValue(uint32_t index, float value) override;
+ void d_setProgram(uint32_t index) ;
+
+ // -------------------------------------------------------------------
+ // Process
+
+ static inline float
+ sanitize_denormal(float v) {
+ if(!std::isnormal(v))
+ return 0.f;
+ return v;
+ }
+
+ static inline float
+ from_dB(float gdb) {
+ return (exp(gdb/20.f*log(10.f)));
+ }
+
+ static inline float
+ to_dB(float g) {
+ return (20.f*log10(g));
+ }
+
+ float wavetable(float in);
+ void d_activate() override;
+ void d_deactivate() override;
+ void d_run(float** inputs, float** outputs, uint32_t frames,
+ const MidiEvent* midievent, uint32_t midicount) override;
+ void d_setState(const char* key, const char* value) override;
+ void d_initStateKey(unsigned int key, d_string& val) override;
+ // -------------------------------------------------------------------
+
+private:
+ float gain, graph;
+ int nvoices;
+ float wave_y[AREAHEIGHT];
+ float env_y[MAX_ENV];
+ typedef struct v {
+ bool playing;
+ int notenum;
+ int envpos;
+ int slowcount;
+ float curamp;
+ float vi;
+ float rampstate;
+ } Voice;
+ Voice voice[128];
+ Voice* curvoice;
+};
+
+// -----------------------------------------------------------------------
+
+END_NAMESPACE_DISTRHO
+
+#endif // ZAMSYNTH_HPP_INCLUDED