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

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
46:e7e438368e16
added fix for directrectangle()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 46:e7e438368e16 1 /**************************************************************************/
Pokitto 46:e7e438368e16 2 /*!
Pokitto 46:e7e438368e16 3 @file PokittoSound.h
Pokitto 46:e7e438368e16 4 @author Jonne Valola
Pokitto 46:e7e438368e16 5
Pokitto 46:e7e438368e16 6 @section LICENSE
Pokitto 46:e7e438368e16 7
Pokitto 46:e7e438368e16 8 Software License Agreement (BSD License)
Pokitto 46:e7e438368e16 9
Pokitto 46:e7e438368e16 10 Copyright (c) 2016, Jonne Valola
Pokitto 46:e7e438368e16 11 All rights reserved.
Pokitto 46:e7e438368e16 12
Pokitto 46:e7e438368e16 13 Redistribution and use in source and binary forms, with or without
Pokitto 46:e7e438368e16 14 modification, are permitted provided that the following conditions are met:
Pokitto 46:e7e438368e16 15 1. Redistributions of source code must retain the above copyright
Pokitto 46:e7e438368e16 16 notice, this list of conditions and the following disclaimer.
Pokitto 46:e7e438368e16 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 46:e7e438368e16 18 notice, this list of conditions and the following disclaimer in the
Pokitto 46:e7e438368e16 19 documentation and/or other materials provided with the distribution.
Pokitto 46:e7e438368e16 20 3. Neither the name of the copyright holders nor the
Pokitto 46:e7e438368e16 21 names of its contributors may be used to endorse or promote products
Pokitto 46:e7e438368e16 22 derived from this software without specific prior written permission.
Pokitto 46:e7e438368e16 23
Pokitto 46:e7e438368e16 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 46:e7e438368e16 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 46:e7e438368e16 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 46:e7e438368e16 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 46:e7e438368e16 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 46:e7e438368e16 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 46:e7e438368e16 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 46:e7e438368e16 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 46:e7e438368e16 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 46:e7e438368e16 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 46:e7e438368e16 34 */
Pokitto 46:e7e438368e16 35 /**************************************************************************/
Pokitto 46:e7e438368e16 36
Pokitto 46:e7e438368e16 37
Pokitto 46:e7e438368e16 38 #ifndef POKITTOSOUND_H
Pokitto 46:e7e438368e16 39 #define POKITTOSOUND_H
Pokitto 46:e7e438368e16 40
Pokitto 46:e7e438368e16 41 #include <stdint.h>
Pokitto 46:e7e438368e16 42 #include "Pokitto_settings.h"
Pokitto 46:e7e438368e16 43 #include "GBcompatibility.h"
Pokitto 46:e7e438368e16 44 #include "PokittoFakeavr.h"
Pokitto 46:e7e438368e16 45 #include "PokittoGlobs.h"
Pokitto 46:e7e438368e16 46
Pokitto 46:e7e438368e16 47 extern void pokPauseStream();
Pokitto 46:e7e438368e16 48 extern void pokPlayStream();
Pokitto 46:e7e438368e16 49 extern uint8_t pokStreamPaused();
Pokitto 46:e7e438368e16 50
Pokitto 46:e7e438368e16 51 //volume levels
Pokitto 46:e7e438368e16 52 #define GLOBVOL_SHIFT 5 //shift global volume to allow for finer increments
Pokitto 46:e7e438368e16 53
Pokitto 46:e7e438368e16 54
Pokitto 46:e7e438368e16 55 #ifndef MAX_VOL_TEST
Pokitto 46:e7e438368e16 56 #define VOLUME_SPEAKER_MAX 255 //((8<<GLOBVOL_SHIFT)-1)
Pokitto 46:e7e438368e16 57 #define VOLUME_HEADPHONE_MAX 127
Pokitto 46:e7e438368e16 58 #define VOLUME_STARTUP VOLUME_HEADPHONE_MAX
Pokitto 46:e7e438368e16 59 #else
Pokitto 46:e7e438368e16 60 #define VOLUME_SPEAKER_MAX 255
Pokitto 46:e7e438368e16 61 #define VOLUME_HEADPHONE_MAX VOLUME_SPEAKER_MAX
Pokitto 46:e7e438368e16 62 #define VOLUME_STARTUP VOLUME_SPEAKER_MAX
Pokitto 46:e7e438368e16 63 #endif // MAXVOLTEST
Pokitto 46:e7e438368e16 64
Pokitto 46:e7e438368e16 65 #ifdef POK_SIM
Pokitto 46:e7e438368e16 66 #define VOLUME_STEP 1
Pokitto 46:e7e438368e16 67 #else
Pokitto 46:e7e438368e16 68 #define VOLUME_STEP 8
Pokitto 46:e7e438368e16 69 #endif
Pokitto 46:e7e438368e16 70
Pokitto 46:e7e438368e16 71
Pokitto 46:e7e438368e16 72 //commands
Pokitto 46:e7e438368e16 73 #define CMD_VOLUME 0
Pokitto 46:e7e438368e16 74 #define CMD_INSTRUMENT 1
Pokitto 46:e7e438368e16 75 #define CMD_SLIDE 2
Pokitto 46:e7e438368e16 76 #define CMD_ARPEGGIO 3
Pokitto 46:e7e438368e16 77 #define CMD_TREMOLO 4
Pokitto 46:e7e438368e16 78
Pokitto 46:e7e438368e16 79 #define STR_PLAYING 0x1
Pokitto 46:e7e438368e16 80 #define STR_VISUALIZER 0x2
Pokitto 46:e7e438368e16 81 #define STR_LOOP 0x4
Pokitto 46:e7e438368e16 82 #define STR_PAUSED 0x8
Pokitto 46:e7e438368e16 83
Pokitto 46:e7e438368e16 84 namespace Pokitto {
Pokitto 46:e7e438368e16 85
Pokitto 46:e7e438368e16 86 /** Sound class.
Pokitto 46:e7e438368e16 87 * The Sound class is an API-compatible version of the Gamebuino Sound API by Aurelien Rodot.
Pokitto 46:e7e438368e16 88 * Because it is a class consisting of only static members, there is only 1 instance running,
Pokitto 46:e7e438368e16 89 * no matter how many Sound classes are declared (see example below). This means sound can
Pokitto 46:e7e438368e16 90 * be used through a simple Sound class object or as a member of the Core class.
Pokitto 46:e7e438368e16 91 *
Pokitto 46:e7e438368e16 92 */
Pokitto 46:e7e438368e16 93
Pokitto 46:e7e438368e16 94 /** discrete_vol* are needed for more accurate volume control levels on hardware **/
Pokitto 46:e7e438368e16 95 extern uint8_t discrete_vol;
Pokitto 46:e7e438368e16 96 extern const uint8_t discrete_vol_levels[];
Pokitto 46:e7e438368e16 97 extern const uint8_t discrete_vol_hw_levels[];
Pokitto 46:e7e438368e16 98 extern const uint8_t discrete_vol_multipliers[];
Pokitto 46:e7e438368e16 99
Pokitto 46:e7e438368e16 100 extern void audio_IRQ(); // audio interrupt
Pokitto 46:e7e438368e16 101
Pokitto 46:e7e438368e16 102 class Sound {
Pokitto 46:e7e438368e16 103 private:
Pokitto 46:e7e438368e16 104 static uint16_t volumeMax;
Pokitto 46:e7e438368e16 105
Pokitto 46:e7e438368e16 106 public:
Pokitto 46:e7e438368e16 107 static void begin();
Pokitto 46:e7e438368e16 108
Pokitto 46:e7e438368e16 109 // Headphonemode
Pokitto 46:e7e438368e16 110 static uint8_t headPhoneLevel; // a workaround to disappearing sound at low volume
Pokitto 46:e7e438368e16 111 static void setMaxVol(int16_t);
Pokitto 46:e7e438368e16 112 static uint16_t getMaxVol();
Pokitto 46:e7e438368e16 113 static void volumeUp();
Pokitto 46:e7e438368e16 114 static void volumeDown();
Pokitto 46:e7e438368e16 115
Pokitto 46:e7e438368e16 116 // Original functions
Pokitto 46:e7e438368e16 117 static void updateStream();
Pokitto 46:e7e438368e16 118 static void playTone(uint8_t os, int frq, uint8_t amp, uint8_t wav,uint8_t arpmode);
Pokitto 46:e7e438368e16 119 static void playTone(uint8_t os, uint16_t freq, uint8_t volume, uint32_t duration);
Pokitto 46:e7e438368e16 120 static uint8_t ampIsOn();
Pokitto 46:e7e438368e16 121 static void ampEnable(uint8_t);
Pokitto 46:e7e438368e16 122 static int playMusicStream(char* filename, uint8_t options);
Pokitto 46:e7e438368e16 123 static int playMusicStream(char* filename);
Pokitto 46:e7e438368e16 124 static int playMusicStream();
Pokitto 46:e7e438368e16 125 static void pauseMusicStream();
Pokitto 46:e7e438368e16 126 static uint32_t getMusicStreamElapsedSec();
Pokitto 46:e7e438368e16 127 static uint32_t getMusicStreamElapsedMilliSec();
Pokitto 46:e7e438368e16 128
Pokitto 46:e7e438368e16 129 // GB compatibility functions
Pokitto 46:e7e438368e16 130 static void playTrack(const uint16_t* track, uint8_t channel);
Pokitto 46:e7e438368e16 131 static void updateTrack(uint8_t channel);
Pokitto 46:e7e438368e16 132 static void updateTrack();
Pokitto 46:e7e438368e16 133 static void stopTrack(uint8_t channel);
Pokitto 46:e7e438368e16 134 static void stopTrack();
Pokitto 46:e7e438368e16 135 static void changePatternSet(const uint16_t* const* patterns, uint8_t channel);
Pokitto 46:e7e438368e16 136 static bool trackIsPlaying[NUM_CHANNELS];
Pokitto 46:e7e438368e16 137
Pokitto 46:e7e438368e16 138 static void playPattern(const uint16_t* pattern, uint8_t channel);
Pokitto 46:e7e438368e16 139 static void changeInstrumentSet(const uint16_t* const* instruments, uint8_t channel);
Pokitto 46:e7e438368e16 140 static void updatePattern(uint8_t i);
Pokitto 46:e7e438368e16 141 static void updatePattern();
Pokitto 46:e7e438368e16 142 static void setPatternLooping(bool loop, uint8_t channel);
Pokitto 46:e7e438368e16 143 static void stopPattern(uint8_t channel);
Pokitto 46:e7e438368e16 144 static void stopPattern();
Pokitto 46:e7e438368e16 145 static bool patternIsPlaying[NUM_CHANNELS];
Pokitto 46:e7e438368e16 146
Pokitto 46:e7e438368e16 147 static void command(uint8_t cmd, uint8_t X, int8_t Y, uint8_t i);
Pokitto 46:e7e438368e16 148 static void playNote(uint8_t pitch, uint8_t duration, uint8_t channel);
Pokitto 46:e7e438368e16 149 static void updateNote();
Pokitto 46:e7e438368e16 150 static void updateNote(uint8_t i);
Pokitto 46:e7e438368e16 151 static void stopNote(uint8_t channel);
Pokitto 46:e7e438368e16 152 static void stopNote();
Pokitto 46:e7e438368e16 153
Pokitto 46:e7e438368e16 154 static uint8_t outputPitch[NUM_CHANNELS];
Pokitto 46:e7e438368e16 155 static int8_t outputVolume[NUM_CHANNELS];
Pokitto 46:e7e438368e16 156
Pokitto 46:e7e438368e16 157 static void setMasterVolume(uint8_t);
Pokitto 46:e7e438368e16 158 static uint8_t GetMasterVolume();
Pokitto 46:e7e438368e16 159 static void setVolume(int16_t volume);
Pokitto 46:e7e438368e16 160 static uint16_t getVolume();
Pokitto 46:e7e438368e16 161 static void setVolume(int8_t volume, uint8_t channel);
Pokitto 46:e7e438368e16 162 static uint8_t getVolume(uint8_t channel);
Pokitto 46:e7e438368e16 163
Pokitto 46:e7e438368e16 164 static void playOK();
Pokitto 46:e7e438368e16 165 static void playCancel();
Pokitto 46:e7e438368e16 166 static void playTick();
Pokitto 46:e7e438368e16 167
Pokitto 46:e7e438368e16 168 static uint8_t prescaler;
Pokitto 46:e7e438368e16 169
Pokitto 46:e7e438368e16 170 static void setChannelHalfPeriod(uint8_t channel, uint8_t halfPeriod);
Pokitto 46:e7e438368e16 171
Pokitto 46:e7e438368e16 172 static void generateOutput(); //!\\ DO NOT USE
Pokitto 46:e7e438368e16 173 static uint16_t globalVolume;
Pokitto 46:e7e438368e16 174
Pokitto 46:e7e438368e16 175
Pokitto 46:e7e438368e16 176 #if (NUM_CHANNELS > 0)
Pokitto 46:e7e438368e16 177 //tracks data
Pokitto 46:e7e438368e16 178 static uint16_t *trackData[NUM_CHANNELS];
Pokitto 46:e7e438368e16 179 static uint8_t trackCursor[NUM_CHANNELS];
Pokitto 46:e7e438368e16 180 static uint16_t **patternSet[NUM_CHANNELS];
Pokitto 46:e7e438368e16 181 static int8_t patternPitch[NUM_CHANNELS];
Pokitto 46:e7e438368e16 182
Pokitto 46:e7e438368e16 183 // pattern data
Pokitto 46:e7e438368e16 184 static uint16_t *patternData[NUM_CHANNELS];
Pokitto 46:e7e438368e16 185 static uint16_t **instrumentSet[NUM_CHANNELS];
Pokitto 46:e7e438368e16 186 static bool patternLooping[NUM_CHANNELS];
Pokitto 46:e7e438368e16 187 static uint16_t patternCursor[NUM_CHANNELS];
Pokitto 46:e7e438368e16 188
Pokitto 46:e7e438368e16 189 // note data
Pokitto 46:e7e438368e16 190 static uint8_t notePitch[NUM_CHANNELS];
Pokitto 46:e7e438368e16 191 static uint8_t noteDuration[NUM_CHANNELS];
Pokitto 46:e7e438368e16 192 static int8_t noteVolume[NUM_CHANNELS];
Pokitto 46:e7e438368e16 193 static bool notePlaying[NUM_CHANNELS];
Pokitto 46:e7e438368e16 194
Pokitto 46:e7e438368e16 195 // commands data
Pokitto 46:e7e438368e16 196 static int8_t commandsCounter[NUM_CHANNELS];
Pokitto 46:e7e438368e16 197 static int8_t volumeSlideStepDuration[NUM_CHANNELS];
Pokitto 46:e7e438368e16 198 static int8_t volumeSlideStepSize[NUM_CHANNELS];
Pokitto 46:e7e438368e16 199 static uint8_t arpeggioStepDuration[NUM_CHANNELS];
Pokitto 46:e7e438368e16 200 static int8_t arpeggioStepSize[NUM_CHANNELS];
Pokitto 46:e7e438368e16 201 static uint8_t tremoloStepDuration[NUM_CHANNELS];
Pokitto 46:e7e438368e16 202 static int8_t tremoloStepSize[NUM_CHANNELS];
Pokitto 46:e7e438368e16 203
Pokitto 46:e7e438368e16 204
Pokitto 46:e7e438368e16 205 // instrument data
Pokitto 46:e7e438368e16 206 static uint16_t *instrumentData[NUM_CHANNELS];
Pokitto 46:e7e438368e16 207 static uint8_t instrumentLength[NUM_CHANNELS]; //number of steps in the instrument
Pokitto 46:e7e438368e16 208 static uint8_t instrumentLooping[NUM_CHANNELS]; //how many steps to loop on when the last step of the instrument is reached
Pokitto 46:e7e438368e16 209 static uint16_t instrumentCursor[NUM_CHANNELS]; //which step is being played
Pokitto 46:e7e438368e16 210 static uint8_t instrumentNextChange[NUM_CHANNELS]; //how many frames before the next step
Pokitto 46:e7e438368e16 211
Pokitto 46:e7e438368e16 212 //current step data
Pokitto 46:e7e438368e16 213 static int8_t stepVolume[NUM_CHANNELS];
Pokitto 46:e7e438368e16 214 static uint8_t stepPitch[NUM_CHANNELS];
Pokitto 46:e7e438368e16 215
Pokitto 46:e7e438368e16 216 static uint8_t chanVolumes[NUM_CHANNELS];
Pokitto 46:e7e438368e16 217 #endif
Pokitto 46:e7e438368e16 218 static void updateOutput();
Pokitto 46:e7e438368e16 219 };
Pokitto 46:e7e438368e16 220
Pokitto 46:e7e438368e16 221 }
Pokitto 46:e7e438368e16 222
Pokitto 46:e7e438368e16 223 #endif // POKITTOSOUND_H
Pokitto 46:e7e438368e16 224
Pokitto 46:e7e438368e16 225
Pokitto 46:e7e438368e16 226
Pokitto 46:e7e438368e16 227
Pokitto 46:e7e438368e16 228
Pokitto 46:e7e438368e16 229