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

Committer:
Pokitto
Date:
Mon May 21 18:08:52 2018 +0000
Revision:
47:8f962908f6a7
Parent:
46:e7e438368e16
Streamcounter fixed

Who changed what in which revision?

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