Files
itgmania212121/stepmania/src/RageSound.h
T

146 lines
4.1 KiB
C++
Raw Normal View History

2002-12-13 07:44:34 +00:00
#ifndef RAGE_SOUND_OBJ_H
#define RAGE_SOUND_OBJ_H
#include "SDL_sound-1.0.0/SDL_sound.h"
#include "RageThreads.h"
#include "RageSoundManager.h"
#include <deque>
2002-12-13 07:44:34 +00:00
class CircBuf
{
string buf;
unsigned cnt, start;
public:
CircBuf() { clear(); }
unsigned size() const { return cnt; }
unsigned capacity() const { return buf.size(); }
void reserve(unsigned n);
void clear();
void write(const char *buf, unsigned size);
void read(char *buf, unsigned size);
};
class RageSound
{
2002-12-22 08:13:33 +00:00
/* If we were copied from another RageSound, this will point to it; otherwise
* this is ourself. */
RageSound *original;
2002-12-13 07:44:34 +00:00
/* These are only used when big == true: */
struct stream_t {
Sound_Sample *Sample;
int FillBuf(int bytes);
CircBuf buf;
} stream;
/* These are only used when big == false: */
basic_string<char> full_buf;
/* If true, we're streaming data through sample and buf. If false, the entire
* file is pre-loaded into full_buf. */
bool big;
/* Sound blocks we've sent out recently through GetPCM. We keep track
* of each block for the last four calls of GetPCM. */
struct pos_map_t {
/* Sample number from the POV of the sound driver: */
int sampleno;
/* Actual sound position within the sample: */
int position;
/* The number of samples in this block: */
int samples;
pos_map_t(int samp, int pos, int cnt) { sampleno=samp, position=pos; samples=cnt; }
};
deque<pos_map_t> pos_map;
2002-12-13 07:44:34 +00:00
CString m_sFilePath;
// float m_Rate;
2002-12-18 06:44:56 +00:00
/* The amount of data to play (or loop): */
2002-12-20 02:00:43 +00:00
int m_StartSample, m_LengthSamples;
2002-12-13 07:44:34 +00:00
bool Loop;
/* Current position of the output sound; if < 0, nothing will play until it
* becomes positive. This is recorded in samples, to avoid rounding error. */
int position;
bool playing;
float speed;
2002-12-19 04:08:38 +00:00
bool AccurateSync;
2002-12-18 06:44:56 +00:00
/* If true, the sound will stop when it reaches the end; otherwise it'll
* continue to move forward, feeding silence, which is useful to continue
* timing longer than the actual sound. (However, if this is false, the
* sound will never stop on its own unless destructed; it must be explitly
* stopped, and PlayCopy can't be used.) Default is true. Ignored when looping. */
bool AutoStop;
2002-12-22 08:13:33 +00:00
void SetPositionSamples( int samples = -1 );
2002-12-13 07:44:34 +00:00
public:
RageSound();
~RageSound();
RageSound(const RageSound &cpy);
2002-12-22 08:13:33 +00:00
/* Used by RageSoundManager: */
RageSound *GetOriginal() { return original; }
2002-12-20 01:45:41 +00:00
/* Called only by the sound drivers: */
/* This function should return the number of bytes actually put into buffer.
* If less than size 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). */
int GetPCM(char *buffer, int size, int sampleno);
void Update(float delta);
/* User API from here on: */
2002-12-13 07:44:34 +00:00
/* If cache is true, we'll preload the entire file into memory if it's
* small enough. False is only generally used when we're going to do
* operations on a file but not actually play it (eg. to find out
* its length). */
void Load(CString fn, bool cache = true);
void LoadAndPlayIfNotAlready( CString sSoundFilePath );
void Unload();
2002-12-20 01:45:41 +00:00
/* If enabled, then the sound will automatically stop when it reaches
2002-12-22 08:13:33 +00:00
* the end; otherwise it'll feed silence until stopped manually. */
2002-12-18 06:44:56 +00:00
void SetAutoStop(bool yes=true) { AutoStop=yes; }
bool GetAutoStop() const { return AutoStop; }
2002-12-20 01:45:41 +00:00
2002-12-18 06:44:56 +00:00
void SetStartSeconds(float secs = 0); /* default = beginning */
void SetLengthSeconds(float secs = -1); /* default = no length limit */
2002-12-22 08:13:33 +00:00
void StartPlaying();
// void Pause();
void StopPlaying();
RageSound *Play();
2002-12-13 07:44:34 +00:00
void Stop();
2002-12-22 08:13:33 +00:00
2002-12-13 07:44:34 +00:00
float GetLengthSeconds();
2002-12-18 06:44:56 +00:00
float GetPositionSeconds() const;
2002-12-22 08:13:33 +00:00
void SetPositionSeconds( float fSeconds = -1);
2002-12-19 04:08:38 +00:00
void SetAccurateSync(bool yes=true) { AccurateSync = yes; }
2002-12-13 07:44:34 +00:00
void SetPlaybackRate( float fScale );
float GetPlaybackRate() const { return speed; }
bool IsPlaying() const { return playing; }
2002-12-18 06:44:56 +00:00
CString GetLoadedFilePath() const { return m_sFilePath; }
void SetLooping(bool yes=true) { Loop=yes; }
bool GetLooping() const { return Loop; }
2002-12-18 06:55:39 +00:00
/* Query only: */
bool IsStreaming() const { return big; }
2002-12-13 07:44:34 +00:00
};
#endif