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

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Synth_oscfuncs.cpp Source File

Synth_oscfuncs.cpp

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     Synth_oscfuncs.cpp
00004     @author   Jonne Valola
00005 
00006     @section LICENSE
00007 
00008     Software License Agreement (BSD License)
00009 
00010     Copyright (c) 2016, Jonne Valola
00011     All rights reserved.
00012 
00013     Redistribution and use in source and binary forms, with or without
00014     modification, are permitted provided that the following conditions are met:
00015     1. Redistributions of source code must retain the above copyright
00016     notice, this list of conditions and the following disclaimer.
00017     2. Redistributions in binary form must reproduce the above copyright
00018     notice, this list of conditions and the following disclaimer in the
00019     documentation and/or other materials provided with the distribution.
00020     3. Neither the name of the copyright holders nor the
00021     names of its contributors may be used to endorse or promote products
00022     derived from this software without specific prior written permission.
00023 
00024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
00025     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00028     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 */
00035 /**************************************************************************/
00036 
00037 #include "Synth.h "
00038 
00039 /** OSCILLATOR FUNCTIONS **/
00040 
00041 void setOSC(OSC* o,byte on=1, byte wave=1, byte loop=0, byte echo=0, byte adsr=0,
00042             uint8_t notenumber=25, uint8_t volume=127,
00043             uint16_t attack=0, uint16_t decay=0, uint16_t sustain=0, uint16_t release=0,
00044             int16_t maxbend=0, int16_t bendrate=0, uint8_t arpmode = 0, uint8_t overdrive=0, uint8_t kick=0){
00045   //Serial.println("SetOsc "); osc1
00046   o->on = on;
00047   o->overdrive = overdrive;
00048   o->kick = kick;
00049   o->wave = wave;
00050   o->loop = loop;
00051   o->echo = echo; //echo shifts left 8 steps to zero
00052   o->echodiv = 0;
00053   o->adsr = adsr;
00054   if (arpmode) {
00055         if (arpmode < 4) {o->arpmode = 1; o->arpspeed = arpmode;}
00056         else if (arpmode < 7) {o->arpmode = 2; o->arpspeed = arpmode-3;}
00057         else if (arpmode < 10) {o->arpmode = 3; o->arpspeed = arpmode-6; } // vibrato trial
00058         else if (arpmode < 13) {o->arpmode = 4; o->arpspeed = arpmode-9; } // octave trial
00059         else if (arpmode < 16) {o->arpmode = 5; o->arpspeed = arpmode-12; } // funk trial*/
00060   } else o->arpmode = 0;
00061   o->arpstep = 0;
00062   o->count = 0;
00063   noiseval = xorshift16(); //random(0,0xFFFF);
00064 
00065   o->cinc = cincs[notenumber]<<POK_CINC_MULTIPLIER; // direct cinc from table, no calculation
00066   o->tonic = notenumber; // save tonic for arpeggio use
00067   if (wave == 2) o->cinc >>= 1; // correct pitch for saw wave
00068   if (wave == 4) o->cinc <<= 1; // enable higher pitch for pure noise
00069   o->vol = volume << 8;//volume;
00070 
00071   if (adsr) {
00072     o->attack = attack;
00073     o->decay = decay;
00074     o->sustain = sustain;
00075     o->release = release;
00076     o->adsrphase = 1;
00077     if (!o->attack) o->adsrvol = o->vol; // start directly, no attack ramp
00078     else o->adsrvol = 0;
00079   } else {
00080     o->attack = 0;
00081     o->decay = 0;
00082     o->sustain = 0;
00083     o->release = 0;
00084     o->adsrphase = 0;
00085     o->adsrvol = o->vol; // will stay same all the time
00086   }
00087 
00088   if (bendrate != 0) {
00089         o->bendrate = bendrate; // test value
00090         o->pitchbend = 0;
00091         o->maxbend = maxbend;
00092   }
00093 }
00094 
00095 void setOSC(OSC* o,byte on, byte wave, uint16_t frq, uint8_t volume, uint32_t duration){
00096   o->on = on;
00097   o->overdrive = 0;
00098   o->kick = 0;
00099   o->wave = wave;
00100   o->loop = 1;
00101   o->echo = 1; //echo shifts left 8 steps to zero
00102   o->echodiv = 0;
00103   o->adsr = 1;
00104   o->attack = 200;
00105   o->decay = 200;
00106   o->sustain = 20;
00107   o->release = 10;
00108   o->adsrphase = 1;
00109   o->arpmode = 0;
00110   o->count = 0;
00111   noiseval = xorshift16(); //random(0,0xFFFF);
00112   o->cinc = (frq/100)*(cincs[18]<<POK_CINC_MULTIPLIER); // its a kludge, i know. cant be bothered.
00113   if (wave == 2) o->cinc >>= 1; // correct pitch for saw wave
00114   if (wave == 4) o->cinc <<= 1; // enable higher pitch for pure noise
00115   o->vol = volume << 8;//volume;
00116   o->adsrvol = o->vol;
00117   o->duration = duration*100;
00118   o->maxbend = -4000;
00119   o->bendrate = 1000;
00120 }
00121 
00122 
00123 
00124 void emptyOscillators(){
00125     osc1.on = false; osc1.wave = 0; osc1.echo = 0; osc1.count = 0; osc1.cinc =0;
00126     osc1.attack = 0; osc1.loop = 0; osc1.adsrphase = 1; osc1.adsr = 1; osc1.decay = 100;
00127     osc1.pitchbend = 0; osc1.bendrate = 0; osc1.maxbend = 0; osc1.sustain = 0; osc1.release = 0;
00128 
00129     osc2.on = false; osc2.wave = 0; osc2.echo = 0; osc2.count = 0; osc2.cinc =0;
00130     osc2.attack = 0; osc2.loop = 0; osc2.adsrphase = 1; osc2.adsr = 1; osc2.decay = 100;
00131     osc2.pitchbend = 0; osc2.bendrate = 0; osc2.maxbend = 0; osc2.sustain = 0; osc2.release = 0;
00132 
00133     osc3.on = false; osc3.wave = 0; osc3.echo = 0; osc3.count = 0; osc3.cinc =0;
00134     osc3.attack = 0; osc3.loop = 0; osc3.adsrphase = 1; osc3.adsr = 1; osc3.decay = 100;
00135     osc3.pitchbend = 0; osc3.bendrate = 0; osc3.maxbend = 0; osc3.sustain = 0; osc3.release = 0;
00136 }
00137 
00138 
00139 void testOsc(){
00140     setOSC(&osc1,1,WTRI,1,0,1,25,127,10,10,20,2,0,0,0,0,0); // C3 = 25
00141     setOSC(&osc2,1,WTRI,1,0,1,29-12,63,2,1,20,2,0,0,14,0,0); // E3 = 29
00142     setOSC(&osc3,1,WSAW,1,0,1,25,15,30,30,20,2,-1,-1000,12,0,0); // G3 = 32
00143 }
00144 
00145 void playNote(uint8_t oscnum, uint8_t notenum, uint8_t i) {
00146     OSC* o;
00147     if (oscnum == 1) o = &osc1; else if (oscnum == 2) o = &osc2; else o = &osc3;
00148     setOSC(o,1,patch[i].wave,patch[i].loop,patch[i].echo,patch[i].adsr,notenum,patch[i].vol,
00149                           patch[i].attack,patch[i].decay,patch[i].sustain,patch[i].release,
00150                           patch[i].maxbend,patch[i].bendrate,patch[i].arpmode,patch[i].overdrive,patch[i].kick);
00151 }
00152 
00153 void makeSampleInstruments() {
00154     /* sample instruments for testing */
00155     patch[0].wave = WSQUARE;
00156     patch[0].on = 1;
00157     patch[0].vol = 127;
00158     patch[0].loop = 0;
00159     patch[0].echo = 0;
00160 
00161     patch[0].adsr = 0;
00162     patch[0].attack = 0;
00163     patch[0].decay = 0;
00164     patch[0].sustain = 0;
00165     patch[0].release = 0;
00166 
00167     patch[0].maxbend = -1000;
00168     patch[0].bendrate = 100;
00169     patch[0].arpmode = 3;
00170     patch[0].overdrive = 0;
00171     patch[0].kick = 0;
00172 
00173     patch[1].wave = WSAW;
00174     patch[1].on = 1;
00175     patch[1].vol = 200;
00176     patch[1].loop = 0;
00177     patch[1].echo = 0;
00178 
00179     patch[1].adsr = 0;
00180     patch[1].attack = 0;
00181     patch[1].decay = 0;
00182     patch[1].sustain = 0;
00183     patch[1].release = 0;
00184 
00185     patch[1].maxbend = 0;
00186     patch[1].bendrate = 0;
00187     patch[1].arpmode = 1;
00188     patch[1].overdrive = 0;
00189     patch[1].kick = 0;
00190 
00191     patch[2].wave = WTRI;
00192     patch[2].on = 1;
00193     patch[2].vol = 127;
00194     patch[2].loop = 0;
00195     patch[2].echo = 0;
00196 
00197     patch[2].adsr = 1;
00198     patch[2].attack = 10;
00199     patch[2].decay = 0;
00200     patch[2].sustain = 0;
00201     patch[2].release = 0;
00202 
00203     patch[2].maxbend = 0;
00204     patch[2].bendrate = 0;
00205     patch[2].arpmode = 1;
00206     patch[2].overdrive = 0;
00207     patch[2].kick = 0;
00208 
00209     patch[3].wave = WNOISE;
00210     patch[3].on = 1;
00211     patch[3].vol = 127;
00212     patch[3].loop = 1;
00213     patch[3].echo = 1;
00214 
00215     patch[3].adsr = 1;
00216     patch[3].attack = 0;
00217     patch[3].decay = 30;
00218     patch[3].sustain = 30;
00219     patch[3].release = 5;
00220 
00221     patch[3].maxbend = 0;
00222     patch[3].bendrate = 0;
00223     patch[3].arpmode = 0;
00224     patch[3].overdrive = 0;
00225     patch[3].kick = 0;
00226 
00227     patch[4].wave = WPNOISE;
00228     patch[4].on = 1;
00229     patch[4].vol = 127;
00230     patch[4].loop = 0;
00231     patch[4].echo = 0;
00232 
00233     patch[4].adsr = 1;
00234     patch[4].attack = 0;
00235     patch[4].decay = 30;
00236     patch[4].sustain = 30;
00237     patch[4].release = 5;
00238 
00239     patch[4].maxbend = 0;
00240     patch[4].bendrate = 0;
00241     patch[4].arpmode = 1;
00242     patch[4].overdrive = 0;
00243     patch[4].kick = 0;
00244 }
00245