PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
spinal
Date:
Sun Oct 15 18:03:02 2017 +0000
Revision:
11:02ad9c807a21
Parent:
2:968589ca3484
fixed 4color refreshRegion code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 0:e8b8f36b4505 1 /**************************************************************************/
Pokitto 0:e8b8f36b4505 2 /*!
Pokitto 0:e8b8f36b4505 3 @file HWSound.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 "mbed.h"
Pokitto 0:e8b8f36b4505 38 #include "HWSound.h"
Pokitto 0:e8b8f36b4505 39 //#include "MCP4018.h"
Pokitto 0:e8b8f36b4505 40 #include "SoftwareI2C.h"
Pokitto 0:e8b8f36b4505 41 #include "Pokitto_settings.h"
Pokitto 0:e8b8f36b4505 42 #include "PokittoDisk.h"
Pokitto 0:e8b8f36b4505 43 #include "PokittoGlobs.h"
Pokitto 0:e8b8f36b4505 44 #include "Synth.h"
Pokitto 0:e8b8f36b4505 45
Pokitto 0:e8b8f36b4505 46
Pokitto 0:e8b8f36b4505 47
Pokitto 0:e8b8f36b4505 48 using namespace Pokitto;
Pokitto 0:e8b8f36b4505 49
Pokitto 0:e8b8f36b4505 50 /** Sound Variables **/
Pokitto 0:e8b8f36b4505 51 #if (POK_STREAMING_MUSIC > 0)
Pokitto 0:e8b8f36b4505 52 unsigned char buffers[4][BUFFER_SIZE];
Pokitto 0:e8b8f36b4505 53 volatile int currentBuffer = 0, oldBuffer = 0;
Pokitto 0:e8b8f36b4505 54 volatile int bufindex = 0, vol=1;
Pokitto 0:e8b8f36b4505 55 volatile unsigned char * currentPtr;
Pokitto 0:e8b8f36b4505 56 volatile unsigned char * endPtr;
Pokitto 0:e8b8f36b4505 57 int8_t streamvol=3;
Pokitto 0:e8b8f36b4505 58 uint32_t streamcounter=0;
Pokitto 0:e8b8f36b4505 59 uint8_t streamstep=0;
Pokitto 0:e8b8f36b4505 60 #endif
Pokitto 0:e8b8f36b4505 61
Pokitto 0:e8b8f36b4505 62 #if POK_ENABLE_SOUND > 0
Pokitto 0:e8b8f36b4505 63 pwmout_t Pokitto::audiopwm; // this way (instead of PwmOut class) pwm doesn't start screaming until it is initialized !
Pokitto 0:e8b8f36b4505 64 Ticker Pokitto::audio;
Pokitto 0:e8b8f36b4505 65
Pokitto 0:e8b8f36b4505 66 using namespace Pokitto;
Pokitto 0:e8b8f36b4505 67
Pokitto 0:e8b8f36b4505 68 /** stream output and status */
Pokitto 0:e8b8f36b4505 69 uint8_t Pokitto::streambyte,Pokitto::streamon;
Pokitto 0:e8b8f36b4505 70
Pokitto 0:e8b8f36b4505 71 uint8_t soundbuf[256], soundbufindex=0, Pokitto::HWvolume=0;
Pokitto 0:e8b8f36b4505 72 bool volpotError=false; //test for broken MCP4018
Pokitto 0:e8b8f36b4505 73
Pokitto 0:e8b8f36b4505 74 uint16_t soundbyte;
Pokitto 0:e8b8f36b4505 75
Pokitto 0:e8b8f36b4505 76
Pokitto 0:e8b8f36b4505 77 #if POK_USE_DAC > 0
Pokitto 0:e8b8f36b4505 78 #if POK_BOARDREV == 1
Pokitto 0:e8b8f36b4505 79 /** 2-layer board rev 1.3 **/
Pokitto 0:e8b8f36b4505 80 DigitalOut dac0(P1_6);
Pokitto 0:e8b8f36b4505 81 DigitalOut dac1(P1_0);
Pokitto 0:e8b8f36b4505 82 DigitalOut dac2(P1_16);
Pokitto 0:e8b8f36b4505 83 DigitalOut dac3(P0_19);
Pokitto 0:e8b8f36b4505 84 DigitalOut dac4(P0_17);
Pokitto 0:e8b8f36b4505 85 DigitalOut dac5(P1_12);
Pokitto 0:e8b8f36b4505 86 DigitalOut dac6(P1_15);
Pokitto 0:e8b8f36b4505 87 DigitalOut dac7(P1_8);
Pokitto 0:e8b8f36b4505 88 #else
Pokitto 0:e8b8f36b4505 89 /** 4-layer board rev 2.1 **/
Pokitto 0:e8b8f36b4505 90 DigitalOut dac0(P1_28);
Pokitto 0:e8b8f36b4505 91 DigitalOut dac1(P1_29);
Pokitto 0:e8b8f36b4505 92 DigitalOut dac2(P1_30);
Pokitto 0:e8b8f36b4505 93 DigitalOut dac3(P1_31);
Pokitto 0:e8b8f36b4505 94 /* has daniel made a mistake ?*/
Pokitto 0:e8b8f36b4505 95 DigitalOut dac4(P2_20);
Pokitto 0:e8b8f36b4505 96 DigitalOut dac5(P2_21);
Pokitto 0:e8b8f36b4505 97 DigitalOut dac6(P2_22);
Pokitto 0:e8b8f36b4505 98 DigitalOut dac7(P2_23);
Pokitto 0:e8b8f36b4505 99
Pokitto 0:e8b8f36b4505 100 DigitalOut amp(P1_17);
Pokitto 0:e8b8f36b4505 101
Pokitto 0:e8b8f36b4505 102 #endif // POK_BOARDREV
Pokitto 0:e8b8f36b4505 103 #endif // POK_USE_DAC
Pokitto 0:e8b8f36b4505 104
Pokitto 0:e8b8f36b4505 105 #if POK_BOARDREV == 2
Pokitto 0:e8b8f36b4505 106 //MCP4018 volpot(P0_5,P0_4);
Pokitto 0:e8b8f36b4505 107
Pokitto 0:e8b8f36b4505 108 void initHWvolumecontrol() {
Pokitto 0:e8b8f36b4505 109 HWvolume=0;
Pokitto 0:e8b8f36b4505 110 volpotError=true;
Pokitto 0:e8b8f36b4505 111 //if (volpot.put(HWvolume)) volpotError=true; //try if MCP4018 answers
Pokitto 0:e8b8f36b4505 112 setHWvolume(VOLUME_STARTUP);
Pokitto 0:e8b8f36b4505 113 }
Pokitto 0:e8b8f36b4505 114
Pokitto 0:e8b8f36b4505 115 int Pokitto::setHWvolume(uint8_t v) {
Pokitto 0:e8b8f36b4505 116 HWvolume = 0x7F&v;
Pokitto 0:e8b8f36b4505 117 //if (!volpotError) return volpot.put(HWvolume); //use normal I2C
Pokitto 0:e8b8f36b4505 118 /* fallback method for broken MCP4018 */
Pokitto 0:e8b8f36b4505 119 SoftwareI2C swvolpot(P0_4, P0_5); //swapped SDA,SCL
Pokitto 0:e8b8f36b4505 120 swvolpot.write(0x5e,HWvolume);
Pokitto 2:968589ca3484 121 return HWvolume;
Pokitto 0:e8b8f36b4505 122 }
Pokitto 0:e8b8f36b4505 123
Pokitto 0:e8b8f36b4505 124 uint8_t Pokitto::getHWvolume() {
Pokitto 0:e8b8f36b4505 125 return HWvolume;
Pokitto 0:e8b8f36b4505 126 }
Pokitto 0:e8b8f36b4505 127
Pokitto 0:e8b8f36b4505 128 void Pokitto::changeHWvolume(int8_t v) {
Pokitto 0:e8b8f36b4505 129 int temp = HWvolume + v;
Pokitto 0:e8b8f36b4505 130 if (temp < 0) temp = 0; //prevent volume "looparound" than can make a massive crack
Pokitto 0:e8b8f36b4505 131 if (temp > 127) temp = 127;
Pokitto 0:e8b8f36b4505 132 setHWvolume(temp);
Pokitto 0:e8b8f36b4505 133 }
Pokitto 0:e8b8f36b4505 134
Pokitto 0:e8b8f36b4505 135 uint8_t Pokitto::ampIsOn() {
Pokitto 0:e8b8f36b4505 136 return amp;
Pokitto 0:e8b8f36b4505 137 }
Pokitto 0:e8b8f36b4505 138
Pokitto 0:e8b8f36b4505 139 void Pokitto::ampEnable(uint8_t v) {
Pokitto 0:e8b8f36b4505 140 if (v>1) v=1; // limit against funny values
Pokitto 0:e8b8f36b4505 141 amp=v;
Pokitto 0:e8b8f36b4505 142 }
Pokitto 0:e8b8f36b4505 143 #endif // POK_BOARDREV == 2
Pokitto 0:e8b8f36b4505 144
Pokitto 0:e8b8f36b4505 145 void Pokitto::dac_write(uint8_t value) {
Pokitto 0:e8b8f36b4505 146 #if POK_USE_DAC > 0
Pokitto 0:e8b8f36b4505 147 #if POK_BOARDREV == 1 // was 1
Pokitto 0:e8b8f36b4505 148 if (value & 1) SET_DAC0 else CLR_DAC0;
Pokitto 0:e8b8f36b4505 149 value >>= 1;
Pokitto 0:e8b8f36b4505 150 if (value & 1) SET_DAC1 else CLR_DAC1;
Pokitto 0:e8b8f36b4505 151 value >>= 1;
Pokitto 0:e8b8f36b4505 152 if (value & 1) SET_DAC2 else CLR_DAC2;
Pokitto 0:e8b8f36b4505 153 value >>= 1;
Pokitto 0:e8b8f36b4505 154 if (value & 1) SET_DAC3 else CLR_DAC3;
Pokitto 0:e8b8f36b4505 155 value >>= 1;
Pokitto 0:e8b8f36b4505 156 if (value & 1) SET_DAC4 else CLR_DAC4;
Pokitto 0:e8b8f36b4505 157 value >>= 1;
Pokitto 0:e8b8f36b4505 158 if (value & 1) SET_DAC5 else CLR_DAC5;
Pokitto 0:e8b8f36b4505 159 value >>= 1;
Pokitto 0:e8b8f36b4505 160 if (value & 1) SET_DAC6 else CLR_DAC6;
Pokitto 0:e8b8f36b4505 161 value >>= 1;
Pokitto 0:e8b8f36b4505 162 if (value & 1) SET_DAC7 else CLR_DAC7;
Pokitto 0:e8b8f36b4505 163 #else
Pokitto 0:e8b8f36b4505 164 //uint32_t val;
Pokitto 0:e8b8f36b4505 165 //val = value<<28; //lower 4 bits go higher - because port mask is used, no AND is needed to clear bits
Pokitto 0:e8b8f36b4505 166 //val += value<<(15-4); //higher 4 bits go lower. No need to shift by 15 because bits are in the higher nibble
Pokitto 0:e8b8f36b4505 167 /* daniel has made a mistake with ports */
Pokitto 0:e8b8f36b4505 168 //val = ((value&0x70)<<(28-4)); //higher 4 bits go higher - because port mask is used, no AND is needed to clear bits
Pokitto 0:e8b8f36b4505 169 //val += value<<(15); //lower 4 bits go lower. No need to shift by 15 because bits are in the higher nibble
Pokitto 0:e8b8f36b4505 170 //SET_MASK_DAC;
Pokitto 0:e8b8f36b4505 171 //LPC_GPIO_PORT->MPIN[1] = val; // write bits to port
Pokitto 0:e8b8f36b4505 172 //CLR_MASK_DAC;
Pokitto 0:e8b8f36b4505 173 /* fixed here */
Pokitto 0:e8b8f36b4505 174 /*val=value;
Pokitto 0:e8b8f36b4505 175 SET_MASK_DAC_LO;
Pokitto 0:e8b8f36b4505 176 LPC_GPIO_PORT->MPIN[1] = val<<28; // write lower 4 bits to port
Pokitto 0:e8b8f36b4505 177 CLR_MASK_DAC_LO;
Pokitto 0:e8b8f36b4505 178 SET_MASK_DAC_HI;
Pokitto 0:e8b8f36b4505 179 LPC_GPIO_PORT->MPIN[2] = val<<(20-4); // write bits to port
Pokitto 0:e8b8f36b4505 180 CLR_MASK_DAC_HI; */
Pokitto 0:e8b8f36b4505 181 if (value & 1) SET_DAC0 else CLR_DAC0;
Pokitto 0:e8b8f36b4505 182 value >>= 1;
Pokitto 0:e8b8f36b4505 183 if (value & 1) SET_DAC1 else CLR_DAC1;
Pokitto 0:e8b8f36b4505 184 value >>= 1;
Pokitto 0:e8b8f36b4505 185 if (value & 1) SET_DAC2 else CLR_DAC2;
Pokitto 0:e8b8f36b4505 186 value >>= 1;
Pokitto 0:e8b8f36b4505 187 if (value & 1) SET_DAC3 else CLR_DAC3;
Pokitto 0:e8b8f36b4505 188 value >>= 1;
Pokitto 0:e8b8f36b4505 189 if (value & 1) SET_DAC4 else CLR_DAC4;
Pokitto 0:e8b8f36b4505 190 value >>= 1;
Pokitto 0:e8b8f36b4505 191 if (value & 1) SET_DAC5 else CLR_DAC5;
Pokitto 0:e8b8f36b4505 192 value >>= 1;
Pokitto 0:e8b8f36b4505 193 if (value & 1) SET_DAC6 else CLR_DAC6;
Pokitto 0:e8b8f36b4505 194 value >>= 1;
Pokitto 0:e8b8f36b4505 195 if (value & 1) SET_DAC7 else CLR_DAC7;
Pokitto 0:e8b8f36b4505 196 //CLR_MASK_DAC;
Pokitto 0:e8b8f36b4505 197 #endif // BOARDREV
Pokitto 0:e8b8f36b4505 198 #endif
Pokitto 0:e8b8f36b4505 199 }
Pokitto 0:e8b8f36b4505 200
Pokitto 0:e8b8f36b4505 201 /** SOUND INIT **/
Pokitto 0:e8b8f36b4505 202 void Pokitto::soundInit() {
Pokitto 0:e8b8f36b4505 203
Pokitto 0:e8b8f36b4505 204 pwmout_init(&audiopwm,POK_AUD_PIN);
Pokitto 0:e8b8f36b4505 205 pwmout_period_us(&audiopwm,POK_AUD_PWM_US); //was 31us
Pokitto 0:e8b8f36b4505 206 pwmout_write(&audiopwm,0.1f);
Pokitto 0:e8b8f36b4505 207
Pokitto 0:e8b8f36b4505 208 #if POK_GBSOUND > 0
Pokitto 0:e8b8f36b4505 209 /** GAMEBUINO SOUND **/
Pokitto 0:e8b8f36b4505 210 audio.attach_us(&audio_IRQ, 1000000/(POK_AUD_FREQ>>0));
Pokitto 0:e8b8f36b4505 211 #else
Pokitto 0:e8b8f36b4505 212 /** NOT GAMEBUINO SOUND **/
Pokitto 0:e8b8f36b4505 213 audio.attach_us(&pokSoundIRQ, 1000000/(POK_AUD_FREQ>>0));
Pokitto 0:e8b8f36b4505 214 #endif // POK_GAMEBUINO_SUPPORT
Pokitto 0:e8b8f36b4505 215
Pokitto 0:e8b8f36b4505 216 //emptySong();
Pokitto 0:e8b8f36b4505 217 //emptyOscillators();
Pokitto 0:e8b8f36b4505 218 //emptyBlocks();
Pokitto 0:e8b8f36b4505 219 //emptyPatches();
Pokitto 0:e8b8f36b4505 220 #ifdef TEST_SOUND
Pokitto 0:e8b8f36b4505 221 testOsc();
Pokitto 0:e8b8f36b4505 222 #endif // TEST_SOUND
Pokitto 0:e8b8f36b4505 223 #if POK_BOARDREV == 2
Pokitto 0:e8b8f36b4505 224 initHWvolumecontrol();
Pokitto 0:e8b8f36b4505 225 #endif
Pokitto 0:e8b8f36b4505 226
Pokitto 0:e8b8f36b4505 227 }
Pokitto 0:e8b8f36b4505 228
Pokitto 0:e8b8f36b4505 229
Pokitto 0:e8b8f36b4505 230 uint8_t Pokitto::streamPaused() {
Pokitto 0:e8b8f36b4505 231 return !streamon;
Pokitto 0:e8b8f36b4505 232 }
Pokitto 0:e8b8f36b4505 233
Pokitto 0:e8b8f36b4505 234 void Pokitto::pauseStream() {
Pokitto 0:e8b8f36b4505 235 streamon=0;
Pokitto 0:e8b8f36b4505 236 }
Pokitto 0:e8b8f36b4505 237
Pokitto 0:e8b8f36b4505 238 void Pokitto::playStream() {
Pokitto 0:e8b8f36b4505 239 streamon=1;
Pokitto 0:e8b8f36b4505 240 }
Pokitto 0:e8b8f36b4505 241
Pokitto 0:e8b8f36b4505 242
Pokitto 0:e8b8f36b4505 243 void pokPauseStream() {
Pokitto 0:e8b8f36b4505 244 streamon=0;
Pokitto 0:e8b8f36b4505 245 }
Pokitto 0:e8b8f36b4505 246
Pokitto 0:e8b8f36b4505 247 void pokPlayStream() {
Pokitto 0:e8b8f36b4505 248 streamon=1;
Pokitto 0:e8b8f36b4505 249 }
Pokitto 0:e8b8f36b4505 250
Pokitto 0:e8b8f36b4505 251 void pokSoundIRQ() {
Pokitto 0:e8b8f36b4505 252 uint8_t output=0;
Pokitto 0:e8b8f36b4505 253 #ifndef POK_SIM
Pokitto 2:968589ca3484 254 //pwmout_t* obj = &audiopwm;
Pokitto 0:e8b8f36b4505 255 #endif
Pokitto 0:e8b8f36b4505 256 #if POK_STREAMING_MUSIC > 0
Pokitto 0:e8b8f36b4505 257 #if POK_STREAMFREQ_HALVE
Pokitto 0:e8b8f36b4505 258 streamstep = 1-streamstep;
Pokitto 0:e8b8f36b4505 259 #else
Pokitto 0:e8b8f36b4505 260 streamstep = 1;
Pokitto 0:e8b8f36b4505 261 #endif // POK_STREAMFREQ_HALVE
Pokitto 0:e8b8f36b4505 262 streamstep &= streamon; // streamon is used to toggle SD music streaming on and off
Pokitto 0:e8b8f36b4505 263 if (streamstep) {
Pokitto 0:e8b8f36b4505 264 output = (*currentPtr++);
Pokitto 0:e8b8f36b4505 265 if(streamvol && streamon) {
Pokitto 0:e8b8f36b4505 266 output >>= 3-streamvol;
Pokitto 0:e8b8f36b4505 267 streambyte = output;
Pokitto 0:e8b8f36b4505 268 } else {
Pokitto 0:e8b8f36b4505 269 streambyte = 0; // duty cycle
Pokitto 0:e8b8f36b4505 270 output = 0;
Pokitto 0:e8b8f36b4505 271 }
Pokitto 0:e8b8f36b4505 272 if (currentPtr >= endPtr)
Pokitto 0:e8b8f36b4505 273 {
Pokitto 0:e8b8f36b4505 274 currentBuffer++;
Pokitto 0:e8b8f36b4505 275 if (currentBuffer==4) currentBuffer=0;
Pokitto 0:e8b8f36b4505 276 currentPtr = buffers[currentBuffer];
Pokitto 0:e8b8f36b4505 277 endPtr = currentPtr + BUFFER_SIZE;
Pokitto 0:e8b8f36b4505 278 }
Pokitto 0:e8b8f36b4505 279 }
Pokitto 0:e8b8f36b4505 280 #endif // POK_STREAMING_MUSIC
Pokitto 0:e8b8f36b4505 281
Pokitto 0:e8b8f36b4505 282 /** DO ADDITIONAL SOUND PROCESSING (NOT STREAM) OF SOUND HERE **/
Pokitto 0:e8b8f36b4505 283
Pokitto 0:e8b8f36b4505 284 #if POK_ENABLE_SYNTH
Pokitto 0:e8b8f36b4505 285 /** if song is being played from sd **/
Pokitto 0:e8b8f36b4505 286 if (playing) {
Pokitto 0:e8b8f36b4505 287 notetick++;
Pokitto 0:e8b8f36b4505 288 updatePlaybackSD(playerpos&7);
Pokitto 0:e8b8f36b4505 289 }
Pokitto 0:e8b8f36b4505 290 /** oscillators update **/
Pokitto 0:e8b8f36b4505 291 osc1.count += osc1.cinc + (osc1.pitchbend >> 4); // counts to 65535 and overflows to zero WAS 8 !
Pokitto 0:e8b8f36b4505 292 osc2.count += osc2.cinc + (osc2.pitchbend >> 4); // counts to 65535 and overflows to zero
Pokitto 0:e8b8f36b4505 293 osc3.count += osc3.cinc + (osc3.pitchbend >> 4); // counts to 65535 and overflows to zero
Pokitto 0:e8b8f36b4505 294 Marr[tick](); // call mixing function
Pokitto 0:e8b8f36b4505 295 --tick;
Pokitto 0:e8b8f36b4505 296
Pokitto 0:e8b8f36b4505 297 /** mixing oscillator output **/
Pokitto 0:e8b8f36b4505 298
Pokitto 0:e8b8f36b4505 299 uint16_t op = (uint16_t) ((osc1.output)*(osc1.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 0:e8b8f36b4505 300 op += (uint16_t) ((osc2.output)*(osc2.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 0:e8b8f36b4505 301 op += (uint16_t) ((osc3.output)*(osc3.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 0:e8b8f36b4505 302 output = (uint8_t) op;
Pokitto 0:e8b8f36b4505 303
Pokitto 0:e8b8f36b4505 304 #endif // POK_ENABLE_SYNTH
Pokitto 0:e8b8f36b4505 305
Pokitto 0:e8b8f36b4505 306 #ifndef POK_SIM
Pokitto 0:e8b8f36b4505 307 /** HARDWARE **/
Pokitto 0:e8b8f36b4505 308 #if POK_ENABLE_SOUND > 0
Pokitto 0:e8b8f36b4505 309 #if POK_STREAMING_MUSIC > 0
Pokitto 0:e8b8f36b4505 310 /** sound is enabled, streaming is enabled */
Pokitto 0:e8b8f36b4505 311 #if POK_STREAM_TO_DAC > 0
Pokitto 0:e8b8f36b4505 312 /** stream goes to DAC */
Pokitto 0:e8b8f36b4505 313 #if POK_USE_DAC > 0
Pokitto 0:e8b8f36b4505 314 if (streamstep) dac_write((uint8_t)streambyte); // duty cycle
Pokitto 0:e8b8f36b4505 315 #endif // POK_USE_DAC
Pokitto 0:e8b8f36b4505 316 #else
Pokitto 0:e8b8f36b4505 317 /** stream goes to PWM */
Pokitto 0:e8b8f36b4505 318 if (streamstep) {
Pokitto 0:e8b8f36b4505 319 pwmout_write(&audiopwm,(float)streambyte/(float)255);
Pokitto 0:e8b8f36b4505 320 //uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*streambyte)>>8); //cut out float
Pokitto 0:e8b8f36b4505 321 //obj->pwm->MATCHREL1 = t_on;
Pokitto 0:e8b8f36b4505 322 //dac_write((uint8_t)streambyte); // duty cycle
Pokitto 0:e8b8f36b4505 323 }
Pokitto 0:e8b8f36b4505 324 #endif // POK_STREAM_TO_DAC
Pokitto 0:e8b8f36b4505 325 #endif // POK_STREAMING_MUSIC
Pokitto 0:e8b8f36b4505 326 #if POK_STREAM_TO_DAC > 0
Pokitto 0:e8b8f36b4505 327 /** synth goes to PWM */
Pokitto 0:e8b8f36b4505 328 pwmout_write(&audiopwm,(float)output/(float)255);
Pokitto 0:e8b8f36b4505 329 //uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float
Pokitto 0:e8b8f36b4505 330 //obj->pwm->MATCHREL1 = t_on;
Pokitto 0:e8b8f36b4505 331 #else
Pokitto 0:e8b8f36b4505 332 dac_write((uint8_t)output);
Pokitto 0:e8b8f36b4505 333 #endif // decide where synth is output
Pokitto 0:e8b8f36b4505 334 soundbyte = (output+streambyte)>>1;
Pokitto 0:e8b8f36b4505 335 soundbuf[soundbufindex++]=soundbyte;
Pokitto 0:e8b8f36b4505 336 #endif //POK_ENABLE_SOUND
Pokitto 0:e8b8f36b4505 337 #endif // HARDWARE
Pokitto 0:e8b8f36b4505 338 }
Pokitto 0:e8b8f36b4505 339
Pokitto 0:e8b8f36b4505 340
Pokitto 0:e8b8f36b4505 341 void Pokitto::updateSDAudioStream() {
Pokitto 0:e8b8f36b4505 342 if (streamPaused()) return;
Pokitto 0:e8b8f36b4505 343
Pokitto 0:e8b8f36b4505 344 #if POK_STREAMING_MUSIC > 0
Pokitto 0:e8b8f36b4505 345 if (oldBuffer != currentBuffer) {
Pokitto 0:e8b8f36b4505 346 if (currentBuffer==0) fileReadBytes(&buffers[3][0],BUFFER_SIZE);
Pokitto 0:e8b8f36b4505 347 else if (currentBuffer==1) fileReadBytes(&buffers[0][0],BUFFER_SIZE);
Pokitto 0:e8b8f36b4505 348 else if (currentBuffer==2) fileReadBytes(&buffers[1][0],BUFFER_SIZE);
Pokitto 0:e8b8f36b4505 349 else fileReadBytes(&buffers[2][0],BUFFER_SIZE);
Pokitto 0:e8b8f36b4505 350 oldBuffer = currentBuffer;
Pokitto 0:e8b8f36b4505 351 streamcounter += BUFFER_SIZE;
Pokitto 0:e8b8f36b4505 352 } else return;
Pokitto 0:e8b8f36b4505 353
Pokitto 0:e8b8f36b4505 354 #ifndef POK_SIM
Pokitto 0:e8b8f36b4505 355 if ( streamcounter > fs.fsize - (BUFFER_SIZE*6)) {
Pokitto 0:e8b8f36b4505 356 #else
Pokitto 0:e8b8f36b4505 357 if ( streamcounter > getFileLength() - (BUFFER_SIZE*6)) {
Pokitto 0:e8b8f36b4505 358 #endif
Pokitto 0:e8b8f36b4505 359 streamcounter=0;
Pokitto 0:e8b8f36b4505 360 #if POK_STREAM_LOOP
Pokitto 0:e8b8f36b4505 361 fileRewind();
Pokitto 0:e8b8f36b4505 362 #else
Pokitto 0:e8b8f36b4505 363 pokPauseStream();
Pokitto 0:e8b8f36b4505 364 #endif
Pokitto 0:e8b8f36b4505 365 }
Pokitto 0:e8b8f36b4505 366 #endif
Pokitto 0:e8b8f36b4505 367 }
Pokitto 0:e8b8f36b4505 368
Pokitto 0:e8b8f36b4505 369
Pokitto 0:e8b8f36b4505 370 #endif // POK_ENABLE_SOUND
Pokitto 0:e8b8f36b4505 371
Pokitto 0:e8b8f36b4505 372