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

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
0:e8b8f36b4505
direct lcd stuff used by sensitive

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_songfuncs.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 "PokittoDisk.h"
Pokitto 0:e8b8f36b4505 38 #include "Synth.h"
Pokitto 0:e8b8f36b4505 39 #ifdef POK_SIM
Pokitto 0:e8b8f36b4505 40 #include "FileIO.h"
Pokitto 0:e8b8f36b4505 41 #endif
Pokitto 0:e8b8f36b4505 42
Pokitto 0:e8b8f36b4505 43 /** SONG FUNCTIONS **/
Pokitto 0:e8b8f36b4505 44
Pokitto 0:e8b8f36b4505 45 //uint8_t chunk1[CHUNKSIZE], chunk2[CHUNKSIZE]; // 8 rows, 3 channels (columns), 2 bytes per entry
Pokitto 0:e8b8f36b4505 46 uint8_t chunk[2][CHUNKSIZE]; // 8 rows, 3 channels (columns), 2 bytes per entry
Pokitto 0:e8b8f36b4505 47 uint8_t cc = 0;
Pokitto 0:e8b8f36b4505 48
Pokitto 0:e8b8f36b4505 49
Pokitto 0:e8b8f36b4505 50 #if POK_ENABLE_SOUND > 0
Pokitto 0:e8b8f36b4505 51 #if POK_ENABLE_SD > 0
Pokitto 0:e8b8f36b4505 52 void updatePlaybackSD(uint8_t row) {
Pokitto 0:e8b8f36b4505 53 // samplespertick determines how long the oscillators are active before they are recalculated (i.e. the next tick
Pokitto 0:e8b8f36b4505 54 uint8_t i=0;
Pokitto 0:e8b8f36b4505 55
Pokitto 0:e8b8f36b4505 56 if (notetick > samplespertick ) {
Pokitto 0:e8b8f36b4505 57 // TRACK 1
Pokitto 0:e8b8f36b4505 58 //if (track1on) i = *song.instrument_stream[0];
Pokitto 0:e8b8f36b4505 59 i = 0xF;
Pokitto 0:e8b8f36b4505 60 if (track1on) i = chunk[cc][row+1]>>4;
Pokitto 0:e8b8f36b4505 61 if (i!=0xF) {
Pokitto 0:e8b8f36b4505 62 setOSC(&osc1,1,patch[i].wave,patch[i].loop, patch[i].echo, patch[i].adsr,
Pokitto 0:e8b8f36b4505 63 chunk[cc][row],patch[i].vol,
Pokitto 0:e8b8f36b4505 64 patch[i].attack, patch[i].decay, patch[i].sustain,patch[i].release,
Pokitto 0:e8b8f36b4505 65 patch[i].maxbend, patch[i].bendrate, patch[i].arpmode, patch[i].overdrive, patch[i].kick );
Pokitto 0:e8b8f36b4505 66 }
Pokitto 0:e8b8f36b4505 67 // TRACK 2
Pokitto 0:e8b8f36b4505 68 //if (track2on) i = *song.instrument_stream[1];
Pokitto 0:e8b8f36b4505 69 i = 0xF;
Pokitto 0:e8b8f36b4505 70 if (track2on) i = chunk[cc][row+3]>>4;
Pokitto 0:e8b8f36b4505 71 if (i!=0xF) {
Pokitto 0:e8b8f36b4505 72 setOSC(&osc2,1,patch[i].wave,patch[i].loop, patch[i].echo, patch[i].adsr,
Pokitto 0:e8b8f36b4505 73 chunk[cc][row+2],patch[i].vol,
Pokitto 0:e8b8f36b4505 74 patch[i].attack, patch[i].decay, patch[i].sustain,patch[i].release,
Pokitto 0:e8b8f36b4505 75 patch[i].maxbend, patch[i].bendrate, patch[i].arpmode, patch[i].overdrive, patch[i].kick );
Pokitto 0:e8b8f36b4505 76 }
Pokitto 0:e8b8f36b4505 77 // TRACK 3
Pokitto 0:e8b8f36b4505 78 i = 0xF;
Pokitto 0:e8b8f36b4505 79 if (track3on) i = chunk[cc][row+5]>>4;
Pokitto 0:e8b8f36b4505 80 if (i!=0xF) {
Pokitto 0:e8b8f36b4505 81 setOSC(&osc3,1,patch[i].wave,patch[i].loop, patch[i].echo, patch[i].adsr,
Pokitto 0:e8b8f36b4505 82 chunk[cc][row]+4,patch[i].vol,
Pokitto 0:e8b8f36b4505 83 patch[i].attack, patch[i].decay, patch[i].sustain,patch[i].release,
Pokitto 0:e8b8f36b4505 84 patch[i].maxbend, patch[i].bendrate, patch[i].arpmode, patch[i].overdrive, patch[i].kick );
Pokitto 0:e8b8f36b4505 85 }
Pokitto 0:e8b8f36b4505 86 playerpos++;
Pokitto 0:e8b8f36b4505 87 if (playerpos == PATTERNLENGTH) {
Pokitto 0:e8b8f36b4505 88 // move to next song position
Pokitto 0:e8b8f36b4505 89 playerpos = 0;
Pokitto 0:e8b8f36b4505 90 /*sequencepos++;
Pokitto 0:e8b8f36b4505 91 if (sequencepos > song.song_end) {
Pokitto 0:e8b8f36b4505 92 if (song.song_loop == -1) {
Pokitto 0:e8b8f36b4505 93 emptyOscillators();
Pokitto 0:e8b8f36b4505 94 playing = false;
Pokitto 0:e8b8f36b4505 95 sequencepos--;
Pokitto 0:e8b8f36b4505 96 } else {
Pokitto 0:e8b8f36b4505 97 sequencepos = song.song_loop;
Pokitto 0:e8b8f36b4505 98 }
Pokitto 0:e8b8f36b4505 99 }
Pokitto 0:e8b8f36b4505 100 playerpos = 0;
Pokitto 0:e8b8f36b4505 101 initStreams(sequencepos);
Pokitto 0:e8b8f36b4505 102 tableRefresh=true;*/
Pokitto 0:e8b8f36b4505 103 }
Pokitto 0:e8b8f36b4505 104 notetick =0;
Pokitto 0:e8b8f36b4505 105 }
Pokitto 0:e8b8f36b4505 106 }
Pokitto 0:e8b8f36b4505 107
Pokitto 0:e8b8f36b4505 108 void updatePlayback() {
Pokitto 0:e8b8f36b4505 109 // samplespertick determines how long the oscillators are active before they are recalculated (i.e. the next tick
Pokitto 0:e8b8f36b4505 110 uint8_t i=0;
Pokitto 0:e8b8f36b4505 111
Pokitto 0:e8b8f36b4505 112 if (notetick > samplespertick ) {
Pokitto 0:e8b8f36b4505 113 // TRACK 1
Pokitto 0:e8b8f36b4505 114 if (track1on) i = *song.instrument_stream[0];
Pokitto 0:e8b8f36b4505 115 else i = 0;
Pokitto 0:e8b8f36b4505 116 if (i) {
Pokitto 0:e8b8f36b4505 117 setOSC(&osc1,1,patch[i].wave,patch[i].loop, patch[i].echo, patch[i].adsr,
Pokitto 0:e8b8f36b4505 118 *song.note_stream[0],patch[i].vol,
Pokitto 0:e8b8f36b4505 119 patch[i].attack, patch[i].decay, patch[i].sustain,patch[i].release,
Pokitto 0:e8b8f36b4505 120 patch[i].maxbend, patch[i].bendrate, patch[i].arpmode, patch[i].overdrive, patch[i].kick );
Pokitto 0:e8b8f36b4505 121 }
Pokitto 0:e8b8f36b4505 122 // TRACK 2
Pokitto 0:e8b8f36b4505 123 if (track2on) i = *song.instrument_stream[1];
Pokitto 0:e8b8f36b4505 124 else i = 0;
Pokitto 0:e8b8f36b4505 125 if (i) {
Pokitto 0:e8b8f36b4505 126 setOSC(&osc2,1,patch[i].wave,patch[i].loop, patch[i].echo, patch[i].adsr,
Pokitto 0:e8b8f36b4505 127 *song.note_stream[1],patch[i].vol,
Pokitto 0:e8b8f36b4505 128 patch[i].attack, patch[i].decay, patch[i].sustain,patch[i].release,
Pokitto 0:e8b8f36b4505 129 patch[i].maxbend, patch[i].bendrate, patch[i].arpmode, patch[i].overdrive, patch[i].kick );
Pokitto 0:e8b8f36b4505 130 }
Pokitto 0:e8b8f36b4505 131 // TRACK 3
Pokitto 0:e8b8f36b4505 132 if (track3on) i = *song.instrument_stream[2];
Pokitto 0:e8b8f36b4505 133 else i = 0;
Pokitto 0:e8b8f36b4505 134 if (i) {
Pokitto 0:e8b8f36b4505 135 setOSC(&osc3,1,patch[i].wave,patch[i].loop, patch[i].echo, patch[i].adsr,
Pokitto 0:e8b8f36b4505 136 *song.note_stream[2],patch[i].vol,
Pokitto 0:e8b8f36b4505 137 patch[i].attack, patch[i].decay, patch[i].sustain,patch[i].release,
Pokitto 0:e8b8f36b4505 138 patch[i].maxbend, patch[i].bendrate, patch[i].arpmode, patch[i].overdrive, patch[i].kick );
Pokitto 0:e8b8f36b4505 139 }
Pokitto 0:e8b8f36b4505 140 playerpos++;
Pokitto 0:e8b8f36b4505 141 song.instrument_stream[0]++;
Pokitto 0:e8b8f36b4505 142 song.note_stream[0]++;
Pokitto 0:e8b8f36b4505 143 song.instrument_stream[1]++;
Pokitto 0:e8b8f36b4505 144 song.note_stream[1]++;
Pokitto 0:e8b8f36b4505 145 song.instrument_stream[2]++;
Pokitto 0:e8b8f36b4505 146 song.note_stream[2]++;
Pokitto 0:e8b8f36b4505 147 if (playerpos == PATTERNLENGTH) {
Pokitto 0:e8b8f36b4505 148 // move to next song position
Pokitto 0:e8b8f36b4505 149 sequencepos++;
Pokitto 0:e8b8f36b4505 150 if (sequencepos > song.song_end) {
Pokitto 0:e8b8f36b4505 151 if (song.song_loop == -1) {
Pokitto 0:e8b8f36b4505 152 emptyOscillators();
Pokitto 0:e8b8f36b4505 153 playing = false;
Pokitto 0:e8b8f36b4505 154 sequencepos--;
Pokitto 0:e8b8f36b4505 155 } else {
Pokitto 0:e8b8f36b4505 156 sequencepos = song.song_loop;
Pokitto 0:e8b8f36b4505 157 }
Pokitto 0:e8b8f36b4505 158 }
Pokitto 0:e8b8f36b4505 159 playerpos = 0;
Pokitto 0:e8b8f36b4505 160 initStreams(sequencepos);
Pokitto 0:e8b8f36b4505 161 tableRefresh=true;
Pokitto 0:e8b8f36b4505 162 }
Pokitto 0:e8b8f36b4505 163 notetick =0;
Pokitto 0:e8b8f36b4505 164 }
Pokitto 0:e8b8f36b4505 165 }
Pokitto 0:e8b8f36b4505 166
Pokitto 0:e8b8f36b4505 167 void emptyPatches(){
Pokitto 0:e8b8f36b4505 168 for (int i=0; i<16; i++) {
Pokitto 0:e8b8f36b4505 169 patch[i].vol = 127;
Pokitto 0:e8b8f36b4505 170 patch[i].on = true; patch[i].wave = 1; patch[i].echo = 0; patch[i].count = 0; patch[i].cinc =0;
Pokitto 0:e8b8f36b4505 171 patch[i].attack = 0; patch[i].loop = 0; patch[i].adsrphase = 0; patch[i].adsr = 0; patch[i].decay = 20;
Pokitto 0:e8b8f36b4505 172 patch[i].pitchbend = 0; patch[i].bendrate = 0; patch[i].maxbend = 0; patch[i].sustain = 0; patch[i].release = 0, patch[i].overdrive = 0, patch[i].kick = 0;
Pokitto 0:e8b8f36b4505 173 }
Pokitto 0:e8b8f36b4505 174 }
Pokitto 0:e8b8f36b4505 175
Pokitto 0:e8b8f36b4505 176 void emptyBlocks(){
Pokitto 0:e8b8f36b4505 177 for (int i=0; i<MAXBLOCKS; i++) {
Pokitto 0:e8b8f36b4505 178 for (int j = 0; j < PATTERNLENGTH; j++) {
Pokitto 0:e8b8f36b4505 179 block[i].instrument[j] = 0;
Pokitto 0:e8b8f36b4505 180 block[i].notenumber[j] = 255;
Pokitto 0:e8b8f36b4505 181 }
Pokitto 0:e8b8f36b4505 182 }
Pokitto 0:e8b8f36b4505 183 }
Pokitto 0:e8b8f36b4505 184
Pokitto 0:e8b8f36b4505 185 void initStreams(uint8_t seqpos){
Pokitto 0:e8b8f36b4505 186 uint8_t blocknum;
Pokitto 0:e8b8f36b4505 187 // retarget pointers for track 1
Pokitto 0:e8b8f36b4505 188 // byte = pgm_read_byte(&(mydata[i][j]));
Pokitto 0:e8b8f36b4505 189 blocknum=song.block_sequence[0][seqpos];
Pokitto 0:e8b8f36b4505 190 //blocknum=pgm_read_byte(Song+SONG_SEQUENCE+seqpos);
Pokitto 0:e8b8f36b4505 191 song.instrument_stream[0]=&block[blocknum].instrument[0];
Pokitto 0:e8b8f36b4505 192 song.note_stream[0]=&block[blocknum].notenumber[0];
Pokitto 0:e8b8f36b4505 193 // retarget pointers for track 2
Pokitto 0:e8b8f36b4505 194 blocknum=song.block_sequence[1][seqpos];
Pokitto 0:e8b8f36b4505 195 song.instrument_stream[1]=&block[blocknum].instrument[0];
Pokitto 0:e8b8f36b4505 196 song.note_stream[1]=&block[blocknum].notenumber[0];
Pokitto 0:e8b8f36b4505 197 // retarget pointers for track 3
Pokitto 0:e8b8f36b4505 198 blocknum=song.block_sequence[2][seqpos];
Pokitto 0:e8b8f36b4505 199 song.instrument_stream[2]=&block[blocknum].instrument[0];
Pokitto 0:e8b8f36b4505 200 song.note_stream[2]=&block[blocknum].notenumber[0];
Pokitto 0:e8b8f36b4505 201 }
Pokitto 0:e8b8f36b4505 202
Pokitto 0:e8b8f36b4505 203 void emptySong(){
Pokitto 0:e8b8f36b4505 204 song.num_channels = 3;
Pokitto 0:e8b8f36b4505 205 song.num_patches = 1;
Pokitto 0:e8b8f36b4505 206 song.song_bpm = 120;
Pokitto 0:e8b8f36b4505 207 song.num_patterns = 1;
Pokitto 0:e8b8f36b4505 208 song.song_end = 0;
Pokitto 0:e8b8f36b4505 209 song.song_loop = 0; // loop back to start
Pokitto 0:e8b8f36b4505 210 song.rb_version = RBTRACKER_VERSION;
Pokitto 0:e8b8f36b4505 211 for (uint8_t i = 0; i<10; i++) {
Pokitto 0:e8b8f36b4505 212 song.block_sequence[0][i]=i*3; // track 1
Pokitto 0:e8b8f36b4505 213 song.block_sequence[1][i]=i*3+1; // track 2
Pokitto 0:e8b8f36b4505 214 song.block_sequence[2][i]=i*3+2; // track 3
Pokitto 0:e8b8f36b4505 215 }
Pokitto 0:e8b8f36b4505 216 song.instrument_stream[0] = &block[0].instrument[0];
Pokitto 0:e8b8f36b4505 217 song.note_stream[0] = &block[0].notenumber[0];
Pokitto 0:e8b8f36b4505 218 song.instrument_stream[1] = &block[1].instrument[0];
Pokitto 0:e8b8f36b4505 219 song.note_stream[1] = &block[1].notenumber[0];
Pokitto 0:e8b8f36b4505 220 song.instrument_stream[2] = &block[2].instrument[0];
Pokitto 0:e8b8f36b4505 221 song.note_stream[2] = &block[2].notenumber[0];
Pokitto 0:e8b8f36b4505 222 sequencepos = 0;
Pokitto 0:e8b8f36b4505 223 }
Pokitto 0:e8b8f36b4505 224
Pokitto 0:e8b8f36b4505 225 int openSongFromSD(char* buffer) {
Pokitto 0:e8b8f36b4505 226 if (!isThisFileOpen(buffer)) {
Pokitto 0:e8b8f36b4505 227 fileClose(); // close any open files
Pokitto 0:e8b8f36b4505 228 fileOpen(buffer,FILE_MODE_OVERWRITE | FILE_MODE_BINARY);
Pokitto 0:e8b8f36b4505 229 }
Pokitto 0:e8b8f36b4505 230 return isThisFileOpen(buffer);
Pokitto 0:e8b8f36b4505 231 }
Pokitto 0:e8b8f36b4505 232
Pokitto 0:e8b8f36b4505 233 void writeChunkToSD(uint8_t* buffer) {
Pokitto 0:e8b8f36b4505 234 if (fileOK()) {
Pokitto 0:e8b8f36b4505 235 fileWriteBytes(buffer, CHUNKSIZE);
Pokitto 0:e8b8f36b4505 236 }
Pokitto 0:e8b8f36b4505 237 }
Pokitto 0:e8b8f36b4505 238
Pokitto 0:e8b8f36b4505 239 void readChunkFromSD(uint8_t* buffer) {
Pokitto 0:e8b8f36b4505 240 if (fileOK()) {
Pokitto 0:e8b8f36b4505 241 fileReadBytes(buffer, CHUNKSIZE);
Pokitto 0:e8b8f36b4505 242 }
Pokitto 0:e8b8f36b4505 243 }
Pokitto 0:e8b8f36b4505 244 #endif
Pokitto 0:e8b8f36b4505 245 #endif // POK_ENABLE_SOUND
Pokitto 0:e8b8f36b4505 246