Tekkotsu Homepage | Demos | Overview | Downloads | Dev. Resources | Reference | Credits |
SoundManager.hGo to the documentation of this file.00001 //-*-c++-*- 00002 #ifndef INCLUDED_SoundManager_h_ 00003 #define INCLUDED_SoundManager_h_ 00004 00005 #include "IPC/RCRegion.h" 00006 #include "Shared/ODataFormats.h" 00007 00008 #include <string> 00009 #include <vector> 00010 #include "IPC/ListMemBuf.h" 00011 #include "IPC/MutexLock.h" 00012 #include "SoundManagerMsg.h" 00013 #include "IPC/ProcessID.h" 00014 #include "Shared/attributes.h" 00015 00016 #ifdef PLATFORM_APERIOS 00017 class OSubject; 00018 class ONotifyEvent; 00019 #else 00020 # include "IPC/MessageQueue.h" 00021 #endif 00022 00023 //! Provides sound effects and caching services, as well as mixing buffers for the SoundPlay process 00024 /*! Provides easy methods for playing back sounds, either from files 00025 * on the memory stick, or from dynamically generated buffers. You 00026 * can chain playback commands so that when one sound finishes, 00027 * another picks up automatically. This might be handy if, say, 00028 * someone wants to write an MP3 player ;) The sounds would be too 00029 * large to load into memory all at once, but you could load a block 00030 * at a time and chain them so it seamlessly moves from one to the 00031 * other. 00032 * 00033 * You can also preload sounds (loadFile()) before playing them (play() / playFile()) so 00034 * there's no delay between requesting a sound and having it start playing while it is loaded from disk/memory stick. 00035 * Just be sure to release the file (releaseFile()) again 00036 * when you're done with it ;) 00037 * 00038 * All functions will attempt to lock the SoundManager. Remember, 00039 * this is running in a shared memory region, accessible by the 00040 * SoundPlay process and both the Main and Motion processes (so 00041 * MotionCommands can play sounds!) 00042 * 00043 * One could be tempted to draw parallels to the MotionManager, and 00044 * envision a system with SoundCommands that are handed over and can 00045 * dynamically compute sound buffers as needed. If you have the time 00046 * and inclination, the job's all yours... (Midi players, speech 00047 * synthesizer, ...?) 00048 * 00049 * @todo Volume control, variable playback speed, support more wav 00050 * file formats (latter two are the same thing if you think about it - need 00051 * to be able to resample on the fly) 00052 * 00053 * @todo Add functions to hand out regions to be filled out to avoid 00054 * copying into the buffer. 00055 * 00056 * @see <a href="http://www.cs.cmu.edu/~dst/Tekkotsu/Tutorial/sound.shtml">David Touretzky's "Playing Sounds" Chapter</a> 00057 */ 00058 class SoundManager { 00059 friend void speechDoneCleanup(long playid); 00060 public: 00061 //! destructor 00062 virtual ~SoundManager(); 00063 00064 //!constructor, should only be called by the receiving process (SoundPlay) 00065 SoundManager(); 00066 #ifdef PLATFORM_APERIOS 00067 //!Each process needs to call this before it can send sounds to the SoundPlay process 00068 void InitAccess(OSubject* subj); 00069 #else //PLATFORM_LOCAL 00070 //!Each process (except SoundPlay) needs to call this before it can send sounds to the SoundPlay process 00071 void InitAccess(MessageQueueBase& sndbufq); 00072 #endif 00073 00074 //!This is used for referring to sound data so you can start playing it or release it 00075 typedef SoundManagerMsg::Snd_ID Snd_ID; 00076 static const Snd_ID invalid_Snd_ID=(Snd_ID)-1; //!< for reporting errors 00077 static const Snd_ID MAX_SND=50; //!< the number of sounds that can be loaded at any given time 00078 00079 //!This is for referring to instances of the play command so you can stop, pause, or monitor progress (later versions will send events upon completion) 00080 typedef unsigned short Play_ID; 00081 static const Play_ID invalid_Play_ID=(Play_ID)-1; //!< for reporting errors 00082 static const Play_ID MAX_PLAY=256; //!< the number of sounds that can be enqueued at the same time (see MixMode_t) 00083 00084 static const unsigned int MAX_NAME_LEN=128; //!<maximum length of a path 00085 00086 //!Used to set the mode for mixing multiple sound channels 00087 /*!Feel free to add a higher quality mixer if you're an audiophile - I'm pretty new to sound processing*/ 00088 enum MixMode_t { 00089 // there's some prototype code for a bit-shifting 'Faster' quality level, but it hasn't been finished... 'Fast' is the default for now. 00090 Fast, //!< uses real division to maintain volume level, without increasing intermediary precision, which causes low-order bit error in exchange for less CPU usage 00091 Quality //!< uses real division to maintain volume level, using an intermediary higher precision buffer for mixing 00092 }; 00093 00094 //! indicates how to handle channel overflow (trying to play more sounds than maximum number of mixing channels). See #queue_mode 00095 enum QueueMode_t { 00096 Enqueue, //!< newer sounds are played when a channel opens up (when old sound finishes) 00097 Pause, //!< newer sounds pause oldest sound, which continues when a channel opens 00098 Stop, //!< newer sounds stop oldest sound 00099 Override, //!< older sounds have play heads advanced, but don't get mixed until a channel opens 00100 }; 00101 00102 //!loads a wav file (if it matches Config::sound_config settings - can't do resampling yet) 00103 /*!Since the SoundManager does the loading, if the same file is being played more than once, only once copy is stored in memory 00104 * @param name can be either a full path, or a partial path relative to Config::sound_config::root 00105 * @return ID number for future references (can also use name) 00106 * The sound data will be cached until releaseFile() or release() is called a matching number of times*/ 00107 Snd_ID loadFile(std::string const &name); 00108 00109 //!loads raw samples from a buffer (assumes matches Config::sound_config settings) 00110 /*!The sound data will be cached until release() is called a matching number of times.\n 00111 * This function is useful for dynamic sound sources. A copy will be made. */ 00112 Snd_ID loadBuffer(const char buf[], unsigned int len); 00113 00114 //!Marks the sound buffer to be released after the last play command completes (or right now if not being played) 00115 void releaseFile(std::string const &name); 00116 00117 //!Marks the sound buffer to be released after the last play command completes (or right now if not being played) 00118 void release(Snd_ID id); 00119 00120 //!play a wav file (if it matches Config::sound_config settings - can't do resampling yet) 00121 /*!Will do a call to loadFile() first, and then automatically release the sound again when complete. 00122 * @param name can be either a full path, or a partial path relative to Config::sound_config::root 00123 * @return ID number for future references 00124 * The sound data will not be cached after done playing unless a call to loadFile is made*/ 00125 Play_ID playFile(std::string const &name); 00126 00127 //!loads raw samples from a buffer (assumes buffer matches Config::sound_config settings) 00128 /*!The sound data will be released after done playing*/ 00129 Play_ID playBuffer(const char buf[], unsigned int len); 00130 00131 //!plays a previously loaded buffer or file 00132 Play_ID play(Snd_ID id); 00133 00134 //!allows automatic queuing of sounds - good for dynamic sound sources! 00135 /*!if you chain more than once to the same base, the new buffers are appended 00136 * to the end of the chain - the new buffer doesn't replace the current chain 00137 * @return @a base - just for convenience of multiple calls*/ 00138 Play_ID chainFile(Play_ID base, std::string const &next); 00139 00140 //!allows automatic queuing of sounds - good for dynamic sound sources! 00141 /*!if you chain more than once to the same base, the new buffers are appended 00142 * to the end of the chain - the new buffer doesn't replace the current chain 00143 * @return @a base - just for convenience of multiple calls*/ 00144 Play_ID chainBuffer(Play_ID base, const char buf[], unsigned int len); 00145 00146 //!allows automatic queuing of sounds - good for dynamic sound sources! 00147 /*!if you chain more than once to the same base, the new buffers are appended 00148 * to the end of the chain - the new buffer doesn't replace the current chain 00149 * @return @a base - just for convenience of multiple calls*/ 00150 Play_ID chain(Play_ID base, Snd_ID next); 00151 00152 //! Speaks its argument using the Mary text-to-speech system to generate a WAV file 00153 /*! On Aperios or unsupported platforms, simply displays the text on stdout */ 00154 SoundManager::Play_ID speak(const std::string& text, bool showText=true, const std::string& voice="female"); 00155 00156 //!Lets you stop playback of all sounds 00157 void stopPlay(); 00158 00159 //!Lets you stop playback of a sound 00160 void stopPlay(Play_ID id); 00161 00162 //!Lets you pause playback 00163 void pausePlay(Play_ID id); 00164 00165 //!Lets you resume playback 00166 void resumePlay(Play_ID id); 00167 00168 //!Lets you control the maximum number of channels (currently playing sounds), method for mixing (applies when max_chan>1), and queuing method (for when overflow channels) 00169 void setMode(unsigned int max_channels, MixMode_t mixer_mode, QueueMode_t queuing_mode); 00170 00171 //!Gives the time until the sound finishes, in milliseconds. Subtract 32 to get guarranteed valid time for this ID. 00172 /*!You should be passing the beginning of a chain to get proper results...\n 00173 * May be slightly conservative (will report too small a time) because this 00174 * does not account for delay until SoundPlay picks up the message that a 00175 * sound has been added.\n 00176 * However, it is slighly optimistic (will report too large a time) because 00177 * it processes a buffer all at one go, so it could mark the sound as finished 00178 * (and cause the ID to go invalid) up to RobotInfo::SoundBufferTime (32 ms) 00179 * before the sound finishes. So subtract SoundBufferTime if you want to be 00180 * absolutely sure the ID will still valid. */ 00181 unsigned int getRemainTime(Play_ID id) const; 00182 00183 #ifdef PLATFORM_APERIOS 00184 //!Copies the sound data to the OPENR buffer, ready to be passed to the system, only called by SoundPlay 00185 /*!@return the number of active sounds */ 00186 unsigned int CopyTo(OSoundVectorData* data); 00187 00188 //!updates internal data structures on the SoundPlay side - you shouldn't be calling this 00189 void ReceivedMsg(const ONotifyEvent& event); 00190 #endif 00191 00192 //!Copies the sound data to the specified memory buffer, ready to be passed to the system 00193 /*!@return the number of active sounds */ 00194 unsigned int CopyTo(void * dest, size_t destSize); 00195 00196 //!updates internal data structures on the SoundPlay side - you shouldn't be calling this 00197 void ProcessMsg(RCRegion * rcr); 00198 00199 //! returns number of sounds currently playing 00200 unsigned int getNumPlaying() { return chanlist.size(); } 00201 00202 //! return the next region serial number -- doesn't actually increment it though, repeated calls will return the same value until the value is actually used 00203 virtual unsigned int getNextKey() { return sn+1; } 00204 00205 protected: 00206 //!Mixes the channel into the buffer 00207 void mixChannel(Play_ID channelId, void* buf, size_t size); 00208 00209 //!Mixes the channel into the buffer additively 00210 /*!If mode is Quality, then the size of the buffer should be double the normal 00211 * size. */ 00212 void mixChannelAdditively(Play_ID channelId, int bitsPerSample, MixMode_t mode, short scalingFactor, void* buf, size_t size); 00213 00214 //!The intermediate mixer buffer used for Quality mode mixing 00215 int* mixerBuffer; 00216 00217 //!Size (in bytes) of the intermediate mixer buffer 00218 size_t mixerBufferSize; 00219 00220 //!Sets up a shared region to hold a sound - rounds to nearest page size 00221 RCRegion* initRegion(unsigned int size); 00222 00223 //!Looks to see if @a name matches any of the sounds in sndlist (converts to absolute path if not already) 00224 Snd_ID lookupPath(std::string const &name) const; 00225 00226 //!selects which of the channels are actually to be mixed together, depending on queue_mode 00227 void selectChannels(std::vector<Play_ID>& mix); 00228 00229 //!update the offsets of sounds which weren't mixed (when needed depending on queue_mode) 00230 void updateChannels(const std::vector<Play_ID>& mixs,size_t used); 00231 00232 //!called when a buffer end is reached, may reset buffer to next in chain, or just stopPlay() 00233 bool endPlay(Play_ID id); 00234 00235 //!Holds data about the loaded sounds 00236 struct SoundData { 00237 SoundData(); //!<constructor 00238 RCRegion * rcr; //!<shared region - don't need to share among processes, just collect in SoundPlay 00239 byte* data; //!<point to data in region (for convenience, only valid in SoundPlay) 00240 unsigned int len; //!<size of the sound 00241 unsigned int ref; //!<reference counter 00242 unsigned int sn; //!<serial number, allows us to verify that a given message buffer does indeed match this sound, and wasn't delayed in processing 00243 #ifdef __APPLE__ 00244 struct MacSpeechState* macSpeech; //!< if non-NULL, stores text string and OS context for speech request, #data and #rcr will be NULL (no PCM samples) 00245 #endif 00246 char name[SoundManager::MAX_NAME_LEN]; //!<stores the path to the file, empty if from a buffer 00247 private: 00248 SoundData(const SoundData&); //!< don't call 00249 SoundData operator=(const SoundData&); //!< don't call 00250 }; 00251 //!For convenience 00252 typedef ListMemBuf<SoundData,MAX_SND,Snd_ID> sndlist_t; 00253 //!Holds a list of all currently loaded sounds 00254 sndlist_t sndlist; 00255 00256 //!Holds data about sounds currently being played 00257 struct PlayState { 00258 PlayState(); //!<constructor 00259 Snd_ID snd_id; //!<index of sound 00260 unsigned int offset; //!<position in the sound 00261 unsigned int cumulative;//!<total time of playing (over queued sounds) 00262 Play_ID next_id; //!<lets you queue for continuous sound, or loop 00263 }; 00264 //!For convenience 00265 typedef ListMemBuf<PlayState,MAX_PLAY,Play_ID> playlist_t; 00266 //!Holds a list of all sounds currently enqueued 00267 playlist_t playlist; 00268 //!For convenience 00269 typedef ListMemBuf<Play_ID,MAX_PLAY,Play_ID> chanlist_t; 00270 //!Holds a list of all currently playing sounds, ordered newest (front) to oldest(back) 00271 chanlist_t chanlist; 00272 00273 //!Current mixing mode, set by setMode(); 00274 MixMode_t mix_mode; 00275 00276 //!Current queuing mode, set by setMode(); 00277 QueueMode_t queue_mode; 00278 00279 //!Current maximum number of sounds to mix together 00280 unsigned int max_chan; 00281 00282 #ifndef __APPLE__ 00283 //!Prevents multiple processes from accessing at the same time 00284 mutable MutexLock<ProcessID::NumProcesses> lock; 00285 #else 00286 //!Prevents multiple processes from accessing at the same time 00287 mutable struct Lock : public MutexLock<ProcessID::NumProcesses> { 00288 Lock() : MutexLock<ProcessID::NumProcesses>(), initiatedSpeech(), completedSpeech() {} 00289 std::vector<struct MacSpeechState*> initiatedSpeech; //!< buffered speech contexts which should be started 00290 std::vector<struct MacSpeechState*> completedSpeech; //!< buffered speech contexts which need to be freed 00291 virtual void releaseResource(Data& d); 00292 //! this buffers launching OS resources until after lock is released so we don't deadlock with OS mutex in CopyTo callback 00293 void initiated(struct MacSpeechState* mss); 00294 //! this buffers freeing OS resources until after lock is released so we don't deadlock with OS mutex in CopyTo callback 00295 void completed(struct MacSpeechState* mss); 00296 } lock; 00297 #endif 00298 00299 //!A serial number, incremented for each sound which is created 00300 /*! This is used to verify that a sound message from a process 00301 * refers to a current sound. If you imaging a pathological 00302 * process, which rapidly creates and releases sounds, it would 00303 * run through the sndlist ids, possibly before the sound process 00304 * can process the incoming buffers. So this is used to ensure 00305 * that a given message refers to the current sound, and not one 00306 * that was already released and then reassigned. */ 00307 unsigned int sn; 00308 00309 //!the size of a SoundManagerMsg, which is prefixed before each region sent/received by SoundManager (rounded up to nearest even word boundary) 00310 static const unsigned int MSG_SIZE=((sizeof(SoundManagerMsg)-1)/8+1)*8; 00311 00312 #ifdef PLATFORM_APERIOS 00313 //!Storage of each process's subject object, used to internally transmit sound buffers to SoundPlay 00314 OSubject * subjs[ProcessID::NumProcesses]; 00315 #else //PLATFORM_LOCAL 00316 //!Storage of each process's attachment of the message queue, used to internally transmit sound buffers to SoundPlay 00317 MessageQueueBase * subjs[ProcessID::NumProcesses]; 00318 #endif 00319 00320 private: 00321 SoundManager(const SoundManager&); //!< don't call 00322 SoundManager operator=(const SoundManager&); //!< don't call 00323 }; 00324 00325 //! lets you play a sound from anywhere in your code - just a one liner! 00326 extern SoundManager * sndman; 00327 00328 /*! @file 00329 * @brief Describes SoundManager, which provides sound effects and caching services, as well as mixing buffers for the SoundPlay process 00330 * @author ejt (Creator) 00331 */ 00332 00333 #endif |
Tekkotsu v5.1CVS |
Generated Mon May 9 04:58:51 2016 by Doxygen 1.6.3 |