Files
itgmania212121/stepmania/src/RageSound.h
T

226 lines
7.5 KiB
C++
Raw Normal View History

2005-06-08 21:22:08 +00:00
/* RageSound - High-level sound object. */
2004-05-06 02:40:33 +00:00
#ifndef RAGE_SOUND_H
#define RAGE_SOUND_H
2002-12-13 07:44:34 +00:00
2004-04-16 22:28:01 +00:00
#include "RageThreads.h"
#include "RageTimer.h"
#include "RageSoundPosMap.h"
2002-12-13 07:44:34 +00:00
2006-11-29 02:20:10 +00:00
class RageSoundReader;
2006-12-04 22:31:25 +00:00
struct lua_State;
2002-12-27 23:15:39 +00:00
2004-01-15 02:28:58 +00:00
/* Driver interface for sounds: this is what drivers see. */
2004-01-15 02:31:09 +00:00
class RageSoundBase
2004-01-15 02:28:58 +00:00
{
public:
2004-01-15 02:31:09 +00:00
virtual ~RageSoundBase() { }
virtual void SoundIsFinishedPlaying() = 0;
2007-01-27 07:18:52 +00:00
virtual int GetDataToPlay( float *buffer, int size, int64_t &iStreamFrame, int &got_bytes ) = 0;
2006-12-23 10:30:33 +00:00
virtual void CommitPlayingPosition( int64_t iFrameno, int64_t iPosition, int iBytesRead ) = 0;
2004-01-15 02:28:58 +00:00
virtual RageTimer GetStartTime() const { return RageZeroTimer; }
2006-01-22 01:00:06 +00:00
virtual RString GetLoadedFilePath() const = 0;
2004-01-15 02:28:58 +00:00
};
2004-02-28 01:02:06 +00:00
/* These are parameters to play a sound. These are normally changed before playing begins,
* and are constant from then on. */
struct RageSoundParams
2002-12-13 07:44:34 +00:00
{
2004-02-28 01:02:06 +00:00
RageSoundParams();
/* The amount of data to play (or loop): */
float m_StartSecond;
float m_LengthSeconds;
2007-04-06 18:26:09 +00:00
/* Number of seconds to spend fading in. */
float m_fFadeInSeconds;
/* Number of seconds to spend fading out. */
2007-04-06 18:09:46 +00:00
float m_fFadeOutSeconds;
2005-10-02 08:35:46 +00:00
float m_Volume; // multiplies with SOUNDMAN->GetMixVolume()
float m_fAttractVolume; // multiplies with m_Volume
2004-02-28 01:02:06 +00:00
/* Number of samples input and output when changing speed. Currently,
* this is either 1/1, 5/4 or 4/5. */
float m_fPitch;
float m_fSpeed;
2004-02-28 01:02:06 +00:00
/* Optional driver feature: time to actually start playing sounds. If zero, or if not
2005-09-23 00:12:32 +00:00
* supported, the sound will start immediately. */
RageTimer m_StartTime;
2004-02-28 01:02:06 +00:00
2005-06-08 21:22:08 +00:00
/*
2005-09-23 00:12:32 +00:00
* M_STOP stops the sound at the end.
2004-02-28 00:14:16 +00:00
* M_LOOP restarts.
2005-06-08 21:22:08 +00:00
* M_CONTINUE feeds silence, which is useful to continue timing longer than the actual sound.
2005-09-23 00:12:32 +00:00
* M_AUTO (default) stops, obeying filename hints.
2005-06-08 21:22:08 +00:00
*/
2002-12-27 23:15:39 +00:00
enum StopMode_t {
2005-09-23 00:12:32 +00:00
M_STOP,
M_LOOP,
M_CONTINUE,
M_AUTO
2004-02-28 01:02:06 +00:00
} StopMode;
bool m_bIsCriticalSound; // "is a sound that should be played even during attract"
2004-02-28 01:02:06 +00:00
};
2002-12-27 23:15:39 +00:00
struct RageSoundLoadParams
{
RageSoundLoadParams();
/* If true, speed and pitch changes will be supported for this sound, at a
* small memory penalty if not used. */
bool m_bSupportRateChanging;
2006-12-10 00:43:51 +00:00
/* If true, panning will be supported for this sound. */
bool m_bSupportPan;
};
2004-02-28 01:02:06 +00:00
class RageSound: public RageSoundBase
{
public:
2002-12-27 23:15:39 +00:00
RageSound();
~RageSound();
2005-06-08 04:47:03 +00:00
RageSound( const RageSound &cpy );
RageSound &operator=( const RageSound &cpy );
2002-12-27 23:15:39 +00:00
2005-06-08 21:22:08 +00:00
/*
* If bPrecache == true, we'll preload the entire file into memory if
* small enough. If this is done, a large number of copies of the sound
* can be played without much performance penalty. This is useful for
* efficiently playing keyed sounds, and for rapidly-repeating sound
* effects, such as the music wheel.
2003-01-21 02:44:35 +00:00
*
2005-06-08 21:22:08 +00:00
* If cache == false, we'll always stream the sound on demand, which
* makes loads much faster.
2003-01-21 02:44:35 +00:00
*
2002-12-27 23:15:39 +00:00
* If the file failed to load, false is returned, Error() is set
2005-06-08 21:22:08 +00:00
* and a null sample will be loaded. This makes failed loads nonfatal;
2002-12-27 23:15:39 +00:00
* they can be ignored most of the time, so we continue to work if a file
2005-06-08 21:22:08 +00:00
* is broken or missing.
*/
bool Load( RString sFile, bool bPrecache, const RageSoundLoadParams *pParams = NULL );
2005-06-08 04:47:03 +00:00
2005-03-16 04:10:10 +00:00
/*
* Using this version means the "don't care" about caching. Currently,
* this always will not cache the sound; this may become a preference.
*/
2006-01-22 01:00:06 +00:00
bool Load( RString sFile );
2006-11-29 02:20:10 +00:00
/* Load a RageSoundReader that you've set up yourself. Sample rate conversion will
* be set up only if needed. Doesn't fail. */
2006-11-29 02:20:10 +00:00
void LoadSoundReader( RageSoundReader *pSound );
2007-01-16 01:39:04 +00:00
/* Get the loaded RageSoundReader. While playing, only properties can be set. */
RageSoundReader *GetSoundReader() { return m_pSource; }
2002-12-27 23:15:39 +00:00
void Unload();
2004-12-04 21:20:02 +00:00
bool IsLoaded() const;
void DeleteSelfWhenFinishedPlaying();
2002-12-27 23:15:39 +00:00
void StartPlaying();
void StopPlaying();
2006-01-22 01:00:06 +00:00
RString GetError() const { return m_sError; }
2002-12-27 23:15:39 +00:00
2007-01-18 08:32:37 +00:00
void Play( const RageSoundParams *params=NULL );
2007-01-18 08:41:47 +00:00
void PlayCopy( const RageSoundParams *pParams = NULL ) const;
2002-12-27 23:15:39 +00:00
void Stop();
/* Cleanly pause or unpause the sound. If the sound wasn't already playing,
* return true and do nothing. */
bool Pause( bool bPause );
2002-12-27 23:15:39 +00:00
float GetLengthSeconds();
2004-01-18 04:01:52 +00:00
float GetPositionSeconds( bool *approximate=NULL, RageTimer *Timestamp=NULL ) const;
2006-01-22 01:00:06 +00:00
RString GetLoadedFilePath() const { return m_sFilePath; }
2005-06-08 04:47:03 +00:00
bool IsPlaying() const { return m_bPlaying; }
2004-02-28 01:02:06 +00:00
2004-02-28 00:14:16 +00:00
float GetPlaybackRate() const;
RageTimer GetStartTime() const;
void SetParams( const RageSoundParams &p );
const RageSoundParams &GetParams() const { return m_Param; }
2006-12-09 23:54:33 +00:00
bool SetProperty( const RString &sProperty, float fValue );
2002-12-27 23:15:39 +00:00
2006-12-04 22:31:25 +00:00
//
// Lua
//
virtual void PushSelf( lua_State *L );
2002-12-27 23:15:39 +00:00
private:
mutable RageMutex m_Mutex;
2006-11-29 02:20:10 +00:00
RageSoundReader *m_pSource;
2002-12-13 07:44:34 +00:00
/* We keep track of sound blocks we've sent out recently through GetDataToPlay. */
pos_map_queue m_HardwareToStreamMap;
pos_map_queue m_StreamToSourceMap;
2006-01-22 01:00:06 +00:00
RString m_sFilePath;
2002-12-13 07:44:34 +00:00
void ApplyParams();
2004-02-28 01:02:06 +00:00
RageSoundParams m_Param;
2002-12-27 23:15:39 +00:00
2004-04-11 04:01:07 +00:00
/* Current position of the output sound, in frames. If < 0, nothing will play
* until it becomes positive. */
int64_t m_iStreamFrame;
2003-01-25 09:01:42 +00:00
/* Hack: When we stop a playing sound, we can't ask the driver the position
* (we're not playing); and we can't seek back to the current playing position
* when we stop (too slow), but we want to be able to report the position we
* were at when we stopped without jumping to the last position we buffered.
* Keep track of the position after a seek or stop, so we can return a sane
* position when stopped, and when playing but pos_map hasn't yet been filled. */
int m_iStoppedSourceFrame;
2005-06-08 04:47:03 +00:00
bool m_bPlaying;
bool m_bDeleteWhenFinished;
2002-12-13 07:44:34 +00:00
2006-01-22 01:00:06 +00:00
RString m_sError;
2002-12-18 06:44:56 +00:00
int GetSourceFrameFromHardwareFrame( int64_t iHardwareFrame, bool *bApproximate = NULL ) const;
2004-02-28 00:14:16 +00:00
bool SetPositionFrames( int frames = -1 );
RageSoundParams::StopMode_t GetStopMode() const; /* resolves M_AUTO */
2002-12-22 08:13:33 +00:00
void SoundIsFinishedPlaying(); // called by sound drivers
2002-12-13 07:44:34 +00:00
public:
2005-06-08 21:22:08 +00:00
/* These functions are called only by sound drivers. */
2006-12-13 18:40:02 +00:00
/* Returns the number of bytes actually put into pBuffer. If 0 is returned, it
* signals the stream to stop; once it's flushed, SoundStopped will be called.
* Until then, SOUNDMAN->GetPosition can still be called; the sound is still
* playing. */
2007-01-27 07:18:52 +00:00
int GetDataToPlay( float *pBuffer, int iSize, int64_t &iStreamFrame, int &iBytesRead );
2006-12-23 10:02:24 +00:00
void CommitPlayingPosition( int64_t iHardwareFrame, int64_t iStreamFrame, int iGotFrames );
2002-12-13 07:44:34 +00:00
};
#endif
2004-05-06 02:40:33 +00:00
2003-03-15 23:27:34 +00:00
/*
2004-05-06 02:40:33 +00:00
* Copyright (c) 2002-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/