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_wavefuncs.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 "Synth.h"
Pokitto 31:f4b9b85c7b62 38
Pokitto 31:f4b9b85c7b62 39 uint16_t noiseval, noiseval2;
Pokitto 31:f4b9b85c7b62 40
Pokitto 31:f4b9b85c7b62 41 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);
Pokitto 31:f4b9b85c7b62 42 void noADSR(OSC* o); void attackFunc(OSC* o); void decayFunc(OSC* o); void releaseFunc(OSC* o);
Pokitto 31:f4b9b85c7b62 43 void mix1(); void mix2(); void mix3(); void updateEnvelopes();
Pokitto 31:f4b9b85c7b62 44
Pokitto 31:f4b9b85c7b62 45 waveFunction Farr [] = {waveoff, sqwave, sawwave, triwave, noise, tonenoise};
Pokitto 31:f4b9b85c7b62 46 envFunction Earr [] = {noADSR, attackFunc, decayFunc, releaseFunc};
Pokitto 31:f4b9b85c7b62 47 mixFunction Marr [] = {updateEnvelopes,mix3,mix2,mix1}; // counts down
Pokitto 31:f4b9b85c7b62 48 mixFunction HWMarr [] = {updateEnvelopes,mix3,mix2,mix1}; // counts down
Pokitto 31:f4b9b85c7b62 49
Pokitto 31:f4b9b85c7b62 50 /** SOUND FUNCTIONS **/
Pokitto 31:f4b9b85c7b62 51
Pokitto 31:f4b9b85c7b62 52 void waveoff(OSC* o){
Pokitto 31:f4b9b85c7b62 53 o->output = 0;
Pokitto 31:f4b9b85c7b62 54 }
Pokitto 31:f4b9b85c7b62 55
Pokitto 31:f4b9b85c7b62 56 void sqwave(OSC* o){
Pokitto 31:f4b9b85c7b62 57 // square. If bit 16 set, its 2nd half of cycle and then output. if not, silence.
Pokitto 31:f4b9b85c7b62 58 if (o->count & 0x80000000) o->output = 0;
Pokitto 31:f4b9b85c7b62 59 else
Pokitto 31:f4b9b85c7b62 60 o->output = 0xFFFF;
Pokitto 31:f4b9b85c7b62 61 }
Pokitto 31:f4b9b85c7b62 62
Pokitto 31:f4b9b85c7b62 63 void sawwave(OSC* o){
Pokitto 31:f4b9b85c7b62 64 // saw is just twice the count, so it happens 2 times in a cycle.
Pokitto 31:f4b9b85c7b62 65 o->output = (o->count >>15); //its now 32 bits (o->count << 1)>>4; // simple, eh ?
Pokitto 31:f4b9b85c7b62 66 }
Pokitto 31:f4b9b85c7b62 67
Pokitto 31:f4b9b85c7b62 68 void triwave(OSC* o){
Pokitto 31:f4b9b85c7b62 69 // exploit the fact that above 0x7FFF bit 16 will be set (ie. when on second side of cycle)
Pokitto 31:f4b9b85c7b62 70 if (o->count & 0x80000000) o->output = (~o->count) >>15;// << 1; // counts down because complement goes other way
Pokitto 31:f4b9b85c7b62 71 else o->output = o->count >>15; // now 32 bits ! << 1; // count up on first side of cycle
Pokitto 31:f4b9b85c7b62 72 }
Pokitto 31:f4b9b85c7b62 73
Pokitto 31:f4b9b85c7b62 74 void noise(OSC* o){
Pokitto 31:f4b9b85c7b62 75 // Standard libc random gives 10-12 fps
Pokitto 31:f4b9b85c7b62 76 // Xorshift16 gives 32-39 fps
Pokitto 31:f4b9b85c7b62 77 // Xorshift8 gives
Pokitto 31:f4b9b85c7b62 78
Pokitto 31:f4b9b85c7b62 79 if (o->count > 0x80000000) {
Pokitto 31:f4b9b85c7b62 80 o->output = noiseval2;
Pokitto 31:f4b9b85c7b62 81 //noiseval = random(0,0xFFFF);
Pokitto 31:f4b9b85c7b62 82 noiseval = xorshift16();
Pokitto 31:f4b9b85c7b62 83 //noiseval = xorshift8()*256;
Pokitto 31:f4b9b85c7b62 84 }
Pokitto 31:f4b9b85c7b62 85 else {
Pokitto 31:f4b9b85c7b62 86 o->output = noiseval;
Pokitto 31:f4b9b85c7b62 87 //noiseval2 = random(0,0xFFFF);
Pokitto 31:f4b9b85c7b62 88 noiseval2 = xorshift16();
Pokitto 31:f4b9b85c7b62 89 //noiseval2 = xorshift8()*256;
Pokitto 31:f4b9b85c7b62 90 }
Pokitto 31:f4b9b85c7b62 91 }
Pokitto 31:f4b9b85c7b62 92
Pokitto 31:f4b9b85c7b62 93 void tonenoise(OSC* o){
Pokitto 31:f4b9b85c7b62 94 // square. If bit 16 set, its 2nd half of cycle and then output. if not, silence.
Pokitto 31:f4b9b85c7b62 95 if (o->count & 0x80000000) o->output = (xorshift16()>>6); //was 0
Pokitto 31:f4b9b85c7b62 96 else o->output = (xorshift16()>>1) + 0x4000;//random(0,0xFFFF);
Pokitto 31:f4b9b85c7b62 97 }
Pokitto 31:f4b9b85c7b62 98
Pokitto 31:f4b9b85c7b62 99 void sample(OSC* o) {
Pokitto 31:f4b9b85c7b62 100
Pokitto 31:f4b9b85c7b62 101 /*if (o->samplepos > o->samplelength ) o->samplepos = 0;
Pokitto 31:f4b9b85c7b62 102
Pokitto 31:f4b9b85c7b62 103 if (o->count > o->wcycle) {
Pokitto 31:f4b9b85c7b62 104 o->count=0;
Pokitto 31:f4b9b85c7b62 105 if (o->output) o->output = 0;
Pokitto 31:f4b9b85c7b62 106 //else o->output = o->output=pgm_read_byte((uint32_t)(sfxBike) + o->inccount);
Pokitto 31:f4b9b85c7b62 107 }*/
Pokitto 31:f4b9b85c7b62 108 }
Pokitto 31:f4b9b85c7b62 109
Pokitto 31:f4b9b85c7b62 110