PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
0:e8b8f36b4505
direct lcd stuff used by sensitive

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 0:e8b8f36b4505 1 /**************************************************************************/
Pokitto 0:e8b8f36b4505 2 /*!
Pokitto 0:e8b8f36b4505 3 @file Synth_oscfuncs.cpp
Pokitto 0:e8b8f36b4505 4 @author Jonne Valola
Pokitto 0:e8b8f36b4505 5
Pokitto 0:e8b8f36b4505 6 @section LICENSE
Pokitto 0:e8b8f36b4505 7
Pokitto 0:e8b8f36b4505 8 Software License Agreement (BSD License)
Pokitto 0:e8b8f36b4505 9
Pokitto 0:e8b8f36b4505 10 Copyright (c) 2016, Jonne Valola
Pokitto 0:e8b8f36b4505 11 All rights reserved.
Pokitto 0:e8b8f36b4505 12
Pokitto 0:e8b8f36b4505 13 Redistribution and use in source and binary forms, with or without
Pokitto 0:e8b8f36b4505 14 modification, are permitted provided that the following conditions are met:
Pokitto 0:e8b8f36b4505 15 1. Redistributions of source code must retain the above copyright
Pokitto 0:e8b8f36b4505 16 notice, this list of conditions and the following disclaimer.
Pokitto 0:e8b8f36b4505 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 0:e8b8f36b4505 18 notice, this list of conditions and the following disclaimer in the
Pokitto 0:e8b8f36b4505 19 documentation and/or other materials provided with the distribution.
Pokitto 0:e8b8f36b4505 20 3. Neither the name of the copyright holders nor the
Pokitto 0:e8b8f36b4505 21 names of its contributors may be used to endorse or promote products
Pokitto 0:e8b8f36b4505 22 derived from this software without specific prior written permission.
Pokitto 0:e8b8f36b4505 23
Pokitto 0:e8b8f36b4505 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 0:e8b8f36b4505 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 0:e8b8f36b4505 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 0:e8b8f36b4505 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 0:e8b8f36b4505 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 0:e8b8f36b4505 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 0:e8b8f36b4505 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 0:e8b8f36b4505 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 0:e8b8f36b4505 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 0:e8b8f36b4505 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 0:e8b8f36b4505 34 */
Pokitto 0:e8b8f36b4505 35 /**************************************************************************/
Pokitto 0:e8b8f36b4505 36
Pokitto 0:e8b8f36b4505 37 #include "Synth.h"
Pokitto 0:e8b8f36b4505 38
Pokitto 0:e8b8f36b4505 39 /** OSCILLATOR FUNCTIONS **/
Pokitto 0:e8b8f36b4505 40
Pokitto 0:e8b8f36b4505 41 void setOSC(OSC* o,byte on=1, byte wave=1, byte loop=0, byte echo=0, byte adsr=0,
Pokitto 0:e8b8f36b4505 42 uint8_t notenumber=25, uint8_t volume=127,
Pokitto 0:e8b8f36b4505 43 uint16_t attack=0, uint16_t decay=0, uint16_t sustain=0, uint16_t release=0,
Pokitto 0:e8b8f36b4505 44 int16_t maxbend=0, int16_t bendrate=0, uint8_t arpmode = 0, uint8_t overdrive=0, uint8_t kick=0){
Pokitto 0:e8b8f36b4505 45 //Serial.println("SetOsc "); osc1
Pokitto 0:e8b8f36b4505 46 o->on = on;
Pokitto 0:e8b8f36b4505 47 o->overdrive = overdrive;
Pokitto 0:e8b8f36b4505 48 o->kick = kick;
Pokitto 0:e8b8f36b4505 49 o->wave = wave;
Pokitto 0:e8b8f36b4505 50 o->loop = loop;
Pokitto 0:e8b8f36b4505 51 o->echo = echo; //echo shifts left 8 steps to zero
Pokitto 0:e8b8f36b4505 52 o->echodiv = 0;
Pokitto 0:e8b8f36b4505 53 o->adsr = adsr;
Pokitto 0:e8b8f36b4505 54 if (arpmode) {
Pokitto 0:e8b8f36b4505 55 if (arpmode < 4) {o->arpmode = 1; o->arpspeed = arpmode;}
Pokitto 0:e8b8f36b4505 56 else if (arpmode < 7) {o->arpmode = 2; o->arpspeed = arpmode-3;}
Pokitto 0:e8b8f36b4505 57 else if (arpmode < 10) {o->arpmode = 3; o->arpspeed = arpmode-6; } // vibrato trial
Pokitto 0:e8b8f36b4505 58 else if (arpmode < 13) {o->arpmode = 4; o->arpspeed = arpmode-9; } // octave trial
Pokitto 0:e8b8f36b4505 59 else if (arpmode < 16) {o->arpmode = 5; o->arpspeed = arpmode-12; } // funk trial*/
Pokitto 0:e8b8f36b4505 60 } else o->arpmode = 0;
Pokitto 0:e8b8f36b4505 61 o->arpstep = 0;
Pokitto 0:e8b8f36b4505 62 o->count = 0;
Pokitto 0:e8b8f36b4505 63 noiseval = xorshift16(); //random(0,0xFFFF);
Pokitto 0:e8b8f36b4505 64
Pokitto 0:e8b8f36b4505 65 o->cinc = cincs[notenumber]<<POK_CINC_MULTIPLIER; // direct cinc from table, no calculation
Pokitto 0:e8b8f36b4505 66 o->tonic = notenumber; // save tonic for arpeggio use
Pokitto 0:e8b8f36b4505 67 if (wave == 2) o->cinc >>= 1; // correct pitch for saw wave
Pokitto 0:e8b8f36b4505 68 if (wave == 4) o->cinc <<= 1; // enable higher pitch for pure noise
Pokitto 0:e8b8f36b4505 69 o->vol = volume << 8;//volume;
Pokitto 0:e8b8f36b4505 70
Pokitto 0:e8b8f36b4505 71 if (adsr) {
Pokitto 0:e8b8f36b4505 72 o->attack = attack;
Pokitto 0:e8b8f36b4505 73 o->decay = decay;
Pokitto 0:e8b8f36b4505 74 o->sustain = sustain;
Pokitto 0:e8b8f36b4505 75 o->release = release;
Pokitto 0:e8b8f36b4505 76 o->adsrphase = 1;
Pokitto 0:e8b8f36b4505 77 if (!o->attack) o->adsrvol = o->vol; // start directly, no attack ramp
Pokitto 0:e8b8f36b4505 78 else o->adsrvol = 0;
Pokitto 0:e8b8f36b4505 79 } else {
Pokitto 0:e8b8f36b4505 80 o->attack = 0;
Pokitto 0:e8b8f36b4505 81 o->decay = 0;
Pokitto 0:e8b8f36b4505 82 o->sustain = 0;
Pokitto 0:e8b8f36b4505 83 o->release = 0;
Pokitto 0:e8b8f36b4505 84 o->adsrphase = 0;
Pokitto 0:e8b8f36b4505 85 o->adsrvol = o->vol; // will stay same all the time
Pokitto 0:e8b8f36b4505 86 }
Pokitto 0:e8b8f36b4505 87
Pokitto 0:e8b8f36b4505 88 if (bendrate != 0) {
Pokitto 0:e8b8f36b4505 89 o->bendrate = bendrate; // test value
Pokitto 0:e8b8f36b4505 90 o->pitchbend = 0;
Pokitto 0:e8b8f36b4505 91 o->maxbend = maxbend;
Pokitto 0:e8b8f36b4505 92 }
Pokitto 0:e8b8f36b4505 93 }
Pokitto 0:e8b8f36b4505 94
Pokitto 0:e8b8f36b4505 95 void setOSC(OSC* o,byte on, byte wave, uint16_t frq, uint8_t volume, uint32_t duration){
Pokitto 0:e8b8f36b4505 96 o->on = on;
Pokitto 0:e8b8f36b4505 97 o->overdrive = 0;
Pokitto 0:e8b8f36b4505 98 o->kick = 0;
Pokitto 0:e8b8f36b4505 99 o->wave = wave;
Pokitto 0:e8b8f36b4505 100 o->loop = 1;
Pokitto 0:e8b8f36b4505 101 o->echo = 1; //echo shifts left 8 steps to zero
Pokitto 0:e8b8f36b4505 102 o->echodiv = 0;
Pokitto 0:e8b8f36b4505 103 o->adsr = 1;
Pokitto 0:e8b8f36b4505 104 o->attack = 200;
Pokitto 0:e8b8f36b4505 105 o->decay = 200;
Pokitto 0:e8b8f36b4505 106 o->sustain = 20;
Pokitto 0:e8b8f36b4505 107 o->release = 10;
Pokitto 0:e8b8f36b4505 108 o->adsrphase = 1;
Pokitto 0:e8b8f36b4505 109 o->arpmode = 0;
Pokitto 0:e8b8f36b4505 110 o->count = 0;
Pokitto 0:e8b8f36b4505 111 noiseval = xorshift16(); //random(0,0xFFFF);
Pokitto 0:e8b8f36b4505 112 o->cinc = (frq/100)*(cincs[18]<<POK_CINC_MULTIPLIER); // its a kludge, i know. cant be bothered.
Pokitto 0:e8b8f36b4505 113 if (wave == 2) o->cinc >>= 1; // correct pitch for saw wave
Pokitto 0:e8b8f36b4505 114 if (wave == 4) o->cinc <<= 1; // enable higher pitch for pure noise
Pokitto 0:e8b8f36b4505 115 o->vol = volume << 8;//volume;
Pokitto 0:e8b8f36b4505 116 o->adsrvol = o->vol;
Pokitto 0:e8b8f36b4505 117 o->duration = duration*100;
Pokitto 0:e8b8f36b4505 118 o->maxbend = -4000;
Pokitto 0:e8b8f36b4505 119 o->bendrate = 1000;
Pokitto 0:e8b8f36b4505 120 }
Pokitto 0:e8b8f36b4505 121
Pokitto 0:e8b8f36b4505 122
Pokitto 0:e8b8f36b4505 123
Pokitto 0:e8b8f36b4505 124 void emptyOscillators(){
Pokitto 0:e8b8f36b4505 125 osc1.on = false; osc1.wave = 0; osc1.echo = 0; osc1.count = 0; osc1.cinc =0;
Pokitto 0:e8b8f36b4505 126 osc1.attack = 0; osc1.loop = 0; osc1.adsrphase = 1; osc1.adsr = 1; osc1.decay = 100;
Pokitto 0:e8b8f36b4505 127 osc1.pitchbend = 0; osc1.bendrate = 0; osc1.maxbend = 0; osc1.sustain = 0; osc1.release = 0;
Pokitto 0:e8b8f36b4505 128
Pokitto 0:e8b8f36b4505 129 osc2.on = false; osc2.wave = 0; osc2.echo = 0; osc2.count = 0; osc2.cinc =0;
Pokitto 0:e8b8f36b4505 130 osc2.attack = 0; osc2.loop = 0; osc2.adsrphase = 1; osc2.adsr = 1; osc2.decay = 100;
Pokitto 0:e8b8f36b4505 131 osc2.pitchbend = 0; osc2.bendrate = 0; osc2.maxbend = 0; osc2.sustain = 0; osc2.release = 0;
Pokitto 0:e8b8f36b4505 132
Pokitto 0:e8b8f36b4505 133 osc3.on = false; osc3.wave = 0; osc3.echo = 0; osc3.count = 0; osc3.cinc =0;
Pokitto 0:e8b8f36b4505 134 osc3.attack = 0; osc3.loop = 0; osc3.adsrphase = 1; osc3.adsr = 1; osc3.decay = 100;
Pokitto 0:e8b8f36b4505 135 osc3.pitchbend = 0; osc3.bendrate = 0; osc3.maxbend = 0; osc3.sustain = 0; osc3.release = 0;
Pokitto 0:e8b8f36b4505 136 }
Pokitto 0:e8b8f36b4505 137
Pokitto 0:e8b8f36b4505 138
Pokitto 0:e8b8f36b4505 139 void testOsc(){
Pokitto 0:e8b8f36b4505 140 setOSC(&osc1,1,WTRI,1,0,1,25,127,10,10,20,2,0,0,0,0,0); // C3 = 25
Pokitto 0:e8b8f36b4505 141 setOSC(&osc2,1,WTRI,1,0,1,29-12,63,2,1,20,2,0,0,14,0,0); // E3 = 29
Pokitto 0:e8b8f36b4505 142 setOSC(&osc3,1,WSAW,1,0,1,25,15,30,30,20,2,-1,-1000,12,0,0); // G3 = 32
Pokitto 0:e8b8f36b4505 143 }
Pokitto 0:e8b8f36b4505 144
Pokitto 0:e8b8f36b4505 145 void playNote(uint8_t oscnum, uint8_t notenum, uint8_t i) {
Pokitto 0:e8b8f36b4505 146 OSC* o;
Pokitto 0:e8b8f36b4505 147 if (oscnum == 1) o = &osc1; else if (oscnum == 2) o = &osc2; else o = &osc3;
Pokitto 0:e8b8f36b4505 148 setOSC(o,1,patch[i].wave,patch[i].loop,patch[i].echo,patch[i].adsr,notenum,patch[i].vol,
Pokitto 0:e8b8f36b4505 149 patch[i].attack,patch[i].decay,patch[i].sustain,patch[i].release,
Pokitto 0:e8b8f36b4505 150 patch[i].maxbend,patch[i].bendrate,patch[i].arpmode,patch[i].overdrive,patch[i].kick);
Pokitto 0:e8b8f36b4505 151 }
Pokitto 0:e8b8f36b4505 152
Pokitto 0:e8b8f36b4505 153 void makeSampleInstruments() {
Pokitto 0:e8b8f36b4505 154 /* sample instruments for testing */
Pokitto 0:e8b8f36b4505 155 patch[0].wave = WSQUARE;
Pokitto 0:e8b8f36b4505 156 patch[0].on = 1;
Pokitto 0:e8b8f36b4505 157 patch[0].vol = 127;
Pokitto 0:e8b8f36b4505 158 patch[0].loop = 0;
Pokitto 0:e8b8f36b4505 159 patch[0].echo = 0;
Pokitto 0:e8b8f36b4505 160
Pokitto 0:e8b8f36b4505 161 patch[0].adsr = 0;
Pokitto 0:e8b8f36b4505 162 patch[0].attack = 0;
Pokitto 0:e8b8f36b4505 163 patch[0].decay = 0;
Pokitto 0:e8b8f36b4505 164 patch[0].sustain = 0;
Pokitto 0:e8b8f36b4505 165 patch[0].release = 0;
Pokitto 0:e8b8f36b4505 166
Pokitto 0:e8b8f36b4505 167 patch[0].maxbend = -1000;
Pokitto 0:e8b8f36b4505 168 patch[0].bendrate = 100;
Pokitto 0:e8b8f36b4505 169 patch[0].arpmode = 3;
Pokitto 0:e8b8f36b4505 170 patch[0].overdrive = 0;
Pokitto 0:e8b8f36b4505 171 patch[0].kick = 0;
Pokitto 0:e8b8f36b4505 172
Pokitto 0:e8b8f36b4505 173 patch[1].wave = WSAW;
Pokitto 0:e8b8f36b4505 174 patch[1].on = 1;
Pokitto 0:e8b8f36b4505 175 patch[1].vol = 200;
Pokitto 0:e8b8f36b4505 176 patch[1].loop = 0;
Pokitto 0:e8b8f36b4505 177 patch[1].echo = 0;
Pokitto 0:e8b8f36b4505 178
Pokitto 0:e8b8f36b4505 179 patch[1].adsr = 0;
Pokitto 0:e8b8f36b4505 180 patch[1].attack = 0;
Pokitto 0:e8b8f36b4505 181 patch[1].decay = 0;
Pokitto 0:e8b8f36b4505 182 patch[1].sustain = 0;
Pokitto 0:e8b8f36b4505 183 patch[1].release = 0;
Pokitto 0:e8b8f36b4505 184
Pokitto 0:e8b8f36b4505 185 patch[1].maxbend = 0;
Pokitto 0:e8b8f36b4505 186 patch[1].bendrate = 0;
Pokitto 0:e8b8f36b4505 187 patch[1].arpmode = 1;
Pokitto 0:e8b8f36b4505 188 patch[1].overdrive = 0;
Pokitto 0:e8b8f36b4505 189 patch[1].kick = 0;
Pokitto 0:e8b8f36b4505 190
Pokitto 0:e8b8f36b4505 191 patch[2].wave = WTRI;
Pokitto 0:e8b8f36b4505 192 patch[2].on = 1;
Pokitto 0:e8b8f36b4505 193 patch[2].vol = 127;
Pokitto 0:e8b8f36b4505 194 patch[2].loop = 0;
Pokitto 0:e8b8f36b4505 195 patch[2].echo = 0;
Pokitto 0:e8b8f36b4505 196
Pokitto 0:e8b8f36b4505 197 patch[2].adsr = 1;
Pokitto 0:e8b8f36b4505 198 patch[2].attack = 10;
Pokitto 0:e8b8f36b4505 199 patch[2].decay = 0;
Pokitto 0:e8b8f36b4505 200 patch[2].sustain = 0;
Pokitto 0:e8b8f36b4505 201 patch[2].release = 0;
Pokitto 0:e8b8f36b4505 202
Pokitto 0:e8b8f36b4505 203 patch[2].maxbend = 0;
Pokitto 0:e8b8f36b4505 204 patch[2].bendrate = 0;
Pokitto 0:e8b8f36b4505 205 patch[2].arpmode = 1;
Pokitto 0:e8b8f36b4505 206 patch[2].overdrive = 0;
Pokitto 0:e8b8f36b4505 207 patch[2].kick = 0;
Pokitto 0:e8b8f36b4505 208
Pokitto 0:e8b8f36b4505 209 patch[3].wave = WNOISE;
Pokitto 0:e8b8f36b4505 210 patch[3].on = 1;
Pokitto 0:e8b8f36b4505 211 patch[3].vol = 127;
Pokitto 0:e8b8f36b4505 212 patch[3].loop = 1;
Pokitto 0:e8b8f36b4505 213 patch[3].echo = 1;
Pokitto 0:e8b8f36b4505 214
Pokitto 0:e8b8f36b4505 215 patch[3].adsr = 1;
Pokitto 0:e8b8f36b4505 216 patch[3].attack = 0;
Pokitto 0:e8b8f36b4505 217 patch[3].decay = 30;
Pokitto 0:e8b8f36b4505 218 patch[3].sustain = 30;
Pokitto 0:e8b8f36b4505 219 patch[3].release = 5;
Pokitto 0:e8b8f36b4505 220
Pokitto 0:e8b8f36b4505 221 patch[3].maxbend = 0;
Pokitto 0:e8b8f36b4505 222 patch[3].bendrate = 0;
Pokitto 0:e8b8f36b4505 223 patch[3].arpmode = 0;
Pokitto 0:e8b8f36b4505 224 patch[3].overdrive = 0;
Pokitto 0:e8b8f36b4505 225 patch[3].kick = 0;
Pokitto 0:e8b8f36b4505 226
Pokitto 0:e8b8f36b4505 227 patch[4].wave = WPNOISE;
Pokitto 0:e8b8f36b4505 228 patch[4].on = 1;
Pokitto 0:e8b8f36b4505 229 patch[4].vol = 127;
Pokitto 0:e8b8f36b4505 230 patch[4].loop = 0;
Pokitto 0:e8b8f36b4505 231 patch[4].echo = 0;
Pokitto 0:e8b8f36b4505 232
Pokitto 0:e8b8f36b4505 233 patch[4].adsr = 1;
Pokitto 0:e8b8f36b4505 234 patch[4].attack = 0;
Pokitto 0:e8b8f36b4505 235 patch[4].decay = 30;
Pokitto 0:e8b8f36b4505 236 patch[4].sustain = 30;
Pokitto 0:e8b8f36b4505 237 patch[4].release = 5;
Pokitto 0:e8b8f36b4505 238
Pokitto 0:e8b8f36b4505 239 patch[4].maxbend = 0;
Pokitto 0:e8b8f36b4505 240 patch[4].bendrate = 0;
Pokitto 0:e8b8f36b4505 241 patch[4].arpmode = 1;
Pokitto 0:e8b8f36b4505 242 patch[4].overdrive = 0;
Pokitto 0:e8b8f36b4505 243 patch[4].kick = 0;
Pokitto 0:e8b8f36b4505 244 }
Pokitto 0:e8b8f36b4505 245