Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Pin
unsigned char LED[] = {6,3,5};
const int AUDIO_L = 9;
const int AUDIO_R = 10;
// 123458,123466
const int RANDOM_SEED = 123466;
const int STATE_ATTACK = 0;
const int STATE_DECAY = 1;
const int STATE_SUSTAIN = 2;
const int STATE_RELEASE = 3;
const int STATE_SLEEP = 4;
const int WAVE_BUFFER_BITS = 8;
const int INT_BITS = 32;
const int FIXED_BITS = 14;
const int FIXED_BITS_ENV = 8;
const int WAVE_ADDR_SHIFT = (INT_BITS - WAVE_BUFFER_BITS);
const int WAVE_ADDR_SHIFT_M = (WAVE_ADDR_SHIFT - FIXED_BITS);
const int FIXED_SCALE = (1 << FIXED_BITS);
const int FIXED_SCALE_M1 = (FIXED_SCALE - 1);
const int WAVE_BUFFER_SIZE = (1 << WAVE_BUFFER_BITS);
const int WAVE_BUFFER_SIZE_M1 = (WAVE_BUFFER_SIZE - 1);
const int OSCS = 4;
const int REVERB_BUFFER_SIZE = 0x1000;
const int TEMPO = (1 << 12);
const int OUT_BITS = 7;
const int OUT_SHIFT = (FIXED_BITS - OUT_BITS);
const int OUT_SHIFT_M1 = (OUT_SHIFT - 1);
const int OUT_OFFSET = (1 << OUT_BITS);
const int SEQ_LENGTH = 16;
const int SAMPLE_US = 40;
const int CONST03 = (1 << FIXED_BITS << FIXED_BITS_ENV);
const int CONST04 = (FIXED_SCALE >> 0);
const int CONST05 = (CONST04 / OSCS);
const int REVERB_VOLUME = (CONST04 >> 2);
typedef struct
{
int envelopeLevelA;
int envelopeLevelS;
int envelopeDiffA;
int envelopeDiffD;
int envelopeDiffR;
int modPatch0;
int modPatch1;
int modLevel0;
int modLevel1;
int levelL;
int levelR;
int levelRev;
int state;
int count;
int currentLevel;
int pitch;
int velocity;
int mod0;
int mod1;
int outData;
int outWaveL;
int outWaveR;
int outRevL;
int outRevR;
boolean mixOut;
boolean noteOn;
boolean noteOnSave;
boolean goToggle;
boolean outDoneToggle;
} params_t;
signed char reverbBufferL[REVERB_BUFFER_SIZE];
signed char reverbBufferR[REVERB_BUFFER_SIZE];
int32_t timerNext;
int counter;
int seqCounter;
int barCounter;
int deleteCounter;
int chord;
int note;
int sc;
int reverbCounter;
int reverbAddrL;
int reverbAddrR;
void synth()
{ // patch
for (int i = 0; i < OSCS; i++)
{
params[i].mod0 = params[params[i].modPatch0].outData;
params[i].mod1 = params[params[i].modPatch1].outData;
}
for (int i = 0; i < OSCS; i++)
{ // envelope generator
if ((params[i].noteOn == true) && (params[i].noteOnSave != params[i].noteOn))
{
params[i].state = STATE_ATTACK;
}
if ((params[i].noteOn == false) && (params[i].noteOnSave != params[i].noteOn))
{
params[i].state = STATE_RELEASE;
}
params[i].noteOnSave = params[i].noteOn;
if (params[i].state == STATE_SLEEP)
{
params[i].count = 0;
}
int limitValue = 0;
int valueDiff = 0;
boolean limitGt = false;