Jonne Valola / PokittoLib Featured

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

PokittoLib

Library for programming Pokitto hardware

How to Use

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Sat Mar 23 20:03:34 2019 +0000
Revision:
66:6281a40d73e6
Parent:
31:f4b9b85c7b62
Updated pokittolib to current embitz dev branch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 31:f4b9b85c7b62 1 /**************************************************************************/
Pokitto 31:f4b9b85c7b62 2 /*!
Pokitto 31:f4b9b85c7b62 3 @file Synth_mixfuncs.cpp
Pokitto 31:f4b9b85c7b62 4 @author Jonne Valola
Pokitto 31:f4b9b85c7b62 5
Pokitto 31:f4b9b85c7b62 6 @section LICENSE
Pokitto 31:f4b9b85c7b62 7
Pokitto 31:f4b9b85c7b62 8 Software License Agreement (BSD License)
Pokitto 31:f4b9b85c7b62 9
Pokitto 31:f4b9b85c7b62 10 Copyright (c) 2016, Jonne Valola
Pokitto 31:f4b9b85c7b62 11 All rights reserved.
Pokitto 31:f4b9b85c7b62 12
Pokitto 31:f4b9b85c7b62 13 Redistribution and use in source and binary forms, with or without
Pokitto 31:f4b9b85c7b62 14 modification, are permitted provided that the following conditions are met:
Pokitto 31:f4b9b85c7b62 15 1. Redistributions of source code must retain the above copyright
Pokitto 31:f4b9b85c7b62 16 notice, this list of conditions and the following disclaimer.
Pokitto 31:f4b9b85c7b62 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 31:f4b9b85c7b62 18 notice, this list of conditions and the following disclaimer in the
Pokitto 31:f4b9b85c7b62 19 documentation and/or other materials provided with the distribution.
Pokitto 31:f4b9b85c7b62 20 3. Neither the name of the copyright holders nor the
Pokitto 31:f4b9b85c7b62 21 names of its contributors may be used to endorse or promote products
Pokitto 31:f4b9b85c7b62 22 derived from this software without specific prior written permission.
Pokitto 31:f4b9b85c7b62 23
Pokitto 31:f4b9b85c7b62 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 31:f4b9b85c7b62 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 31:f4b9b85c7b62 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 31:f4b9b85c7b62 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 31:f4b9b85c7b62 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 31:f4b9b85c7b62 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 31:f4b9b85c7b62 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 31:f4b9b85c7b62 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 31:f4b9b85c7b62 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 31:f4b9b85c7b62 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 31:f4b9b85c7b62 34 */
Pokitto 31:f4b9b85c7b62 35 /**************************************************************************/
Pokitto 31:f4b9b85c7b62 36
Pokitto 31:f4b9b85c7b62 37 #include "PokittoGlobs.h"
Pokitto 31:f4b9b85c7b62 38 #include "Synth.h"
Pokitto 31:f4b9b85c7b62 39
Pokitto 31:f4b9b85c7b62 40 /** MIXING FUNCTIONS **/
Pokitto 31:f4b9b85c7b62 41
Pokitto 31:f4b9b85c7b62 42 char voltick=0; // i need to make volume changes even slower
Pokitto 31:f4b9b85c7b62 43 uint16_t arptick=0; // i need to make volume changes even slower
Pokitto 31:f4b9b85c7b62 44 int8_t bendtick = 0; // ditto for bend.
Pokitto 31:f4b9b85c7b62 45
Pokitto 31:f4b9b85c7b62 46
Pokitto 31:f4b9b85c7b62 47 void mix1(){
Pokitto 31:f4b9b85c7b62 48 // Track 1
Pokitto 31:f4b9b85c7b62 49 if (osc1.on) {
Pokitto 31:f4b9b85c7b62 50 Farr[osc1.wave](&osc1);
Pokitto 66:6281a40d73e6 51 //#if PROJ_ARDUBOY > 0
Pokitto 31:f4b9b85c7b62 52 if (osc1.duration) {
Pokitto 31:f4b9b85c7b62 53 /**this is special for osc1 and is only used to emulate arduino Tone(); */
Pokitto 31:f4b9b85c7b62 54 osc1.duration--;
Pokitto 31:f4b9b85c7b62 55 } else osc1.on = 0;
Pokitto 66:6281a40d73e6 56 //#endif
Pokitto 31:f4b9b85c7b62 57
Pokitto 31:f4b9b85c7b62 58 #ifdef POK_SIM
Pokitto 31:f4b9b85c7b62 59 soundbyte = (((osc1.output>>8) * (osc1.adsrvol >>8 )) >> 8) >> osc1.echodiv; // To output, shift back to 8-bit
Pokitto 31:f4b9b85c7b62 60 if (osc1.overdrive) soundbyte *= OVERDRIVE;
Pokitto 31:f4b9b85c7b62 61 if (osc1.kick ) soundbyte >>= 2;
Pokitto 31:f4b9b85c7b62 62 osc1.output = soundbyte;
Pokitto 31:f4b9b85c7b62 63 #else
Pokitto 31:f4b9b85c7b62 64 //OCR2B = osc1.output>>8;
Pokitto 31:f4b9b85c7b62 65 #if POK_ENABLE_SOUND > 0
Pokitto 31:f4b9b85c7b62 66 soundbyte = (((osc1.output>>8) * (osc1.adsrvol >>8 )) >> 8) >> osc1.echodiv; // To output, shift back to 8-bit
Pokitto 31:f4b9b85c7b62 67 if (osc1.overdrive) soundbyte *= OVERDRIVE;
Pokitto 31:f4b9b85c7b62 68 if (osc1.kick ) soundbyte >>= 2;
Pokitto 31:f4b9b85c7b62 69 osc1.output = soundbyte;
Pokitto 31:f4b9b85c7b62 70 #endif
Pokitto 31:f4b9b85c7b62 71 #endif
Pokitto 31:f4b9b85c7b62 72 }
Pokitto 31:f4b9b85c7b62 73 }
Pokitto 31:f4b9b85c7b62 74
Pokitto 31:f4b9b85c7b62 75 void mix2(){
Pokitto 31:f4b9b85c7b62 76 // Track 2
Pokitto 31:f4b9b85c7b62 77 if (osc2.on) {
Pokitto 31:f4b9b85c7b62 78 Farr[osc2.wave](&osc2);
Pokitto 66:6281a40d73e6 79 if (osc2.duration) {
Pokitto 66:6281a40d73e6 80 osc2.duration--;
Pokitto 66:6281a40d73e6 81 } else osc2.on = 0;
Pokitto 66:6281a40d73e6 82
Pokitto 31:f4b9b85c7b62 83 #ifdef POK_SIM
Pokitto 31:f4b9b85c7b62 84 soundbyte = (((osc2.output>>8) * (osc2.adsrvol >>8 )) >> 8) >> osc2.echodiv;
Pokitto 31:f4b9b85c7b62 85 if (osc2.overdrive) soundbyte *= OVERDRIVE;
Pokitto 31:f4b9b85c7b62 86 if (osc2.kick ) soundbyte >>= 2;
Pokitto 31:f4b9b85c7b62 87 osc2.output = soundbyte;
Pokitto 31:f4b9b85c7b62 88 #else
Pokitto 31:f4b9b85c7b62 89 //OCR2B = osc2.output>>8;
Pokitto 31:f4b9b85c7b62 90 #if POK_ENABLE_SOUND > 0
Pokitto 31:f4b9b85c7b62 91 soundbyte = (((osc2.output>>8) * (osc2.adsrvol >>8 )) >> 8) >> osc2.echodiv;
Pokitto 31:f4b9b85c7b62 92 if (osc2.overdrive) soundbyte *= OVERDRIVE;
Pokitto 31:f4b9b85c7b62 93 if (osc2.kick ) soundbyte >>= 2;
Pokitto 31:f4b9b85c7b62 94 osc2.output = soundbyte;
Pokitto 31:f4b9b85c7b62 95 #endif
Pokitto 31:f4b9b85c7b62 96 #endif
Pokitto 31:f4b9b85c7b62 97 }
Pokitto 31:f4b9b85c7b62 98 }
Pokitto 31:f4b9b85c7b62 99
Pokitto 31:f4b9b85c7b62 100 void mix3(){
Pokitto 31:f4b9b85c7b62 101 // Track 3
Pokitto 31:f4b9b85c7b62 102 if (osc3.on) {
Pokitto 31:f4b9b85c7b62 103 Farr[osc3.wave](&osc3);
Pokitto 66:6281a40d73e6 104 if (osc3.duration) {
Pokitto 66:6281a40d73e6 105 osc3.duration--;
Pokitto 66:6281a40d73e6 106 } else osc3.on = 0;
Pokitto 31:f4b9b85c7b62 107 #ifdef POK_SIM
Pokitto 31:f4b9b85c7b62 108 soundbyte = (((osc3.output>>8) * (osc3.adsrvol >>8 )) >> 8) >> osc3.echodiv;
Pokitto 31:f4b9b85c7b62 109 if (osc3.overdrive) soundbyte *= OVERDRIVE;
Pokitto 31:f4b9b85c7b62 110 if (osc3.kick ) soundbyte >>= 2;
Pokitto 31:f4b9b85c7b62 111 osc3.output = soundbyte;
Pokitto 31:f4b9b85c7b62 112 #else
Pokitto 31:f4b9b85c7b62 113 //OCR2B = osc3.output>>8;
Pokitto 31:f4b9b85c7b62 114 #if POK_ENABLE_SOUND > 0
Pokitto 31:f4b9b85c7b62 115 soundbyte = (((osc3.output>>8) * (osc3.adsrvol >>8 )) >> 8) >> osc3.echodiv;
Pokitto 31:f4b9b85c7b62 116 if (osc3.overdrive) soundbyte *= OVERDRIVE;
Pokitto 31:f4b9b85c7b62 117 if (osc3.kick ) soundbyte >>= 2;
Pokitto 31:f4b9b85c7b62 118 osc3.output = soundbyte;
Pokitto 31:f4b9b85c7b62 119 #endif
Pokitto 31:f4b9b85c7b62 120 #endif
Pokitto 31:f4b9b85c7b62 121 }
Pokitto 31:f4b9b85c7b62 122 }
Pokitto 31:f4b9b85c7b62 123
Pokitto 31:f4b9b85c7b62 124 void updateEnvelopes(){
Pokitto 31:f4b9b85c7b62 125 //calculate volume envelopes, I do this to save cpu power
Pokitto 66:6281a40d73e6 126 #if POK_ALT_MIXING > 0
Pokitto 31:f4b9b85c7b62 127 if (arptick) --arptick;
Pokitto 66:6281a40d73e6 128 #else
Pokitto 66:6281a40d73e6 129 if (arptick) --arptick;
Pokitto 66:6281a40d73e6 130 #endif
Pokitto 31:f4b9b85c7b62 131 else {
Pokitto 31:f4b9b85c7b62 132 if (osc1.arpmode && osc1.on) {
Pokitto 31:f4b9b85c7b62 133 osc1.cinc = cincs[osc1.tonic+arptable[osc1.arpmode][osc1.arpstep]];
Pokitto 31:f4b9b85c7b62 134 osc1.arpstep++;
Pokitto 31:f4b9b85c7b62 135 if (osc1.arpstep==ARPSTEPMAX) osc1.arpstep = 0;
Pokitto 31:f4b9b85c7b62 136 arptick = ARPTICK << (3-osc1.arpspeed);
Pokitto 31:f4b9b85c7b62 137 }
Pokitto 31:f4b9b85c7b62 138 if (osc2.arpmode && osc2.on) {
Pokitto 31:f4b9b85c7b62 139 osc2.cinc = cincs[osc2.tonic+arptable[osc2.arpmode][osc2.arpstep]];
Pokitto 31:f4b9b85c7b62 140 osc2.arpstep++;
Pokitto 31:f4b9b85c7b62 141 if (osc2.arpstep==ARPSTEPMAX) osc2.arpstep = 0;
Pokitto 31:f4b9b85c7b62 142 arptick = ARPTICK << (3-osc2.arpspeed);
Pokitto 31:f4b9b85c7b62 143 }
Pokitto 31:f4b9b85c7b62 144 if (osc3.arpmode && osc3.on) {
Pokitto 31:f4b9b85c7b62 145 osc3.cinc = cincs[osc3.tonic+arptable[osc3.arpmode][osc3.arpstep]];
Pokitto 31:f4b9b85c7b62 146 osc3.arpstep++;
Pokitto 31:f4b9b85c7b62 147 if (osc3.arpstep==ARPSTEPMAX) osc3.arpstep = 0;
Pokitto 31:f4b9b85c7b62 148 arptick = ARPTICK << (3-osc3.arpspeed);
Pokitto 31:f4b9b85c7b62 149 }
Pokitto 31:f4b9b85c7b62 150
Pokitto 31:f4b9b85c7b62 151 }
Pokitto 31:f4b9b85c7b62 152
Pokitto 66:6281a40d73e6 153 #if POK_ALT_MIXING > 0
Pokitto 31:f4b9b85c7b62 154 if (voltick) --voltick;
Pokitto 66:6281a40d73e6 155 #else
Pokitto 66:6281a40d73e6 156 if (voltick) --voltick;
Pokitto 66:6281a40d73e6 157 #endif
Pokitto 31:f4b9b85c7b62 158 else {
Pokitto 31:f4b9b85c7b62 159 bendtick = !bendtick;
Pokitto 31:f4b9b85c7b62 160 if (osc1.on) Earr[osc1.adsrphase](&osc1);
Pokitto 66:6281a40d73e6 161 if (bendtick) {
Pokitto 66:6281a40d73e6 162 osc1.pitchbend += osc1.bendrate; //slow bend to every second beat
Pokitto 66:6281a40d73e6 163 /*if (osc1.wave == 6 && osc1.bendrate) {
Pokitto 66:6281a40d73e6 164 if (osc1.samplebendtick > osc1.samplebendcount) {
Pokitto 66:6281a40d73e6 165 if (osc1.bendrate>0) osc1.samplestep++;
Pokitto 66:6281a40d73e6 166 else if (osc1.bendrate<0) osc1.samplestep--;
Pokitto 66:6281a40d73e6 167 osc1.samplebendtick=0;
Pokitto 66:6281a40d73e6 168 } else osc1.samplebendtick++;
Pokitto 66:6281a40d73e6 169
Pokitto 66:6281a40d73e6 170 }*/
Pokitto 66:6281a40d73e6 171 }
Pokitto 31:f4b9b85c7b62 172 if (osc1.bendrate > 0 && osc1.pitchbend > osc1.maxbend) {
Pokitto 31:f4b9b85c7b62 173 osc1.pitchbend = osc1.maxbend;
Pokitto 31:f4b9b85c7b62 174 osc1.bendrate = 0; // STOP BENDING !
Pokitto 31:f4b9b85c7b62 175 }
Pokitto 31:f4b9b85c7b62 176 else if (osc1.bendrate < 0 && osc1.pitchbend < osc1.maxbend) {
Pokitto 31:f4b9b85c7b62 177 osc1.pitchbend = osc1.maxbend;
Pokitto 31:f4b9b85c7b62 178 osc1.bendrate = 0; // STOP BENDING !
Pokitto 31:f4b9b85c7b62 179 }
Pokitto 31:f4b9b85c7b62 180
Pokitto 31:f4b9b85c7b62 181 if (osc2.on) Earr[osc2.adsrphase](&osc2);
Pokitto 31:f4b9b85c7b62 182 if (bendtick) osc2.pitchbend += osc2.bendrate;
Pokitto 31:f4b9b85c7b62 183 if (osc2.bendrate > 0 && osc2.pitchbend > osc2.maxbend) osc2.pitchbend = osc2.maxbend;
Pokitto 31:f4b9b85c7b62 184 else if (osc2.bendrate < 0 && osc2.pitchbend < osc2.maxbend) osc2.pitchbend = osc2.maxbend;
Pokitto 31:f4b9b85c7b62 185
Pokitto 31:f4b9b85c7b62 186 if (osc3.on) Earr[osc3.adsrphase](&osc3);
Pokitto 31:f4b9b85c7b62 187 if (bendtick) osc3.pitchbend += osc3.bendrate;
Pokitto 31:f4b9b85c7b62 188 if (osc3.bendrate > 0 && osc3.pitchbend > osc3.maxbend) osc3.pitchbend = osc3.maxbend;
Pokitto 31:f4b9b85c7b62 189 else if (osc3.bendrate < 0 && osc3.pitchbend < osc3.maxbend) osc3.pitchbend = osc3.maxbend;
Pokitto 31:f4b9b85c7b62 190
Pokitto 31:f4b9b85c7b62 191 voltick = VOLTICK;
Pokitto 31:f4b9b85c7b62 192 }
Pokitto 31:f4b9b85c7b62 193 tick = 4;
Pokitto 31:f4b9b85c7b62 194 }
Pokitto 31:f4b9b85c7b62 195
Pokitto 31:f4b9b85c7b62 196