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

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
0:e8b8f36b4505
direct lcd stuff used by sensitive

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.h
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 #ifndef POKITTOSOUND_H
Pokitto 0:e8b8f36b4505 39 #define POKITTOSOUND_H
Pokitto 0:e8b8f36b4505 40
Pokitto 0:e8b8f36b4505 41 #include <stdint.h>
Pokitto 0:e8b8f36b4505 42 #include "Pokitto_settings.h"
Pokitto 0:e8b8f36b4505 43 #include "GBcompatibility.h"
Pokitto 0:e8b8f36b4505 44 #include "PokittoFakeavr.h"
Pokitto 0:e8b8f36b4505 45 #include "PokittoGlobs.h"
Pokitto 0:e8b8f36b4505 46
Pokitto 0:e8b8f36b4505 47 extern void pokPauseStream();
Pokitto 0:e8b8f36b4505 48 extern void pokPlayStream();
Pokitto 0:e8b8f36b4505 49 extern uint8_t pokStreamPaused();
Pokitto 0:e8b8f36b4505 50
Pokitto 0:e8b8f36b4505 51 //volume levels
Pokitto 0:e8b8f36b4505 52 #define GLOBVOL_SHIFT 5 //shift global volume to allow for finer increments
Pokitto 0:e8b8f36b4505 53 #ifndef MAX_VOL_TEST
Pokitto 0:e8b8f36b4505 54 #define VOLUME_SPEAKER_MAX 255 //((8<<GLOBVOL_SHIFT)-1)
Pokitto 0:e8b8f36b4505 55 #define VOLUME_HEADPHONE_MAX (1<<GLOBVOL_SHIFT)
Pokitto 0:e8b8f36b4505 56 #define VOLUME_STARTUP ((1<<GLOBVOL_SHIFT)/2)
Pokitto 0:e8b8f36b4505 57 #else
Pokitto 0:e8b8f36b4505 58 #define VOLUME_SPEAKER_MAX ((8<<GLOBVOL_SHIFT)-1)
Pokitto 0:e8b8f36b4505 59 #define VOLUME_HEADPHONE_MAX VOLUME_SPEAKER_MAX
Pokitto 0:e8b8f36b4505 60 #define VOLUME_STARTUP VOLUME_SPEAKER_MAX
Pokitto 0:e8b8f36b4505 61 #endif // MAXVOLTEST
Pokitto 0:e8b8f36b4505 62
Pokitto 0:e8b8f36b4505 63 #ifdef POK_SIM
Pokitto 0:e8b8f36b4505 64 #define VOLUME_STEP 1
Pokitto 0:e8b8f36b4505 65 #else
Pokitto 0:e8b8f36b4505 66 #define VOLUME_STEP 8
Pokitto 0:e8b8f36b4505 67 #endif
Pokitto 0:e8b8f36b4505 68
Pokitto 0:e8b8f36b4505 69
Pokitto 0:e8b8f36b4505 70 //commands
Pokitto 0:e8b8f36b4505 71 #define CMD_VOLUME 0
Pokitto 0:e8b8f36b4505 72 #define CMD_INSTRUMENT 1
Pokitto 0:e8b8f36b4505 73 #define CMD_SLIDE 2
Pokitto 0:e8b8f36b4505 74 #define CMD_ARPEGGIO 3
Pokitto 0:e8b8f36b4505 75 #define CMD_TREMOLO 4
Pokitto 0:e8b8f36b4505 76
Pokitto 0:e8b8f36b4505 77 #define STR_PLAYING 0x1
Pokitto 0:e8b8f36b4505 78 #define STR_VISUALIZER 0x2
Pokitto 0:e8b8f36b4505 79 #define STR_LOOP 0x4
Pokitto 0:e8b8f36b4505 80 #define STR_PAUSED 0x8
Pokitto 0:e8b8f36b4505 81
Pokitto 0:e8b8f36b4505 82 namespace Pokitto {
Pokitto 0:e8b8f36b4505 83
Pokitto 0:e8b8f36b4505 84 /** Sound class.
Pokitto 0:e8b8f36b4505 85 * The Sound class is an API-compatible version of the Gamebuino Sound API by Aurelien Rodot.
Pokitto 0:e8b8f36b4505 86 * Because it is a class consisting of only static members, there is only 1 instance running,
Pokitto 0:e8b8f36b4505 87 * no matter how many Sound classes are declared (see example below). This means sound can
Pokitto 0:e8b8f36b4505 88 * be used through a simple Sound class object or as a member of the Core class.
Pokitto 0:e8b8f36b4505 89 *
Pokitto 0:e8b8f36b4505 90 */
Pokitto 0:e8b8f36b4505 91
Pokitto 0:e8b8f36b4505 92 extern void audio_IRQ(); // audio interrupt
Pokitto 0:e8b8f36b4505 93
Pokitto 0:e8b8f36b4505 94 class Sound {
Pokitto 0:e8b8f36b4505 95 private:
Pokitto 0:e8b8f36b4505 96 static uint16_t volumeMax;
Pokitto 0:e8b8f36b4505 97 public:
Pokitto 0:e8b8f36b4505 98 static void begin();
Pokitto 0:e8b8f36b4505 99
Pokitto 0:e8b8f36b4505 100 // Headphonemode
Pokitto 0:e8b8f36b4505 101 static void setMaxVol(int16_t);
Pokitto 0:e8b8f36b4505 102 static uint16_t getMaxVol();
Pokitto 0:e8b8f36b4505 103 static void volumeUp();
Pokitto 0:e8b8f36b4505 104 static void volumeDown();
Pokitto 0:e8b8f36b4505 105
Pokitto 0:e8b8f36b4505 106 // Original functions
Pokitto 0:e8b8f36b4505 107 static void updateStream();
Pokitto 0:e8b8f36b4505 108 static void playTone(uint8_t os, int frq, uint8_t amp, uint8_t wav,uint8_t arpmode);
Pokitto 0:e8b8f36b4505 109 static void playTone(uint8_t os, uint16_t freq, uint8_t volume, uint32_t duration);
Pokitto 0:e8b8f36b4505 110 static uint8_t ampIsOn();
Pokitto 0:e8b8f36b4505 111 static void ampEnable(uint8_t);
Pokitto 0:e8b8f36b4505 112 static int playMusicStream(char* filename, uint8_t options);
Pokitto 0:e8b8f36b4505 113 static int playMusicStream(char* filename);
Pokitto 0:e8b8f36b4505 114 static int playMusicStream();
Pokitto 0:e8b8f36b4505 115 static void pauseMusicStream();
Pokitto 0:e8b8f36b4505 116
Pokitto 0:e8b8f36b4505 117 // GB compatibility functions
Pokitto 0:e8b8f36b4505 118 static void playTrack(const uint16_t* track, uint8_t channel);
Pokitto 0:e8b8f36b4505 119 static void updateTrack(uint8_t channel);
Pokitto 0:e8b8f36b4505 120 static void updateTrack();
Pokitto 0:e8b8f36b4505 121 static void stopTrack(uint8_t channel);
Pokitto 0:e8b8f36b4505 122 static void stopTrack();
Pokitto 0:e8b8f36b4505 123 static void changePatternSet(const uint16_t* const* patterns, uint8_t channel);
Pokitto 0:e8b8f36b4505 124 static bool trackIsPlaying[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 125
Pokitto 0:e8b8f36b4505 126 static void playPattern(const uint16_t* pattern, uint8_t channel);
Pokitto 0:e8b8f36b4505 127 static void changeInstrumentSet(const uint16_t* const* instruments, uint8_t channel);
Pokitto 0:e8b8f36b4505 128 static void updatePattern(uint8_t i);
Pokitto 0:e8b8f36b4505 129 static void updatePattern();
Pokitto 0:e8b8f36b4505 130 static void setPatternLooping(bool loop, uint8_t channel);
Pokitto 0:e8b8f36b4505 131 static void stopPattern(uint8_t channel);
Pokitto 0:e8b8f36b4505 132 static void stopPattern();
Pokitto 0:e8b8f36b4505 133 static bool patternIsPlaying[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 134
Pokitto 0:e8b8f36b4505 135 static void command(uint8_t cmd, uint8_t X, int8_t Y, uint8_t i);
Pokitto 0:e8b8f36b4505 136 static void playNote(uint8_t pitch, uint8_t duration, uint8_t channel);
Pokitto 0:e8b8f36b4505 137 static void updateNote();
Pokitto 0:e8b8f36b4505 138 static void updateNote(uint8_t i);
Pokitto 0:e8b8f36b4505 139 static void stopNote(uint8_t channel);
Pokitto 0:e8b8f36b4505 140 static void stopNote();
Pokitto 0:e8b8f36b4505 141
Pokitto 0:e8b8f36b4505 142 static uint8_t outputPitch[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 143 static int8_t outputVolume[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 144
Pokitto 0:e8b8f36b4505 145 static void setMasterVolume(uint8_t);
Pokitto 0:e8b8f36b4505 146 static uint8_t GetMasterVolume();
Pokitto 0:e8b8f36b4505 147 static void setVolume(int16_t volume);
Pokitto 0:e8b8f36b4505 148 static uint16_t getVolume();
Pokitto 0:e8b8f36b4505 149 static void setVolume(int8_t volume, uint8_t channel);
Pokitto 0:e8b8f36b4505 150 static uint8_t getVolume(uint8_t channel);
Pokitto 0:e8b8f36b4505 151
Pokitto 0:e8b8f36b4505 152 static void playOK();
Pokitto 0:e8b8f36b4505 153 static void playCancel();
Pokitto 0:e8b8f36b4505 154 static void playTick();
Pokitto 0:e8b8f36b4505 155
Pokitto 0:e8b8f36b4505 156 static uint8_t prescaler;
Pokitto 0:e8b8f36b4505 157
Pokitto 0:e8b8f36b4505 158 static void setChannelHalfPeriod(uint8_t channel, uint8_t halfPeriod);
Pokitto 0:e8b8f36b4505 159
Pokitto 0:e8b8f36b4505 160 static void generateOutput(); //!\\ DO NOT USE
Pokitto 0:e8b8f36b4505 161 static uint16_t globalVolume;
Pokitto 0:e8b8f36b4505 162
Pokitto 0:e8b8f36b4505 163
Pokitto 0:e8b8f36b4505 164 #if (NUM_CHANNELS > 0)
Pokitto 0:e8b8f36b4505 165 //tracks data
Pokitto 0:e8b8f36b4505 166 static uint16_t *trackData[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 167 static uint8_t trackCursor[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 168 static uint16_t **patternSet[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 169 static int8_t patternPitch[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 170
Pokitto 0:e8b8f36b4505 171 // pattern data
Pokitto 0:e8b8f36b4505 172 static uint16_t *patternData[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 173 static uint16_t **instrumentSet[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 174 static bool patternLooping[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 175 static uint16_t patternCursor[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 176
Pokitto 0:e8b8f36b4505 177 // note data
Pokitto 0:e8b8f36b4505 178 static uint8_t notePitch[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 179 static uint8_t noteDuration[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 180 static int8_t noteVolume[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 181 static bool notePlaying[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 182
Pokitto 0:e8b8f36b4505 183 // commands data
Pokitto 0:e8b8f36b4505 184 static int8_t commandsCounter[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 185 static int8_t volumeSlideStepDuration[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 186 static int8_t volumeSlideStepSize[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 187 static uint8_t arpeggioStepDuration[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 188 static int8_t arpeggioStepSize[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 189 static uint8_t tremoloStepDuration[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 190 static int8_t tremoloStepSize[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 191
Pokitto 0:e8b8f36b4505 192
Pokitto 0:e8b8f36b4505 193 // instrument data
Pokitto 0:e8b8f36b4505 194 static uint16_t *instrumentData[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 195 static uint8_t instrumentLength[NUM_CHANNELS]; //number of steps in the instrument
Pokitto 0:e8b8f36b4505 196 static uint8_t instrumentLooping[NUM_CHANNELS]; //how many steps to loop on when the last step of the instrument is reached
Pokitto 0:e8b8f36b4505 197 static uint16_t instrumentCursor[NUM_CHANNELS]; //which step is being played
Pokitto 0:e8b8f36b4505 198 static uint8_t instrumentNextChange[NUM_CHANNELS]; //how many frames before the next step
Pokitto 0:e8b8f36b4505 199
Pokitto 0:e8b8f36b4505 200 //current step data
Pokitto 0:e8b8f36b4505 201 static int8_t stepVolume[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 202 static uint8_t stepPitch[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 203
Pokitto 0:e8b8f36b4505 204 static uint8_t chanVolumes[NUM_CHANNELS];
Pokitto 0:e8b8f36b4505 205 #endif
Pokitto 0:e8b8f36b4505 206 static void updateOutput();
Pokitto 0:e8b8f36b4505 207 };
Pokitto 0:e8b8f36b4505 208
Pokitto 0:e8b8f36b4505 209 }
Pokitto 0:e8b8f36b4505 210
Pokitto 0:e8b8f36b4505 211 #endif // POKITTOSOUND_H
Pokitto 0:e8b8f36b4505 212
Pokitto 0:e8b8f36b4505 213
Pokitto 0:e8b8f36b4505 214
Pokitto 0:e8b8f36b4505 215
Pokitto 0:e8b8f36b4505 216