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

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

PokittoLib

Library for programming Pokitto hardware

How to Use

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Tue Jan 30 10:41:47 2018 +0000
Revision:
31:f4b9b85c7b62
Sound output improvements added:  louder, clearer, faster!

Who changed what in which revision?

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