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

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
Pokitto
Date:
Mon Sep 18 11:47:51 2017 +0000
Revision:
0:e8b8f36b4505
Child:
1:4b1511a0a2c2
Initial;

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_mixfuncs.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 "PokittoGlobs.h"
Pokitto 0:e8b8f36b4505 38 #include "Synth.h"
Pokitto 0:e8b8f36b4505 39
Pokitto 0:e8b8f36b4505 40 /** MIXING FUNCTIONS **/
Pokitto 0:e8b8f36b4505 41
Pokitto 0:e8b8f36b4505 42 char voltick=0; // i need to make volume changes even slower
Pokitto 0:e8b8f36b4505 43 uint16_t arptick=0; // i need to make volume changes even slower
Pokitto 0:e8b8f36b4505 44 int8_t bendtick = 0; // ditto for bend.
Pokitto 0:e8b8f36b4505 45
Pokitto 0:e8b8f36b4505 46
Pokitto 0:e8b8f36b4505 47 void mix1(){
Pokitto 0:e8b8f36b4505 48 // Track 1
Pokitto 0:e8b8f36b4505 49 if (osc1.on) {
Pokitto 0:e8b8f36b4505 50 Farr[osc1.wave](&osc1);
Pokitto 0:e8b8f36b4505 51 #if PROJ_ARDUBOY > 0
Pokitto 0:e8b8f36b4505 52 if (osc1.duration) {
Pokitto 0:e8b8f36b4505 53 /**this is special for osc1 and is only used to emulate arduino Tone(); */
Pokitto 0:e8b8f36b4505 54 osc1.duration--;
Pokitto 0:e8b8f36b4505 55 } else osc1.on = 0;
Pokitto 0:e8b8f36b4505 56 #endif
Pokitto 0:e8b8f36b4505 57
Pokitto 0:e8b8f36b4505 58 #ifdef POK_SIM
Pokitto 0:e8b8f36b4505 59 soundbyte = (((osc1.output>>8) * (osc1.adsrvol >>8 )) >> 8) >> osc1.echodiv; // To output, shift back to 8-bit
Pokitto 0:e8b8f36b4505 60 if (osc1.overdrive) soundbyte *= OVERDRIVE;
Pokitto 0:e8b8f36b4505 61 if (osc1.kick ) soundbyte >>= 2;
Pokitto 0:e8b8f36b4505 62 osc1.output = soundbyte;
Pokitto 0:e8b8f36b4505 63 #else
Pokitto 0:e8b8f36b4505 64 //OCR2B = osc1.output>>8;
Pokitto 0:e8b8f36b4505 65 soundbyte = (((osc1.output>>8) * (osc1.adsrvol >>8 )) >> 8) >> osc1.echodiv; // To output, shift back to 8-bit
Pokitto 0:e8b8f36b4505 66 if (osc1.overdrive) soundbyte *= OVERDRIVE;
Pokitto 0:e8b8f36b4505 67 if (osc1.kick ) soundbyte >>= 2;
Pokitto 0:e8b8f36b4505 68 osc1.output = soundbyte;
Pokitto 0:e8b8f36b4505 69 #endif
Pokitto 0:e8b8f36b4505 70 }
Pokitto 0:e8b8f36b4505 71 }
Pokitto 0:e8b8f36b4505 72
Pokitto 0:e8b8f36b4505 73 void mix2(){
Pokitto 0:e8b8f36b4505 74 // Track 2
Pokitto 0:e8b8f36b4505 75 if (osc2.on) {
Pokitto 0:e8b8f36b4505 76 Farr[osc2.wave](&osc2);
Pokitto 0:e8b8f36b4505 77 #ifdef POK_SIM
Pokitto 0:e8b8f36b4505 78 soundbyte = (((osc2.output>>8) * (osc2.adsrvol >>8 )) >> 8) >> osc2.echodiv;
Pokitto 0:e8b8f36b4505 79 if (osc2.overdrive) soundbyte *= OVERDRIVE;
Pokitto 0:e8b8f36b4505 80 if (osc2.kick ) soundbyte >>= 2;
Pokitto 0:e8b8f36b4505 81 osc2.output = soundbyte;
Pokitto 0:e8b8f36b4505 82 #else
Pokitto 0:e8b8f36b4505 83 //OCR2B = osc2.output>>8;
Pokitto 0:e8b8f36b4505 84 soundbyte = (((osc2.output>>8) * (osc2.adsrvol >>8 )) >> 8) >> osc2.echodiv;
Pokitto 0:e8b8f36b4505 85 if (osc2.overdrive) soundbyte *= OVERDRIVE;
Pokitto 0:e8b8f36b4505 86 if (osc2.kick ) soundbyte >>= 2;
Pokitto 0:e8b8f36b4505 87 osc2.output = soundbyte;
Pokitto 0:e8b8f36b4505 88 #endif
Pokitto 0:e8b8f36b4505 89 }
Pokitto 0:e8b8f36b4505 90 }
Pokitto 0:e8b8f36b4505 91
Pokitto 0:e8b8f36b4505 92 void mix3(){
Pokitto 0:e8b8f36b4505 93 // Track 3
Pokitto 0:e8b8f36b4505 94 if (osc3.on) {
Pokitto 0:e8b8f36b4505 95 Farr[osc3.wave](&osc3);
Pokitto 0:e8b8f36b4505 96 #ifdef POK_SIM
Pokitto 0:e8b8f36b4505 97 soundbyte = (((osc3.output>>8) * (osc3.adsrvol >>8 )) >> 8) >> osc3.echodiv;
Pokitto 0:e8b8f36b4505 98 if (osc3.overdrive) soundbyte *= OVERDRIVE;
Pokitto 0:e8b8f36b4505 99 if (osc3.kick ) soundbyte >>= 2;
Pokitto 0:e8b8f36b4505 100 osc3.output = soundbyte;
Pokitto 0:e8b8f36b4505 101 #else
Pokitto 0:e8b8f36b4505 102 //OCR2B = osc3.output>>8;
Pokitto 0:e8b8f36b4505 103 soundbyte = (((osc3.output>>8) * (osc3.adsrvol >>8 )) >> 8) >> osc3.echodiv;
Pokitto 0:e8b8f36b4505 104 if (osc3.overdrive) soundbyte *= OVERDRIVE;
Pokitto 0:e8b8f36b4505 105 if (osc3.kick ) soundbyte >>= 2;
Pokitto 0:e8b8f36b4505 106 osc3.output = soundbyte;
Pokitto 0:e8b8f36b4505 107 #endif
Pokitto 0:e8b8f36b4505 108 }
Pokitto 0:e8b8f36b4505 109 }
Pokitto 0:e8b8f36b4505 110
Pokitto 0:e8b8f36b4505 111 void updateEnvelopes(){
Pokitto 0:e8b8f36b4505 112 //calculate volume envelopes, I do this to save cpu power
Pokitto 0:e8b8f36b4505 113 if (arptick) --arptick;
Pokitto 0:e8b8f36b4505 114 else {
Pokitto 0:e8b8f36b4505 115 if (osc1.arpmode && osc1.on) {
Pokitto 0:e8b8f36b4505 116 osc1.cinc = cincs[osc1.tonic+arptable[osc1.arpmode][osc1.arpstep]];
Pokitto 0:e8b8f36b4505 117 osc1.arpstep++;
Pokitto 0:e8b8f36b4505 118 if (osc1.arpstep==ARPSTEPMAX) osc1.arpstep = 0;
Pokitto 0:e8b8f36b4505 119 arptick = ARPTICK << (3-osc1.arpspeed);
Pokitto 0:e8b8f36b4505 120 }
Pokitto 0:e8b8f36b4505 121 if (osc2.arpmode && osc2.on) {
Pokitto 0:e8b8f36b4505 122 osc2.cinc = cincs[osc2.tonic+arptable[osc2.arpmode][osc2.arpstep]];
Pokitto 0:e8b8f36b4505 123 osc2.arpstep++;
Pokitto 0:e8b8f36b4505 124 if (osc2.arpstep==ARPSTEPMAX) osc2.arpstep = 0;
Pokitto 0:e8b8f36b4505 125 arptick = ARPTICK << (3-osc2.arpspeed);
Pokitto 0:e8b8f36b4505 126 }
Pokitto 0:e8b8f36b4505 127 if (osc3.arpmode && osc3.on) {
Pokitto 0:e8b8f36b4505 128 osc3.cinc = cincs[osc3.tonic+arptable[osc3.arpmode][osc3.arpstep]];
Pokitto 0:e8b8f36b4505 129 osc3.arpstep++;
Pokitto 0:e8b8f36b4505 130 if (osc3.arpstep==ARPSTEPMAX) osc3.arpstep = 0;
Pokitto 0:e8b8f36b4505 131 arptick = ARPTICK << (3-osc3.arpspeed);
Pokitto 0:e8b8f36b4505 132 }
Pokitto 0:e8b8f36b4505 133
Pokitto 0:e8b8f36b4505 134 }
Pokitto 0:e8b8f36b4505 135
Pokitto 0:e8b8f36b4505 136 if (voltick) --voltick;
Pokitto 0:e8b8f36b4505 137 else {
Pokitto 0:e8b8f36b4505 138 bendtick = !bendtick;
Pokitto 0:e8b8f36b4505 139 if (osc1.on) Earr[osc1.adsrphase](&osc1);
Pokitto 0:e8b8f36b4505 140 if (bendtick) osc1.pitchbend += osc1.bendrate; //slow bend to every second beat
Pokitto 0:e8b8f36b4505 141 if (osc1.bendrate > 0 && osc1.pitchbend > osc1.maxbend) {
Pokitto 0:e8b8f36b4505 142 osc1.pitchbend = osc1.maxbend;
Pokitto 0:e8b8f36b4505 143 osc1.bendrate = 0; // STOP BENDING !
Pokitto 0:e8b8f36b4505 144 }
Pokitto 0:e8b8f36b4505 145 else if (osc1.bendrate < 0 && osc1.pitchbend < osc1.maxbend) {
Pokitto 0:e8b8f36b4505 146 osc1.pitchbend = osc1.maxbend;
Pokitto 0:e8b8f36b4505 147 osc1.bendrate = 0; // STOP BENDING !
Pokitto 0:e8b8f36b4505 148 }
Pokitto 0:e8b8f36b4505 149
Pokitto 0:e8b8f36b4505 150 if (osc2.on) Earr[osc2.adsrphase](&osc2);
Pokitto 0:e8b8f36b4505 151 if (bendtick) osc2.pitchbend += osc2.bendrate;
Pokitto 0:e8b8f36b4505 152 if (osc2.bendrate > 0 && osc2.pitchbend > osc2.maxbend) osc2.pitchbend = osc2.maxbend;
Pokitto 0:e8b8f36b4505 153 else if (osc2.bendrate < 0 && osc2.pitchbend < osc2.maxbend) osc2.pitchbend = osc2.maxbend;
Pokitto 0:e8b8f36b4505 154
Pokitto 0:e8b8f36b4505 155 if (osc3.on) Earr[osc3.adsrphase](&osc3);
Pokitto 0:e8b8f36b4505 156 if (bendtick) osc3.pitchbend += osc3.bendrate;
Pokitto 0:e8b8f36b4505 157 if (osc3.bendrate > 0 && osc3.pitchbend > osc3.maxbend) osc3.pitchbend = osc3.maxbend;
Pokitto 0:e8b8f36b4505 158 else if (osc3.bendrate < 0 && osc3.pitchbend < osc3.maxbend) osc3.pitchbend = osc3.maxbend;
Pokitto 0:e8b8f36b4505 159
Pokitto 0:e8b8f36b4505 160 voltick = VOLTICK;
Pokitto 0:e8b8f36b4505 161 }
Pokitto 0:e8b8f36b4505 162 tick = 4;
Pokitto 0:e8b8f36b4505 163 }
Pokitto 0:e8b8f36b4505 164