| 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 so 00034 * there's no delay while loading after you request a sound to be 00035 * played. 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 class SoundManager { 00057 public: 00058 //! destructor 00059 virtual ~SoundManager(); 00060 00061 //!constructor, should only be called by the receiving process (SoundPlay) 00062 SoundManager(); 00063 #ifdef PLATFORM_APERIOS 00064 //!Each process needs to call this before it can send sounds to the SoundPlay process 00065 void InitAccess(OSubject* subj); 00066 #else //PLATFORM_LOCAL 00067 //!Each process (except SoundPlay) needs to call this before it can send sounds to the SoundPlay process 00068 void InitAccess(MessageQueueBase& sndbufq); 00069 #endif 00070 00071 //!This is used for referring to sound data so you can start playing it or release it 00072 typedef SoundManagerMsg::Snd_ID Snd_ID; 00073 static const Snd_ID invalid_Snd_ID=(Snd_ID)-1; //!< for reporting errors 00074 static const Snd_ID MAX_SND=50; //!< the number of sounds that can be loaded at any given time 00075 00076 //!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) 00077 typedef unsigned short Play_ID; 00078 static const Play_ID invalid_Play_ID=(Play_ID)-1; //!< for reporting errors 00079 static const Play_ID MAX_PLAY=256; //!< the number of sounds that can be enqueued at the same time (see MixMode_t) 00080 00081 static const unsigned int MAX_NAME_LEN=128; //!<maximum length of a path 00082 00083 //!Used to set the mode for mixing multiple sound channels 00084 /*!Feel free to add a higher quality mixer if you're an audiophile - I'm pretty new to sound processing*/ 00085 enum MixMode_t { 00086 // there's some prototype code for a bit-shifting 'Faster' quality level, but it hasn't been finished... 'Fast' is the default for now. 00087 Fast, //!< uses real division to maintain volume level, without increasing intermediary precision, which causes low-order bit error in exchange for less CPU usage 00088 Quality //!< uses real division to maintain volume level, using an intermediary higher precision buffer for mixing 00089 }; 00090 00091 //! indicates how to handle channel overflow (trying to play more sounds than maximum number of mixing channels). See #queue_mode 00092 enum QueueMode_t { 00093 Enqueue, //!< newer sounds are played when a channel opens up (when old sound finishes) 00094 Pause, //!< newer sounds pause oldest sound, which continues when a channel opens 00095 Stop, //!< newer sounds stop oldest sound 00096 Override, //!< older sounds have play heads advanced, but don't get mixed until a channel opens 00097 }; 00098 00099 //!loads a wav file (if it matches Config::sound_config settings - can't do resampling yet) 00100 /*!Since the SoundManager does the loading, if the same file is being played more than once, only once copy is stored in memory 00101 * @param name can be either a full path, or a partial path relative to Config::sound_config::root 00102 * @return ID number for future references (can also use name) 00103 * The sound data will be cached until releaseFile() or release() is called a matching number of times*/ 00104 Snd_ID loadFile(std::string const &name); 00105 00106 //!loads raw samples from a buffer (assumes matches Config::sound_config settings) 00107 /*!The sound data will be cached until release() is called a matching number of times.\n 00108 * This function is useful for dynamic sound sources. A copy will be made. */ 00109 Snd_ID loadBuffer(const char buf[], unsigned int len); 00110 00111 //!Marks the sound buffer to be released after the last play command completes (or right now if not being played) 00112 void releaseFile(std::string const &name); 00113 00114 //!Marks the sound buffer to be released after the last play command completes (or right now if not being played) 00115 void release(Snd_ID id); 00116 00117 //!play a wav file (if it matches Config::sound_config settings - can't do resampling yet) 00118 /*!Will do a call to loadFile() first, and then automatically release the sound again when complete. 00119 * @param name can be either a full path, or a partial path relative to Config::sound_config::root 00120 * @return ID number for future references 00121 * The sound data will not be cached after done playing unless a call to loadFile is made*/ 00122 Play_ID playFile(std::string const &name); 00123 00124 //!loads raw samples from a buffer (assumes buffer matches Config::sound_config settings) 00125 /*!The sound data will be released after done playing*/ 00126 Play_ID playBuffer(const char buf[], unsigned int len); 00127 00128 //!plays a previously loaded buffer or file 00129 Play_ID play(Snd_ID id); 00130 00131 //!allows automatic queuing of sounds - good for dynamic sound sources! 00132 /*!if you chain more than once to the same base, the new buffers are appended 00133 * to the end of the chain - the new buffer doesn't replace the current chain 00134 * @return @a base - just for convenience of multiple calls*/ 00135 Play_ID chainFile(Play_ID base, std::string const &next); 00136 00137 //!allows automatic queuing of sounds - good for dynamic sound sources! 00138 /*!if you chain more than once to the same base, the new buffers are appended 00139 * to the end of the chain - the new buffer doesn't replace the current chain 00140 * @return @a base - just for convenience of multiple calls*/ 00141 Play_ID chainBuffer(Play_ID base, const char buf[], unsigned int len); 00142 00143 //!allows automatic queuing of sounds - good for dynamic sound sources! 00144 /*!if you chain more than once to the same base, the new buffers are appended 00145 * to the end of the chain - the new buffer doesn't replace the current chain 00146 * @return @a base - just for convenience of multiple calls*/ 00147 Play_ID chain(Play_ID base, Snd_ID next); 00148 00149 //!Lets you stop playback of all sounds 00150 void stopPlay(); 00151 00152 //!Lets you stop playback of a sound 00153 void stopPlay(Play_ID id); 00154 00155 //!Lets you pause playback 00156 void pausePlay(Play_ID id); 00157 00158 //!Lets you resume playback 00159 void resumePlay(Play_ID id); 00160 00161 //!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) 00162 void setMode(unsigned int max_channels, MixMode_t mixer_mode, QueueMode_t queuing_mode); 00163 00164 //!Gives the time until the sound finishes, in milliseconds. Subtract 32 to get guarranteed valid time for this ID. 00165 /*!You should be passing the beginning of a chain to get proper results...\n 00166 * May be slightly conservative (will report too small a time) because this 00167 * does not account for delay until SoundPlay picks up the message that a 00168 * sound has been added.\n 00169 * However, it is slighly optimistic (will report too large a time) because 00170 * it processes a buffer all at one go, so it could mark the sound as finished 00171 * (and cause the ID to go invalid) up to RobotInfo::SoundBufferTime (32 ms) 00172 * before the sound finishes. So subtract SoundBufferTime if you want to be 00173 * absolutely sure the ID will still valid. */ 00174 unsigned int getRemainTime(Play_ID id) const; 00175 00176 #ifdef PLATFORM_APERIOS 00177 //!Copies the sound data to the OPENR buffer, ready to be passed to the system, only called by SoundPlay 00178 /*!@return the number of active sounds */ 00179 unsigned int CopyTo(OSoundVectorData* data); 00180 00181 //!updates internal data structures on the SoundPlay side - you shouldn't be calling this 00182 void ReceivedMsg(const ONotifyEvent& event); 00183 #endif 00184 00185 //!Copies the sound data to the specified memory buffer, ready to be passed to the system 00186 /*!@return the number of active sounds */ 00187 unsigned int CopyTo(void * dest, size_t destSize); 00188 00189 //!updates internal data structures on the SoundPlay side - you shouldn't be calling this 00190 void ProcessMsg(RCRegion * rcr); 00191 00192 //! returns number of sounds currently playing 00193 unsigned int getNumPlaying() { return chanlist.size(); } 00194 00195 //! 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 00196 virtual unsigned int getNextKey() { return sn+1; } 00197 00198 /*! @brief These functions retain an older style first-letter capitalization derived from the OPEN-R 00199 * sample code. You should use the Java-style naming convention instead (first letter lowercase) */ 00200 //!@name Deprecated 00201 Snd_ID LoadFile(std::string const &name) ATTR_deprecated; //!< deprecated, use loadFile() (note first letter lower case) 00202 Snd_ID LoadBuffer(const char buf[], unsigned int len) ATTR_deprecated; //!< deprecated, use loadBuffer() (note first letter lower case) 00203 void ReleaseFile(std::string const &name) ATTR_deprecated; //!< deprecated, use releaseFile() (note first letter lower case) 00204 void Release(Snd_ID id) ATTR_deprecated; //!< deprecated, use release() (note first letter lower case) 00205 Play_ID PlayFile(std::string const &name) ATTR_deprecated; //!< deprecated, use playFile() (note first letter lower case) 00206 Play_ID PlayBuffer(const char buf[], unsigned int len) ATTR_deprecated; //!< deprecated, use playBuffer() (note first letter lower case) 00207 Play_ID Play(Snd_ID id) ATTR_deprecated; //!< deprecated, use play() (note first letter lower case) 00208 Play_ID ChainFile(Play_ID base, std::string const &next) ATTR_deprecated; //!< deprecated, use chainFile() (note first letter lower case) 00209 Play_ID ChainBuffer(Play_ID base, const char buf[], unsigned int len) ATTR_deprecated; //!< deprecated, use chainBuffer() (note first letter lower case) 00210 Play_ID Chain(Play_ID base, Snd_ID next) ATTR_deprecated; //!< deprecated, use chain() (note first letter lower case) 00211 void StopPlay() ATTR_deprecated; //!< deprecated, use stopPlay() (note first letter lower case) 00212 void StopPlay(Play_ID id) ATTR_deprecated; //!< deprecated, use stopPlay() (note first letter lower case) 00213 void PausePlay(Play_ID id) ATTR_deprecated; //!< deprecated, use pausePlay() (note first letter lower case) 00214 void ResumePlay(Play_ID id) ATTR_deprecated; //!< deprecated, use resumePlay() (note first letter lower case) 00215 void SetMode(unsigned int max_channels, MixMode_t mixer_mode, QueueMode_t queuing_mode) ATTR_deprecated; //!< deprecated, use setMode() (note first letter lower case) 00216 unsigned int GetRemainTime(Play_ID id) const ATTR_deprecated; //!< deprecated, use getRemainTime() (note first letter lower case) 00217 unsigned int GetNumPlaying() ATTR_deprecated; //!< deprecated, use getNumPlaying() (note first letter lower case) 00218 //@} 00219 00220 protected: 00221 //!Mixes the channel into the buffer 00222 void mixChannel(Play_ID channelId, void* buf, size_t size); 00223 00224 //!Mixes the channel into the buffer additively 00225 /*!If mode is Quality, then the size of the buffer should be double the normal 00226 * size. */ 00227 void mixChannelAdditively(Play_ID channelId, int bitsPerSample, MixMode_t mode, short scalingFactor, void* buf, size_t size); 00228 00229 //!The intermediate mixer buffer used for Quality mode mixing 00230 int* mixerBuffer; 00231 00232 //!Size (in bytes) of the intermediate mixer buffer 00233 size_t mixerBufferSize; 00234 00235 //!Sets up a shared region to hold a sound - rounds to nearest page size 00236 RCRegion* initRegion(unsigned int size); 00237 00238 //!Looks to see if @a name matches any of the sounds in sndlist (converts to absolute path if not already) 00239 Snd_ID lookupPath(std::string const &name) const; 00240 00241 //!selects which of the channels are actually to be mixed together, depending on queue_mode 00242 void selectChannels(std::vector<Play_ID>& mix); 00243 00244 //!update the offsets of sounds which weren't mixed (when needed depending on queue_mode) 00245 void updateChannels(const std::vector<Play_ID>& mixs,size_t used); 00246 00247 //!called when a buffer end is reached, may reset buffer to next in chain, or just stopPlay() 00248 bool endPlay(Play_ID id); 00249 00250 //!Holds data about the loaded sounds 00251 struct SoundData { 00252 SoundData(); //!<constructor 00253 RCRegion * rcr; //!<shared region - don't need to share among processes, just collect in SoundPlay 00254 byte* data; //!<point to data in region (for convenience, only valid in SoundPlay) 00255 unsigned int len; //!<size of the sound 00256 unsigned int ref; //!<reference counter 00257 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 00258 char name[SoundManager::MAX_NAME_LEN]; //!<stores the path to the file, empty if from a buffer 00259 private: 00260 SoundData(const SoundData&); //!< don't call 00261 SoundData operator=(const SoundData&); //!< don't call 00262 }; 00263 //!For convenience 00264 typedef ListMemBuf<SoundData,MAX_SND,Snd_ID> sndlist_t; 00265 //!Holds a list of all currently loaded sounds 00266 sndlist_t sndlist; 00267 00268 //!Holds data about sounds currently being played 00269 struct PlayState { 00270 PlayState(); //!<constructor 00271 Snd_ID snd_id; //!<index of sound 00272 unsigned int offset; //!<position in the sound 00273 unsigned int cumulative;//!<total time of playing (over queued sounds) 00274 Play_ID next_id; //!<lets you queue for continuous sound, or loop 00275 }; 00276 //!For convenience 00277 typedef ListMemBuf<PlayState,MAX_PLAY,Play_ID> playlist_t; 00278 //!Holds a list of all sounds currently enqueued 00279 playlist_t playlist; 00280 //!For convenience 00281 typedef ListMemBuf<Play_ID,MAX_PLAY,Play_ID> chanlist_t; 00282 //!Holds a list of all currently playing sounds, ordered newest (front) to oldest(back) 00283 chanlist_t chanlist; 00284 00285 //!Current mixing mode, set by setMode(); 00286 MixMode_t mix_mode; 00287 00288 //!Current queuing mode, set by setMode(); 00289 QueueMode_t queue_mode; 00290 00291 //!Current maximum number of sounds to mix together 00292 unsigned int max_chan; 00293 00294 //!Prevents multiple processes from accessing at the same time 00295 mutable MutexLock<ProcessID::NumProcesses> lock; 00296 00297 //!A serial number, incremented for each sound which is created 00298 /*! This is used to verify that a sound message from a process 00299 * refers to a current sound. If you imaging a pathological 00300 * process, which rapidly creates and releases sounds, it would 00301 * run through the sndlist ids, possibly before the sound process 00302 * can process the incoming buffers. So this is used to ensure 00303 * that a given message refers to the current sound, and not one 00304 * that was already released and then reassigned. */ 00305 unsigned int sn; 00306 00307 //!the size of a SoundManagerMsg, which is prefixed before each region sent/received by SoundManager (rounded up to nearest even word boundary) 00308 static const unsigned int MSG_SIZE=((sizeof(SoundManagerMsg)-1)/8+1)*8; 00309 00310 #ifdef PLATFORM_APERIOS 00311 //!Storage of each process's subject object, used to internally transmit sound buffers to SoundPlay 00312 OSubject * subjs[ProcessID::NumProcesses]; 00313 #else //PLATFORM_LOCAL 00314 //!Storage of each process's attachment of the message queue, used to internally transmit sound buffers to SoundPlay 00315 MessageQueueBase * subjs[ProcessID::NumProcesses]; 00316 #endif 00317 00318 private: 00319 SoundManager(const SoundManager&); //!< don't call 00320 SoundManager operator=(const SoundManager&); //!< don't call 00321 }; 00322 00323 //! lets you play a sound from anywhere in your code - just a one liner! 00324 extern SoundManager * sndman; 00325 00326 /*! @file 00327 * @brief Describes SoundManager, which provides sound effects and caching services, as well as mixing buffers for the SoundPlay process 00328 * @author ejt (Creator) 00329 * 00330 * $Author: ejt $ 00331 * $Name: tekkotsu-3_0 $ 00332 * $Revision: 1.8 $ 00333 * $State: Exp $ 00334 * $Date: 2006/09/29 16:56:10 $ 00335 */ 00336 00337 #endif |
|
Tekkotsu v3.0 |
Generated Wed Oct 4 00:03:46 2006 by Doxygen 1.4.7 |