OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4MusicFile.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 /* Handles Music Files */
17 
18 #ifndef INC_C4MusicFile
19 #define INC_C4MusicFile
20 
23 
24 /* Base class */
25 
27 {
28 public:
29 
31  pNext(nullptr), LastPlayed(-1), NoPlay(false), loop(false), SongExtracted(false), announced(false)
32  { }
33  virtual ~C4MusicFile() { }
34 
35  // data
36  char FileName[_MAX_FNAME +1];
39  bool NoPlay;
40  bool loop;
41  bool announced;
42 
43  virtual bool Init(const char *strFile);
44  virtual bool Play(bool loop = false, double max_resume_time = 0.0) = 0;
45  virtual void Stop(int fadeout_ms = 0) = 0;
46  virtual void CheckIfPlaying() = 0;
47  virtual void SetVolume(int) = 0;
48  virtual bool HasCategory(const char *szcat) const { return false; }
49  virtual double GetRemainingTime() { return 0.0; }
50  virtual bool HasResumePos() const { return false; }
51  virtual void ClearResumePos() { }
53 
54  virtual StdStrBuf GetDebugInfo() const { return StdStrBuf(FileName); }
55 
56  bool IsLooping() const { return loop; }
57 
58  bool HasBeenAnnounced() const { return announced; }
59  void Announce();
60 
61 protected:
62 
63  // helper: copy data to a (temp) file
64  bool ExtractFile();
65  bool RemTempFile(); // remove the temp file
66 
68 
69 };
70 
71 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
72 typedef struct _Mix_Music Mix_Music;
74 {
75 public:
78  bool Play(bool loop = false, double max_resume_time = 0.0);
79  void Stop(int fadeout_ms = 0);
80  void CheckIfPlaying();
81  void SetVolume(int);
82 protected:
83  char *Data;
85 };
86 
87 #elif AUDIO_TK == AUDIO_TK_OPENAL
88 
89 class C4MusicFileOgg : public C4MusicFile
90 {
91 public:
92  C4MusicFileOgg();
93  ~C4MusicFileOgg();
94  void Clear();
95  virtual bool Init(const char *strFile);
96  bool Play(bool loop = false, double max_resume_time = 0.0);
97  void Stop(int fadeout_ms = 0);
98  void CheckIfPlaying();
99  void SetVolume(int);
100  virtual bool HasCategory(const char *szcat) const;
101  double GetRemainingTime();
102  bool HasResumePos() const { return (last_playback_pos_sec > 0); }
103  void ClearResumePos() { last_playback_pos_sec = 0.0; last_interruption_time = C4TimeMilliseconds(); }
104  C4TimeMilliseconds GetLastInterruptionTime() const { return last_interruption_time; }
105  virtual StdStrBuf GetDebugInfo() const;
106 private:
107  enum { num_buffers = 4, buffer_size = 160*1024 };
110  OggVorbis_File ogg_file;
111  bool is_loading_from_file;
112  CStdFile source_file;
113  long last_source_file_pos; // remember position in source_file because it may be closed and re-opened
114  bool playing, streaming_done, loaded;
115  ALuint buffers[num_buffers];
116  ALuint channel;
117  double last_playback_pos_sec; // last playback position for resume when fading between pieces
118  C4TimeMilliseconds last_interruption_time; // set to nonzero when song is interrupted
119  int current_section;
120  size_t byte_pos_total;
121  float volume;
122  std::vector<StdCopyStrBuf> categories; // cateogries stored in meta info
123 
124  bool FillBuffer(size_t idx);
125  void Execute(); // fill processed buffers
126  void UnprepareSourceFileReading(); // close file handle but remember buffer position for re-opening
127  bool PrepareSourceFileReading(); // close file handle but remember buffer position for re-opening
128 
129  friend class C4SoundLoaders::VorbisLoader;
130 };
131 
132 #endif
133 
134 #endif
virtual StdStrBuf GetDebugInfo() const
Definition: C4MusicFile.h:54
void Announce()
Definition: C4MusicFile.cpp:41
bool Play(bool loop=false, double max_resume_time=0.0)
Definition: C4MusicFile.cpp:85
void CheckIfPlaying()
C4MusicFile * pNext
Definition: C4MusicFile.h:37
virtual bool Play(bool loop=false, double max_resume_time=0.0)=0
virtual void Stop(int fadeout_ms=0)=0
virtual void CheckIfPlaying()=0
bool ExtractFile()
Definition: C4MusicFile.cpp:47
void SetVolume(int)
bool RemTempFile()
Definition: C4MusicFile.cpp:58
bool announced
Definition: C4MusicFile.h:41
bool HasBeenAnnounced() const
Definition: C4MusicFile.h:58
void Stop(int fadeout_ms=0)
virtual bool Init(const char *strFile)
Definition: C4MusicFile.cpp:67
struct _Mix_Music Mix_Music
Definition: C4MusicFile.h:72
virtual ~C4MusicFile()
Definition: C4MusicFile.h:33
bool IsLooping() const
Definition: C4MusicFile.h:56
#define _MAX_FNAME
virtual double GetRemainingTime()
Definition: C4MusicFile.h:49
virtual bool HasCategory(const char *szcat) const
Definition: C4MusicFile.h:48
virtual bool HasResumePos() const
Definition: C4MusicFile.h:50
virtual C4TimeMilliseconds GetLastInterruptionTime() const
Definition: C4MusicFile.h:52
int LastPlayed
Definition: C4MusicFile.h:38
virtual void ClearResumePos()
Definition: C4MusicFile.h:51
bool SongExtracted
Definition: C4MusicFile.h:67
Mix_Music * Music
Definition: C4MusicFile.h:84
virtual void SetVolume(int)=0
char FileName[_MAX_FNAME+1]
Definition: C4MusicFile.h:36