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

Committer:
Pokitto
Date:
Tue May 01 18:42:56 2018 +0000
Revision:
43:6183b12dd99c
New volume control;

Who changed what in which revision?

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