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_mixfuncs.cpp Source File

Synth_mixfuncs.cpp

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     Synth_mixfuncs.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 "PokittoGlobs.h "
00038 #include "Synth.h "
00039 
00040 /** MIXING FUNCTIONS **/
00041 
00042 char voltick=0; // i need to make volume changes even slower
00043 uint16_t arptick=0; // i need to make volume changes even slower
00044 int8_t bendtick = 0; // ditto for bend.
00045 
00046 
00047 void mix1 (){
00048     // Track 1
00049     if (osc1.on) {
00050     Farr[osc1.wave](&osc1);
00051     #if PROJ_ARDUBOY > 0
00052     if (osc1.duration) {
00053         /**this is special for osc1 and is only used to emulate arduino Tone(); */
00054         osc1.duration--;
00055     } else osc1.on = 0;
00056     #endif
00057 
00058     #ifdef POK_SIM
00059     soundbyte = (((osc1.output>>8) * (osc1.adsrvol >>8 )) >> 8) >> osc1.echodiv; // To output, shift back to 8-bit
00060     if (osc1.overdrive) soundbyte *= OVERDRIVE;
00061     if (osc1.kick ) soundbyte >>= 2;
00062     osc1.output = soundbyte;
00063     #else
00064     //OCR2B = osc1.output>>8;
00065     #if POK_ENABLE_SOUND > 1
00066     soundbyte = (((osc1.output>>8) * (osc1.adsrvol >>8 )) >> 8) >> osc1.echodiv; // To output, shift back to 8-bit
00067     if (osc1.overdrive) soundbyte *= OVERDRIVE;
00068     if (osc1.kick ) soundbyte >>= 2;
00069     osc1.output = soundbyte;
00070     #endif
00071     #endif
00072     }
00073 }
00074 
00075 void mix2(){
00076     // Track 2
00077     if (osc2.on) {
00078     Farr[osc2.wave](&osc2);
00079     #ifdef POK_SIM
00080     soundbyte = (((osc2.output>>8) * (osc2.adsrvol >>8 )) >> 8) >> osc2.echodiv;
00081     if (osc2.overdrive) soundbyte *= OVERDRIVE;
00082     if (osc2.kick ) soundbyte >>= 2;
00083     osc2.output = soundbyte;
00084     #else
00085     //OCR2B = osc2.output>>8;
00086     #if POK_ENABLE_SOUND > 1
00087     soundbyte = (((osc2.output>>8) * (osc2.adsrvol >>8 )) >> 8) >> osc2.echodiv;
00088     if (osc2.overdrive) soundbyte *= OVERDRIVE;
00089     if (osc2.kick ) soundbyte >>= 2;
00090     osc2.output = soundbyte;
00091     #endif
00092     #endif
00093     }
00094 }
00095 
00096 void mix3(){
00097     // Track 3
00098     if (osc3.on) {
00099     Farr[osc3.wave](&osc3);
00100     #ifdef POK_SIM
00101     soundbyte = (((osc3.output>>8) * (osc3.adsrvol >>8 )) >> 8) >> osc3.echodiv;
00102     if (osc3.overdrive) soundbyte *= OVERDRIVE;
00103     if (osc3.kick ) soundbyte >>= 2;
00104     osc3.output = soundbyte;
00105     #else
00106     //OCR2B = osc3.output>>8;
00107     #if POK_ENABLE_SOUND > 1
00108     soundbyte = (((osc3.output>>8) * (osc3.adsrvol >>8 )) >> 8) >> osc3.echodiv;
00109     if (osc3.overdrive) soundbyte *= OVERDRIVE;
00110     if (osc3.kick ) soundbyte >>= 2;
00111     osc3.output = soundbyte;
00112     #endif
00113     #endif
00114     }
00115 }
00116 
00117 void updateEnvelopes(){
00118     //calculate volume envelopes, I do this to save cpu power
00119     if (arptick) --arptick;
00120     else {
00121             if (osc1.arpmode && osc1.on) {
00122                 osc1.cinc = cincs[osc1.tonic+arptable[osc1.arpmode][osc1.arpstep]];
00123                 osc1.arpstep++;
00124                 if (osc1.arpstep==ARPSTEPMAX) osc1.arpstep = 0;
00125                 arptick = ARPTICK << (3-osc1.arpspeed);
00126             }
00127             if (osc2.arpmode && osc2.on) {
00128                 osc2.cinc = cincs[osc2.tonic+arptable[osc2.arpmode][osc2.arpstep]];
00129                 osc2.arpstep++;
00130                 if (osc2.arpstep==ARPSTEPMAX) osc2.arpstep = 0;
00131                 arptick = ARPTICK << (3-osc2.arpspeed);
00132             }
00133             if (osc3.arpmode && osc3.on) {
00134                 osc3.cinc = cincs[osc3.tonic+arptable[osc3.arpmode][osc3.arpstep]];
00135                 osc3.arpstep++;
00136                 if (osc3.arpstep==ARPSTEPMAX) osc3.arpstep = 0;
00137                 arptick = ARPTICK << (3-osc3.arpspeed);
00138             }
00139 
00140     }
00141 
00142     if (voltick) --voltick;
00143     else {
00144             bendtick = !bendtick;
00145             if (osc1.on) Earr[osc1.adsrphase](&osc1);
00146             if (bendtick) osc1.pitchbend += osc1.bendrate; //slow bend to every second beat
00147             if (osc1.bendrate > 0 && osc1.pitchbend > osc1.maxbend) {
00148                     osc1.pitchbend = osc1.maxbend;
00149                     osc1.bendrate = 0; // STOP BENDING !
00150             }
00151             else if (osc1.bendrate < 0 && osc1.pitchbend < osc1.maxbend) {
00152                     osc1.pitchbend = osc1.maxbend;
00153                     osc1.bendrate = 0; // STOP BENDING !
00154             }
00155 
00156             if (osc2.on) Earr[osc2.adsrphase](&osc2);
00157             if (bendtick) osc2.pitchbend += osc2.bendrate;
00158             if (osc2.bendrate > 0 && osc2.pitchbend > osc2.maxbend) osc2.pitchbend = osc2.maxbend;
00159             else if (osc2.bendrate < 0 && osc2.pitchbend < osc2.maxbend) osc2.pitchbend = osc2.maxbend;
00160 
00161             if (osc3.on) Earr[osc3.adsrphase](&osc3);
00162             if (bendtick) osc3.pitchbend += osc3.bendrate;
00163             if (osc3.bendrate > 0 && osc3.pitchbend > osc3.maxbend) osc3.pitchbend = osc3.maxbend;
00164             else if (osc3.bendrate < 0 && osc3.pitchbend < osc3.maxbend) osc3.pitchbend = osc3.maxbend;
00165 
00166             voltick = VOLTICK;
00167     }
00168     tick = 4;
00169 }
00170