1 /* $Id: SoundMixer.h,v 1.6 2017/08/30 22:53:13 sarrazip Exp $ 2 SoundMixer.h - Sound mixer based on the SDL_mixer library 3 4 flatzebra - Generic 2D Game Engine library 5 Copyright (C) 2001, 2002, 2003 Pierre Sarrazin <http://sarrazip.com/> 6 7 This program is free software; you can redistribute it and/or 8 modify it under the terms of the GNU General Public License 9 as published by the Free Software Foundation; either version 2 10 of the License, or (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20 02110-1301, USA. 21 */ 22 23 #ifndef _H_SoundMixer 24 #define _H_SoundMixer 25 26 #include <SDL_mixer.h> 27 28 #include <string> 29 30 31 namespace flatzebra { 32 33 34 class SoundMixer 35 { 36 public: 37 38 class Error 39 { 40 public: Error(const std::string & s)41 Error(const std::string &s) : errMsg(s) {} what()42 const std::string &what() const { return errMsg; } 43 private: 44 std::string errMsg; 45 }; 46 47 SoundMixer(int numChannels = 8); 48 /* Initializes the SDL_mixer system at a rate of 11025 Hz, mono. 49 'numChannels' must be the number of channels to be allocated. 50 SDL must already have been initialized. 51 If the SDL_mixer initialization fails, throws the error message 52 as an exception. 53 Only one instance of this class should be created. 54 */ 55 56 ~SoundMixer(); 57 /* Shuts down the SDL_mixer system. 58 */ 59 60 61 class Chunk 62 { 63 public: 64 65 Chunk(); 66 /* Initializes an empty sound chunk. Call init() after this. 67 */ 68 69 Chunk(const std::string &wavFilename); 70 /* Calls init() with 'wavFilename' and it if returns a (non-empty) 71 error message, throws it as an exception. 72 */ 73 74 void init(const std::string &wavFilename); 75 /* Loads the WAV file whose name is given. 76 If the load fails, throws the error message as an exception. 77 */ 78 79 ~Chunk(); 80 /* Frees the resources used by the chunk. 81 */ 82 83 private: 84 Mix_Chunk *sample; 85 friend class SoundMixer; 86 87 // Forbidden operations: 88 Chunk(const Chunk &); 89 Chunk &operator = (const Chunk &); 90 }; 91 92 93 void playChunk(Chunk &theSound); 94 /* Schedules 'theSound' to be played now on a free unreserved channel. 95 On failure, throws the error message as an exception. 96 */ 97 98 99 private: 100 101 /* Forbidden operations: 102 */ 103 SoundMixer(const SoundMixer &x); 104 SoundMixer &operator = (const SoundMixer &x); 105 }; 106 107 108 } // namespace flatzebra 109 110 111 #endif /* _H_SoundMixer */ 112