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:
Pokitto
Date:
Mon Sep 18 11:47:51 2017 +0000
Revision:
0:e8b8f36b4505
Child:
2:968589ca3484
Initial;

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 0:e8b8f36b4505 121 }
Pokitto 0:e8b8f36b4505 122
Pokitto 0:e8b8f36b4505 123 uint8_t Pokitto::getHWvolume() {
Pokitto 0:e8b8f36b4505 124 return HWvolume;
Pokitto 0:e8b8f36b4505 125 }
Pokitto 0:e8b8f36b4505 126
Pokitto 0:e8b8f36b4505 127 void Pokitto::changeHWvolume(int8_t v) {
Pokitto 0:e8b8f36b4505 128 int temp = HWvolume + v;
Pokitto 0:e8b8f36b4505 129 if (temp < 0) temp = 0; //prevent volume "looparound" than can make a massive crack
Pokitto 0:e8b8f36b4505 130 if (temp > 127) temp = 127;
Pokitto 0:e8b8f36b4505 131 setHWvolume(temp);
Pokitto 0:e8b8f36b4505 132 }
Pokitto 0:e8b8f36b4505 133
Pokitto 0:e8b8f36b4505 134 uint8_t Pokitto::ampIsOn() {
Pokitto 0:e8b8f36b4505 135 return amp;
Pokitto 0:e8b8f36b4505 136 }
Pokitto 0:e8b8f36b4505 137
Pokitto 0:e8b8f36b4505 138 void Pokitto::ampEnable(uint8_t v) {
Pokitto 0:e8b8f36b4505 139 if (v>1) v=1; // limit against funny values
Pokitto 0:e8b8f36b4505 140 amp=v;
Pokitto 0:e8b8f36b4505 141 }
Pokitto 0:e8b8f36b4505 142 #endif // POK_BOARDREV == 2
Pokitto 0:e8b8f36b4505 143
Pokitto 0:e8b8f36b4505 144 void Pokitto::dac_write(uint8_t value) {
Pokitto 0:e8b8f36b4505 145 #if POK_USE_DAC > 0
Pokitto 0:e8b8f36b4505 146 #if POK_BOARDREV == 1 // was 1
Pokitto 0:e8b8f36b4505 147 if (value & 1) SET_DAC0 else CLR_DAC0;
Pokitto 0:e8b8f36b4505 148 value >>= 1;
Pokitto 0:e8b8f36b4505 149 if (value & 1) SET_DAC1 else CLR_DAC1;
Pokitto 0:e8b8f36b4505 150 value >>= 1;
Pokitto 0:e8b8f36b4505 151 if (value & 1) SET_DAC2 else CLR_DAC2;
Pokitto 0:e8b8f36b4505 152 value >>= 1;
Pokitto 0:e8b8f36b4505 153 if (value & 1) SET_DAC3 else CLR_DAC3;
Pokitto 0:e8b8f36b4505 154 value >>= 1;
Pokitto 0:e8b8f36b4505 155 if (value & 1) SET_DAC4 else CLR_DAC4;
Pokitto 0:e8b8f36b4505 156 value >>= 1;
Pokitto 0:e8b8f36b4505 157 if (value & 1) SET_DAC5 else CLR_DAC5;
Pokitto 0:e8b8f36b4505 158 value >>= 1;
Pokitto 0:e8b8f36b4505 159 if (value & 1) SET_DAC6 else CLR_DAC6;
Pokitto 0:e8b8f36b4505 160 value >>= 1;
Pokitto 0:e8b8f36b4505 161 if (value & 1) SET_DAC7 else CLR_DAC7;
Pokitto 0:e8b8f36b4505 162 #else
Pokitto 0:e8b8f36b4505 163 //uint32_t val;
Pokitto 0:e8b8f36b4505 164 //val = value<<28; //lower 4 bits go higher - because port mask is used, no AND is needed to clear bits
Pokitto 0:e8b8f36b4505 165 //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 166 /* daniel has made a mistake with ports */
Pokitto 0:e8b8f36b4505 167 //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 168 //val += value<<(15); //lower 4 bits go lower. No need to shift by 15 because bits are in the higher nibble
Pokitto 0:e8b8f36b4505 169 //SET_MASK_DAC;
Pokitto 0:e8b8f36b4505 170 //LPC_GPIO_PORT->MPIN[1] = val; // write bits to port
Pokitto 0:e8b8f36b4505 171 //CLR_MASK_DAC;
Pokitto 0:e8b8f36b4505 172 /* fixed here */
Pokitto 0:e8b8f36b4505 173 /*val=value;
Pokitto 0:e8b8f36b4505 174 SET_MASK_DAC_LO;
Pokitto 0:e8b8f36b4505 175 LPC_GPIO_PORT->MPIN[1] = val<<28; // write lower 4 bits to port
Pokitto 0:e8b8f36b4505 176 CLR_MASK_DAC_LO;
Pokitto 0:e8b8f36b4505 177 SET_MASK_DAC_HI;
Pokitto 0:e8b8f36b4505 178 LPC_GPIO_PORT->MPIN[2] = val<<(20-4); // write bits to port
Pokitto 0:e8b8f36b4505 179 CLR_MASK_DAC_HI; */
Pokitto 0:e8b8f36b4505 180 if (value & 1) SET_DAC0 else CLR_DAC0;
Pokitto 0:e8b8f36b4505 181 value >>= 1;
Pokitto 0:e8b8f36b4505 182 if (value & 1) SET_DAC1 else CLR_DAC1;
Pokitto 0:e8b8f36b4505 183 value >>= 1;
Pokitto 0:e8b8f36b4505 184 if (value & 1) SET_DAC2 else CLR_DAC2;
Pokitto 0:e8b8f36b4505 185 value >>= 1;
Pokitto 0:e8b8f36b4505 186 if (value & 1) SET_DAC3 else CLR_DAC3;
Pokitto 0:e8b8f36b4505 187 value >>= 1;
Pokitto 0:e8b8f36b4505 188 if (value & 1) SET_DAC4 else CLR_DAC4;
Pokitto 0:e8b8f36b4505 189 value >>= 1;
Pokitto 0:e8b8f36b4505 190 if (value & 1) SET_DAC5 else CLR_DAC5;
Pokitto 0:e8b8f36b4505 191 value >>= 1;
Pokitto 0:e8b8f36b4505 192 if (value & 1) SET_DAC6 else CLR_DAC6;
Pokitto 0:e8b8f36b4505 193 value >>= 1;
Pokitto 0:e8b8f36b4505 194 if (value & 1) SET_DAC7 else CLR_DAC7;
Pokitto 0:e8b8f36b4505 195 //CLR_MASK_DAC;
Pokitto 0:e8b8f36b4505 196 #endif // BOARDREV
Pokitto 0:e8b8f36b4505 197 #endif
Pokitto 0:e8b8f36b4505 198 }
Pokitto 0:e8b8f36b4505 199
Pokitto 0:e8b8f36b4505 200 /** SOUND INIT **/
Pokitto 0:e8b8f36b4505 201 void Pokitto::soundInit() {
Pokitto 0:e8b8f36b4505 202
Pokitto 0:e8b8f36b4505 203 pwmout_init(&audiopwm,POK_AUD_PIN);
Pokitto 0:e8b8f36b4505 204 pwmout_period_us(&audiopwm,POK_AUD_PWM_US); //was 31us
Pokitto 0:e8b8f36b4505 205 pwmout_write(&audiopwm,0.1f);
Pokitto 0:e8b8f36b4505 206
Pokitto 0:e8b8f36b4505 207 #if POK_GBSOUND > 0
Pokitto 0:e8b8f36b4505 208 /** GAMEBUINO SOUND **/
Pokitto 0:e8b8f36b4505 209 audio.attach_us(&audio_IRQ, 1000000/(POK_AUD_FREQ>>0));
Pokitto 0:e8b8f36b4505 210 #else
Pokitto 0:e8b8f36b4505 211 /** NOT GAMEBUINO SOUND **/
Pokitto 0:e8b8f36b4505 212 audio.attach_us(&pokSoundIRQ, 1000000/(POK_AUD_FREQ>>0));
Pokitto 0:e8b8f36b4505 213 #endif // POK_GAMEBUINO_SUPPORT
Pokitto 0:e8b8f36b4505 214
Pokitto 0:e8b8f36b4505 215 //emptySong();
Pokitto 0:e8b8f36b4505 216 //emptyOscillators();
Pokitto 0:e8b8f36b4505 217 //emptyBlocks();
Pokitto 0:e8b8f36b4505 218 //emptyPatches();
Pokitto 0:e8b8f36b4505 219 #ifdef TEST_SOUND
Pokitto 0:e8b8f36b4505 220 testOsc();
Pokitto 0:e8b8f36b4505 221 #endif // TEST_SOUND
Pokitto 0:e8b8f36b4505 222 #if POK_BOARDREV == 2
Pokitto 0:e8b8f36b4505 223 initHWvolumecontrol();
Pokitto 0:e8b8f36b4505 224 #endif
Pokitto 0:e8b8f36b4505 225
Pokitto 0:e8b8f36b4505 226 }
Pokitto 0:e8b8f36b4505 227
Pokitto 0:e8b8f36b4505 228
Pokitto 0:e8b8f36b4505 229 uint8_t Pokitto::streamPaused() {
Pokitto 0:e8b8f36b4505 230 return !streamon;
Pokitto 0:e8b8f36b4505 231 }
Pokitto 0:e8b8f36b4505 232
Pokitto 0:e8b8f36b4505 233 void Pokitto::pauseStream() {
Pokitto 0:e8b8f36b4505 234 streamon=0;
Pokitto 0:e8b8f36b4505 235 }
Pokitto 0:e8b8f36b4505 236
Pokitto 0:e8b8f36b4505 237 void Pokitto::playStream() {
Pokitto 0:e8b8f36b4505 238 streamon=1;
Pokitto 0:e8b8f36b4505 239 }
Pokitto 0:e8b8f36b4505 240
Pokitto 0:e8b8f36b4505 241
Pokitto 0:e8b8f36b4505 242 void pokPauseStream() {
Pokitto 0:e8b8f36b4505 243 streamon=0;
Pokitto 0:e8b8f36b4505 244 }
Pokitto 0:e8b8f36b4505 245
Pokitto 0:e8b8f36b4505 246 void pokPlayStream() {
Pokitto 0:e8b8f36b4505 247 streamon=1;
Pokitto 0:e8b8f36b4505 248 }
Pokitto 0:e8b8f36b4505 249
Pokitto 0:e8b8f36b4505 250 void pokSoundIRQ() {
Pokitto 0:e8b8f36b4505 251 uint8_t output=0;
Pokitto 0:e8b8f36b4505 252 #ifndef POK_SIM
Pokitto 0:e8b8f36b4505 253 pwmout_t* obj = &audiopwm;
Pokitto 0:e8b8f36b4505 254 #endif
Pokitto 0:e8b8f36b4505 255 #if POK_STREAMING_MUSIC > 0
Pokitto 0:e8b8f36b4505 256 #if POK_STREAMFREQ_HALVE
Pokitto 0:e8b8f36b4505 257 streamstep = 1-streamstep;
Pokitto 0:e8b8f36b4505 258 #else
Pokitto 0:e8b8f36b4505 259 streamstep = 1;
Pokitto 0:e8b8f36b4505 260 #endif // POK_STREAMFREQ_HALVE
Pokitto 0:e8b8f36b4505 261 streamstep &= streamon; // streamon is used to toggle SD music streaming on and off
Pokitto 0:e8b8f36b4505 262 if (streamstep) {
Pokitto 0:e8b8f36b4505 263 output = (*currentPtr++);
Pokitto 0:e8b8f36b4505 264 if(streamvol && streamon) {
Pokitto 0:e8b8f36b4505 265 output >>= 3-streamvol;
Pokitto 0:e8b8f36b4505 266 streambyte = output;
Pokitto 0:e8b8f36b4505 267 } else {
Pokitto 0:e8b8f36b4505 268 streambyte = 0; // duty cycle
Pokitto 0:e8b8f36b4505 269 output = 0;
Pokitto 0:e8b8f36b4505 270 }
Pokitto 0:e8b8f36b4505 271 if (currentPtr >= endPtr)
Pokitto 0:e8b8f36b4505 272 {
Pokitto 0:e8b8f36b4505 273 currentBuffer++;
Pokitto 0:e8b8f36b4505 274 if (currentBuffer==4) currentBuffer=0;
Pokitto 0:e8b8f36b4505 275 currentPtr = buffers[currentBuffer];
Pokitto 0:e8b8f36b4505 276 endPtr = currentPtr + BUFFER_SIZE;
Pokitto 0:e8b8f36b4505 277 }
Pokitto 0:e8b8f36b4505 278 }
Pokitto 0:e8b8f36b4505 279 #endif // POK_STREAMING_MUSIC
Pokitto 0:e8b8f36b4505 280
Pokitto 0:e8b8f36b4505 281 /** DO ADDITIONAL SOUND PROCESSING (NOT STREAM) OF SOUND HERE **/
Pokitto 0:e8b8f36b4505 282
Pokitto 0:e8b8f36b4505 283 #if POK_ENABLE_SYNTH
Pokitto 0:e8b8f36b4505 284 /** if song is being played from sd **/
Pokitto 0:e8b8f36b4505 285 if (playing) {
Pokitto 0:e8b8f36b4505 286 notetick++;
Pokitto 0:e8b8f36b4505 287 updatePlaybackSD(playerpos&7);
Pokitto 0:e8b8f36b4505 288 }
Pokitto 0:e8b8f36b4505 289 /** oscillators update **/
Pokitto 0:e8b8f36b4505 290 osc1.count += osc1.cinc + (osc1.pitchbend >> 4); // counts to 65535 and overflows to zero WAS 8 !
Pokitto 0:e8b8f36b4505 291 osc2.count += osc2.cinc + (osc2.pitchbend >> 4); // counts to 65535 and overflows to zero
Pokitto 0:e8b8f36b4505 292 osc3.count += osc3.cinc + (osc3.pitchbend >> 4); // counts to 65535 and overflows to zero
Pokitto 0:e8b8f36b4505 293 Marr[tick](); // call mixing function
Pokitto 0:e8b8f36b4505 294 --tick;
Pokitto 0:e8b8f36b4505 295
Pokitto 0:e8b8f36b4505 296 /** mixing oscillator output **/
Pokitto 0:e8b8f36b4505 297
Pokitto 0:e8b8f36b4505 298 uint16_t op = (uint16_t) ((osc1.output)*(osc1.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 0:e8b8f36b4505 299 op += (uint16_t) ((osc2.output)*(osc2.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 0:e8b8f36b4505 300 op += (uint16_t) ((osc3.output)*(osc3.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 0:e8b8f36b4505 301 output = (uint8_t) op;
Pokitto 0:e8b8f36b4505 302
Pokitto 0:e8b8f36b4505 303 #endif // POK_ENABLE_SYNTH
Pokitto 0:e8b8f36b4505 304
Pokitto 0:e8b8f36b4505 305 #ifndef POK_SIM
Pokitto 0:e8b8f36b4505 306 /** HARDWARE **/
Pokitto 0:e8b8f36b4505 307 #if POK_ENABLE_SOUND > 0
Pokitto 0:e8b8f36b4505 308 #if POK_STREAMING_MUSIC > 0
Pokitto 0:e8b8f36b4505 309 /** sound is enabled, streaming is enabled */
Pokitto 0:e8b8f36b4505 310 #if POK_STREAM_TO_DAC > 0
Pokitto 0:e8b8f36b4505 311 /** stream goes to DAC */
Pokitto 0:e8b8f36b4505 312 #if POK_USE_DAC > 0
Pokitto 0:e8b8f36b4505 313 if (streamstep) dac_write((uint8_t)streambyte); // duty cycle
Pokitto 0:e8b8f36b4505 314 #endif // POK_USE_DAC
Pokitto 0:e8b8f36b4505 315 #else
Pokitto 0:e8b8f36b4505 316 /** stream goes to PWM */
Pokitto 0:e8b8f36b4505 317 if (streamstep) {
Pokitto 0:e8b8f36b4505 318 pwmout_write(&audiopwm,(float)streambyte/(float)255);
Pokitto 0:e8b8f36b4505 319 //uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*streambyte)>>8); //cut out float
Pokitto 0:e8b8f36b4505 320 //obj->pwm->MATCHREL1 = t_on;
Pokitto 0:e8b8f36b4505 321 //dac_write((uint8_t)streambyte); // duty cycle
Pokitto 0:e8b8f36b4505 322 }
Pokitto 0:e8b8f36b4505 323 #endif // POK_STREAM_TO_DAC
Pokitto 0:e8b8f36b4505 324 #endif // POK_STREAMING_MUSIC
Pokitto 0:e8b8f36b4505 325 #if POK_STREAM_TO_DAC > 0
Pokitto 0:e8b8f36b4505 326 /** synth goes to PWM */
Pokitto 0:e8b8f36b4505 327 pwmout_write(&audiopwm,(float)output/(float)255);
Pokitto 0:e8b8f36b4505 328 //uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float
Pokitto 0:e8b8f36b4505 329 //obj->pwm->MATCHREL1 = t_on;
Pokitto 0:e8b8f36b4505 330 #else
Pokitto 0:e8b8f36b4505 331 dac_write((uint8_t)output);
Pokitto 0:e8b8f36b4505 332 #endif // decide where synth is output
Pokitto 0:e8b8f36b4505 333 soundbyte = (output+streambyte)>>1;
Pokitto 0:e8b8f36b4505 334 soundbuf[soundbufindex++]=soundbyte;
Pokitto 0:e8b8f36b4505 335 #endif //POK_ENABLE_SOUND
Pokitto 0:e8b8f36b4505 336 #endif // HARDWARE
Pokitto 0:e8b8f36b4505 337 }
Pokitto 0:e8b8f36b4505 338
Pokitto 0:e8b8f36b4505 339
Pokitto 0:e8b8f36b4505 340 void Pokitto::updateSDAudioStream() {
Pokitto 0:e8b8f36b4505 341 if (streamPaused()) return;
Pokitto 0:e8b8f36b4505 342
Pokitto 0:e8b8f36b4505 343 #if POK_STREAMING_MUSIC > 0
Pokitto 0:e8b8f36b4505 344 if (oldBuffer != currentBuffer) {
Pokitto 0:e8b8f36b4505 345 if (currentBuffer==0) fileReadBytes(&buffers[3][0],BUFFER_SIZE);
Pokitto 0:e8b8f36b4505 346 else if (currentBuffer==1) fileReadBytes(&buffers[0][0],BUFFER_SIZE);
Pokitto 0:e8b8f36b4505 347 else if (currentBuffer==2) fileReadBytes(&buffers[1][0],BUFFER_SIZE);
Pokitto 0:e8b8f36b4505 348 else fileReadBytes(&buffers[2][0],BUFFER_SIZE);
Pokitto 0:e8b8f36b4505 349 oldBuffer = currentBuffer;
Pokitto 0:e8b8f36b4505 350 streamcounter += BUFFER_SIZE;
Pokitto 0:e8b8f36b4505 351 } else return;
Pokitto 0:e8b8f36b4505 352
Pokitto 0:e8b8f36b4505 353 #ifndef POK_SIM
Pokitto 0:e8b8f36b4505 354 if ( streamcounter > fs.fsize - (BUFFER_SIZE*6)) {
Pokitto 0:e8b8f36b4505 355 #else
Pokitto 0:e8b8f36b4505 356 if ( streamcounter > getFileLength() - (BUFFER_SIZE*6)) {
Pokitto 0:e8b8f36b4505 357 #endif
Pokitto 0:e8b8f36b4505 358 streamcounter=0;
Pokitto 0:e8b8f36b4505 359 #if POK_STREAM_LOOP
Pokitto 0:e8b8f36b4505 360 fileRewind();
Pokitto 0:e8b8f36b4505 361 #else
Pokitto 0:e8b8f36b4505 362 pokPauseStream();
Pokitto 0:e8b8f36b4505 363 #endif
Pokitto 0:e8b8f36b4505 364 }
Pokitto 0:e8b8f36b4505 365 #endif
Pokitto 0:e8b8f36b4505 366 }
Pokitto 0:e8b8f36b4505 367
Pokitto 0:e8b8f36b4505 368
Pokitto 0:e8b8f36b4505 369 #endif // POK_ENABLE_SOUND
Pokitto 0:e8b8f36b4505 370
Pokitto 0:e8b8f36b4505 371