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

Committer:
Pokitto
Date:
Tue Jan 30 10:41:47 2018 +0000
Revision:
31:f4b9b85c7b62
Sound output improvements added:  louder, clearer, faster!

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 31:f4b9b85c7b62 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 31:f4b9b85c7b62 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 31:f4b9b85c7b62 79 #ifdef POK_SIM
Pokitto 31:f4b9b85c7b62 80 soundbyte = (((osc2.output>>8) * (osc2.adsrvol >>8 )) >> 8) >> osc2.echodiv;
Pokitto 31:f4b9b85c7b62 81 if (osc2.overdrive) soundbyte *= OVERDRIVE;
Pokitto 31:f4b9b85c7b62 82 if (osc2.kick ) soundbyte >>= 2;
Pokitto 31:f4b9b85c7b62 83 osc2.output = soundbyte;
Pokitto 31:f4b9b85c7b62 84 #else
Pokitto 31:f4b9b85c7b62 85 //OCR2B = osc2.output>>8;
Pokitto 31:f4b9b85c7b62 86 #if POK_ENABLE_SOUND > 0
Pokitto 31:f4b9b85c7b62 87 soundbyte = (((osc2.output>>8) * (osc2.adsrvol >>8 )) >> 8) >> osc2.echodiv;
Pokitto 31:f4b9b85c7b62 88 if (osc2.overdrive) soundbyte *= OVERDRIVE;
Pokitto 31:f4b9b85c7b62 89 if (osc2.kick ) soundbyte >>= 2;
Pokitto 31:f4b9b85c7b62 90 osc2.output = soundbyte;
Pokitto 31:f4b9b85c7b62 91 #endif
Pokitto 31:f4b9b85c7b62 92 #endif
Pokitto 31:f4b9b85c7b62 93 }
Pokitto 31:f4b9b85c7b62 94 }
Pokitto 31:f4b9b85c7b62 95
Pokitto 31:f4b9b85c7b62 96 void mix3(){
Pokitto 31:f4b9b85c7b62 97 // Track 3
Pokitto 31:f4b9b85c7b62 98 if (osc3.on) {
Pokitto 31:f4b9b85c7b62 99 Farr[osc3.wave](&osc3);
Pokitto 31:f4b9b85c7b62 100 #ifdef POK_SIM
Pokitto 31:f4b9b85c7b62 101 soundbyte = (((osc3.output>>8) * (osc3.adsrvol >>8 )) >> 8) >> osc3.echodiv;
Pokitto 31:f4b9b85c7b62 102 if (osc3.overdrive) soundbyte *= OVERDRIVE;
Pokitto 31:f4b9b85c7b62 103 if (osc3.kick ) soundbyte >>= 2;
Pokitto 31:f4b9b85c7b62 104 osc3.output = soundbyte;
Pokitto 31:f4b9b85c7b62 105 #else
Pokitto 31:f4b9b85c7b62 106 //OCR2B = osc3.output>>8;
Pokitto 31:f4b9b85c7b62 107 #if POK_ENABLE_SOUND > 0
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 #endif
Pokitto 31:f4b9b85c7b62 113 #endif
Pokitto 31:f4b9b85c7b62 114 }
Pokitto 31:f4b9b85c7b62 115 }
Pokitto 31:f4b9b85c7b62 116
Pokitto 31:f4b9b85c7b62 117 void updateEnvelopes(){
Pokitto 31:f4b9b85c7b62 118 //calculate volume envelopes, I do this to save cpu power
Pokitto 31:f4b9b85c7b62 119 if (arptick) --arptick;
Pokitto 31:f4b9b85c7b62 120 else {
Pokitto 31:f4b9b85c7b62 121 if (osc1.arpmode && osc1.on) {
Pokitto 31:f4b9b85c7b62 122 osc1.cinc = cincs[osc1.tonic+arptable[osc1.arpmode][osc1.arpstep]];
Pokitto 31:f4b9b85c7b62 123 osc1.arpstep++;
Pokitto 31:f4b9b85c7b62 124 if (osc1.arpstep==ARPSTEPMAX) osc1.arpstep = 0;
Pokitto 31:f4b9b85c7b62 125 arptick = ARPTICK << (3-osc1.arpspeed);
Pokitto 31:f4b9b85c7b62 126 }
Pokitto 31:f4b9b85c7b62 127 if (osc2.arpmode && osc2.on) {
Pokitto 31:f4b9b85c7b62 128 osc2.cinc = cincs[osc2.tonic+arptable[osc2.arpmode][osc2.arpstep]];
Pokitto 31:f4b9b85c7b62 129 osc2.arpstep++;
Pokitto 31:f4b9b85c7b62 130 if (osc2.arpstep==ARPSTEPMAX) osc2.arpstep = 0;
Pokitto 31:f4b9b85c7b62 131 arptick = ARPTICK << (3-osc2.arpspeed);
Pokitto 31:f4b9b85c7b62 132 }
Pokitto 31:f4b9b85c7b62 133 if (osc3.arpmode && osc3.on) {
Pokitto 31:f4b9b85c7b62 134 osc3.cinc = cincs[osc3.tonic+arptable[osc3.arpmode][osc3.arpstep]];
Pokitto 31:f4b9b85c7b62 135 osc3.arpstep++;
Pokitto 31:f4b9b85c7b62 136 if (osc3.arpstep==ARPSTEPMAX) osc3.arpstep = 0;
Pokitto 31:f4b9b85c7b62 137 arptick = ARPTICK << (3-osc3.arpspeed);
Pokitto 31:f4b9b85c7b62 138 }
Pokitto 31:f4b9b85c7b62 139
Pokitto 31:f4b9b85c7b62 140 }
Pokitto 31:f4b9b85c7b62 141
Pokitto 31:f4b9b85c7b62 142 if (voltick) --voltick;
Pokitto 31:f4b9b85c7b62 143 else {
Pokitto 31:f4b9b85c7b62 144 bendtick = !bendtick;
Pokitto 31:f4b9b85c7b62 145 if (osc1.on) Earr[osc1.adsrphase](&osc1);
Pokitto 31:f4b9b85c7b62 146 if (bendtick) osc1.pitchbend += osc1.bendrate; //slow bend to every second beat
Pokitto 31:f4b9b85c7b62 147 if (osc1.bendrate > 0 && osc1.pitchbend > osc1.maxbend) {
Pokitto 31:f4b9b85c7b62 148 osc1.pitchbend = osc1.maxbend;
Pokitto 31:f4b9b85c7b62 149 osc1.bendrate = 0; // STOP BENDING !
Pokitto 31:f4b9b85c7b62 150 }
Pokitto 31:f4b9b85c7b62 151 else if (osc1.bendrate < 0 && osc1.pitchbend < osc1.maxbend) {
Pokitto 31:f4b9b85c7b62 152 osc1.pitchbend = osc1.maxbend;
Pokitto 31:f4b9b85c7b62 153 osc1.bendrate = 0; // STOP BENDING !
Pokitto 31:f4b9b85c7b62 154 }
Pokitto 31:f4b9b85c7b62 155
Pokitto 31:f4b9b85c7b62 156 if (osc2.on) Earr[osc2.adsrphase](&osc2);
Pokitto 31:f4b9b85c7b62 157 if (bendtick) osc2.pitchbend += osc2.bendrate;
Pokitto 31:f4b9b85c7b62 158 if (osc2.bendrate > 0 && osc2.pitchbend > osc2.maxbend) osc2.pitchbend = osc2.maxbend;
Pokitto 31:f4b9b85c7b62 159 else if (osc2.bendrate < 0 && osc2.pitchbend < osc2.maxbend) osc2.pitchbend = osc2.maxbend;
Pokitto 31:f4b9b85c7b62 160
Pokitto 31:f4b9b85c7b62 161 if (osc3.on) Earr[osc3.adsrphase](&osc3);
Pokitto 31:f4b9b85c7b62 162 if (bendtick) osc3.pitchbend += osc3.bendrate;
Pokitto 31:f4b9b85c7b62 163 if (osc3.bendrate > 0 && osc3.pitchbend > osc3.maxbend) osc3.pitchbend = osc3.maxbend;
Pokitto 31:f4b9b85c7b62 164 else if (osc3.bendrate < 0 && osc3.pitchbend < osc3.maxbend) osc3.pitchbend = osc3.maxbend;
Pokitto 31:f4b9b85c7b62 165
Pokitto 31:f4b9b85c7b62 166 voltick = VOLTICK;
Pokitto 31:f4b9b85c7b62 167 }
Pokitto 31:f4b9b85c7b62 168 tick = 4;
Pokitto 31:f4b9b85c7b62 169 }
Pokitto 31:f4b9b85c7b62 170
Pokitto 31:f4b9b85c7b62 171