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

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Synth_wavefuncs.cpp Source File

Synth_wavefuncs.cpp

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     Synth_wavefuncs.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 uint16_t noiseval, noiseval2;
00040 
00041 void waveoff(OSC* o); void sqwave(OSC* o); void sawwave(OSC* o); void triwave(OSC* o); void noise(OSC* o); void tonenoise(OSC* o); void sample(OSC* o);
00042 void noADSR(OSC* o); void attackFunc(OSC* o); void decayFunc(OSC* o); void releaseFunc(OSC* o);
00043 void mix1 (); void mix2(); void mix3(); void updateEnvelopes();
00044 
00045 waveFunction Farr []  = {waveoff, sqwave, sawwave, triwave, noise, tonenoise, sample};
00046 envFunction Earr [] = {noADSR, attackFunc, decayFunc, releaseFunc};
00047 mixFunction Marr [] = {updateEnvelopes,mix3,mix2,mix1 }; // counts down
00048 mixFunction HWMarr [] = {updateEnvelopes,mix3,mix2,mix1 }; // counts down
00049 
00050 /** SOUND FUNCTIONS **/
00051 
00052 void waveoff(OSC* o){
00053   o->output = 0;
00054 }
00055 
00056 void sqwave(OSC* o){
00057 // square. If bit 16 set, its 2nd half of cycle and then output. if not, silence.
00058  if (o->count & 0x80000000) o->output = 0;
00059  else
00060      o->output = 0xFFFF;
00061 }
00062 
00063 void sawwave(OSC* o){
00064  // saw is just twice the count, so it happens 2 times in a cycle.
00065  o->output = (o->count >>15);  //its now 32 bits (o->count << 1)>>4; // simple, eh ?
00066 }
00067 
00068 void triwave(OSC* o){
00069     // exploit the fact that above 0x7FFF bit 16 will be set (ie. when on second side of cycle)
00070     if (o->count & 0x80000000) o->output = (~o->count) >>15;// << 1; // counts down because complement goes other way
00071     else o->output = o->count >>15; // now 32 bits ! << 1; // count up on first side of cycle
00072 }
00073 
00074 void noise(OSC* o){
00075   // Standard libc random gives 10-12 fps
00076   // Xorshift16 gives 32-39 fps
00077   // Xorshift8 gives
00078 
00079   if (o->count > 0x80000000) {
00080     o->output = noiseval2;
00081     //noiseval = random(0,0xFFFF);
00082     noiseval = xorshift16();
00083     //noiseval = xorshift8()*256;
00084   }
00085   else  {
00086     o->output = noiseval;
00087     //noiseval2 = random(0,0xFFFF);
00088     noiseval2 = xorshift16();
00089     //noiseval2 = xorshift8()*256;
00090   }
00091 }
00092 
00093 void tonenoise(OSC* o){
00094   // square. If bit 16 set, its 2nd half of cycle and then output. if not, silence.
00095  if (o->count & 0x80000000) o->output = (xorshift16()>>6); //was 0
00096  else  o->output = (xorshift16()>>1) + 0x4000;//random(0,0xFFFF);
00097 }
00098 
00099 void sample(OSC* o) {
00100     if (sample==NULL) o->output = 0;
00101     else {
00102           o->samplepos+=o->samplestep;
00103           if ((o->samplepos>>8) > o->samplelength ) {
00104                 o->samplepos = 0;
00105                 if (o->loop == 0) o->duration=0;
00106           }
00107           o->output = *(o->sample + (o->samplepos>>8))<<8;
00108     }
00109 }
00110 
00111