PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Fork of PokittoLib by
PokittoSound.h
00001 /**************************************************************************/ 00002 /*! 00003 @file PokittoSound.h 00004 @author Jonne Valola 00005 00006 @section LICENSE 00007 00008 Software License Agreement (BSD License) 00009 00010 Copyright (c) 2016, Jonne Valola 00011 All rights reserved. 00012 00013 Redistribution and use in source and binary forms, with or without 00014 modification, are permitted provided that the following conditions are met: 00015 1. Redistributions of source code must retain the above copyright 00016 notice, this list of conditions and the following disclaimer. 00017 2. Redistributions in binary form must reproduce the above copyright 00018 notice, this list of conditions and the following disclaimer in the 00019 documentation and/or other materials provided with the distribution. 00020 3. Neither the name of the copyright holders nor the 00021 names of its contributors may be used to endorse or promote products 00022 derived from this software without specific prior written permission. 00023 00024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 00025 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 00028 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 */ 00035 /**************************************************************************/ 00036 00037 00038 #ifndef POKITTOSOUND_H 00039 #define POKITTOSOUND_H 00040 00041 #include <stdint.h> 00042 #include "Pokitto_settings.h " 00043 #include "GBcompatibility.h" 00044 #include "PokittoFakeavr.h " 00045 #include "PokittoGlobs.h " 00046 00047 extern void pokPauseStream(); 00048 extern void pokPlayStream(); 00049 extern uint8_t pokStreamPaused(); 00050 00051 //volume levels 00052 #define GLOBVOL_SHIFT 5 //shift global volume to allow for finer increments 00053 #ifndef MAX_VOL_TEST 00054 #define VOLUME_SPEAKER_MAX 255 //((8<<GLOBVOL_SHIFT)-1) 00055 #define VOLUME_HEADPHONE_MAX (1<<GLOBVOL_SHIFT) 00056 #define VOLUME_STARTUP ((1<<GLOBVOL_SHIFT)/2) 00057 #else 00058 #define VOLUME_SPEAKER_MAX ((8<<GLOBVOL_SHIFT)-1) 00059 #define VOLUME_HEADPHONE_MAX VOLUME_SPEAKER_MAX 00060 #define VOLUME_STARTUP VOLUME_SPEAKER_MAX 00061 #endif // MAXVOLTEST 00062 00063 #ifdef POK_SIM 00064 #define VOLUME_STEP 1 00065 #else 00066 #define VOLUME_STEP 8 00067 #endif 00068 00069 00070 //commands 00071 #define CMD_VOLUME 0 00072 #define CMD_INSTRUMENT 1 00073 #define CMD_SLIDE 2 00074 #define CMD_ARPEGGIO 3 00075 #define CMD_TREMOLO 4 00076 00077 #define STR_PLAYING 0x1 00078 #define STR_VISUALIZER 0x2 00079 #define STR_LOOP 0x4 00080 #define STR_PAUSED 0x8 00081 00082 namespace Pokitto { 00083 00084 /** Sound class. 00085 * The Sound class is an API-compatible version of the Gamebuino Sound API by Aurelien Rodot. 00086 * Because it is a class consisting of only static members, there is only 1 instance running, 00087 * no matter how many Sound classes are declared (see example below). This means sound can 00088 * be used through a simple Sound class object or as a member of the Core class. 00089 * 00090 */ 00091 00092 extern void audio_IRQ(); // audio interrupt 00093 00094 class Sound { 00095 private: 00096 static uint16_t volumeMax; 00097 public: 00098 static void begin(); 00099 00100 // Headphonemode 00101 static void setMaxVol(int16_t); 00102 static uint16_t getMaxVol(); 00103 static void volumeUp(); 00104 static void volumeDown(); 00105 00106 // Original functions 00107 static void updateStream(); 00108 static void playTone(uint8_t os, int frq, uint8_t amp, uint8_t wav,uint8_t arpmode); 00109 static void playTone(uint8_t os, uint16_t freq, uint8_t volume, uint32_t duration); 00110 static uint8_t ampIsOn(); 00111 static void ampEnable(uint8_t); 00112 static int playMusicStream(char* filename, uint8_t options); 00113 static int playMusicStream(char* filename); 00114 static int playMusicStream(); 00115 static void pauseMusicStream(); 00116 00117 // GB compatibility functions 00118 static void playTrack(const uint16_t* track, uint8_t channel); 00119 static void updateTrack(uint8_t channel); 00120 static void updateTrack(); 00121 static void stopTrack(uint8_t channel); 00122 static void stopTrack(); 00123 static void changePatternSet(const uint16_t* const* patterns, uint8_t channel); 00124 static bool trackIsPlaying[NUM_CHANNELS]; 00125 00126 static void playPattern(const uint16_t* pattern, uint8_t channel); 00127 static void changeInstrumentSet(const uint16_t* const* instruments, uint8_t channel); 00128 static void updatePattern(uint8_t i); 00129 static void updatePattern(); 00130 static void setPatternLooping(bool loop, uint8_t channel); 00131 static void stopPattern(uint8_t channel); 00132 static void stopPattern(); 00133 static bool patternIsPlaying[NUM_CHANNELS]; 00134 00135 static void command(uint8_t cmd, uint8_t X, int8_t Y, uint8_t i); 00136 static void playNote(uint8_t pitch, uint8_t duration, uint8_t channel); 00137 static void updateNote(); 00138 static void updateNote(uint8_t i); 00139 static void stopNote(uint8_t channel); 00140 static void stopNote(); 00141 00142 static uint8_t outputPitch[NUM_CHANNELS]; 00143 static int8_t outputVolume[NUM_CHANNELS]; 00144 00145 static void setMasterVolume(uint8_t); 00146 static uint8_t GetMasterVolume(); 00147 static void setVolume(int16_t volume); 00148 static uint16_t getVolume(); 00149 static void setVolume(int8_t volume, uint8_t channel); 00150 static uint8_t getVolume(uint8_t channel); 00151 00152 static void playOK(); 00153 static void playCancel(); 00154 static void playTick(); 00155 00156 static uint8_t prescaler; 00157 00158 static void setChannelHalfPeriod(uint8_t channel, uint8_t halfPeriod); 00159 00160 static void generateOutput(); //!\\ DO NOT USE 00161 static uint16_t globalVolume; 00162 00163 00164 #if (NUM_CHANNELS > 0) 00165 //tracks data 00166 static uint16_t *trackData[NUM_CHANNELS]; 00167 static uint8_t trackCursor[NUM_CHANNELS]; 00168 static uint16_t **patternSet[NUM_CHANNELS]; 00169 static int8_t patternPitch[NUM_CHANNELS]; 00170 00171 // pattern data 00172 static uint16_t *patternData[NUM_CHANNELS]; 00173 static uint16_t **instrumentSet[NUM_CHANNELS]; 00174 static bool patternLooping[NUM_CHANNELS]; 00175 static uint16_t patternCursor[NUM_CHANNELS]; 00176 00177 // note data 00178 static uint8_t notePitch[NUM_CHANNELS]; 00179 static uint8_t noteDuration[NUM_CHANNELS]; 00180 static int8_t noteVolume[NUM_CHANNELS]; 00181 static bool notePlaying[NUM_CHANNELS]; 00182 00183 // commands data 00184 static int8_t commandsCounter[NUM_CHANNELS]; 00185 static int8_t volumeSlideStepDuration[NUM_CHANNELS]; 00186 static int8_t volumeSlideStepSize[NUM_CHANNELS]; 00187 static uint8_t arpeggioStepDuration[NUM_CHANNELS]; 00188 static int8_t arpeggioStepSize[NUM_CHANNELS]; 00189 static uint8_t tremoloStepDuration[NUM_CHANNELS]; 00190 static int8_t tremoloStepSize[NUM_CHANNELS]; 00191 00192 00193 // instrument data 00194 static uint16_t *instrumentData[NUM_CHANNELS]; 00195 static uint8_t instrumentLength[NUM_CHANNELS]; //number of steps in the instrument 00196 static uint8_t instrumentLooping[NUM_CHANNELS]; //how many steps to loop on when the last step of the instrument is reached 00197 static uint16_t instrumentCursor[NUM_CHANNELS]; //which step is being played 00198 static uint8_t instrumentNextChange[NUM_CHANNELS]; //how many frames before the next step 00199 00200 //current step data 00201 static int8_t stepVolume[NUM_CHANNELS]; 00202 static uint8_t stepPitch[NUM_CHANNELS]; 00203 00204 static uint8_t chanVolumes[NUM_CHANNELS]; 00205 #endif 00206 static void updateOutput(); 00207 }; 00208 00209 } 00210 00211 #endif // POKITTOSOUND_H 00212 00213 00214 00215 00216
Generated on Tue Jul 12 2022 18:08:13 by 1.7.2