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:
7:72f87b7c7400
direct lcd stuff used by sensitive

Who changed what in which revision?

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