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

Committer:
Pokitto
Date:
Mon Apr 02 22:37:22 2018 +0000
Revision:
36:771321e70814
Synced with Github repo

Who changed what in which revision?

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