PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
POKITTO_CORE/PokittoSound.cpp@43:6183b12dd99c, 2018-05-01 (annotated)
- Committer:
- Pokitto
- Date:
- Tue May 01 18:42:56 2018 +0000
- Revision:
- 43:6183b12dd99c
New volume control;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 43:6183b12dd99c | 1 | /**************************************************************************/ |
Pokitto | 43:6183b12dd99c | 2 | /*! |
Pokitto | 43:6183b12dd99c | 3 | @file PokittoSound.cpp |
Pokitto | 43:6183b12dd99c | 4 | @author Jonne Valola |
Pokitto | 43:6183b12dd99c | 5 | |
Pokitto | 43:6183b12dd99c | 6 | @section LICENSE |
Pokitto | 43:6183b12dd99c | 7 | |
Pokitto | 43:6183b12dd99c | 8 | Software License Agreement (BSD License) |
Pokitto | 43:6183b12dd99c | 9 | |
Pokitto | 43:6183b12dd99c | 10 | Copyright (c) 2016, Jonne Valola |
Pokitto | 43:6183b12dd99c | 11 | All rights reserved. |
Pokitto | 43:6183b12dd99c | 12 | |
Pokitto | 43:6183b12dd99c | 13 | Redistribution and use in source and binary forms, with or without |
Pokitto | 43:6183b12dd99c | 14 | modification, are permitted provided that the following conditions are met: |
Pokitto | 43:6183b12dd99c | 15 | 1. Redistributions of source code must retain the above copyright |
Pokitto | 43:6183b12dd99c | 16 | notice, this list of conditions and the following disclaimer. |
Pokitto | 43:6183b12dd99c | 17 | 2. Redistributions in binary form must reproduce the above copyright |
Pokitto | 43:6183b12dd99c | 18 | notice, this list of conditions and the following disclaimer in the |
Pokitto | 43:6183b12dd99c | 19 | documentation and/or other materials provided with the distribution. |
Pokitto | 43:6183b12dd99c | 20 | 3. Neither the name of the copyright holders nor the |
Pokitto | 43:6183b12dd99c | 21 | names of its contributors may be used to endorse or promote products |
Pokitto | 43:6183b12dd99c | 22 | derived from this software without specific prior written permission. |
Pokitto | 43:6183b12dd99c | 23 | |
Pokitto | 43:6183b12dd99c | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
Pokitto | 43:6183b12dd99c | 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Pokitto | 43:6183b12dd99c | 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Pokitto | 43:6183b12dd99c | 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
Pokitto | 43:6183b12dd99c | 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Pokitto | 43:6183b12dd99c | 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Pokitto | 43:6183b12dd99c | 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
Pokitto | 43:6183b12dd99c | 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Pokitto | 43:6183b12dd99c | 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Pokitto | 43:6183b12dd99c | 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Pokitto | 43:6183b12dd99c | 34 | */ |
Pokitto | 43:6183b12dd99c | 35 | /**************************************************************************/ |
Pokitto | 43:6183b12dd99c | 36 | |
Pokitto | 43:6183b12dd99c | 37 | /* |
Pokitto | 43:6183b12dd99c | 38 | * NOTE: |
Pokitto | 43:6183b12dd99c | 39 | * API of the Pokitto Sound library is partially identical to the Gamebuino Sound API. |
Pokitto | 43:6183b12dd99c | 40 | * Due to the difference in architecture (ARM Cortex-M0+ in mbed environment vs. 8-bit AVR) |
Pokitto | 43:6183b12dd99c | 41 | * large parts are not identical. Most functions were rewritten, with only API remaining. |
Pokitto | 43:6183b12dd99c | 42 | * We want to give attribution to the original author's project: |
Pokitto | 43:6183b12dd99c | 43 | * |
Pokitto | 43:6183b12dd99c | 44 | * License for Gamebuino-identical code: |
Pokitto | 43:6183b12dd99c | 45 | * |
Pokitto | 43:6183b12dd99c | 46 | * (C) Copyright 2014 Aurélien Rodot. All rights reserved. |
Pokitto | 43:6183b12dd99c | 47 | * |
Pokitto | 43:6183b12dd99c | 48 | * This file is part of the Gamebuino Library (http://gamebuino.com) |
Pokitto | 43:6183b12dd99c | 49 | * |
Pokitto | 43:6183b12dd99c | 50 | * The Gamebuino Library is free software: you can redistribute it and/or modify |
Pokitto | 43:6183b12dd99c | 51 | * it under the terms of the GNU Lesser General Public License as published by |
Pokitto | 43:6183b12dd99c | 52 | * the Free Software Foundation, either version 3 of the License, or |
Pokitto | 43:6183b12dd99c | 53 | * (at your option) any later version. |
Pokitto | 43:6183b12dd99c | 54 | * |
Pokitto | 43:6183b12dd99c | 55 | * This program is distributed in the hope that it will be useful, |
Pokitto | 43:6183b12dd99c | 56 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Pokitto | 43:6183b12dd99c | 57 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Pokitto | 43:6183b12dd99c | 58 | * GNU Lesser General Public License for more details. |
Pokitto | 43:6183b12dd99c | 59 | * |
Pokitto | 43:6183b12dd99c | 60 | * You should have received a copy of the GNU Lesser General Public License |
Pokitto | 43:6183b12dd99c | 61 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
Pokitto | 43:6183b12dd99c | 62 | */ |
Pokitto | 43:6183b12dd99c | 63 | |
Pokitto | 43:6183b12dd99c | 64 | #include "PokittoSound.h" |
Pokitto | 43:6183b12dd99c | 65 | #include "Pokitto_settings.h" |
Pokitto | 43:6183b12dd99c | 66 | #include "PokittoCore.h" |
Pokitto | 43:6183b12dd99c | 67 | #include "Synth.h" |
Pokitto | 43:6183b12dd99c | 68 | |
Pokitto | 43:6183b12dd99c | 69 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 70 | #include "HWSound.h" |
Pokitto | 43:6183b12dd99c | 71 | #else |
Pokitto | 43:6183b12dd99c | 72 | #include "SimSound.h" |
Pokitto | 43:6183b12dd99c | 73 | #include "PokittoSimulator.h" |
Pokitto | 43:6183b12dd99c | 74 | #endif |
Pokitto | 43:6183b12dd99c | 75 | |
Pokitto | 43:6183b12dd99c | 76 | typedef uint8_t byte; |
Pokitto | 43:6183b12dd99c | 77 | |
Pokitto | 43:6183b12dd99c | 78 | using namespace Pokitto; |
Pokitto | 43:6183b12dd99c | 79 | |
Pokitto | 43:6183b12dd99c | 80 | /** discrete hardware volume control **/ |
Pokitto | 43:6183b12dd99c | 81 | |
Pokitto | 43:6183b12dd99c | 82 | uint8_t Pokitto::discrete_vol = 0; |
Pokitto | 43:6183b12dd99c | 83 | uint8_t const Pokitto::discrete_vol_levels[8] {0,32,64,96,128,160,192,224}; |
Pokitto | 43:6183b12dd99c | 84 | uint8_t const Pokitto::discrete_vol_hw_levels[8] {0,27,64,96,36,117,127,127}; |
Pokitto | 43:6183b12dd99c | 85 | uint8_t const Pokitto::discrete_vol_multipliers[8] {0,127,127,127,192,192,255,255}; |
Pokitto | 43:6183b12dd99c | 86 | |
Pokitto | 43:6183b12dd99c | 87 | Pokitto::Core _soundc; |
Pokitto | 43:6183b12dd99c | 88 | |
Pokitto | 43:6183b12dd99c | 89 | uint8_t Sound::prescaler; |
Pokitto | 43:6183b12dd99c | 90 | uint16_t Sound::globalVolume; |
Pokitto | 43:6183b12dd99c | 91 | uint16_t Sound::volumeMax = VOLUME_HEADPHONE_MAX; |
Pokitto | 43:6183b12dd99c | 92 | uint8_t Sound::headPhoneLevel=1; |
Pokitto | 43:6183b12dd99c | 93 | |
Pokitto | 43:6183b12dd99c | 94 | bool Sound::trackIsPlaying[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 95 | bool Sound::patternIsPlaying[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 96 | uint8_t Sound::outputPitch[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 97 | int8_t Sound::outputVolume[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 98 | |
Pokitto | 43:6183b12dd99c | 99 | uint16_t *Sound::trackData[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 100 | uint8_t Sound::trackCursor[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 101 | uint16_t **Sound::patternSet[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 102 | int8_t Sound::patternPitch[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 103 | |
Pokitto | 43:6183b12dd99c | 104 | // pattern data |
Pokitto | 43:6183b12dd99c | 105 | uint16_t *Sound::patternData[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 106 | uint16_t **Sound::instrumentSet[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 107 | bool Sound::patternLooping[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 108 | uint16_t Sound::patternCursor[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 109 | |
Pokitto | 43:6183b12dd99c | 110 | // note data |
Pokitto | 43:6183b12dd99c | 111 | uint8_t Sound::notePitch[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 112 | uint8_t Sound::noteDuration[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 113 | int8_t Sound::noteVolume[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 114 | bool Sound::notePlaying[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 115 | |
Pokitto | 43:6183b12dd99c | 116 | // commands data |
Pokitto | 43:6183b12dd99c | 117 | int8_t Sound::commandsCounter[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 118 | int8_t Sound::volumeSlideStepDuration[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 119 | int8_t Sound::volumeSlideStepSize[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 120 | uint8_t Sound::arpeggioStepDuration[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 121 | int8_t Sound::arpeggioStepSize[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 122 | uint8_t Sound::tremoloStepDuration[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 123 | int8_t Sound::tremoloStepSize[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 124 | |
Pokitto | 43:6183b12dd99c | 125 | // instrument data |
Pokitto | 43:6183b12dd99c | 126 | uint16_t *Sound::instrumentData[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 127 | uint8_t Sound::instrumentLength[NUM_CHANNELS]; //number of steps in the instrument |
Pokitto | 43:6183b12dd99c | 128 | uint8_t Sound::instrumentLooping[NUM_CHANNELS]; //how many steps to loop on when the last step of the instrument is reached |
Pokitto | 43:6183b12dd99c | 129 | uint16_t Sound::instrumentCursor[NUM_CHANNELS]; //which step is being played |
Pokitto | 43:6183b12dd99c | 130 | uint8_t Sound::instrumentNextChange[NUM_CHANNELS]; //how many frames before the next step |
Pokitto | 43:6183b12dd99c | 131 | |
Pokitto | 43:6183b12dd99c | 132 | //current step data |
Pokitto | 43:6183b12dd99c | 133 | int8_t Sound::stepVolume[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 134 | uint8_t Sound::stepPitch[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 135 | uint8_t Sound::chanVolumes[NUM_CHANNELS]; |
Pokitto | 43:6183b12dd99c | 136 | |
Pokitto | 43:6183b12dd99c | 137 | #if (POK_ENABLE_SOUND < 1) |
Pokitto | 43:6183b12dd99c | 138 | #define NUM_CHANNELS 0 |
Pokitto | 43:6183b12dd99c | 139 | #endif |
Pokitto | 43:6183b12dd99c | 140 | |
Pokitto | 43:6183b12dd99c | 141 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 142 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 143 | uint32_t sbyte; |
Pokitto | 43:6183b12dd99c | 144 | #else |
Pokitto | 43:6183b12dd99c | 145 | uint32_t sbyte; |
Pokitto | 43:6183b12dd99c | 146 | float pwm1; |
Pokitto | 43:6183b12dd99c | 147 | #endif // POK_SIM |
Pokitto | 43:6183b12dd99c | 148 | |
Pokitto | 43:6183b12dd99c | 149 | //declare these variables globally for faster access |
Pokitto | 43:6183b12dd99c | 150 | uint8_t _rand = 1; |
Pokitto | 43:6183b12dd99c | 151 | uint8_t _chanCount[NUM_CHANNELS]; //counts until the next change of the waveform |
Pokitto | 43:6183b12dd99c | 152 | bool _chanState[NUM_CHANNELS]; //if the waveform is currently high or low |
Pokitto | 43:6183b12dd99c | 153 | uint8_t _chanHalfPeriod[NUM_CHANNELS]; //duration of half the period of the waveform |
Pokitto | 43:6183b12dd99c | 154 | uint8_t _chanOutputVolume[NUM_CHANNELS]; //amplitude of the outputted waveform |
Pokitto | 43:6183b12dd99c | 155 | uint8_t _chanOutput[NUM_CHANNELS]; //current value of the outputted waveform |
Pokitto | 43:6183b12dd99c | 156 | bool _chanNoise[NUM_CHANNELS]; //if a random value should be added to the waveform to generate noise |
Pokitto | 43:6183b12dd99c | 157 | |
Pokitto | 43:6183b12dd99c | 158 | #if POK_GBSOUND > 0 |
Pokitto | 43:6183b12dd99c | 159 | const uint16_t squareWaveInstrument[] = {0x0101, 0x03F7}; |
Pokitto | 43:6183b12dd99c | 160 | const uint16_t noiseInstrument[] = {0x0101, 0x03FF}; |
Pokitto | 43:6183b12dd99c | 161 | const uint16_t* const defaultInstruments[] = {squareWaveInstrument,noiseInstrument}; |
Pokitto | 43:6183b12dd99c | 162 | |
Pokitto | 43:6183b12dd99c | 163 | const uint16_t playOKPattern[] = {0x0005,0x138,0x168,0x0000}; |
Pokitto | 43:6183b12dd99c | 164 | const uint16_t playCancelPattern[] = {0x0005,0x168,0x138,0x0000}; |
Pokitto | 43:6183b12dd99c | 165 | const uint16_t playTickP[] = {0x0045,0x168,0x0000}; |
Pokitto | 43:6183b12dd99c | 166 | #endif |
Pokitto | 43:6183b12dd99c | 167 | #if(EXTENDED_NOTE_RANGE > 0) |
Pokitto | 43:6183b12dd99c | 168 | //extended note range |
Pokitto | 43:6183b12dd99c | 169 | #define NUM_PITCH 59 |
Pokitto | 43:6183b12dd99c | 170 | const uint8_t _halfPeriods[NUM_PITCH] = {246,232,219,207,195,184,174,164,155,146,138,130,123,116,110,104,98,92,87,82,78,73,69,65,62,58,55,52,49,46,44,41,39,37,35,33,31,29,28,26,25,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6}; |
Pokitto | 43:6183b12dd99c | 171 | #else |
Pokitto | 43:6183b12dd99c | 172 | //regular note range |
Pokitto | 43:6183b12dd99c | 173 | #define NUM_PITCH 36 |
Pokitto | 43:6183b12dd99c | 174 | const uint8_t _halfPeriods[NUM_PITCH] = {246,232,219,207,195,184,174,164,155,146,138,130,123,116,110,104,98,92,87,82,78,73,69,65,62,58,55,52,49,46,44,41,39,37,35,33}; |
Pokitto | 43:6183b12dd99c | 175 | #endif |
Pokitto | 43:6183b12dd99c | 176 | |
Pokitto | 43:6183b12dd99c | 177 | #endif |
Pokitto | 43:6183b12dd99c | 178 | |
Pokitto | 43:6183b12dd99c | 179 | void Pokitto::audio_IRQ() { |
Pokitto | 43:6183b12dd99c | 180 | #if POK_STREAMING_MUSIC > 0 |
Pokitto | 43:6183b12dd99c | 181 | #if POK_STREAMFREQ_HALVE > 0 |
Pokitto | 43:6183b12dd99c | 182 | streamstep = 1-streamstep; |
Pokitto | 43:6183b12dd99c | 183 | #else |
Pokitto | 43:6183b12dd99c | 184 | streamstep=1; |
Pokitto | 43:6183b12dd99c | 185 | #endif |
Pokitto | 43:6183b12dd99c | 186 | |
Pokitto | 43:6183b12dd99c | 187 | streamstep &= streamon; //check if stream is on |
Pokitto | 43:6183b12dd99c | 188 | |
Pokitto | 43:6183b12dd99c | 189 | if(streamvol && streamstep) { |
Pokitto | 43:6183b12dd99c | 190 | uint8_t output = (*currentPtr++); |
Pokitto | 43:6183b12dd99c | 191 | sbyte = output; |
Pokitto | 43:6183b12dd99c | 192 | } else { |
Pokitto | 43:6183b12dd99c | 193 | sbyte = 0; // duty cycle |
Pokitto | 43:6183b12dd99c | 194 | } |
Pokitto | 43:6183b12dd99c | 195 | |
Pokitto | 43:6183b12dd99c | 196 | if (currentPtr >= endPtr) |
Pokitto | 43:6183b12dd99c | 197 | { |
Pokitto | 43:6183b12dd99c | 198 | currentBuffer++; |
Pokitto | 43:6183b12dd99c | 199 | if (currentBuffer==4) currentBuffer=0; |
Pokitto | 43:6183b12dd99c | 200 | currentPtr = buffers[currentBuffer]; |
Pokitto | 43:6183b12dd99c | 201 | endPtr = currentPtr + BUFFER_SIZE; |
Pokitto | 43:6183b12dd99c | 202 | } |
Pokitto | 43:6183b12dd99c | 203 | |
Pokitto | 43:6183b12dd99c | 204 | #endif // POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 205 | #if (NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 206 | Sound::generateOutput(); |
Pokitto | 43:6183b12dd99c | 207 | #endif |
Pokitto | 43:6183b12dd99c | 208 | } |
Pokitto | 43:6183b12dd99c | 209 | |
Pokitto | 43:6183b12dd99c | 210 | void Sound::volumeUp() { |
Pokitto | 43:6183b12dd99c | 211 | Pokitto::discrete_vol++; |
Pokitto | 43:6183b12dd99c | 212 | if (discrete_vol>7) discrete_vol=7; |
Pokitto | 43:6183b12dd99c | 213 | globalVolume = discrete_vol_levels[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 214 | setVolume(globalVolume); |
Pokitto | 43:6183b12dd99c | 215 | //if (globalVolume>VOLUME_HEADPHONE_MAX) setVolume(getVolume()+VOLUME_STEP*2); |
Pokitto | 43:6183b12dd99c | 216 | //else setVolume(getVolume()+VOLUME_STEP); |
Pokitto | 43:6183b12dd99c | 217 | } |
Pokitto | 43:6183b12dd99c | 218 | |
Pokitto | 43:6183b12dd99c | 219 | void Sound::volumeDown() { |
Pokitto | 43:6183b12dd99c | 220 | if (discrete_vol) Pokitto::discrete_vol--; |
Pokitto | 43:6183b12dd99c | 221 | globalVolume = discrete_vol_levels[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 222 | setVolume(globalVolume); |
Pokitto | 43:6183b12dd99c | 223 | //if (globalVolume>VOLUME_HEADPHONE_MAX) setVolume(getVolume()-VOLUME_STEP*4); |
Pokitto | 43:6183b12dd99c | 224 | //else setVolume(getVolume()-VOLUME_STEP); |
Pokitto | 43:6183b12dd99c | 225 | } |
Pokitto | 43:6183b12dd99c | 226 | |
Pokitto | 43:6183b12dd99c | 227 | |
Pokitto | 43:6183b12dd99c | 228 | void Sound::setMaxVol(int16_t v) { |
Pokitto | 43:6183b12dd99c | 229 | if (v < 0) v = 0; //prevent nasty wraparound |
Pokitto | 43:6183b12dd99c | 230 | if (v > VOLUME_SPEAKER_MAX) { |
Pokitto | 43:6183b12dd99c | 231 | v = VOLUME_SPEAKER_MAX; |
Pokitto | 43:6183b12dd99c | 232 | } |
Pokitto | 43:6183b12dd99c | 233 | volumeMax = v; |
Pokitto | 43:6183b12dd99c | 234 | setVolume(globalVolume); |
Pokitto | 43:6183b12dd99c | 235 | } |
Pokitto | 43:6183b12dd99c | 236 | |
Pokitto | 43:6183b12dd99c | 237 | uint16_t Sound::getMaxVol() { |
Pokitto | 43:6183b12dd99c | 238 | return volumeMax; |
Pokitto | 43:6183b12dd99c | 239 | } |
Pokitto | 43:6183b12dd99c | 240 | |
Pokitto | 43:6183b12dd99c | 241 | void Sound::updateStream() { |
Pokitto | 43:6183b12dd99c | 242 | #ifndef NOPETITFATFS |
Pokitto | 43:6183b12dd99c | 243 | #if POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 244 | if (oldBuffer != currentBuffer) { |
Pokitto | 43:6183b12dd99c | 245 | if (currentBuffer==0) fileReadBytes(&buffers[3][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 246 | else if (currentBuffer==1) fileReadBytes(&buffers[0][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 247 | else if (currentBuffer==2) fileReadBytes(&buffers[1][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 248 | else fileReadBytes(&buffers[2][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 249 | oldBuffer = currentBuffer; |
Pokitto | 43:6183b12dd99c | 250 | streamcounter += BUFFER_SIZE; |
Pokitto | 43:6183b12dd99c | 251 | } |
Pokitto | 43:6183b12dd99c | 252 | |
Pokitto | 43:6183b12dd99c | 253 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 254 | if ( streamcounter > fs.fsize - (BUFFER_SIZE)) { |
Pokitto | 43:6183b12dd99c | 255 | #else |
Pokitto | 43:6183b12dd99c | 256 | if ( streamcounter > getFileLength() - (BUFFER_SIZE)) { |
Pokitto | 43:6183b12dd99c | 257 | #endif |
Pokitto | 43:6183b12dd99c | 258 | streamcounter=0; |
Pokitto | 43:6183b12dd99c | 259 | #if POK_STREAM_LOOP > 0 |
Pokitto | 43:6183b12dd99c | 260 | fileRewind(); |
Pokitto | 43:6183b12dd99c | 261 | #else |
Pokitto | 43:6183b12dd99c | 262 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 263 | streamon=0; |
Pokitto | 43:6183b12dd99c | 264 | #endif // POK_SIM |
Pokitto | 43:6183b12dd99c | 265 | #endif // POK_STREAM_LOOP |
Pokitto | 43:6183b12dd99c | 266 | } |
Pokitto | 43:6183b12dd99c | 267 | #endif |
Pokitto | 43:6183b12dd99c | 268 | #endif // NOPETITFATFS |
Pokitto | 43:6183b12dd99c | 269 | } |
Pokitto | 43:6183b12dd99c | 270 | |
Pokitto | 43:6183b12dd99c | 271 | void Sound::begin() { |
Pokitto | 43:6183b12dd99c | 272 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 273 | soundInit(); |
Pokitto | 43:6183b12dd99c | 274 | ampEnable(true); |
Pokitto | 43:6183b12dd99c | 275 | #endif |
Pokitto | 43:6183b12dd99c | 276 | #if (NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 277 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 278 | #if POK_GBSOUND > 0 |
Pokitto | 43:6183b12dd99c | 279 | prescaler = 1; |
Pokitto | 43:6183b12dd99c | 280 | for(byte i=0; i<NUM_CHANNELS; i++){ |
Pokitto | 43:6183b12dd99c | 281 | chanVolumes[i] = VOLUME_CHANNEL_MAX; |
Pokitto | 43:6183b12dd99c | 282 | changeInstrumentSet(defaultInstruments, i); //load default instruments. #0:square wave, #1: noise |
Pokitto | 43:6183b12dd99c | 283 | command(CMD_INSTRUMENT, 0, 0, i); //set the default instrument to square wave |
Pokitto | 43:6183b12dd99c | 284 | } |
Pokitto | 43:6183b12dd99c | 285 | #endif // POK_GBSOUND |
Pokitto | 43:6183b12dd99c | 286 | #endif //POK_ENABLE_SOUND |
Pokitto | 43:6183b12dd99c | 287 | #endif |
Pokitto | 43:6183b12dd99c | 288 | } |
Pokitto | 43:6183b12dd99c | 289 | |
Pokitto | 43:6183b12dd99c | 290 | void Sound::playTrack(const uint16_t* track, uint8_t channel){ |
Pokitto | 43:6183b12dd99c | 291 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 292 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 293 | return; |
Pokitto | 43:6183b12dd99c | 294 | stopTrack(channel); |
Pokitto | 43:6183b12dd99c | 295 | trackCursor[channel] = 0; |
Pokitto | 43:6183b12dd99c | 296 | trackData[channel] = (uint16_t*)track; |
Pokitto | 43:6183b12dd99c | 297 | trackIsPlaying[channel] = true; |
Pokitto | 43:6183b12dd99c | 298 | #endif |
Pokitto | 43:6183b12dd99c | 299 | } |
Pokitto | 43:6183b12dd99c | 300 | |
Pokitto | 43:6183b12dd99c | 301 | void Sound::stopTrack(uint8_t channel){ |
Pokitto | 43:6183b12dd99c | 302 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 303 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 304 | return; |
Pokitto | 43:6183b12dd99c | 305 | trackIsPlaying[channel] = false; |
Pokitto | 43:6183b12dd99c | 306 | stopPattern(channel); |
Pokitto | 43:6183b12dd99c | 307 | #endif |
Pokitto | 43:6183b12dd99c | 308 | } |
Pokitto | 43:6183b12dd99c | 309 | |
Pokitto | 43:6183b12dd99c | 310 | void Sound::stopTrack(){ |
Pokitto | 43:6183b12dd99c | 311 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 312 | for(uint8_t i=0; i<NUM_CHANNELS; i++){ |
Pokitto | 43:6183b12dd99c | 313 | stopTrack(i); |
Pokitto | 43:6183b12dd99c | 314 | } |
Pokitto | 43:6183b12dd99c | 315 | #endif |
Pokitto | 43:6183b12dd99c | 316 | } |
Pokitto | 43:6183b12dd99c | 317 | |
Pokitto | 43:6183b12dd99c | 318 | void Sound::updateTrack(uint8_t channel){ |
Pokitto | 43:6183b12dd99c | 319 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 320 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 321 | return; |
Pokitto | 43:6183b12dd99c | 322 | if(trackIsPlaying[channel] && !patternIsPlaying[channel]){ |
Pokitto | 43:6183b12dd99c | 323 | uint16_t data = pgm_read_word(trackData[channel] + trackCursor[channel]); |
Pokitto | 43:6183b12dd99c | 324 | if(data == 0xFFFF){ //en of the track |
Pokitto | 43:6183b12dd99c | 325 | trackIsPlaying[channel] = false; |
Pokitto | 43:6183b12dd99c | 326 | return; |
Pokitto | 43:6183b12dd99c | 327 | } |
Pokitto | 43:6183b12dd99c | 328 | uint8_t patternID = data & 0xFF; |
Pokitto | 43:6183b12dd99c | 329 | data >>= 8; |
Pokitto | 43:6183b12dd99c | 330 | patternPitch[channel] = data; |
Pokitto | 43:6183b12dd99c | 331 | playPattern((const uint16_t*)pgm_read_word(&(patternSet[channel][patternID])), channel); |
Pokitto | 43:6183b12dd99c | 332 | trackCursor[channel] ++; |
Pokitto | 43:6183b12dd99c | 333 | } |
Pokitto | 43:6183b12dd99c | 334 | #endif |
Pokitto | 43:6183b12dd99c | 335 | } |
Pokitto | 43:6183b12dd99c | 336 | |
Pokitto | 43:6183b12dd99c | 337 | void Sound::updateTrack(){ |
Pokitto | 43:6183b12dd99c | 338 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 339 | for (uint8_t i=0; i<NUM_CHANNELS; i++){ |
Pokitto | 43:6183b12dd99c | 340 | updateTrack(i); |
Pokitto | 43:6183b12dd99c | 341 | } |
Pokitto | 43:6183b12dd99c | 342 | #endif |
Pokitto | 43:6183b12dd99c | 343 | } |
Pokitto | 43:6183b12dd99c | 344 | |
Pokitto | 43:6183b12dd99c | 345 | void Sound::changePatternSet(const uint16_t* const* patterns, uint8_t channel){ |
Pokitto | 43:6183b12dd99c | 346 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 347 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 348 | return; |
Pokitto | 43:6183b12dd99c | 349 | patternSet[channel] = (uint16_t**)patterns; |
Pokitto | 43:6183b12dd99c | 350 | #endif |
Pokitto | 43:6183b12dd99c | 351 | } |
Pokitto | 43:6183b12dd99c | 352 | |
Pokitto | 43:6183b12dd99c | 353 | void Sound::playPattern(const uint16_t* pattern, uint8_t channel){ |
Pokitto | 43:6183b12dd99c | 354 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 355 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 356 | return; |
Pokitto | 43:6183b12dd99c | 357 | stopPattern(channel); |
Pokitto | 43:6183b12dd99c | 358 | patternData[channel] = (uint16_t*)pattern; |
Pokitto | 43:6183b12dd99c | 359 | patternCursor[channel] = 0; |
Pokitto | 43:6183b12dd99c | 360 | patternIsPlaying[channel] = true; |
Pokitto | 43:6183b12dd99c | 361 | noteVolume[channel] = 9; |
Pokitto | 43:6183b12dd99c | 362 | //reinit commands |
Pokitto | 43:6183b12dd99c | 363 | volumeSlideStepDuration[channel] = 0; |
Pokitto | 43:6183b12dd99c | 364 | arpeggioStepDuration[channel] = 0; |
Pokitto | 43:6183b12dd99c | 365 | tremoloStepDuration[channel] = 0; |
Pokitto | 43:6183b12dd99c | 366 | #endif |
Pokitto | 43:6183b12dd99c | 367 | } |
Pokitto | 43:6183b12dd99c | 368 | |
Pokitto | 43:6183b12dd99c | 369 | void Sound::updatePattern(){ |
Pokitto | 43:6183b12dd99c | 370 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 371 | for (uint8_t i=0; i<NUM_CHANNELS; i++){ |
Pokitto | 43:6183b12dd99c | 372 | updatePattern(i); |
Pokitto | 43:6183b12dd99c | 373 | } |
Pokitto | 43:6183b12dd99c | 374 | #endif |
Pokitto | 43:6183b12dd99c | 375 | } |
Pokitto | 43:6183b12dd99c | 376 | |
Pokitto | 43:6183b12dd99c | 377 | void Sound::changeInstrumentSet(const uint16_t* const* instruments, uint8_t channel){ |
Pokitto | 43:6183b12dd99c | 378 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 379 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 380 | return; |
Pokitto | 43:6183b12dd99c | 381 | instrumentSet[channel] = (uint16_t**)instruments; |
Pokitto | 43:6183b12dd99c | 382 | #endif |
Pokitto | 43:6183b12dd99c | 383 | } |
Pokitto | 43:6183b12dd99c | 384 | |
Pokitto | 43:6183b12dd99c | 385 | void Sound::updatePattern(uint8_t i){ |
Pokitto | 43:6183b12dd99c | 386 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 387 | if(i>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 388 | return; |
Pokitto | 43:6183b12dd99c | 389 | if(patternIsPlaying[i]){ |
Pokitto | 43:6183b12dd99c | 390 | if(noteDuration[i]==0){//if the end of the previous note is reached |
Pokitto | 43:6183b12dd99c | 391 | |
Pokitto | 43:6183b12dd99c | 392 | uint16_t data = pgm_read_word(patternCursor[i] + patternData[i]); |
Pokitto | 43:6183b12dd99c | 393 | |
Pokitto | 43:6183b12dd99c | 394 | if(data == 0){ //end of the pattern reached |
Pokitto | 43:6183b12dd99c | 395 | if(patternLooping[i] == true){ |
Pokitto | 43:6183b12dd99c | 396 | patternCursor[i] = 0; |
Pokitto | 43:6183b12dd99c | 397 | data = pgm_read_word(patternCursor[i] + patternData[i]); |
Pokitto | 43:6183b12dd99c | 398 | } |
Pokitto | 43:6183b12dd99c | 399 | else{ |
Pokitto | 43:6183b12dd99c | 400 | patternIsPlaying[i] = false; |
Pokitto | 43:6183b12dd99c | 401 | if(trackIsPlaying[i]){ //if this pattern is part of a track, get the next pattern |
Pokitto | 43:6183b12dd99c | 402 | updateTrack(i); |
Pokitto | 43:6183b12dd99c | 403 | data = pgm_read_word(patternCursor[i] + patternData[i]); |
Pokitto | 43:6183b12dd99c | 404 | } else { |
Pokitto | 43:6183b12dd99c | 405 | stopNote(i); |
Pokitto | 43:6183b12dd99c | 406 | //Serial.print("pattern end\n"); |
Pokitto | 43:6183b12dd99c | 407 | return; |
Pokitto | 43:6183b12dd99c | 408 | } |
Pokitto | 43:6183b12dd99c | 409 | } |
Pokitto | 43:6183b12dd99c | 410 | } |
Pokitto | 43:6183b12dd99c | 411 | |
Pokitto | 43:6183b12dd99c | 412 | while (data & 0x0001){ //read all commands and instrument changes |
Pokitto | 43:6183b12dd99c | 413 | data >>= 2; |
Pokitto | 43:6183b12dd99c | 414 | //Serial.print("\ncmd\t"); |
Pokitto | 43:6183b12dd99c | 415 | uint8_t cmd = data & 0x0F; |
Pokitto | 43:6183b12dd99c | 416 | data >>= 4; |
Pokitto | 43:6183b12dd99c | 417 | uint8_t X = data & 0x1F; |
Pokitto | 43:6183b12dd99c | 418 | data >>= 5; |
Pokitto | 43:6183b12dd99c | 419 | int8_t Y = data - 16; |
Pokitto | 43:6183b12dd99c | 420 | command(cmd,X,Y,i); |
Pokitto | 43:6183b12dd99c | 421 | patternCursor[i]++; |
Pokitto | 43:6183b12dd99c | 422 | data = pgm_read_word(patternCursor[i] + patternData[i]); |
Pokitto | 43:6183b12dd99c | 423 | } |
Pokitto | 43:6183b12dd99c | 424 | data >>= 2; |
Pokitto | 43:6183b12dd99c | 425 | |
Pokitto | 43:6183b12dd99c | 426 | uint8_t pitch = data & 0x003F; |
Pokitto | 43:6183b12dd99c | 427 | data >>= 6; |
Pokitto | 43:6183b12dd99c | 428 | |
Pokitto | 43:6183b12dd99c | 429 | uint8_t duration = data; |
Pokitto | 43:6183b12dd99c | 430 | if(pitch != 63){ |
Pokitto | 43:6183b12dd99c | 431 | } |
Pokitto | 43:6183b12dd99c | 432 | |
Pokitto | 43:6183b12dd99c | 433 | playNote(pitch, duration, i); |
Pokitto | 43:6183b12dd99c | 434 | |
Pokitto | 43:6183b12dd99c | 435 | patternCursor[i]++; |
Pokitto | 43:6183b12dd99c | 436 | } |
Pokitto | 43:6183b12dd99c | 437 | } |
Pokitto | 43:6183b12dd99c | 438 | #endif |
Pokitto | 43:6183b12dd99c | 439 | } |
Pokitto | 43:6183b12dd99c | 440 | |
Pokitto | 43:6183b12dd99c | 441 | void Sound::stopPattern(uint8_t channel){ |
Pokitto | 43:6183b12dd99c | 442 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 443 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 444 | return; |
Pokitto | 43:6183b12dd99c | 445 | stopNote(channel); |
Pokitto | 43:6183b12dd99c | 446 | patternIsPlaying[channel] = false; |
Pokitto | 43:6183b12dd99c | 447 | #endif |
Pokitto | 43:6183b12dd99c | 448 | } |
Pokitto | 43:6183b12dd99c | 449 | |
Pokitto | 43:6183b12dd99c | 450 | void Sound::stopPattern(){ |
Pokitto | 43:6183b12dd99c | 451 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 452 | for(uint8_t i=0; i<NUM_CHANNELS; i++){ |
Pokitto | 43:6183b12dd99c | 453 | stopPattern(i); |
Pokitto | 43:6183b12dd99c | 454 | } |
Pokitto | 43:6183b12dd99c | 455 | #endif |
Pokitto | 43:6183b12dd99c | 456 | } |
Pokitto | 43:6183b12dd99c | 457 | |
Pokitto | 43:6183b12dd99c | 458 | void Sound::command(uint8_t cmd, uint8_t X, int8_t Y, uint8_t i){ |
Pokitto | 43:6183b12dd99c | 459 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 460 | if(i>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 461 | return; |
Pokitto | 43:6183b12dd99c | 462 | switch(cmd){ |
Pokitto | 43:6183b12dd99c | 463 | case CMD_VOLUME: //volume |
Pokitto | 43:6183b12dd99c | 464 | X = constrain((int8_t)X, 0, 10); |
Pokitto | 43:6183b12dd99c | 465 | noteVolume[i] = X; |
Pokitto | 43:6183b12dd99c | 466 | break; |
Pokitto | 43:6183b12dd99c | 467 | case CMD_INSTRUMENT: //instrument |
Pokitto | 43:6183b12dd99c | 468 | instrumentData[i] = (uint16_t*)pgm_read_word(&(instrumentSet[i][X])); |
Pokitto | 43:6183b12dd99c | 469 | instrumentLength[i] = pgm_read_word(&(instrumentData[i][0])) & 0x00FF; //8 LSB |
Pokitto | 43:6183b12dd99c | 470 | instrumentLength[i] *= prescaler; |
Pokitto | 43:6183b12dd99c | 471 | instrumentLooping[i] = min2((pgm_read_word(&(instrumentData[i][0])) >> 8), instrumentLength[i]); //8 MSB - check that the loop is shorter than the instrument length |
Pokitto | 43:6183b12dd99c | 472 | instrumentLooping[i] *= prescaler; |
Pokitto | 43:6183b12dd99c | 473 | break; |
Pokitto | 43:6183b12dd99c | 474 | case CMD_SLIDE: //volume slide |
Pokitto | 43:6183b12dd99c | 475 | volumeSlideStepDuration[i] = X * prescaler; |
Pokitto | 43:6183b12dd99c | 476 | volumeSlideStepSize[i] = Y; |
Pokitto | 43:6183b12dd99c | 477 | break; |
Pokitto | 43:6183b12dd99c | 478 | case CMD_ARPEGGIO: |
Pokitto | 43:6183b12dd99c | 479 | arpeggioStepDuration[i] = X * prescaler; |
Pokitto | 43:6183b12dd99c | 480 | arpeggioStepSize[i] = Y; |
Pokitto | 43:6183b12dd99c | 481 | break; |
Pokitto | 43:6183b12dd99c | 482 | case CMD_TREMOLO: |
Pokitto | 43:6183b12dd99c | 483 | tremoloStepDuration[i] = X * prescaler; |
Pokitto | 43:6183b12dd99c | 484 | tremoloStepSize[i] = Y; |
Pokitto | 43:6183b12dd99c | 485 | break; |
Pokitto | 43:6183b12dd99c | 486 | default: |
Pokitto | 43:6183b12dd99c | 487 | break; |
Pokitto | 43:6183b12dd99c | 488 | } |
Pokitto | 43:6183b12dd99c | 489 | #endif |
Pokitto | 43:6183b12dd99c | 490 | } |
Pokitto | 43:6183b12dd99c | 491 | |
Pokitto | 43:6183b12dd99c | 492 | void Sound::playNote(uint8_t pitch, uint8_t duration, uint8_t channel){ |
Pokitto | 43:6183b12dd99c | 493 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 494 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 495 | return; |
Pokitto | 43:6183b12dd99c | 496 | //set note |
Pokitto | 43:6183b12dd99c | 497 | notePitch[channel] = pitch; |
Pokitto | 43:6183b12dd99c | 498 | noteDuration[channel] = duration * prescaler; |
Pokitto | 43:6183b12dd99c | 499 | //reinit vars |
Pokitto | 43:6183b12dd99c | 500 | instrumentNextChange[channel] = 0; |
Pokitto | 43:6183b12dd99c | 501 | instrumentCursor[channel] = 0; |
Pokitto | 43:6183b12dd99c | 502 | notePlaying[channel] = true; |
Pokitto | 43:6183b12dd99c | 503 | _chanState[channel] = true; |
Pokitto | 43:6183b12dd99c | 504 | commandsCounter[channel] = 0; |
Pokitto | 43:6183b12dd99c | 505 | #endif |
Pokitto | 43:6183b12dd99c | 506 | } |
Pokitto | 43:6183b12dd99c | 507 | |
Pokitto | 43:6183b12dd99c | 508 | void Sound::stopNote(uint8_t channel) { |
Pokitto | 43:6183b12dd99c | 509 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 510 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 511 | return; |
Pokitto | 43:6183b12dd99c | 512 | notePlaying[channel] = false; |
Pokitto | 43:6183b12dd99c | 513 | //counters |
Pokitto | 43:6183b12dd99c | 514 | noteDuration[channel] = 0; |
Pokitto | 43:6183b12dd99c | 515 | instrumentCursor[channel] = 0; |
Pokitto | 43:6183b12dd99c | 516 | commandsCounter[channel] = 0; |
Pokitto | 43:6183b12dd99c | 517 | //output |
Pokitto | 43:6183b12dd99c | 518 | _chanOutput[channel] = 0; |
Pokitto | 43:6183b12dd99c | 519 | _chanOutputVolume[channel] = 0; |
Pokitto | 43:6183b12dd99c | 520 | _chanState[channel] = false; |
Pokitto | 43:6183b12dd99c | 521 | updateOutput(); |
Pokitto | 43:6183b12dd99c | 522 | #endif |
Pokitto | 43:6183b12dd99c | 523 | } |
Pokitto | 43:6183b12dd99c | 524 | |
Pokitto | 43:6183b12dd99c | 525 | void Sound::stopNote() { |
Pokitto | 43:6183b12dd99c | 526 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 527 | for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) { |
Pokitto | 43:6183b12dd99c | 528 | stopNote(channel); |
Pokitto | 43:6183b12dd99c | 529 | } |
Pokitto | 43:6183b12dd99c | 530 | #endif |
Pokitto | 43:6183b12dd99c | 531 | } |
Pokitto | 43:6183b12dd99c | 532 | |
Pokitto | 43:6183b12dd99c | 533 | void Sound::updateNote() { |
Pokitto | 43:6183b12dd99c | 534 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 535 | for (uint8_t i = 0; i < NUM_CHANNELS; i++) { |
Pokitto | 43:6183b12dd99c | 536 | updateNote(i); |
Pokitto | 43:6183b12dd99c | 537 | } |
Pokitto | 43:6183b12dd99c | 538 | #endif |
Pokitto | 43:6183b12dd99c | 539 | } |
Pokitto | 43:6183b12dd99c | 540 | |
Pokitto | 43:6183b12dd99c | 541 | void Sound::updateNote(uint8_t i) { |
Pokitto | 43:6183b12dd99c | 542 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 543 | if(i>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 544 | return; |
Pokitto | 43:6183b12dd99c | 545 | if (notePlaying[i]) { |
Pokitto | 43:6183b12dd99c | 546 | |
Pokitto | 43:6183b12dd99c | 547 | if(noteDuration[i] == 0){ |
Pokitto | 43:6183b12dd99c | 548 | stopNote(i); |
Pokitto | 43:6183b12dd99c | 549 | //Serial.println("note end"); |
Pokitto | 43:6183b12dd99c | 550 | return; |
Pokitto | 43:6183b12dd99c | 551 | } else { |
Pokitto | 43:6183b12dd99c | 552 | noteDuration[i]--; |
Pokitto | 43:6183b12dd99c | 553 | } |
Pokitto | 43:6183b12dd99c | 554 | |
Pokitto | 43:6183b12dd99c | 555 | if (instrumentNextChange[i] == 0) { |
Pokitto | 43:6183b12dd99c | 556 | |
Pokitto | 43:6183b12dd99c | 557 | //read the step data from the progmem and decode it |
Pokitto | 43:6183b12dd99c | 558 | uint16_t thisStep = pgm_read_word(&(instrumentData[i][1 + instrumentCursor[i]])); |
Pokitto | 43:6183b12dd99c | 559 | |
Pokitto | 43:6183b12dd99c | 560 | stepVolume[i] = thisStep & 0x0007; |
Pokitto | 43:6183b12dd99c | 561 | thisStep >>= 3; |
Pokitto | 43:6183b12dd99c | 562 | |
Pokitto | 43:6183b12dd99c | 563 | uint8_t stepNoise = thisStep & 0x0001; |
Pokitto | 43:6183b12dd99c | 564 | thisStep >>= 1; |
Pokitto | 43:6183b12dd99c | 565 | |
Pokitto | 43:6183b12dd99c | 566 | uint8_t stepDuration = thisStep & 0x003F; |
Pokitto | 43:6183b12dd99c | 567 | thisStep >>= 6; |
Pokitto | 43:6183b12dd99c | 568 | |
Pokitto | 43:6183b12dd99c | 569 | stepPitch[i] = thisStep; |
Pokitto | 43:6183b12dd99c | 570 | |
Pokitto | 43:6183b12dd99c | 571 | //apply the step settings |
Pokitto | 43:6183b12dd99c | 572 | instrumentNextChange[i] = stepDuration * prescaler; |
Pokitto | 43:6183b12dd99c | 573 | |
Pokitto | 43:6183b12dd99c | 574 | _chanNoise[i] = stepNoise; |
Pokitto | 43:6183b12dd99c | 575 | |
Pokitto | 43:6183b12dd99c | 576 | |
Pokitto | 43:6183b12dd99c | 577 | instrumentCursor[i]++; |
Pokitto | 43:6183b12dd99c | 578 | |
Pokitto | 43:6183b12dd99c | 579 | if (instrumentCursor[i] >= instrumentLength[i]) { |
Pokitto | 43:6183b12dd99c | 580 | if (instrumentLooping[i]) { |
Pokitto | 43:6183b12dd99c | 581 | instrumentCursor[i] = instrumentLength[i] - instrumentLooping[i]; |
Pokitto | 43:6183b12dd99c | 582 | } else { |
Pokitto | 43:6183b12dd99c | 583 | stopNote(i); |
Pokitto | 43:6183b12dd99c | 584 | } |
Pokitto | 43:6183b12dd99c | 585 | } |
Pokitto | 43:6183b12dd99c | 586 | } |
Pokitto | 43:6183b12dd99c | 587 | instrumentNextChange[i]--; |
Pokitto | 43:6183b12dd99c | 588 | |
Pokitto | 43:6183b12dd99c | 589 | commandsCounter[i]++; |
Pokitto | 43:6183b12dd99c | 590 | |
Pokitto | 43:6183b12dd99c | 591 | //UPDATE VALUES |
Pokitto | 43:6183b12dd99c | 592 | //pitch |
Pokitto | 43:6183b12dd99c | 593 | outputPitch[i] = notePitch[i] + stepPitch[i] + patternPitch[i]; |
Pokitto | 43:6183b12dd99c | 594 | if(arpeggioStepDuration[i]){ |
Pokitto | 43:6183b12dd99c | 595 | outputPitch[i] += commandsCounter[i] / arpeggioStepDuration[i] * arpeggioStepSize[i]; |
Pokitto | 43:6183b12dd99c | 596 | } |
Pokitto | 43:6183b12dd99c | 597 | outputPitch[i] = (outputPitch[i] + NUM_PITCH) % NUM_PITCH; //wrap |
Pokitto | 43:6183b12dd99c | 598 | //volume |
Pokitto | 43:6183b12dd99c | 599 | outputVolume[i] = noteVolume[i]; |
Pokitto | 43:6183b12dd99c | 600 | if(volumeSlideStepDuration[i]){ |
Pokitto | 43:6183b12dd99c | 601 | outputVolume[i] += commandsCounter[i] / volumeSlideStepDuration[i] * volumeSlideStepSize[i]; |
Pokitto | 43:6183b12dd99c | 602 | } |
Pokitto | 43:6183b12dd99c | 603 | if(tremoloStepDuration[i]){ |
Pokitto | 43:6183b12dd99c | 604 | outputVolume[i] += ((commandsCounter[i]/tremoloStepDuration[i]) % 2) * tremoloStepSize[i]; |
Pokitto | 43:6183b12dd99c | 605 | } |
Pokitto | 43:6183b12dd99c | 606 | outputVolume[i] = constrain(outputVolume[i], 0, 9); |
Pokitto | 43:6183b12dd99c | 607 | if(notePitch[i] == 63){ |
Pokitto | 43:6183b12dd99c | 608 | outputVolume[i] = 0; |
Pokitto | 43:6183b12dd99c | 609 | } |
Pokitto | 43:6183b12dd99c | 610 | // jonnehw noInterrupts(); |
Pokitto | 43:6183b12dd99c | 611 | _chanHalfPeriod[i] = pgm_read_byte(_halfPeriods + outputPitch[i]); |
Pokitto | 43:6183b12dd99c | 612 | _chanOutput[i] = _chanOutputVolume[i] = outputVolume[i] * (globalVolume>>GLOBVOL_SHIFT) * chanVolumes[i] * stepVolume[i]; |
Pokitto | 43:6183b12dd99c | 613 | //Serial.println(outputVolume[i]); |
Pokitto | 43:6183b12dd99c | 614 | // jonnehw interrupts(); |
Pokitto | 43:6183b12dd99c | 615 | } |
Pokitto | 43:6183b12dd99c | 616 | #endif |
Pokitto | 43:6183b12dd99c | 617 | } |
Pokitto | 43:6183b12dd99c | 618 | |
Pokitto | 43:6183b12dd99c | 619 | void Sound::setChannelHalfPeriod(uint8_t channel, uint8_t halfPeriod) { |
Pokitto | 43:6183b12dd99c | 620 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 621 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 622 | return; |
Pokitto | 43:6183b12dd99c | 623 | _chanHalfPeriod[channel] = halfPeriod; |
Pokitto | 43:6183b12dd99c | 624 | _chanState[channel] = false; |
Pokitto | 43:6183b12dd99c | 625 | _chanCount[channel] = 0; |
Pokitto | 43:6183b12dd99c | 626 | updateOutput(); |
Pokitto | 43:6183b12dd99c | 627 | #endif |
Pokitto | 43:6183b12dd99c | 628 | } |
Pokitto | 43:6183b12dd99c | 629 | |
Pokitto | 43:6183b12dd99c | 630 | |
Pokitto | 43:6183b12dd99c | 631 | void Sound::generateOutput() { |
Pokitto | 43:6183b12dd99c | 632 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 633 | bool outputChanged = false; |
Pokitto | 43:6183b12dd99c | 634 | //no for loop here, for the performance sake (this function runs 15 000 times per second...) |
Pokitto | 43:6183b12dd99c | 635 | //CHANNEL 0 |
Pokitto | 43:6183b12dd99c | 636 | if (_chanOutputVolume[0]) { |
Pokitto | 43:6183b12dd99c | 637 | _chanCount[0]++; |
Pokitto | 43:6183b12dd99c | 638 | if (_chanCount[0] >= _chanHalfPeriod[0]) { |
Pokitto | 43:6183b12dd99c | 639 | outputChanged = true; |
Pokitto | 43:6183b12dd99c | 640 | _chanState[0] = !_chanState[0]; |
Pokitto | 43:6183b12dd99c | 641 | _chanCount[0] = 0; |
Pokitto | 43:6183b12dd99c | 642 | if (_chanNoise[0]) { |
Pokitto | 43:6183b12dd99c | 643 | _rand = 67 * _rand + 71; |
Pokitto | 43:6183b12dd99c | 644 | _chanOutput[0] = _rand % _chanOutputVolume[0]; |
Pokitto | 43:6183b12dd99c | 645 | } |
Pokitto | 43:6183b12dd99c | 646 | } |
Pokitto | 43:6183b12dd99c | 647 | } |
Pokitto | 43:6183b12dd99c | 648 | |
Pokitto | 43:6183b12dd99c | 649 | |
Pokitto | 43:6183b12dd99c | 650 | //CHANNEL 1 |
Pokitto | 43:6183b12dd99c | 651 | #if (NUM_CHANNELS > 1) |
Pokitto | 43:6183b12dd99c | 652 | if (_chanOutputVolume[1]) { |
Pokitto | 43:6183b12dd99c | 653 | _chanCount[1]++; |
Pokitto | 43:6183b12dd99c | 654 | if (_chanCount[1] >= _chanHalfPeriod[1]) { |
Pokitto | 43:6183b12dd99c | 655 | outputChanged = true; |
Pokitto | 43:6183b12dd99c | 656 | _chanState[1] = !_chanState[1]; |
Pokitto | 43:6183b12dd99c | 657 | _chanCount[1] = 0; |
Pokitto | 43:6183b12dd99c | 658 | if (_chanNoise[1]) { |
Pokitto | 43:6183b12dd99c | 659 | _rand = 67 * _rand + 71; |
Pokitto | 43:6183b12dd99c | 660 | _chanOutput[1] = _rand % _chanOutputVolume[1]; |
Pokitto | 43:6183b12dd99c | 661 | } |
Pokitto | 43:6183b12dd99c | 662 | } |
Pokitto | 43:6183b12dd99c | 663 | } |
Pokitto | 43:6183b12dd99c | 664 | #endif |
Pokitto | 43:6183b12dd99c | 665 | |
Pokitto | 43:6183b12dd99c | 666 | //CHANNEL 2 |
Pokitto | 43:6183b12dd99c | 667 | #if (NUM_CHANNELS > 2) |
Pokitto | 43:6183b12dd99c | 668 | if (_chanOutputVolume[2]) { |
Pokitto | 43:6183b12dd99c | 669 | _chanCount[2]++; |
Pokitto | 43:6183b12dd99c | 670 | if (_chanCount[2] >= _chanHalfPeriod[2]) { |
Pokitto | 43:6183b12dd99c | 671 | outputChanged = true; |
Pokitto | 43:6183b12dd99c | 672 | _chanState[2] = !_chanState[2]; |
Pokitto | 43:6183b12dd99c | 673 | _chanCount[2] = 0; |
Pokitto | 43:6183b12dd99c | 674 | if (_chanNoise[2]) { |
Pokitto | 43:6183b12dd99c | 675 | _rand = 67 * _rand + 71; |
Pokitto | 43:6183b12dd99c | 676 | _chanOutput[2] = _rand % _chanOutputVolume[2]; |
Pokitto | 43:6183b12dd99c | 677 | } |
Pokitto | 43:6183b12dd99c | 678 | } |
Pokitto | 43:6183b12dd99c | 679 | } |
Pokitto | 43:6183b12dd99c | 680 | #endif |
Pokitto | 43:6183b12dd99c | 681 | |
Pokitto | 43:6183b12dd99c | 682 | //CHANNEL 3 |
Pokitto | 43:6183b12dd99c | 683 | #if (NUM_CHANNELS > 3) |
Pokitto | 43:6183b12dd99c | 684 | if (_chanOutputVolume[3]) { |
Pokitto | 43:6183b12dd99c | 685 | _chanCount[3]++; |
Pokitto | 43:6183b12dd99c | 686 | if (_chanCount[3] >= _chanHalfPeriod[3]) { |
Pokitto | 43:6183b12dd99c | 687 | outputChanged = true; |
Pokitto | 43:6183b12dd99c | 688 | _chanState[3] = !_chanState[3]; |
Pokitto | 43:6183b12dd99c | 689 | _chanCount[3] = 0; |
Pokitto | 43:6183b12dd99c | 690 | if (_chanNoise[3]) { |
Pokitto | 43:6183b12dd99c | 691 | _rand = 67 * _rand + 71; |
Pokitto | 43:6183b12dd99c | 692 | _chanOutput[3] = _rand % _chanOutputVolume[3]; |
Pokitto | 43:6183b12dd99c | 693 | } |
Pokitto | 43:6183b12dd99c | 694 | } |
Pokitto | 43:6183b12dd99c | 695 | } |
Pokitto | 43:6183b12dd99c | 696 | #endif |
Pokitto | 43:6183b12dd99c | 697 | |
Pokitto | 43:6183b12dd99c | 698 | #if POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 699 | if (streamstep) { |
Pokitto | 43:6183b12dd99c | 700 | outputChanged=true; |
Pokitto | 43:6183b12dd99c | 701 | } |
Pokitto | 43:6183b12dd99c | 702 | #endif |
Pokitto | 43:6183b12dd99c | 703 | |
Pokitto | 43:6183b12dd99c | 704 | if (outputChanged) { |
Pokitto | 43:6183b12dd99c | 705 | updateOutput(); |
Pokitto | 43:6183b12dd99c | 706 | } |
Pokitto | 43:6183b12dd99c | 707 | #endif |
Pokitto | 43:6183b12dd99c | 708 | } |
Pokitto | 43:6183b12dd99c | 709 | |
Pokitto | 43:6183b12dd99c | 710 | void Sound::updateOutput() { |
Pokitto | 43:6183b12dd99c | 711 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 712 | uint32_t output = 0; |
Pokitto | 43:6183b12dd99c | 713 | |
Pokitto | 43:6183b12dd99c | 714 | //CHANNEL 0 |
Pokitto | 43:6183b12dd99c | 715 | if (_chanState[0]) { |
Pokitto | 43:6183b12dd99c | 716 | output += _chanOutput[0]; |
Pokitto | 43:6183b12dd99c | 717 | } |
Pokitto | 43:6183b12dd99c | 718 | |
Pokitto | 43:6183b12dd99c | 719 | //CHANNEL 1 |
Pokitto | 43:6183b12dd99c | 720 | #if (NUM_CHANNELS > 1) |
Pokitto | 43:6183b12dd99c | 721 | if (_chanState[1]) { |
Pokitto | 43:6183b12dd99c | 722 | output += _chanOutput[1]; |
Pokitto | 43:6183b12dd99c | 723 | } |
Pokitto | 43:6183b12dd99c | 724 | #endif |
Pokitto | 43:6183b12dd99c | 725 | |
Pokitto | 43:6183b12dd99c | 726 | //CHANNEL 2 |
Pokitto | 43:6183b12dd99c | 727 | #if (NUM_CHANNELS > 2) |
Pokitto | 43:6183b12dd99c | 728 | if (_chanState[2]) { |
Pokitto | 43:6183b12dd99c | 729 | output += _chanOutput[2]; |
Pokitto | 43:6183b12dd99c | 730 | } |
Pokitto | 43:6183b12dd99c | 731 | #endif |
Pokitto | 43:6183b12dd99c | 732 | |
Pokitto | 43:6183b12dd99c | 733 | //CHANNEL 3 |
Pokitto | 43:6183b12dd99c | 734 | #if (NUM_CHANNELS > 3) |
Pokitto | 43:6183b12dd99c | 735 | if (_chanState[3]) { |
Pokitto | 43:6183b12dd99c | 736 | output += _chanOutput[3]; |
Pokitto | 43:6183b12dd99c | 737 | } |
Pokitto | 43:6183b12dd99c | 738 | #endif |
Pokitto | 43:6183b12dd99c | 739 | |
Pokitto | 43:6183b12dd99c | 740 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 741 | #if POK_ENABLE_SOUND |
Pokitto | 43:6183b12dd99c | 742 | /** HARDWARE **/ |
Pokitto | 43:6183b12dd99c | 743 | |
Pokitto | 43:6183b12dd99c | 744 | #if POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 745 | if (streamstep) { |
Pokitto | 43:6183b12dd99c | 746 | //pwmout_write(&audiopwm,(float)(sbyte>>headPhoneLevel)/(float)255); |
Pokitto | 43:6183b12dd99c | 747 | sbyte *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 748 | sbyte >>= 8; |
Pokitto | 43:6183b12dd99c | 749 | pwmout_write(&audiopwm,(float)(sbyte)/(float)255); |
Pokitto | 43:6183b12dd99c | 750 | } |
Pokitto | 43:6183b12dd99c | 751 | #endif |
Pokitto | 43:6183b12dd99c | 752 | output *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 753 | output >>= 8; |
Pokitto | 43:6183b12dd99c | 754 | dac_write((uint8_t)output); //direct hardware mixing baby ! |
Pokitto | 43:6183b12dd99c | 755 | soundbyte = output; |
Pokitto | 43:6183b12dd99c | 756 | #endif //POK_ENABLE_SOUND |
Pokitto | 43:6183b12dd99c | 757 | #else |
Pokitto | 43:6183b12dd99c | 758 | /** SIMULATOR **/ |
Pokitto | 43:6183b12dd99c | 759 | #if POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 760 | if (streamstep) { |
Pokitto | 43:6183b12dd99c | 761 | uint16_t o = output + sbyte; |
Pokitto | 43:6183b12dd99c | 762 | output = (o/2);//>>headPhoneLevel; |
Pokitto | 43:6183b12dd99c | 763 | } |
Pokitto | 43:6183b12dd99c | 764 | #endif |
Pokitto | 43:6183b12dd99c | 765 | soundbyte = output;//<<headPhoneLevel; |
Pokitto | 43:6183b12dd99c | 766 | #endif // POK_SIM |
Pokitto | 43:6183b12dd99c | 767 | #endif |
Pokitto | 43:6183b12dd99c | 768 | } |
Pokitto | 43:6183b12dd99c | 769 | |
Pokitto | 43:6183b12dd99c | 770 | void Sound::setPatternLooping(bool loop, uint8_t channel) { |
Pokitto | 43:6183b12dd99c | 771 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 772 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 773 | return; |
Pokitto | 43:6183b12dd99c | 774 | patternLooping[channel] = loop; |
Pokitto | 43:6183b12dd99c | 775 | #endif |
Pokitto | 43:6183b12dd99c | 776 | } |
Pokitto | 43:6183b12dd99c | 777 | |
Pokitto | 43:6183b12dd99c | 778 | void Sound::playOK(){ |
Pokitto | 43:6183b12dd99c | 779 | #if POK_GBSOUND |
Pokitto | 43:6183b12dd99c | 780 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 781 | playPattern(playOKPattern,0); |
Pokitto | 43:6183b12dd99c | 782 | #endif |
Pokitto | 43:6183b12dd99c | 783 | #endif // POK_GBSOUND |
Pokitto | 43:6183b12dd99c | 784 | } |
Pokitto | 43:6183b12dd99c | 785 | |
Pokitto | 43:6183b12dd99c | 786 | void Sound::playCancel(){ |
Pokitto | 43:6183b12dd99c | 787 | #if POK_GBSOUND |
Pokitto | 43:6183b12dd99c | 788 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 789 | playPattern(playCancelPattern,0); |
Pokitto | 43:6183b12dd99c | 790 | #endif |
Pokitto | 43:6183b12dd99c | 791 | #endif |
Pokitto | 43:6183b12dd99c | 792 | } |
Pokitto | 43:6183b12dd99c | 793 | |
Pokitto | 43:6183b12dd99c | 794 | void Sound::playTick(){ |
Pokitto | 43:6183b12dd99c | 795 | #if POK_GBSOUND |
Pokitto | 43:6183b12dd99c | 796 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 797 | playPattern(playTickP,0); |
Pokitto | 43:6183b12dd99c | 798 | #endif |
Pokitto | 43:6183b12dd99c | 799 | #endif // POK_GBSOUND |
Pokitto | 43:6183b12dd99c | 800 | } |
Pokitto | 43:6183b12dd99c | 801 | |
Pokitto | 43:6183b12dd99c | 802 | void Sound::setVolume(int16_t volume) { |
Pokitto | 43:6183b12dd99c | 803 | //#if NUM_CHANNELS > 0 |
Pokitto | 43:6183b12dd99c | 804 | if (volume<0) volume = 0; |
Pokitto | 43:6183b12dd99c | 805 | //if (volume>volumeMax) volume = volumeMax; |
Pokitto | 43:6183b12dd99c | 806 | globalVolume = volume; |
Pokitto | 43:6183b12dd99c | 807 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 808 | discrete_vol = (volume>>5); |
Pokitto | 43:6183b12dd99c | 809 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 810 | setHWvolume(discrete_vol_hw_levels[discrete_vol]); //boost volume if headphonelevel |
Pokitto | 43:6183b12dd99c | 811 | #endif |
Pokitto | 43:6183b12dd99c | 812 | #endif |
Pokitto | 43:6183b12dd99c | 813 | #if POK_SHOW_VOLUME > 0 |
Pokitto | 43:6183b12dd99c | 814 | _soundc.volbar_visible = VOLUMEBAR_TIMEOUT; |
Pokitto | 43:6183b12dd99c | 815 | #endif |
Pokitto | 43:6183b12dd99c | 816 | //#endif |
Pokitto | 43:6183b12dd99c | 817 | } |
Pokitto | 43:6183b12dd99c | 818 | |
Pokitto | 43:6183b12dd99c | 819 | uint16_t Sound::getVolume() { |
Pokitto | 43:6183b12dd99c | 820 | //#if NUM_CHANNELS > 0 |
Pokitto | 43:6183b12dd99c | 821 | return globalVolume; |
Pokitto | 43:6183b12dd99c | 822 | //#else |
Pokitto | 43:6183b12dd99c | 823 | // return 0; |
Pokitto | 43:6183b12dd99c | 824 | //#endif |
Pokitto | 43:6183b12dd99c | 825 | } |
Pokitto | 43:6183b12dd99c | 826 | |
Pokitto | 43:6183b12dd99c | 827 | void Sound::setVolume(int8_t volume, uint8_t channel) { |
Pokitto | 43:6183b12dd99c | 828 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 829 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 830 | return; |
Pokitto | 43:6183b12dd99c | 831 | volume = (volume > VOLUME_CHANNEL_MAX) ? VOLUME_CHANNEL_MAX : volume; |
Pokitto | 43:6183b12dd99c | 832 | volume = (volume < 0) ? 0 : volume; |
Pokitto | 43:6183b12dd99c | 833 | chanVolumes[channel] = volume; |
Pokitto | 43:6183b12dd99c | 834 | #endif |
Pokitto | 43:6183b12dd99c | 835 | } |
Pokitto | 43:6183b12dd99c | 836 | |
Pokitto | 43:6183b12dd99c | 837 | uint8_t Sound::getVolume(uint8_t channel) { |
Pokitto | 43:6183b12dd99c | 838 | #if(NUM_CHANNELS > 0) |
Pokitto | 43:6183b12dd99c | 839 | if(channel>=NUM_CHANNELS) |
Pokitto | 43:6183b12dd99c | 840 | return 255; |
Pokitto | 43:6183b12dd99c | 841 | return (chanVolumes[channel]); |
Pokitto | 43:6183b12dd99c | 842 | #else |
Pokitto | 43:6183b12dd99c | 843 | return 0; |
Pokitto | 43:6183b12dd99c | 844 | #endif |
Pokitto | 43:6183b12dd99c | 845 | } |
Pokitto | 43:6183b12dd99c | 846 | |
Pokitto | 43:6183b12dd99c | 847 | void Sound::playTone(uint8_t os, int frq, uint8_t amp, uint8_t wav,uint8_t arpmode) |
Pokitto | 43:6183b12dd99c | 848 | { |
Pokitto | 43:6183b12dd99c | 849 | if (wav>5) wav=0; |
Pokitto | 43:6183b12dd99c | 850 | if (arpmode>MAX_ARPMODE) arpmode=MAX_ARPMODE; |
Pokitto | 43:6183b12dd99c | 851 | if (os==1) setOSC(&osc1,1,wav,1,0,0,frq,amp,0,0,0,0,0,0,arpmode,0,0); |
Pokitto | 43:6183b12dd99c | 852 | else if (os==2) setOSC(&osc2,1,wav,1,0,0,frq,amp,0,0,0,0,0,0,arpmode,0,0); |
Pokitto | 43:6183b12dd99c | 853 | else if (os==3) setOSC(&osc3,1,wav,1,0,0,frq,amp,0,0,0,0,0,0,arpmode,0,0); |
Pokitto | 43:6183b12dd99c | 854 | } |
Pokitto | 43:6183b12dd99c | 855 | |
Pokitto | 43:6183b12dd99c | 856 | void Sound::playTone(uint8_t os, uint16_t frq, uint8_t volume, uint32_t duration) |
Pokitto | 43:6183b12dd99c | 857 | { |
Pokitto | 43:6183b12dd99c | 858 | if (os==1) setOSC(&osc1,1,WSQUARE,frq,volume,duration); |
Pokitto | 43:6183b12dd99c | 859 | else if (os==2) setOSC(&osc2,1,WTRI,frq,volume,duration); |
Pokitto | 43:6183b12dd99c | 860 | else if (os==3) setOSC(&osc3,1,WTRI,frq,volume,duration); |
Pokitto | 43:6183b12dd99c | 861 | } |
Pokitto | 43:6183b12dd99c | 862 | |
Pokitto | 43:6183b12dd99c | 863 | uint8_t Sound::ampIsOn() |
Pokitto | 43:6183b12dd99c | 864 | { |
Pokitto | 43:6183b12dd99c | 865 | #ifdef POK_SIM |
Pokitto | 43:6183b12dd99c | 866 | return core.ampIsOn(); |
Pokitto | 43:6183b12dd99c | 867 | #else |
Pokitto | 43:6183b12dd99c | 868 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 869 | return Pokitto::ampIsOn(); |
Pokitto | 43:6183b12dd99c | 870 | #endif |
Pokitto | 43:6183b12dd99c | 871 | #endif // POK_SIM |
Pokitto | 43:6183b12dd99c | 872 | return 0; |
Pokitto | 43:6183b12dd99c | 873 | } |
Pokitto | 43:6183b12dd99c | 874 | |
Pokitto | 43:6183b12dd99c | 875 | void Sound::ampEnable(uint8_t v) { |
Pokitto | 43:6183b12dd99c | 876 | #ifdef POK_SIM |
Pokitto | 43:6183b12dd99c | 877 | core.ampEnable(v); |
Pokitto | 43:6183b12dd99c | 878 | #else |
Pokitto | 43:6183b12dd99c | 879 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 880 | Pokitto::ampEnable(v); |
Pokitto | 43:6183b12dd99c | 881 | #endif |
Pokitto | 43:6183b12dd99c | 882 | #endif // POK_SIM |
Pokitto | 43:6183b12dd99c | 883 | |
Pokitto | 43:6183b12dd99c | 884 | } |
Pokitto | 43:6183b12dd99c | 885 | |
Pokitto | 43:6183b12dd99c | 886 | int Sound::playMusicStream(char* filename) |
Pokitto | 43:6183b12dd99c | 887 | { |
Pokitto | 43:6183b12dd99c | 888 | return playMusicStream(filename,0); |
Pokitto | 43:6183b12dd99c | 889 | } |
Pokitto | 43:6183b12dd99c | 890 | |
Pokitto | 43:6183b12dd99c | 891 | int Sound::playMusicStream() |
Pokitto | 43:6183b12dd99c | 892 | { |
Pokitto | 43:6183b12dd99c | 893 | #if POK_STREAMING_MUSIC >0 |
Pokitto | 43:6183b12dd99c | 894 | if (currentPtr) { |
Pokitto | 43:6183b12dd99c | 895 | pokPlayStream(); |
Pokitto | 43:6183b12dd99c | 896 | return 1; |
Pokitto | 43:6183b12dd99c | 897 | } else return 0; //no stream |
Pokitto | 43:6183b12dd99c | 898 | #endif // POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 899 | } |
Pokitto | 43:6183b12dd99c | 900 | |
Pokitto | 43:6183b12dd99c | 901 | void Sound::pauseMusicStream() { |
Pokitto | 43:6183b12dd99c | 902 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 903 | pokPauseStream(); |
Pokitto | 43:6183b12dd99c | 904 | #endif |
Pokitto | 43:6183b12dd99c | 905 | } |
Pokitto | 43:6183b12dd99c | 906 | |
Pokitto | 43:6183b12dd99c | 907 | int Sound::playMusicStream(char* filename, uint8_t options) |
Pokitto | 43:6183b12dd99c | 908 | { |
Pokitto | 43:6183b12dd99c | 909 | #if POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 910 | uint8_t result; |
Pokitto | 43:6183b12dd99c | 911 | result = pokInitSD(); |
Pokitto | 43:6183b12dd99c | 912 | if (!isThisFileOpen(filename)) { |
Pokitto | 43:6183b12dd99c | 913 | fileClose(); // close any open files |
Pokitto | 43:6183b12dd99c | 914 | result = fileOpen(filename,FILE_MODE_OVERWRITE | FILE_MODE_BINARY); |
Pokitto | 43:6183b12dd99c | 915 | } |
Pokitto | 43:6183b12dd99c | 916 | |
Pokitto | 43:6183b12dd99c | 917 | if (result) { |
Pokitto | 43:6183b12dd99c | 918 | currentPtr = 0; // mark that no stream is available |
Pokitto | 43:6183b12dd99c | 919 | return 0; // opening music file failed |
Pokitto | 43:6183b12dd99c | 920 | } |
Pokitto | 43:6183b12dd99c | 921 | |
Pokitto | 43:6183b12dd99c | 922 | fileReadBytes(&buffers[0][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 923 | fileReadBytes(&buffers[1][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 924 | fileReadBytes(&buffers[2][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 925 | fileReadBytes(&buffers[3][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 926 | currentBuffer = 0; |
Pokitto | 43:6183b12dd99c | 927 | currentPtr = buffers[currentBuffer]; |
Pokitto | 43:6183b12dd99c | 928 | endPtr = currentPtr + BUFFER_SIZE; |
Pokitto | 43:6183b12dd99c | 929 | |
Pokitto | 43:6183b12dd99c | 930 | //streaming = STR_PLAYING|options; |
Pokitto | 43:6183b12dd99c | 931 | |
Pokitto | 43:6183b12dd99c | 932 | if (!options) pokPlayStream(); // activate stream |
Pokitto | 43:6183b12dd99c | 933 | #endif //POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 934 | return 1; // opening music file succeeded |
Pokitto | 43:6183b12dd99c | 935 | } |
Pokitto | 43:6183b12dd99c | 936 | |
Pokitto | 43:6183b12dd99c | 937 |