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

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
52:c04087025cab
added fix for directrectangle()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 52:c04087025cab 1 /**************************************************************************/
Pokitto 52:c04087025cab 2 /*!
Pokitto 52:c04087025cab 3 @file HWSound.cpp
Pokitto 52:c04087025cab 4 @author Jonne Valola
Pokitto 52:c04087025cab 5
Pokitto 52:c04087025cab 6 @section LICENSE
Pokitto 52:c04087025cab 7
Pokitto 52:c04087025cab 8 Software License Agreement (BSD License)
Pokitto 52:c04087025cab 9
Pokitto 52:c04087025cab 10 Copyright (c) 2016, Jonne Valola
Pokitto 52:c04087025cab 11 All rights reserved.
Pokitto 52:c04087025cab 12
Pokitto 52:c04087025cab 13 Redistribution and use in source and binary forms, with or without
Pokitto 52:c04087025cab 14 modification, are permitted provided that the following conditions are met:
Pokitto 52:c04087025cab 15 1. Redistributions of source code must retain the above copyright
Pokitto 52:c04087025cab 16 notice, this list of conditions and the following disclaimer.
Pokitto 52:c04087025cab 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 52:c04087025cab 18 notice, this list of conditions and the following disclaimer in the
Pokitto 52:c04087025cab 19 documentation and/or other materials provided with the distribution.
Pokitto 52:c04087025cab 20 3. Neither the name of the copyright holders nor the
Pokitto 52:c04087025cab 21 names of its contributors may be used to endorse or promote products
Pokitto 52:c04087025cab 22 derived from this software without specific prior written permission.
Pokitto 52:c04087025cab 23
Pokitto 52:c04087025cab 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 52:c04087025cab 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 52:c04087025cab 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 52:c04087025cab 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 52:c04087025cab 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 52:c04087025cab 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 52:c04087025cab 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 52:c04087025cab 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 52:c04087025cab 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 52:c04087025cab 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 52:c04087025cab 34 */
Pokitto 52:c04087025cab 35 /**************************************************************************/
Pokitto 52:c04087025cab 36
Pokitto 52:c04087025cab 37 #include "mbed.h"
Pokitto 52:c04087025cab 38 #include "HWSound.h"
Pokitto 52:c04087025cab 39 //#include "MCP4018.h"
Pokitto 52:c04087025cab 40 #include "SoftwareI2C.h"
Pokitto 52:c04087025cab 41 #include "Pokitto_settings.h"
Pokitto 52:c04087025cab 42 #include "PokittoDisk.h"
Pokitto 52:c04087025cab 43 #include "PokittoGlobs.h"
Pokitto 52:c04087025cab 44 #include "Synth.h"
Pokitto 52:c04087025cab 45 #include "timer_11u6x.h"
Pokitto 52:c04087025cab 46 #include "clock_11u6x.h"
Pokitto 52:c04087025cab 47 #include "HWLCD.h"
Pokitto 52:c04087025cab 48 //#include "beat_11025.h"
Pokitto 52:c04087025cab 49
Pokitto 52:c04087025cab 50
Pokitto 52:c04087025cab 51 Pokitto::Sound __shw;
Pokitto 52:c04087025cab 52
Pokitto 52:c04087025cab 53 #ifdef XPERIMENTAL
Pokitto 52:c04087025cab 54 DigitalOut e4(EXT4);
Pokitto 52:c04087025cab 55 e4=0;
Pokitto 52:c04087025cab 56 #endif
Pokitto 52:c04087025cab 57
Pokitto 52:c04087025cab 58 using namespace Pokitto;
Pokitto 52:c04087025cab 59
Pokitto 52:c04087025cab 60
Pokitto 52:c04087025cab 61
Pokitto 52:c04087025cab 62 #ifndef POK_SIM
Pokitto 52:c04087025cab 63 #if POK_ENABLE_SOUND
Pokitto 52:c04087025cab 64 pwmout_t* obj = &audiopwm;
Pokitto 52:c04087025cab 65 #endif
Pokitto 52:c04087025cab 66 #endif
Pokitto 52:c04087025cab 67
Pokitto 52:c04087025cab 68 /** Sound Variables **/
Pokitto 52:c04087025cab 69 #if (POK_STREAMING_MUSIC > 0)
Pokitto 52:c04087025cab 70 unsigned char buffers[4][BUFFER_SIZE];
Pokitto 52:c04087025cab 71 volatile int currentBuffer = 0, oldBuffer = 0;
Pokitto 52:c04087025cab 72 volatile int bufindex = 0, vol=1;
Pokitto 52:c04087025cab 73 volatile unsigned char * currentPtr;
Pokitto 52:c04087025cab 74 volatile unsigned char * endPtr;
Pokitto 52:c04087025cab 75 int8_t streamvol=3;
Pokitto 52:c04087025cab 76 uint32_t streamcounter=0;
Pokitto 52:c04087025cab 77 uint8_t streamstep=0;
Pokitto 52:c04087025cab 78 #endif
Pokitto 52:c04087025cab 79
Pokitto 52:c04087025cab 80 #if POK_ENABLE_SOUND > 0
Pokitto 52:c04087025cab 81 pwmout_t Pokitto::audiopwm; // this way (instead of PwmOut class) pwm doesn't start screaming until it is initialized !
Pokitto 52:c04087025cab 82 //Ticker Pokitto::audio;
Pokitto 52:c04087025cab 83
Pokitto 52:c04087025cab 84 using namespace Pokitto;
Pokitto 52:c04087025cab 85
Pokitto 52:c04087025cab 86
Pokitto 52:c04087025cab 87 /** stream output and status */
Pokitto 52:c04087025cab 88 uint8_t Pokitto::streambyte,Pokitto::streamon;
Pokitto 52:c04087025cab 89
Pokitto 52:c04087025cab 90 uint8_t soundbuf[SBUFSIZE];
Pokitto 52:c04087025cab 91 uint8_t Pokitto::HWvolume=0;
Pokitto 52:c04087025cab 92 uint16_t soundbufindex;
Pokitto 52:c04087025cab 93 uint8_t* soundbufptr;
Pokitto 52:c04087025cab 94
Pokitto 52:c04087025cab 95 bool volpotError=false; //test for broken MCP4018
Pokitto 52:c04087025cab 96
Pokitto 52:c04087025cab 97 uint16_t soundbyte;
Pokitto 52:c04087025cab 98
Pokitto 52:c04087025cab 99
Pokitto 52:c04087025cab 100 #if POK_USE_DAC > 0
Pokitto 52:c04087025cab 101 #if POK_BOARDREV == 1
Pokitto 52:c04087025cab 102 /** 2-layer board rev 1.3 **/
Pokitto 52:c04087025cab 103 DigitalOut dac0(P1_6);
Pokitto 52:c04087025cab 104 DigitalOut dac1(P1_0);
Pokitto 52:c04087025cab 105 DigitalOut dac2(P1_16);
Pokitto 52:c04087025cab 106 DigitalOut dac3(P0_19);
Pokitto 52:c04087025cab 107 DigitalOut dac4(P0_17);
Pokitto 52:c04087025cab 108 DigitalOut dac5(P1_12);
Pokitto 52:c04087025cab 109 DigitalOut dac6(P1_15);
Pokitto 52:c04087025cab 110 DigitalOut dac7(P1_8);
Pokitto 52:c04087025cab 111 #else
Pokitto 52:c04087025cab 112 /** 4-layer board rev 2.1 **/
Pokitto 52:c04087025cab 113 DigitalOut dac0(P1_28);
Pokitto 52:c04087025cab 114 DigitalOut dac1(P1_29);
Pokitto 52:c04087025cab 115 DigitalOut dac2(P1_30);
Pokitto 52:c04087025cab 116 DigitalOut dac3(P1_31);
Pokitto 52:c04087025cab 117 /* has daniel made a mistake ?*/
Pokitto 52:c04087025cab 118 DigitalOut dac4(P2_20);
Pokitto 52:c04087025cab 119 DigitalOut dac5(P2_21);
Pokitto 52:c04087025cab 120 DigitalOut dac6(P2_22);
Pokitto 52:c04087025cab 121 DigitalOut dac7(P2_23);
Pokitto 52:c04087025cab 122
Pokitto 52:c04087025cab 123 DigitalOut amp(P1_17);
Pokitto 52:c04087025cab 124
Pokitto 52:c04087025cab 125 #endif // POK_BOARDREV
Pokitto 52:c04087025cab 126 #endif // POK_USE_DAC
Pokitto 52:c04087025cab 127
Pokitto 52:c04087025cab 128 #if POK_BOARDREV == 2
Pokitto 52:c04087025cab 129 //MCP4018 volpot(P0_5,P0_4);
Pokitto 52:c04087025cab 130
Pokitto 52:c04087025cab 131 /**
Pokitto 52:c04087025cab 132 * @brief Handle interrupt from 32-bit timer 0
Pokitto 52:c04087025cab 133 * @return Nothing
Pokitto 52:c04087025cab 134 */
Pokitto 52:c04087025cab 135
Pokitto 52:c04087025cab 136 uint32_t p=0;
Pokitto 52:c04087025cab 137 uint8_t pixx=0, pixy=0;
Pokitto 52:c04087025cab 138
Pokitto 52:c04087025cab 139 extern "C" void TIMER32_0_IRQHandler(void)
Pokitto 52:c04087025cab 140 {
Pokitto 52:c04087025cab 141 if (Chip_TIMER_MatchPending(LPC_TIMER32_0, 1)) {
Pokitto 52:c04087025cab 142 Chip_TIMER_ClearMatch(LPC_TIMER32_0, 1);
Pokitto 52:c04087025cab 143 //pokSoundBufferedIRQ();
Pokitto 52:c04087025cab 144 #if POK_GBSOUND > 0
Pokitto 52:c04087025cab 145 /** GAMEBUINO SOUND **/
Pokitto 52:c04087025cab 146 Pokitto::audio_IRQ();
Pokitto 52:c04087025cab 147 #else
Pokitto 52:c04087025cab 148 /** NOT GAMEBUINO SOUND **/
Pokitto 52:c04087025cab 149 #if POK_SOUND_BUFFERED
Pokitto 52:c04087025cab 150 pokSoundBufferedIRQ();
Pokitto 52:c04087025cab 151 #else
Pokitto 52:c04087025cab 152 pokSoundIRQ();
Pokitto 52:c04087025cab 153 #endif
Pokitto 52:c04087025cab 154 #endif
Pokitto 52:c04087025cab 155 }
Pokitto 52:c04087025cab 156 }
Pokitto 52:c04087025cab 157
Pokitto 52:c04087025cab 158
Pokitto 52:c04087025cab 159 void initHWvolumecontrol() {
Pokitto 52:c04087025cab 160 HWvolume=0;
Pokitto 52:c04087025cab 161 volpotError=true;
Pokitto 52:c04087025cab 162 //if (volpot.put(HWvolume)) volpotError=true; //try if MCP4018 answers
Pokitto 52:c04087025cab 163 setHWvolume(discrete_vol_hw_levels[VOLUME_STARTUP>>5]);
Pokitto 52:c04087025cab 164 }
Pokitto 52:c04087025cab 165
Pokitto 52:c04087025cab 166 int Pokitto::setHWvolume(uint8_t v) {
Pokitto 52:c04087025cab 167 HWvolume=v;
Pokitto 52:c04087025cab 168 if (HWvolume>127) HWvolume=127;
Pokitto 52:c04087025cab 169 //HWvolume = 0x7F&v;
Pokitto 52:c04087025cab 170 //if (!volpotError) return volpot.put(HWvolume); //use normal I2C
Pokitto 52:c04087025cab 171 /* fallback method for broken MCP4018 */
Pokitto 52:c04087025cab 172 SoftwareI2C swvolpot(P0_4, P0_5); //swapped SDA,SCL
Pokitto 52:c04087025cab 173 swvolpot.write(0x5e,HWvolume);
Pokitto 52:c04087025cab 174 return HWvolume;
Pokitto 52:c04087025cab 175 }
Pokitto 52:c04087025cab 176
Pokitto 52:c04087025cab 177 uint8_t Pokitto::getHWvolume() {
Pokitto 52:c04087025cab 178 return HWvolume;
Pokitto 52:c04087025cab 179 }
Pokitto 52:c04087025cab 180
Pokitto 52:c04087025cab 181 void Pokitto::changeHWvolume(int8_t v) {
Pokitto 52:c04087025cab 182 int temp = HWvolume + v;
Pokitto 52:c04087025cab 183 if (temp < 0) temp = 0; //prevent volume "looparound" than can make a massive crack
Pokitto 52:c04087025cab 184 if (temp > 127) temp = 127;
Pokitto 52:c04087025cab 185 setHWvolume(temp);
Pokitto 52:c04087025cab 186 }
Pokitto 52:c04087025cab 187
Pokitto 52:c04087025cab 188 uint8_t Pokitto::ampIsOn() {
Pokitto 52:c04087025cab 189 return amp;
Pokitto 52:c04087025cab 190 }
Pokitto 52:c04087025cab 191
Pokitto 52:c04087025cab 192 void Pokitto::ampEnable(uint8_t v) {
Pokitto 52:c04087025cab 193 if (v>1) v=1; // limit against funny values
Pokitto 52:c04087025cab 194 amp=v;
Pokitto 52:c04087025cab 195 }
Pokitto 52:c04087025cab 196 #endif // POK_BOARDREV == 2
Pokitto 52:c04087025cab 197
Pokitto 52:c04087025cab 198 void Pokitto::dac_write(uint8_t value) {
Pokitto 52:c04087025cab 199 #if POK_USE_DAC > 0
Pokitto 52:c04087025cab 200 #if POK_BOARDREV == 1 // was 1
Pokitto 52:c04087025cab 201 if (value & 1) SET_DAC0 else CLR_DAC0;
Pokitto 52:c04087025cab 202 value >>= 1;
Pokitto 52:c04087025cab 203 if (value & 1) SET_DAC1 else CLR_DAC1;
Pokitto 52:c04087025cab 204 value >>= 1;
Pokitto 52:c04087025cab 205 if (value & 1) SET_DAC2 else CLR_DAC2;
Pokitto 52:c04087025cab 206 value >>= 1;
Pokitto 52:c04087025cab 207 if (value & 1) SET_DAC3 else CLR_DAC3;
Pokitto 52:c04087025cab 208 value >>= 1;
Pokitto 52:c04087025cab 209 if (value & 1) SET_DAC4 else CLR_DAC4;
Pokitto 52:c04087025cab 210 value >>= 1;
Pokitto 52:c04087025cab 211 if (value & 1) SET_DAC5 else CLR_DAC5;
Pokitto 52:c04087025cab 212 value >>= 1;
Pokitto 52:c04087025cab 213 if (value & 1) SET_DAC6 else CLR_DAC6;
Pokitto 52:c04087025cab 214 value >>= 1;
Pokitto 52:c04087025cab 215 if (value & 1) SET_DAC7 else CLR_DAC7;
Pokitto 52:c04087025cab 216 #else
Pokitto 52:c04087025cab 217
Pokitto 52:c04087025cab 218 //val = value<<28; //lower 4 bits go higher - because port mask is used, no AND is needed to clear bits
Pokitto 52:c04087025cab 219 //val += value<<(15-4); //higher 4 bits go lower. No need to shift by 15 because bits are in the higher nibble
Pokitto 52:c04087025cab 220 /* daniel has made a mistake with ports */
Pokitto 52:c04087025cab 221 //val = ((value&0x70)<<(28-4)); //higher 4 bits go higher - because port mask is used, no AND is needed to clear bits
Pokitto 52:c04087025cab 222 //val += value<<(15); //lower 4 bits go lower. No need to shift by 15 because bits are in the higher nibble
Pokitto 52:c04087025cab 223 //SET_MASK_DAC;
Pokitto 52:c04087025cab 224 //LPC_GPIO_PORT->MPIN[1] = val; // write bits to port
Pokitto 52:c04087025cab 225 //CLR_MASK_DAC;
Pokitto 52:c04087025cab 226 /* fixed here */
Pokitto 52:c04087025cab 227 #define MASKED_DAC 0
Pokitto 52:c04087025cab 228 #if MASKED_DAC
Pokitto 52:c04087025cab 229 uint32_t val;
Pokitto 52:c04087025cab 230 val=value;
Pokitto 52:c04087025cab 231 SET_MASK_DAC_LO;
Pokitto 52:c04087025cab 232 LPC_GPIO_PORT->MPIN[1] = val<<28; // write lower 4 bits to port
Pokitto 52:c04087025cab 233 CLR_MASK_DAC_LO;
Pokitto 52:c04087025cab 234 SET_MASK_DAC_HI;
Pokitto 52:c04087025cab 235 LPC_GPIO_PORT->MPIN[2] = val<<(20-4); // write bits to port
Pokitto 52:c04087025cab 236 CLR_MASK_DAC_HI;
Pokitto 52:c04087025cab 237 #else
Pokitto 52:c04087025cab 238 if (value & 1) SET_DAC0 else CLR_DAC0;
Pokitto 52:c04087025cab 239 value >>= 1;
Pokitto 52:c04087025cab 240 if (value & 1) SET_DAC1 else CLR_DAC1;
Pokitto 52:c04087025cab 241 value >>= 1;
Pokitto 52:c04087025cab 242 if (value & 1) SET_DAC2 else CLR_DAC2;
Pokitto 52:c04087025cab 243 value >>= 1;
Pokitto 52:c04087025cab 244 if (value & 1) SET_DAC3 else CLR_DAC3;
Pokitto 52:c04087025cab 245 value >>= 1;
Pokitto 52:c04087025cab 246 if (value & 1) SET_DAC4 else CLR_DAC4;
Pokitto 52:c04087025cab 247 value >>= 1;
Pokitto 52:c04087025cab 248 if (value & 1) SET_DAC5 else CLR_DAC5;
Pokitto 52:c04087025cab 249 value >>= 1;
Pokitto 52:c04087025cab 250 if (value & 1) SET_DAC6 else CLR_DAC6;
Pokitto 52:c04087025cab 251 value >>= 1;
Pokitto 52:c04087025cab 252 if (value & 1) SET_DAC7 else CLR_DAC7;
Pokitto 52:c04087025cab 253 #endif //MASKED_DAC
Pokitto 52:c04087025cab 254 //CLR_MASK_DAC;
Pokitto 52:c04087025cab 255 #endif // BOARDREV
Pokitto 52:c04087025cab 256 #endif
Pokitto 52:c04087025cab 257 }
Pokitto 52:c04087025cab 258
Pokitto 52:c04087025cab 259 /** SOUND INIT **/
Pokitto 52:c04087025cab 260 void Pokitto::soundInit() {
Pokitto 52:c04087025cab 261 #if POK_ENABLE_SOUND > 0
Pokitto 52:c04087025cab 262 uint32_t timerFreq;
Pokitto 52:c04087025cab 263 #if POK_USE_PWM
Pokitto 52:c04087025cab 264
Pokitto 52:c04087025cab 265 pwmout_init(&audiopwm,POK_AUD_PIN);
Pokitto 52:c04087025cab 266 pwmout_period_us(&audiopwm,POK_AUD_PWM_US); //was 31us
Pokitto 52:c04087025cab 267 pwmout_write(&audiopwm,0.1f);
Pokitto 52:c04087025cab 268 #endif
Pokitto 52:c04087025cab 269
Pokitto 52:c04087025cab 270 //#if POK_GBSOUND > 0
Pokitto 52:c04087025cab 271 /** GAMEBUINO SOUND **/
Pokitto 52:c04087025cab 272 //audio.attach_us(&audio_IRQ, 1000000/(POK_AUD_FREQ>>0));
Pokitto 52:c04087025cab 273 //#else
Pokitto 52:c04087025cab 274 /** NOT GAMEBUINO SOUND **/
Pokitto 52:c04087025cab 275 //audio.attach_us(&pokSoundBufferedIRQ, 1000000/(POK_AUD_FREQ>>0));
Pokitto 52:c04087025cab 276 /* Initialize 32-bit timer 0 clock */
Pokitto 52:c04087025cab 277 Chip_TIMER_Init(LPC_TIMER32_0);
Pokitto 52:c04087025cab 278
Pokitto 52:c04087025cab 279 /* Timer rate is system clock rate */
Pokitto 52:c04087025cab 280 timerFreq = Chip_Clock_GetSystemClockRate();
Pokitto 52:c04087025cab 281
Pokitto 52:c04087025cab 282 /* Timer setup for match and interrupt at TICKRATE_HZ */
Pokitto 52:c04087025cab 283 Chip_TIMER_Reset(LPC_TIMER32_0);
Pokitto 52:c04087025cab 284
Pokitto 52:c04087025cab 285 /* Enable both timers to generate interrupts when time matches */
Pokitto 52:c04087025cab 286 Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 1);
Pokitto 52:c04087025cab 287
Pokitto 52:c04087025cab 288 /* Setup 32-bit timer's duration (32-bit match time) */
Pokitto 52:c04087025cab 289 Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, (timerFreq / POK_AUD_FREQ));
Pokitto 52:c04087025cab 290
Pokitto 52:c04087025cab 291 /* Setup both timers to restart when match occurs */
Pokitto 52:c04087025cab 292 Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 1);
Pokitto 52:c04087025cab 293
Pokitto 52:c04087025cab 294 /* Start both timers */
Pokitto 52:c04087025cab 295 Chip_TIMER_Enable(LPC_TIMER32_0);
Pokitto 52:c04087025cab 296
Pokitto 52:c04087025cab 297 /* Clear both timers of any pending interrupts */
Pokitto 52:c04087025cab 298 #define TIMER_32_0_IRQn 18
Pokitto 52:c04087025cab 299 NVIC_ClearPendingIRQ((IRQn_Type)TIMER_32_0_IRQn);
Pokitto 52:c04087025cab 300
Pokitto 52:c04087025cab 301 /* Redirect IRQ vector - Jonne*/
Pokitto 52:c04087025cab 302 NVIC_SetVector((IRQn_Type)TIMER_32_0_IRQn, (uint32_t)&TIMER32_0_IRQHandler);
Pokitto 52:c04087025cab 303
Pokitto 52:c04087025cab 304 /* Enable both timer interrupts */
Pokitto 52:c04087025cab 305 NVIC_EnableIRQ((IRQn_Type)TIMER_32_0_IRQn);
Pokitto 52:c04087025cab 306 //#endif // POK_GAMEBUINO_SUPPORT
Pokitto 52:c04087025cab 307
Pokitto 52:c04087025cab 308 //emptySong();
Pokitto 52:c04087025cab 309 //emptyOscillators();
Pokitto 52:c04087025cab 310 //emptyBlocks();
Pokitto 52:c04087025cab 311 //emptyPatches();
Pokitto 52:c04087025cab 312 #ifdef TEST_SOUND
Pokitto 52:c04087025cab 313 testOsc();
Pokitto 52:c04087025cab 314 #endif // TEST_SOUND
Pokitto 52:c04087025cab 315 #if POK_BOARDREV == 2
Pokitto 52:c04087025cab 316 //initHWvolumecontrol();
Pokitto 52:c04087025cab 317 #endif
Pokitto 52:c04087025cab 318
Pokitto 52:c04087025cab 319 #if POK_ENABLE_SYNTH
Pokitto 52:c04087025cab 320 emptyOscillators();
Pokitto 52:c04087025cab 321 #endif
Pokitto 52:c04087025cab 322
Pokitto 52:c04087025cab 323
Pokitto 52:c04087025cab 324 #endif // POK_ENABLE_SOUND
Pokitto 52:c04087025cab 325
Pokitto 52:c04087025cab 326 }
Pokitto 52:c04087025cab 327
Pokitto 52:c04087025cab 328
Pokitto 52:c04087025cab 329 uint8_t Pokitto::streamPaused() {
Pokitto 52:c04087025cab 330 return !streamon;
Pokitto 52:c04087025cab 331 }
Pokitto 52:c04087025cab 332
Pokitto 52:c04087025cab 333 void Pokitto::pauseStream() {
Pokitto 52:c04087025cab 334 streamon=0;
Pokitto 52:c04087025cab 335 }
Pokitto 52:c04087025cab 336
Pokitto 52:c04087025cab 337 void Pokitto::playStream() {
Pokitto 52:c04087025cab 338 streamon=1;
Pokitto 52:c04087025cab 339 }
Pokitto 52:c04087025cab 340
Pokitto 52:c04087025cab 341
Pokitto 52:c04087025cab 342 void pokPauseStream() {
Pokitto 52:c04087025cab 343 streamon=0;
Pokitto 52:c04087025cab 344 }
Pokitto 52:c04087025cab 345
Pokitto 52:c04087025cab 346 void pokPlayStream() {
Pokitto 52:c04087025cab 347 streamon=1;
Pokitto 52:c04087025cab 348 }
Pokitto 52:c04087025cab 349
Pokitto 52:c04087025cab 350
Pokitto 52:c04087025cab 351
Pokitto 52:c04087025cab 352 inline void pokSoundBufferedIRQ() {
Pokitto 52:c04087025cab 353 uint32_t output = soundbuf[soundbufindex+=Pokitto::streamon];
Pokitto 52:c04087025cab 354 if (soundbufindex==SBUFSIZE) soundbufindex=0;
Pokitto 52:c04087025cab 355 //if (p==sizeof(beat_11025_raw)) p=0;
Pokitto 52:c04087025cab 356 //soundbuf[soundbufindex++] = output;
Pokitto 52:c04087025cab 357 //uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float
Pokitto 52:c04087025cab 358 //obj->pwm->MATCHREL1 = t_on;
Pokitto 52:c04087025cab 359 output *= discrete_vol_multipliers[discrete_vol];
Pokitto 52:c04087025cab 360 output >>= 8;
Pokitto 52:c04087025cab 361 dac_write(output);
Pokitto 52:c04087025cab 362
Pokitto 52:c04087025cab 363 //setDRAMpoint(pixx, pixy);
Pokitto 52:c04087025cab 364 }
Pokitto 52:c04087025cab 365
Pokitto 52:c04087025cab 366 inline void pokSoundIRQ() {
Pokitto 52:c04087025cab 367 #if POK_ENABLE_SOUND > 0
Pokitto 52:c04087025cab 368 //#define TICKY 0xFFFF //160
Pokitto 52:c04087025cab 369 //#define INCY 409
Pokitto 52:c04087025cab 370 uint32_t output=0;uint32_t op;
Pokitto 52:c04087025cab 371 //streamon=1;
Pokitto 52:c04087025cab 372 //if (test==TICKY) test=0;
Pokitto 52:c04087025cab 373 //if (test<(TICKY/2)) { tpin=1; pwmout_write(&audiopwm,(float)0/(float)255);}//dac_write(0);}
Pokitto 52:c04087025cab 374 //else {tpin=0; pwmout_write(&audiopwm,(float)255/(float)255);}//dac_write(64);}
Pokitto 52:c04087025cab 375 //test+=INCY;
Pokitto 52:c04087025cab 376 //return;
Pokitto 52:c04087025cab 377 #ifndef POK_SIM
Pokitto 52:c04087025cab 378 #if POK_USE_PWM
Pokitto 52:c04087025cab 379 pwmout_t* obj = &audiopwm;
Pokitto 52:c04087025cab 380 #endif
Pokitto 52:c04087025cab 381 #endif
Pokitto 52:c04087025cab 382 #if POK_STREAMING_MUSIC > 0
Pokitto 52:c04087025cab 383 #if POK_STREAMFREQ_HALVE
Pokitto 52:c04087025cab 384 streamstep = 1-streamstep;
Pokitto 52:c04087025cab 385 #else
Pokitto 52:c04087025cab 386 streamstep = 1;
Pokitto 52:c04087025cab 387 #endif // POK_STREAMFREQ_HALVE
Pokitto 52:c04087025cab 388 streamstep &= streamon; // streamon is used to toggle SD music streaming on and off
Pokitto 52:c04087025cab 389 if (streamstep) {
Pokitto 52:c04087025cab 390 output = (*currentPtr++);
Pokitto 52:c04087025cab 391 if(streamvol && streamon) {
Pokitto 52:c04087025cab 392 output >>= 3-streamvol;
Pokitto 52:c04087025cab 393 streambyte = output;
Pokitto 52:c04087025cab 394 } else {
Pokitto 52:c04087025cab 395 streambyte = 0; // duty cycle
Pokitto 52:c04087025cab 396 output = 0;
Pokitto 52:c04087025cab 397 }
Pokitto 52:c04087025cab 398 if (currentPtr >= endPtr)
Pokitto 52:c04087025cab 399 {
Pokitto 52:c04087025cab 400 currentBuffer++;
Pokitto 52:c04087025cab 401 if (currentBuffer==4) currentBuffer=0;
Pokitto 52:c04087025cab 402 currentPtr = buffers[currentBuffer];
Pokitto 52:c04087025cab 403 endPtr = currentPtr + BUFFER_SIZE;
Pokitto 52:c04087025cab 404 }
Pokitto 52:c04087025cab 405 }
Pokitto 52:c04087025cab 406 #endif // POK_STREAMING_MUSIC
Pokitto 52:c04087025cab 407
Pokitto 52:c04087025cab 408 /** DO ADDITIONAL SOUND PROCESSING (NOT STREAM) OF SOUND HERE **/
Pokitto 52:c04087025cab 409
Pokitto 52:c04087025cab 410 #if POK_ENABLE_SYNTH
Pokitto 52:c04087025cab 411 /** if song is being played from sd **/
Pokitto 52:c04087025cab 412 if (playing) {
Pokitto 52:c04087025cab 413 notetick++;
Pokitto 52:c04087025cab 414 updatePlayback();
Pokitto 52:c04087025cab 415 }
Pokitto 52:c04087025cab 416 /** oscillators update **/
Pokitto 52:c04087025cab 417 osc1.count += osc1.cinc + (osc1.pitchbend >> 4); // counts to 65535 and overflows to zero WAS 8 !
Pokitto 52:c04087025cab 418 osc2.count += osc2.cinc + (osc2.pitchbend >> 4); // counts to 65535 and overflows to zero
Pokitto 52:c04087025cab 419 osc3.count += osc3.cinc + (osc3.pitchbend >> 4); // counts to 65535 and overflows to zero
Pokitto 52:c04087025cab 420 Marr[tick](); // call mixing function
Pokitto 52:c04087025cab 421 --tick;
Pokitto 52:c04087025cab 422
Pokitto 52:c04087025cab 423 /** mixing oscillator output **/
Pokitto 52:c04087025cab 424
Pokitto 52:c04087025cab 425 op = (uint32_t) ((osc1.output)*(osc1.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 52:c04087025cab 426 op += (uint32_t) ((osc2.output)*(osc2.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 52:c04087025cab 427 op += (uint32_t) ((osc3.output)*(osc3.vol>>8))>>9;// >> 2 osc1.vol Marr;
Pokitto 52:c04087025cab 428 op *= discrete_vol_multipliers[discrete_vol];
Pokitto 52:c04087025cab 429 op >>= 8;
Pokitto 52:c04087025cab 430 output = op & 0xFF;
Pokitto 52:c04087025cab 431
Pokitto 52:c04087025cab 432 #endif // POK_ENABLE_SYNTH
Pokitto 52:c04087025cab 433
Pokitto 52:c04087025cab 434 #ifndef POK_SIM
Pokitto 52:c04087025cab 435 /** HARDWARE **/
Pokitto 52:c04087025cab 436 #if POK_ENABLE_SOUND > 0
Pokitto 52:c04087025cab 437 #if POK_STREAMING_MUSIC > 0
Pokitto 52:c04087025cab 438 /** sound is enabled, streaming is enabled */
Pokitto 52:c04087025cab 439 #if POK_STREAM_TO_DAC > 0
Pokitto 52:c04087025cab 440 /** stream goes to DAC */
Pokitto 52:c04087025cab 441 #if POK_USE_DAC > 0
Pokitto 52:c04087025cab 442 uint32_t sbyte = streambyte;
Pokitto 52:c04087025cab 443 sbyte *= discrete_vol_multipliers[discrete_vol];
Pokitto 52:c04087025cab 444 sbyte >>= 8;
Pokitto 52:c04087025cab 445 dac_write((uint8_t)sbyte); // duty cycle
Pokitto 52:c04087025cab 446 #endif // POK_USE_DAC
Pokitto 52:c04087025cab 447 #else
Pokitto 52:c04087025cab 448 /** stream goes to PWM */
Pokitto 52:c04087025cab 449 if (streamstep) {
Pokitto 52:c04087025cab 450 //pwmout_write(&audiopwm,(float)streambyte/(float)255);
Pokitto 52:c04087025cab 451 #if POK_USE_PWM
Pokitto 52:c04087025cab 452 uint32_t sbyte = streambyte;
Pokitto 52:c04087025cab 453 sbyte *= discrete_vol_multipliers[discrete_vol];
Pokitto 52:c04087025cab 454 sbyte >>= 8;
Pokitto 52:c04087025cab 455 uint32_t t_on = (uint32_t)((((obj->pwm->MATCHREL0)*sbyte)>>8)); //cut out float
Pokitto 52:c04087025cab 456 obj->pwm->MATCHREL1 = t_on;
Pokitto 52:c04087025cab 457 #endif
Pokitto 52:c04087025cab 458 //dac_write((uint8_t)streambyte); // duty cycle
Pokitto 52:c04087025cab 459 }
Pokitto 52:c04087025cab 460 #endif // POK_STREAM_TO_DAC
Pokitto 52:c04087025cab 461 #endif // POK_STREAMING_MUSIC
Pokitto 52:c04087025cab 462 #if POK_STREAM_TO_DAC > 0
Pokitto 52:c04087025cab 463 /** synth goes to PWM */
Pokitto 52:c04087025cab 464 //pwmout_write(&audiopwm,(float)output/(float)255);
Pokitto 52:c04087025cab 465 #if POK_USE_PWM
Pokitto 52:c04087025cab 466 op = output;
Pokitto 52:c04087025cab 467 op *= discrete_vol_multipliers[discrete_vol];
Pokitto 52:c04087025cab 468 op >>= 8;
Pokitto 52:c04087025cab 469 uint32_t t_on = (uint32_t)((((obj->pwm->MATCHREL0)*op)>>8)); //cut out float
Pokitto 52:c04087025cab 470 obj->pwm->MATCHREL1 = t_on;
Pokitto 52:c04087025cab 471 #endif
Pokitto 52:c04087025cab 472 #else // POK_STREAMING_MUSIC
Pokitto 52:c04087025cab 473 op = output;
Pokitto 52:c04087025cab 474 op *= discrete_vol_multipliers[discrete_vol];
Pokitto 52:c04087025cab 475 op >>= 8;
Pokitto 52:c04087025cab 476 dac_write((uint8_t)op); // SYNTH to DAC
Pokitto 52:c04087025cab 477 #endif
Pokitto 52:c04087025cab 478 soundbyte = (output+streambyte)>>1;
Pokitto 52:c04087025cab 479 soundbuf[soundbufindex++]=soundbyte;
Pokitto 52:c04087025cab 480 if (soundbufindex==256) soundbufindex=0;
Pokitto 52:c04087025cab 481 #endif //POK_ENABLE_SOUND
Pokitto 52:c04087025cab 482 #endif // HARDWARE
Pokitto 52:c04087025cab 483 #endif //POK_ENABLE_SOUND
Pokitto 52:c04087025cab 484 }
Pokitto 52:c04087025cab 485
Pokitto 52:c04087025cab 486
Pokitto 52:c04087025cab 487 void Pokitto::updateSDAudioStream() {
Pokitto 52:c04087025cab 488 if (streamPaused()) return;
Pokitto 52:c04087025cab 489
Pokitto 52:c04087025cab 490 #if POK_STREAMING_MUSIC > 0
Pokitto 52:c04087025cab 491 if (oldBuffer != currentBuffer) {
Pokitto 52:c04087025cab 492 if (currentBuffer==0) fileReadBytes(&buffers[3][0],BUFFER_SIZE);
Pokitto 52:c04087025cab 493 else if (currentBuffer==1) fileReadBytes(&buffers[0][0],BUFFER_SIZE);
Pokitto 52:c04087025cab 494 else if (currentBuffer==2) fileReadBytes(&buffers[1][0],BUFFER_SIZE);
Pokitto 52:c04087025cab 495 else fileReadBytes(&buffers[2][0],BUFFER_SIZE);
Pokitto 52:c04087025cab 496 oldBuffer = currentBuffer;
Pokitto 52:c04087025cab 497 streamcounter += BUFFER_SIZE;
Pokitto 52:c04087025cab 498 } else return;
Pokitto 52:c04087025cab 499
Pokitto 52:c04087025cab 500 #ifndef POK_SIM
Pokitto 52:c04087025cab 501 if ( streamcounter > fs.fsize - (BUFFER_SIZE*6)) {
Pokitto 52:c04087025cab 502 #else
Pokitto 52:c04087025cab 503 if ( streamcounter > getFileLength() - (BUFFER_SIZE*6)) {
Pokitto 52:c04087025cab 504 #endif
Pokitto 52:c04087025cab 505 streamcounter=0;
Pokitto 52:c04087025cab 506 #if POK_STREAM_LOOP
Pokitto 52:c04087025cab 507 fileRewind();
Pokitto 52:c04087025cab 508 #else
Pokitto 52:c04087025cab 509 pokPauseStream();
Pokitto 52:c04087025cab 510 #endif
Pokitto 52:c04087025cab 511 }
Pokitto 52:c04087025cab 512 #endif
Pokitto 52:c04087025cab 513 }
Pokitto 52:c04087025cab 514
Pokitto 52:c04087025cab 515
Pokitto 52:c04087025cab 516 #endif // POK_ENABLE_SOUND
Pokitto 52:c04087025cab 517
Pokitto 52:c04087025cab 518
Pokitto 52:c04087025cab 519