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

Committer:
Pokitto
Date:
Mon Sep 18 11:47:51 2017 +0000
Revision:
0:e8b8f36b4505
Child:
1:4b1511a0a2c2
Initial;

Who changed what in which revision?

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