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