PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoLib
Library for programming Pokitto hardware
How to Use
- Import this library to online compiler (see button "import" on the right hand side
- DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
- Change My_settings.h according to your project
- Start coding!
POKITTO_HW/HWSound.cpp@67:068fa6345036, 2019-04-04 (annotated)
- Committer:
- Pokitto
- Date:
- Thu Apr 04 17:07:28 2019 +0000
- Revision:
- 67:068fa6345036
- Parent:
- 52:c04087025cab
PlaySFX fixed for mbed online ide
Who changed what in which revision?
User | Revision | Line number | New 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 | using namespace Pokitto; |
Pokitto | 52:c04087025cab | 54 | |
Pokitto | 52:c04087025cab | 55 | #ifndef POK_SIM |
Pokitto | 52:c04087025cab | 56 | #if POK_ENABLE_SOUND |
Pokitto | 52:c04087025cab | 57 | pwmout_t* obj = &audiopwm; |
Pokitto | 52:c04087025cab | 58 | #endif |
Pokitto | 52:c04087025cab | 59 | #endif |
Pokitto | 52:c04087025cab | 60 | |
Pokitto | 52:c04087025cab | 61 | /** Sound Variables **/ |
Pokitto | 52:c04087025cab | 62 | #if (POK_STREAMING_MUSIC > 0) |
Pokitto | 67:068fa6345036 | 63 | |
Pokitto | 67:068fa6345036 | 64 | #if POK_HIGH_RAM == HIGH_RAM_MUSIC |
Pokitto | 67:068fa6345036 | 65 | unsigned char *buffers[4] = { |
Pokitto | 67:068fa6345036 | 66 | (unsigned char *) 0x20000000, |
Pokitto | 67:068fa6345036 | 67 | (unsigned char *) 0x20000400, |
Pokitto | 67:068fa6345036 | 68 | (unsigned char *) 0x20004000, |
Pokitto | 67:068fa6345036 | 69 | (unsigned char *) 0x20004400 |
Pokitto | 67:068fa6345036 | 70 | }; |
Pokitto | 67:068fa6345036 | 71 | #else |
Pokitto | 52:c04087025cab | 72 | unsigned char buffers[4][BUFFER_SIZE]; |
Pokitto | 67:068fa6345036 | 73 | #endif |
Pokitto | 67:068fa6345036 | 74 | |
Pokitto | 52:c04087025cab | 75 | volatile int currentBuffer = 0, oldBuffer = 0; |
Pokitto | 52:c04087025cab | 76 | volatile int bufindex = 0, vol=1; |
Pokitto | 52:c04087025cab | 77 | volatile unsigned char * currentPtr; |
Pokitto | 52:c04087025cab | 78 | volatile unsigned char * endPtr; |
Pokitto | 52:c04087025cab | 79 | int8_t streamvol=3; |
Pokitto | 52:c04087025cab | 80 | uint32_t streamcounter=0; |
Pokitto | 52:c04087025cab | 81 | uint8_t streamstep=0; |
Pokitto | 52:c04087025cab | 82 | #endif |
Pokitto | 52:c04087025cab | 83 | |
Pokitto | 52:c04087025cab | 84 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 52:c04087025cab | 85 | pwmout_t Pokitto::audiopwm; // this way (instead of PwmOut class) pwm doesn't start screaming until it is initialized ! |
Pokitto | 52:c04087025cab | 86 | //Ticker Pokitto::audio; |
Pokitto | 52:c04087025cab | 87 | |
Pokitto | 52:c04087025cab | 88 | using namespace Pokitto; |
Pokitto | 52:c04087025cab | 89 | |
Pokitto | 52:c04087025cab | 90 | |
Pokitto | 52:c04087025cab | 91 | /** stream output and status */ |
Pokitto | 52:c04087025cab | 92 | uint8_t Pokitto::streambyte,Pokitto::streamon; |
Pokitto | 52:c04087025cab | 93 | |
Pokitto | 52:c04087025cab | 94 | uint8_t soundbuf[SBUFSIZE]; |
Pokitto | 52:c04087025cab | 95 | uint8_t Pokitto::HWvolume=0; |
Pokitto | 52:c04087025cab | 96 | uint16_t soundbufindex; |
Pokitto | 52:c04087025cab | 97 | uint8_t* soundbufptr; |
Pokitto | 52:c04087025cab | 98 | |
Pokitto | 52:c04087025cab | 99 | bool volpotError=false; //test for broken MCP4018 |
Pokitto | 52:c04087025cab | 100 | |
Pokitto | 52:c04087025cab | 101 | uint16_t soundbyte; |
Pokitto | 52:c04087025cab | 102 | |
Pokitto | 52:c04087025cab | 103 | |
Pokitto | 52:c04087025cab | 104 | #if POK_USE_DAC > 0 |
Pokitto | 52:c04087025cab | 105 | #if POK_BOARDREV == 1 |
Pokitto | 52:c04087025cab | 106 | /** 2-layer board rev 1.3 **/ |
Pokitto | 52:c04087025cab | 107 | DigitalOut dac0(P1_6); |
Pokitto | 52:c04087025cab | 108 | DigitalOut dac1(P1_0); |
Pokitto | 52:c04087025cab | 109 | DigitalOut dac2(P1_16); |
Pokitto | 52:c04087025cab | 110 | DigitalOut dac3(P0_19); |
Pokitto | 52:c04087025cab | 111 | DigitalOut dac4(P0_17); |
Pokitto | 52:c04087025cab | 112 | DigitalOut dac5(P1_12); |
Pokitto | 52:c04087025cab | 113 | DigitalOut dac6(P1_15); |
Pokitto | 52:c04087025cab | 114 | DigitalOut dac7(P1_8); |
Pokitto | 52:c04087025cab | 115 | #else |
Pokitto | 52:c04087025cab | 116 | /** 4-layer board rev 2.1 **/ |
Pokitto | 52:c04087025cab | 117 | DigitalOut dac0(P1_28); |
Pokitto | 52:c04087025cab | 118 | DigitalOut dac1(P1_29); |
Pokitto | 52:c04087025cab | 119 | DigitalOut dac2(P1_30); |
Pokitto | 52:c04087025cab | 120 | DigitalOut dac3(P1_31); |
Pokitto | 52:c04087025cab | 121 | /* has daniel made a mistake ?*/ |
Pokitto | 52:c04087025cab | 122 | DigitalOut dac4(P2_20); |
Pokitto | 52:c04087025cab | 123 | DigitalOut dac5(P2_21); |
Pokitto | 52:c04087025cab | 124 | DigitalOut dac6(P2_22); |
Pokitto | 52:c04087025cab | 125 | DigitalOut dac7(P2_23); |
Pokitto | 52:c04087025cab | 126 | |
Pokitto | 52:c04087025cab | 127 | DigitalOut amp(P1_17); |
Pokitto | 52:c04087025cab | 128 | |
Pokitto | 52:c04087025cab | 129 | #endif // POK_BOARDREV |
Pokitto | 52:c04087025cab | 130 | #endif // POK_USE_DAC |
Pokitto | 52:c04087025cab | 131 | |
Pokitto | 52:c04087025cab | 132 | #if POK_BOARDREV == 2 |
Pokitto | 52:c04087025cab | 133 | //MCP4018 volpot(P0_5,P0_4); |
Pokitto | 52:c04087025cab | 134 | |
Pokitto | 52:c04087025cab | 135 | /** |
Pokitto | 52:c04087025cab | 136 | * @brief Handle interrupt from 32-bit timer 0 |
Pokitto | 52:c04087025cab | 137 | * @return Nothing |
Pokitto | 52:c04087025cab | 138 | */ |
Pokitto | 52:c04087025cab | 139 | |
Pokitto | 52:c04087025cab | 140 | uint32_t p=0; |
Pokitto | 52:c04087025cab | 141 | uint8_t pixx=0, pixy=0; |
Pokitto | 52:c04087025cab | 142 | |
Pokitto | 52:c04087025cab | 143 | extern "C" void TIMER32_0_IRQHandler(void) |
Pokitto | 52:c04087025cab | 144 | { |
Pokitto | 52:c04087025cab | 145 | if (Chip_TIMER_MatchPending(LPC_TIMER32_0, 1)) { |
Pokitto | 52:c04087025cab | 146 | Chip_TIMER_ClearMatch(LPC_TIMER32_0, 1); |
Pokitto | 52:c04087025cab | 147 | //pokSoundBufferedIRQ(); |
Pokitto | 52:c04087025cab | 148 | #if POK_GBSOUND > 0 |
Pokitto | 52:c04087025cab | 149 | /** GAMEBUINO SOUND **/ |
Pokitto | 52:c04087025cab | 150 | Pokitto::audio_IRQ(); |
Pokitto | 52:c04087025cab | 151 | #else |
Pokitto | 52:c04087025cab | 152 | /** NOT GAMEBUINO SOUND **/ |
Pokitto | 52:c04087025cab | 153 | #if POK_SOUND_BUFFERED |
Pokitto | 52:c04087025cab | 154 | pokSoundBufferedIRQ(); |
Pokitto | 52:c04087025cab | 155 | #else |
Pokitto | 52:c04087025cab | 156 | pokSoundIRQ(); |
Pokitto | 52:c04087025cab | 157 | #endif |
Pokitto | 52:c04087025cab | 158 | #endif |
Pokitto | 52:c04087025cab | 159 | } |
Pokitto | 52:c04087025cab | 160 | } |
Pokitto | 52:c04087025cab | 161 | |
Pokitto | 52:c04087025cab | 162 | |
Pokitto | 52:c04087025cab | 163 | void initHWvolumecontrol() { |
Pokitto | 52:c04087025cab | 164 | HWvolume=0; |
Pokitto | 52:c04087025cab | 165 | volpotError=true; |
Pokitto | 52:c04087025cab | 166 | //if (volpot.put(HWvolume)) volpotError=true; //try if MCP4018 answers |
Pokitto | 52:c04087025cab | 167 | setHWvolume(discrete_vol_hw_levels[VOLUME_STARTUP>>5]); |
Pokitto | 52:c04087025cab | 168 | } |
Pokitto | 52:c04087025cab | 169 | |
Pokitto | 52:c04087025cab | 170 | int Pokitto::setHWvolume(uint8_t v) { |
Pokitto | 52:c04087025cab | 171 | HWvolume=v; |
Pokitto | 52:c04087025cab | 172 | if (HWvolume>127) HWvolume=127; |
Pokitto | 52:c04087025cab | 173 | //HWvolume = 0x7F&v; |
Pokitto | 52:c04087025cab | 174 | //if (!volpotError) return volpot.put(HWvolume); //use normal I2C |
Pokitto | 52:c04087025cab | 175 | /* fallback method for broken MCP4018 */ |
Pokitto | 52:c04087025cab | 176 | SoftwareI2C swvolpot(P0_4, P0_5); //swapped SDA,SCL |
Pokitto | 52:c04087025cab | 177 | swvolpot.write(0x5e,HWvolume); |
Pokitto | 52:c04087025cab | 178 | return HWvolume; |
Pokitto | 52:c04087025cab | 179 | } |
Pokitto | 52:c04087025cab | 180 | |
Pokitto | 52:c04087025cab | 181 | uint8_t Pokitto::getHWvolume() { |
Pokitto | 52:c04087025cab | 182 | return HWvolume; |
Pokitto | 52:c04087025cab | 183 | } |
Pokitto | 52:c04087025cab | 184 | |
Pokitto | 52:c04087025cab | 185 | void Pokitto::changeHWvolume(int8_t v) { |
Pokitto | 52:c04087025cab | 186 | int temp = HWvolume + v; |
Pokitto | 52:c04087025cab | 187 | if (temp < 0) temp = 0; //prevent volume "looparound" than can make a massive crack |
Pokitto | 52:c04087025cab | 188 | if (temp > 127) temp = 127; |
Pokitto | 52:c04087025cab | 189 | setHWvolume(temp); |
Pokitto | 52:c04087025cab | 190 | } |
Pokitto | 52:c04087025cab | 191 | |
Pokitto | 52:c04087025cab | 192 | uint8_t Pokitto::ampIsOn() { |
Pokitto | 52:c04087025cab | 193 | return amp; |
Pokitto | 52:c04087025cab | 194 | } |
Pokitto | 52:c04087025cab | 195 | |
Pokitto | 52:c04087025cab | 196 | void Pokitto::ampEnable(uint8_t v) { |
Pokitto | 52:c04087025cab | 197 | if (v>1) v=1; // limit against funny values |
Pokitto | 52:c04087025cab | 198 | amp=v; |
Pokitto | 52:c04087025cab | 199 | } |
Pokitto | 52:c04087025cab | 200 | #endif // POK_BOARDREV == 2 |
Pokitto | 52:c04087025cab | 201 | |
Pokitto | 52:c04087025cab | 202 | void Pokitto::dac_write(uint8_t value) { |
Pokitto | 52:c04087025cab | 203 | #if POK_USE_DAC > 0 |
Pokitto | 52:c04087025cab | 204 | #if POK_BOARDREV == 1 // was 1 |
Pokitto | 52:c04087025cab | 205 | if (value & 1) SET_DAC0 else CLR_DAC0; |
Pokitto | 52:c04087025cab | 206 | value >>= 1; |
Pokitto | 52:c04087025cab | 207 | if (value & 1) SET_DAC1 else CLR_DAC1; |
Pokitto | 52:c04087025cab | 208 | value >>= 1; |
Pokitto | 52:c04087025cab | 209 | if (value & 1) SET_DAC2 else CLR_DAC2; |
Pokitto | 52:c04087025cab | 210 | value >>= 1; |
Pokitto | 52:c04087025cab | 211 | if (value & 1) SET_DAC3 else CLR_DAC3; |
Pokitto | 52:c04087025cab | 212 | value >>= 1; |
Pokitto | 52:c04087025cab | 213 | if (value & 1) SET_DAC4 else CLR_DAC4; |
Pokitto | 52:c04087025cab | 214 | value >>= 1; |
Pokitto | 52:c04087025cab | 215 | if (value & 1) SET_DAC5 else CLR_DAC5; |
Pokitto | 52:c04087025cab | 216 | value >>= 1; |
Pokitto | 52:c04087025cab | 217 | if (value & 1) SET_DAC6 else CLR_DAC6; |
Pokitto | 52:c04087025cab | 218 | value >>= 1; |
Pokitto | 52:c04087025cab | 219 | if (value & 1) SET_DAC7 else CLR_DAC7; |
Pokitto | 52:c04087025cab | 220 | #else |
Pokitto | 52:c04087025cab | 221 | |
Pokitto | 52:c04087025cab | 222 | //val = value<<28; //lower 4 bits go higher - because port mask is used, no AND is needed to clear bits |
Pokitto | 52:c04087025cab | 223 | //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 | 224 | /* daniel has made a mistake with ports */ |
Pokitto | 52:c04087025cab | 225 | //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 | 226 | //val += value<<(15); //lower 4 bits go lower. No need to shift by 15 because bits are in the higher nibble |
Pokitto | 52:c04087025cab | 227 | //SET_MASK_DAC; |
Pokitto | 52:c04087025cab | 228 | //LPC_GPIO_PORT->MPIN[1] = val; // write bits to port |
Pokitto | 52:c04087025cab | 229 | //CLR_MASK_DAC; |
Pokitto | 52:c04087025cab | 230 | /* fixed here */ |
Pokitto | 52:c04087025cab | 231 | #define MASKED_DAC 0 |
Pokitto | 52:c04087025cab | 232 | #if MASKED_DAC |
Pokitto | 52:c04087025cab | 233 | uint32_t val; |
Pokitto | 52:c04087025cab | 234 | val=value; |
Pokitto | 52:c04087025cab | 235 | SET_MASK_DAC_LO; |
Pokitto | 52:c04087025cab | 236 | LPC_GPIO_PORT->MPIN[1] = val<<28; // write lower 4 bits to port |
Pokitto | 52:c04087025cab | 237 | CLR_MASK_DAC_LO; |
Pokitto | 52:c04087025cab | 238 | SET_MASK_DAC_HI; |
Pokitto | 52:c04087025cab | 239 | LPC_GPIO_PORT->MPIN[2] = val<<(20-4); // write bits to port |
Pokitto | 52:c04087025cab | 240 | CLR_MASK_DAC_HI; |
Pokitto | 52:c04087025cab | 241 | #else |
Pokitto | 52:c04087025cab | 242 | if (value & 1) SET_DAC0 else CLR_DAC0; |
Pokitto | 52:c04087025cab | 243 | value >>= 1; |
Pokitto | 52:c04087025cab | 244 | if (value & 1) SET_DAC1 else CLR_DAC1; |
Pokitto | 52:c04087025cab | 245 | value >>= 1; |
Pokitto | 52:c04087025cab | 246 | if (value & 1) SET_DAC2 else CLR_DAC2; |
Pokitto | 52:c04087025cab | 247 | value >>= 1; |
Pokitto | 52:c04087025cab | 248 | if (value & 1) SET_DAC3 else CLR_DAC3; |
Pokitto | 52:c04087025cab | 249 | value >>= 1; |
Pokitto | 52:c04087025cab | 250 | if (value & 1) SET_DAC4 else CLR_DAC4; |
Pokitto | 52:c04087025cab | 251 | value >>= 1; |
Pokitto | 52:c04087025cab | 252 | if (value & 1) SET_DAC5 else CLR_DAC5; |
Pokitto | 52:c04087025cab | 253 | value >>= 1; |
Pokitto | 52:c04087025cab | 254 | if (value & 1) SET_DAC6 else CLR_DAC6; |
Pokitto | 52:c04087025cab | 255 | value >>= 1; |
Pokitto | 52:c04087025cab | 256 | if (value & 1) SET_DAC7 else CLR_DAC7; |
Pokitto | 52:c04087025cab | 257 | #endif //MASKED_DAC |
Pokitto | 52:c04087025cab | 258 | //CLR_MASK_DAC; |
Pokitto | 52:c04087025cab | 259 | #endif // BOARDREV |
Pokitto | 52:c04087025cab | 260 | #endif |
Pokitto | 52:c04087025cab | 261 | } |
Pokitto | 52:c04087025cab | 262 | |
Pokitto | 67:068fa6345036 | 263 | void Pokitto::soundInit() { |
Pokitto | 67:068fa6345036 | 264 | soundInit(false); |
Pokitto | 67:068fa6345036 | 265 | } |
Pokitto | 67:068fa6345036 | 266 | |
Pokitto | 52:c04087025cab | 267 | /** SOUND INIT **/ |
Pokitto | 67:068fa6345036 | 268 | void Pokitto::soundInit(uint8_t reinit) { |
Pokitto | 67:068fa6345036 | 269 | #ifdef XPERIMENTAL |
Pokitto | 67:068fa6345036 | 270 | mbed::DigitalOut expr4(EXT4); |
Pokitto | 67:068fa6345036 | 271 | expr4=0; |
Pokitto | 67:068fa6345036 | 272 | #endif |
Pokitto | 52:c04087025cab | 273 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 52:c04087025cab | 274 | uint32_t timerFreq; |
Pokitto | 52:c04087025cab | 275 | #if POK_USE_PWM |
Pokitto | 67:068fa6345036 | 276 | if (!reinit) { |
Pokitto | 52:c04087025cab | 277 | pwmout_init(&audiopwm,POK_AUD_PIN); |
Pokitto | 52:c04087025cab | 278 | pwmout_period_us(&audiopwm,POK_AUD_PWM_US); //was 31us |
Pokitto | 52:c04087025cab | 279 | pwmout_write(&audiopwm,0.1f); |
Pokitto | 67:068fa6345036 | 280 | } |
Pokitto | 52:c04087025cab | 281 | #endif |
Pokitto | 52:c04087025cab | 282 | |
Pokitto | 52:c04087025cab | 283 | //#if POK_GBSOUND > 0 |
Pokitto | 52:c04087025cab | 284 | /** GAMEBUINO SOUND **/ |
Pokitto | 52:c04087025cab | 285 | //audio.attach_us(&audio_IRQ, 1000000/(POK_AUD_FREQ>>0)); |
Pokitto | 52:c04087025cab | 286 | //#else |
Pokitto | 52:c04087025cab | 287 | /** NOT GAMEBUINO SOUND **/ |
Pokitto | 52:c04087025cab | 288 | //audio.attach_us(&pokSoundBufferedIRQ, 1000000/(POK_AUD_FREQ>>0)); |
Pokitto | 52:c04087025cab | 289 | /* Initialize 32-bit timer 0 clock */ |
Pokitto | 52:c04087025cab | 290 | Chip_TIMER_Init(LPC_TIMER32_0); |
Pokitto | 52:c04087025cab | 291 | |
Pokitto | 52:c04087025cab | 292 | /* Timer rate is system clock rate */ |
Pokitto | 52:c04087025cab | 293 | timerFreq = Chip_Clock_GetSystemClockRate(); |
Pokitto | 52:c04087025cab | 294 | |
Pokitto | 52:c04087025cab | 295 | /* Timer setup for match and interrupt at TICKRATE_HZ */ |
Pokitto | 52:c04087025cab | 296 | Chip_TIMER_Reset(LPC_TIMER32_0); |
Pokitto | 52:c04087025cab | 297 | |
Pokitto | 52:c04087025cab | 298 | /* Enable both timers to generate interrupts when time matches */ |
Pokitto | 52:c04087025cab | 299 | Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 1); |
Pokitto | 52:c04087025cab | 300 | |
Pokitto | 52:c04087025cab | 301 | /* Setup 32-bit timer's duration (32-bit match time) */ |
Pokitto | 52:c04087025cab | 302 | Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, (timerFreq / POK_AUD_FREQ)); |
Pokitto | 52:c04087025cab | 303 | |
Pokitto | 52:c04087025cab | 304 | /* Setup both timers to restart when match occurs */ |
Pokitto | 52:c04087025cab | 305 | Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 1); |
Pokitto | 52:c04087025cab | 306 | |
Pokitto | 52:c04087025cab | 307 | /* Start both timers */ |
Pokitto | 52:c04087025cab | 308 | Chip_TIMER_Enable(LPC_TIMER32_0); |
Pokitto | 52:c04087025cab | 309 | |
Pokitto | 52:c04087025cab | 310 | /* Clear both timers of any pending interrupts */ |
Pokitto | 52:c04087025cab | 311 | #define TIMER_32_0_IRQn 18 |
Pokitto | 52:c04087025cab | 312 | NVIC_ClearPendingIRQ((IRQn_Type)TIMER_32_0_IRQn); |
Pokitto | 52:c04087025cab | 313 | |
Pokitto | 52:c04087025cab | 314 | /* Redirect IRQ vector - Jonne*/ |
Pokitto | 52:c04087025cab | 315 | NVIC_SetVector((IRQn_Type)TIMER_32_0_IRQn, (uint32_t)&TIMER32_0_IRQHandler); |
Pokitto | 52:c04087025cab | 316 | |
Pokitto | 52:c04087025cab | 317 | /* Enable both timer interrupts */ |
Pokitto | 52:c04087025cab | 318 | NVIC_EnableIRQ((IRQn_Type)TIMER_32_0_IRQn); |
Pokitto | 52:c04087025cab | 319 | //#endif // POK_GAMEBUINO_SUPPORT |
Pokitto | 52:c04087025cab | 320 | |
Pokitto | 52:c04087025cab | 321 | //emptySong(); |
Pokitto | 52:c04087025cab | 322 | //emptyOscillators(); |
Pokitto | 52:c04087025cab | 323 | //emptyBlocks(); |
Pokitto | 52:c04087025cab | 324 | //emptyPatches(); |
Pokitto | 52:c04087025cab | 325 | #ifdef TEST_SOUND |
Pokitto | 52:c04087025cab | 326 | testOsc(); |
Pokitto | 52:c04087025cab | 327 | #endif // TEST_SOUND |
Pokitto | 52:c04087025cab | 328 | #if POK_BOARDREV == 2 |
Pokitto | 52:c04087025cab | 329 | //initHWvolumecontrol(); |
Pokitto | 52:c04087025cab | 330 | #endif |
Pokitto | 52:c04087025cab | 331 | |
Pokitto | 52:c04087025cab | 332 | #if POK_ENABLE_SYNTH |
Pokitto | 52:c04087025cab | 333 | emptyOscillators(); |
Pokitto | 52:c04087025cab | 334 | #endif |
Pokitto | 52:c04087025cab | 335 | |
Pokitto | 52:c04087025cab | 336 | |
Pokitto | 52:c04087025cab | 337 | #endif // POK_ENABLE_SOUND |
Pokitto | 52:c04087025cab | 338 | |
Pokitto | 52:c04087025cab | 339 | } |
Pokitto | 52:c04087025cab | 340 | |
Pokitto | 52:c04087025cab | 341 | |
Pokitto | 52:c04087025cab | 342 | uint8_t Pokitto::streamPaused() { |
Pokitto | 52:c04087025cab | 343 | return !streamon; |
Pokitto | 52:c04087025cab | 344 | } |
Pokitto | 52:c04087025cab | 345 | |
Pokitto | 52:c04087025cab | 346 | void Pokitto::pauseStream() { |
Pokitto | 52:c04087025cab | 347 | streamon=0; |
Pokitto | 52:c04087025cab | 348 | } |
Pokitto | 52:c04087025cab | 349 | |
Pokitto | 52:c04087025cab | 350 | void Pokitto::playStream() { |
Pokitto | 52:c04087025cab | 351 | streamon=1; |
Pokitto | 52:c04087025cab | 352 | } |
Pokitto | 52:c04087025cab | 353 | |
Pokitto | 52:c04087025cab | 354 | |
Pokitto | 52:c04087025cab | 355 | void pokPauseStream() { |
Pokitto | 52:c04087025cab | 356 | streamon=0; |
Pokitto | 52:c04087025cab | 357 | } |
Pokitto | 52:c04087025cab | 358 | |
Pokitto | 52:c04087025cab | 359 | void pokPlayStream() { |
Pokitto | 52:c04087025cab | 360 | streamon=1; |
Pokitto | 52:c04087025cab | 361 | } |
Pokitto | 52:c04087025cab | 362 | |
Pokitto | 52:c04087025cab | 363 | |
Pokitto | 52:c04087025cab | 364 | |
Pokitto | 52:c04087025cab | 365 | inline void pokSoundBufferedIRQ() { |
Pokitto | 52:c04087025cab | 366 | uint32_t output = soundbuf[soundbufindex+=Pokitto::streamon]; |
Pokitto | 52:c04087025cab | 367 | if (soundbufindex==SBUFSIZE) soundbufindex=0; |
Pokitto | 52:c04087025cab | 368 | //if (p==sizeof(beat_11025_raw)) p=0; |
Pokitto | 52:c04087025cab | 369 | //soundbuf[soundbufindex++] = output; |
Pokitto | 52:c04087025cab | 370 | //uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float |
Pokitto | 52:c04087025cab | 371 | //obj->pwm->MATCHREL1 = t_on; |
Pokitto | 52:c04087025cab | 372 | output *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 52:c04087025cab | 373 | output >>= 8; |
Pokitto | 52:c04087025cab | 374 | dac_write(output); |
Pokitto | 52:c04087025cab | 375 | |
Pokitto | 52:c04087025cab | 376 | //setDRAMpoint(pixx, pixy); |
Pokitto | 52:c04087025cab | 377 | } |
Pokitto | 52:c04087025cab | 378 | |
Pokitto | 52:c04087025cab | 379 | inline void pokSoundIRQ() { |
Pokitto | 52:c04087025cab | 380 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 52:c04087025cab | 381 | //#define TICKY 0xFFFF //160 |
Pokitto | 52:c04087025cab | 382 | //#define INCY 409 |
Pokitto | 52:c04087025cab | 383 | uint32_t output=0;uint32_t op; |
Pokitto | 52:c04087025cab | 384 | //streamon=1; |
Pokitto | 52:c04087025cab | 385 | //if (test==TICKY) test=0; |
Pokitto | 52:c04087025cab | 386 | //if (test<(TICKY/2)) { tpin=1; pwmout_write(&audiopwm,(float)0/(float)255);}//dac_write(0);} |
Pokitto | 52:c04087025cab | 387 | //else {tpin=0; pwmout_write(&audiopwm,(float)255/(float)255);}//dac_write(64);} |
Pokitto | 52:c04087025cab | 388 | //test+=INCY; |
Pokitto | 52:c04087025cab | 389 | //return; |
Pokitto | 52:c04087025cab | 390 | #ifndef POK_SIM |
Pokitto | 52:c04087025cab | 391 | #if POK_USE_PWM |
Pokitto | 52:c04087025cab | 392 | pwmout_t* obj = &audiopwm; |
Pokitto | 52:c04087025cab | 393 | #endif |
Pokitto | 52:c04087025cab | 394 | #endif |
Pokitto | 52:c04087025cab | 395 | #if POK_STREAMING_MUSIC > 0 |
Pokitto | 52:c04087025cab | 396 | #if POK_STREAMFREQ_HALVE |
Pokitto | 52:c04087025cab | 397 | streamstep = 1-streamstep; |
Pokitto | 52:c04087025cab | 398 | #else |
Pokitto | 52:c04087025cab | 399 | streamstep = 1; |
Pokitto | 52:c04087025cab | 400 | #endif // POK_STREAMFREQ_HALVE |
Pokitto | 67:068fa6345036 | 401 | #ifndef PROJ_SDFS_STREAMING |
Pokitto | 67:068fa6345036 | 402 | streamon=1; // force enable stream |
Pokitto | 67:068fa6345036 | 403 | #endif |
Pokitto | 52:c04087025cab | 404 | streamstep &= streamon; // streamon is used to toggle SD music streaming on and off |
Pokitto | 52:c04087025cab | 405 | if (streamstep) { |
Pokitto | 52:c04087025cab | 406 | output = (*currentPtr++); |
Pokitto | 67:068fa6345036 | 407 | if( Pokitto::Sound::sfxDataPtr != Pokitto::Sound::sfxEndPtr ){ |
Pokitto | 67:068fa6345036 | 408 | #ifdef PROJ_SDFS_STREAMING |
Pokitto | 67:068fa6345036 | 409 | int32_t s = (int32_t(output) + int32_t(*Pokitto::Sound::sfxDataPtr++)) - 128; |
Pokitto | 67:068fa6345036 | 410 | #else |
Pokitto | 67:068fa6345036 | 411 | int32_t s = (127 + int32_t(*Pokitto::Sound::sfxDataPtr++)) - 128; |
Pokitto | 67:068fa6345036 | 412 | #endif |
Pokitto | 67:068fa6345036 | 413 | if( s < 0 ) s = 0; |
Pokitto | 67:068fa6345036 | 414 | else if( s > 255 ) s = 255; |
Pokitto | 67:068fa6345036 | 415 | output = s; |
Pokitto | 67:068fa6345036 | 416 | } |
Pokitto | 67:068fa6345036 | 417 | if(streamvol && streamon) { |
Pokitto | 52:c04087025cab | 418 | output >>= 3-streamvol; |
Pokitto | 52:c04087025cab | 419 | streambyte = output; |
Pokitto | 52:c04087025cab | 420 | } else { |
Pokitto | 52:c04087025cab | 421 | streambyte = 0; // duty cycle |
Pokitto | 52:c04087025cab | 422 | output = 0; |
Pokitto | 52:c04087025cab | 423 | } |
Pokitto | 52:c04087025cab | 424 | if (currentPtr >= endPtr) |
Pokitto | 52:c04087025cab | 425 | { |
Pokitto | 52:c04087025cab | 426 | currentBuffer++; |
Pokitto | 52:c04087025cab | 427 | if (currentBuffer==4) currentBuffer=0; |
Pokitto | 52:c04087025cab | 428 | currentPtr = buffers[currentBuffer]; |
Pokitto | 52:c04087025cab | 429 | endPtr = currentPtr + BUFFER_SIZE; |
Pokitto | 52:c04087025cab | 430 | } |
Pokitto | 52:c04087025cab | 431 | } |
Pokitto | 52:c04087025cab | 432 | #endif // POK_STREAMING_MUSIC |
Pokitto | 52:c04087025cab | 433 | |
Pokitto | 52:c04087025cab | 434 | /** DO ADDITIONAL SOUND PROCESSING (NOT STREAM) OF SOUND HERE **/ |
Pokitto | 52:c04087025cab | 435 | |
Pokitto | 52:c04087025cab | 436 | #if POK_ENABLE_SYNTH |
Pokitto | 52:c04087025cab | 437 | /** if song is being played from sd **/ |
Pokitto | 52:c04087025cab | 438 | if (playing) { |
Pokitto | 52:c04087025cab | 439 | notetick++; |
Pokitto | 52:c04087025cab | 440 | updatePlayback(); |
Pokitto | 52:c04087025cab | 441 | } |
Pokitto | 52:c04087025cab | 442 | /** oscillators update **/ |
Pokitto | 67:068fa6345036 | 443 | osc1.count += osc1.cinc + (osc1.pitchbend); // counts to 65535 and overflows to zero WAS 8 ! |
Pokitto | 67:068fa6345036 | 444 | osc2.count += osc2.cinc + (osc2.pitchbend); // counts to 65535 and overflows to zero |
Pokitto | 67:068fa6345036 | 445 | osc3.count += osc3.cinc + (osc3.pitchbend); // counts to 65535 and overflows to zero |
Pokitto | 67:068fa6345036 | 446 | #if POK_ALT_MIXING > 0 // heaviest cpu load, recalculate envelopes on each cycle |
Pokitto | 67:068fa6345036 | 447 | uint32_t o = 0; |
Pokitto | 67:068fa6345036 | 448 | Marr[3](); |
Pokitto | 67:068fa6345036 | 449 | Marr[2](); |
Pokitto | 67:068fa6345036 | 450 | Marr[1](); |
Pokitto | 67:068fa6345036 | 451 | if (tick==0) Marr[0](); |
Pokitto | 67:068fa6345036 | 452 | #else |
Pokitto | 52:c04087025cab | 453 | Marr[tick](); // call mixing function |
Pokitto | 67:068fa6345036 | 454 | #endif // ALT_MIXING |
Pokitto | 52:c04087025cab | 455 | --tick; |
Pokitto | 52:c04087025cab | 456 | |
Pokitto | 52:c04087025cab | 457 | /** mixing oscillator output **/ |
Pokitto | 52:c04087025cab | 458 | |
Pokitto | 52:c04087025cab | 459 | op = (uint32_t) ((osc1.output)*(osc1.vol>>8))>>9;// >> 2 osc1.vol Marr; |
Pokitto | 52:c04087025cab | 460 | op += (uint32_t) ((osc2.output)*(osc2.vol>>8))>>9;// >> 2 osc1.vol Marr; |
Pokitto | 52:c04087025cab | 461 | op += (uint32_t) ((osc3.output)*(osc3.vol>>8))>>9;// >> 2 osc1.vol Marr; |
Pokitto | 52:c04087025cab | 462 | op *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 52:c04087025cab | 463 | op >>= 8; |
Pokitto | 52:c04087025cab | 464 | output = op & 0xFF; |
Pokitto | 52:c04087025cab | 465 | |
Pokitto | 52:c04087025cab | 466 | #endif // POK_ENABLE_SYNTH |
Pokitto | 52:c04087025cab | 467 | |
Pokitto | 52:c04087025cab | 468 | #ifndef POK_SIM |
Pokitto | 52:c04087025cab | 469 | /** HARDWARE **/ |
Pokitto | 52:c04087025cab | 470 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 52:c04087025cab | 471 | #if POK_STREAMING_MUSIC > 0 |
Pokitto | 52:c04087025cab | 472 | /** sound is enabled, streaming is enabled */ |
Pokitto | 52:c04087025cab | 473 | #if POK_STREAM_TO_DAC > 0 |
Pokitto | 52:c04087025cab | 474 | /** stream goes to DAC */ |
Pokitto | 52:c04087025cab | 475 | #if POK_USE_DAC > 0 |
Pokitto | 52:c04087025cab | 476 | uint32_t sbyte = streambyte; |
Pokitto | 52:c04087025cab | 477 | sbyte *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 52:c04087025cab | 478 | sbyte >>= 8; |
Pokitto | 52:c04087025cab | 479 | dac_write((uint8_t)sbyte); // duty cycle |
Pokitto | 52:c04087025cab | 480 | #endif // POK_USE_DAC |
Pokitto | 52:c04087025cab | 481 | #else |
Pokitto | 52:c04087025cab | 482 | /** stream goes to PWM */ |
Pokitto | 52:c04087025cab | 483 | if (streamstep) { |
Pokitto | 52:c04087025cab | 484 | //pwmout_write(&audiopwm,(float)streambyte/(float)255); |
Pokitto | 52:c04087025cab | 485 | #if POK_USE_PWM |
Pokitto | 52:c04087025cab | 486 | uint32_t sbyte = streambyte; |
Pokitto | 52:c04087025cab | 487 | sbyte *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 52:c04087025cab | 488 | sbyte >>= 8; |
Pokitto | 52:c04087025cab | 489 | uint32_t t_on = (uint32_t)((((obj->pwm->MATCHREL0)*sbyte)>>8)); //cut out float |
Pokitto | 52:c04087025cab | 490 | obj->pwm->MATCHREL1 = t_on; |
Pokitto | 52:c04087025cab | 491 | #endif |
Pokitto | 52:c04087025cab | 492 | //dac_write((uint8_t)streambyte); // duty cycle |
Pokitto | 52:c04087025cab | 493 | } |
Pokitto | 52:c04087025cab | 494 | #endif // POK_STREAM_TO_DAC |
Pokitto | 52:c04087025cab | 495 | #endif // POK_STREAMING_MUSIC |
Pokitto | 52:c04087025cab | 496 | #if POK_STREAM_TO_DAC > 0 |
Pokitto | 52:c04087025cab | 497 | /** synth goes to PWM */ |
Pokitto | 52:c04087025cab | 498 | //pwmout_write(&audiopwm,(float)output/(float)255); |
Pokitto | 52:c04087025cab | 499 | #if POK_USE_PWM |
Pokitto | 52:c04087025cab | 500 | op = output; |
Pokitto | 52:c04087025cab | 501 | op *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 52:c04087025cab | 502 | op >>= 8; |
Pokitto | 52:c04087025cab | 503 | uint32_t t_on = (uint32_t)((((obj->pwm->MATCHREL0)*op)>>8)); //cut out float |
Pokitto | 52:c04087025cab | 504 | obj->pwm->MATCHREL1 = t_on; |
Pokitto | 52:c04087025cab | 505 | #endif |
Pokitto | 52:c04087025cab | 506 | #else // POK_STREAMING_MUSIC |
Pokitto | 52:c04087025cab | 507 | op = output; |
Pokitto | 52:c04087025cab | 508 | op *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 52:c04087025cab | 509 | op >>= 8; |
Pokitto | 52:c04087025cab | 510 | dac_write((uint8_t)op); // SYNTH to DAC |
Pokitto | 52:c04087025cab | 511 | #endif |
Pokitto | 52:c04087025cab | 512 | soundbyte = (output+streambyte)>>1; |
Pokitto | 52:c04087025cab | 513 | soundbuf[soundbufindex++]=soundbyte; |
Pokitto | 52:c04087025cab | 514 | if (soundbufindex==256) soundbufindex=0; |
Pokitto | 52:c04087025cab | 515 | #endif //POK_ENABLE_SOUND |
Pokitto | 52:c04087025cab | 516 | #endif // HARDWARE |
Pokitto | 52:c04087025cab | 517 | #endif //POK_ENABLE_SOUND |
Pokitto | 52:c04087025cab | 518 | } |
Pokitto | 52:c04087025cab | 519 | |
Pokitto | 52:c04087025cab | 520 | |
Pokitto | 52:c04087025cab | 521 | void Pokitto::updateSDAudioStream() { |
Pokitto | 52:c04087025cab | 522 | if (streamPaused()) return; |
Pokitto | 52:c04087025cab | 523 | |
Pokitto | 52:c04087025cab | 524 | #if POK_STREAMING_MUSIC > 0 |
Pokitto | 52:c04087025cab | 525 | if (oldBuffer != currentBuffer) { |
Pokitto | 52:c04087025cab | 526 | if (currentBuffer==0) fileReadBytes(&buffers[3][0],BUFFER_SIZE); |
Pokitto | 52:c04087025cab | 527 | else if (currentBuffer==1) fileReadBytes(&buffers[0][0],BUFFER_SIZE); |
Pokitto | 52:c04087025cab | 528 | else if (currentBuffer==2) fileReadBytes(&buffers[1][0],BUFFER_SIZE); |
Pokitto | 52:c04087025cab | 529 | else fileReadBytes(&buffers[2][0],BUFFER_SIZE); |
Pokitto | 52:c04087025cab | 530 | oldBuffer = currentBuffer; |
Pokitto | 52:c04087025cab | 531 | streamcounter += BUFFER_SIZE; |
Pokitto | 52:c04087025cab | 532 | } else return; |
Pokitto | 52:c04087025cab | 533 | |
Pokitto | 52:c04087025cab | 534 | #ifndef POK_SIM |
Pokitto | 52:c04087025cab | 535 | if ( streamcounter > fs.fsize - (BUFFER_SIZE*6)) { |
Pokitto | 52:c04087025cab | 536 | #else |
Pokitto | 52:c04087025cab | 537 | if ( streamcounter > getFileLength() - (BUFFER_SIZE*6)) { |
Pokitto | 52:c04087025cab | 538 | #endif |
Pokitto | 52:c04087025cab | 539 | streamcounter=0; |
Pokitto | 52:c04087025cab | 540 | #if POK_STREAM_LOOP |
Pokitto | 52:c04087025cab | 541 | fileRewind(); |
Pokitto | 52:c04087025cab | 542 | #else |
Pokitto | 52:c04087025cab | 543 | pokPauseStream(); |
Pokitto | 52:c04087025cab | 544 | #endif |
Pokitto | 52:c04087025cab | 545 | } |
Pokitto | 52:c04087025cab | 546 | #endif |
Pokitto | 52:c04087025cab | 547 | } |
Pokitto | 52:c04087025cab | 548 | |
Pokitto | 52:c04087025cab | 549 | |
Pokitto | 52:c04087025cab | 550 | #endif // POK_ENABLE_SOUND |
Pokitto | 52:c04087025cab | 551 | |
Pokitto | 52:c04087025cab | 552 | |
Pokitto | 52:c04087025cab | 553 |