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 
30  C4MusicFile() = default;
31  virtual ~C4MusicFile() = default;
32 
33  // data
34  char FileName[_MAX_FNAME +1];
35  C4MusicFile *pNext{nullptr};
36  int LastPlayed{-1};
37  bool NoPlay{false};
38  bool loop{false};
39  bool announced{false};
40 
41  virtual bool Init(const char *strFile);
42  virtual bool Play(bool loop = false, double max_resume_time = 0.0) = 0;
43  virtual void Stop(int fadeout_ms = 0) = 0;
44  virtual void CheckIfPlaying() = 0;
45  virtual void SetVolume(int) = 0;
46  virtual bool HasCategory(const char *szcat) const { return false; }
47  virtual double GetRemainingTime() { return 0.0; }
48  virtual bool HasResumePos() const { return false; }
49  virtual void ClearResumePos() { }
51 
52  virtual StdStrBuf GetDebugInfo() const { return StdStrBuf(FileName); }
53 
54  bool IsLooping() const { return loop; }
55 
56  bool HasBeenAnnounced() const { return announced; }
57  void Announce();
58 
59 protected:
60 
61  // helper: copy data to a (temp) file
62  bool ExtractFile();
63  bool RemTempFile(); // remove the temp file
64 
65  bool SongExtracted{false};
66 
67 };
68 
69 #if AUDIO_TK == AUDIO_TK_SDL_MIXER
70 typedef struct _Mix_Music Mix_Music;
72 {
73 public:
75  ~C4MusicFileSDL() override;
76  bool Play(bool loop = false, double max_resume_time = 0.0) override;
77  void Stop(int fadeout_ms = 0) override;
78  void CheckIfPlaying() override;
79  void SetVolume(int) override;
80 protected:
81  char *Data;
83 };
84 
85 #elif AUDIO_TK == AUDIO_TK_OPENAL
86 
87 class C4MusicFileOgg : public C4MusicFile
88 {
89 public:
90  C4MusicFileOgg();
91  ~C4MusicFileOgg() override;
92  void Clear();
93  bool Init(const char *strFile) override;
94  bool Play(bool loop = false, double max_resume_time = 0.0) override;
95  void Stop(int fadeout_ms = 0) override;
96  void CheckIfPlaying() override;
97  void SetVolume(int) override;
98  bool HasCategory(const char *szcat) const override;
99  double GetRemainingTime() override;
100  bool HasResumePos() const override { return (last_playback_pos_sec > 0); }
101  void ClearResumePos() override { last_playback_pos_sec = 0.0; last_interruption_time = C4TimeMilliseconds(); }
102  C4TimeMilliseconds GetLastInterruptionTime() const override { return last_interruption_time; }
103  StdStrBuf GetDebugInfo() const override;
104 private:
105  enum { num_buffers = 4, buffer_size = 160*1024 };
108  OggVorbis_File ogg_file;
109  bool is_loading_from_file{false};
110  CStdFile source_file;
111  long last_source_file_pos{0}; // remember position in source_file because it may be closed and re-opened
112  bool playing{false}, streaming_done{false}, loaded{false};
113  ALuint buffers[num_buffers];
114  ALuint channel{0};
115  double last_playback_pos_sec{0}; // last playback position for resume when fading between pieces
116  C4TimeMilliseconds last_interruption_time; // set to nonzero when song is interrupted
117  int current_section{0};
118  size_t byte_pos_total{0};
119  float volume{1.0f};
120  std::vector<StdCopyStrBuf> categories; // cateogries stored in meta info
121 
122  bool FillBuffer(size_t idx);
123  void Execute(); // fill processed buffers
124  void UnprepareSourceFileReading(); // close file handle but remember buffer position for re-opening
125  bool PrepareSourceFileReading(); // close file handle but remember buffer position for re-opening
126 
127  friend class C4SoundLoaders::VorbisLoader;
128 };
129 
130 #endif
131 
132 #endif
virtual StdStrBuf GetDebugInfo() const
Definition: C4MusicFile.h:52
void Announce()
Definition: C4MusicFile.cpp:40
virtual ~C4MusicFile()=default
C4MusicFile * pNext
Definition: C4MusicFile.h:35
virtual bool Play(bool loop=false, double max_resume_time=0.0)=0
C4MusicFile()=default
virtual void Stop(int fadeout_ms=0)=0
virtual void CheckIfPlaying()=0
bool ExtractFile()
Definition: C4MusicFile.cpp:46
bool RemTempFile()
Definition: C4MusicFile.cpp:57
bool announced
Definition: C4MusicFile.h:39
bool HasBeenAnnounced() const
Definition: C4MusicFile.h:56
void Stop(int fadeout_ms=0) override
virtual bool Init(const char *strFile)
Definition: C4MusicFile.cpp:66
struct _Mix_Music Mix_Music
Definition: C4MusicFile.h:70
bool IsLooping() const
Definition: C4MusicFile.h:54
#define _MAX_FNAME
~C4MusicFileSDL() override
Definition: C4MusicFile.cpp:79
virtual double GetRemainingTime()
Definition: C4MusicFile.h:47
bool Play(bool loop=false, double max_resume_time=0.0) override
Definition: C4MusicFile.cpp:84
void CheckIfPlaying() override
virtual bool HasCategory(const char *szcat) const
Definition: C4MusicFile.h:46
virtual bool HasResumePos() const
Definition: C4MusicFile.h:48
virtual C4TimeMilliseconds GetLastInterruptionTime() const
Definition: C4MusicFile.h:50
int LastPlayed
Definition: C4MusicFile.h:36
void SetVolume(int) override
virtual void ClearResumePos()
Definition: C4MusicFile.h:49
bool SongExtracted
Definition: C4MusicFile.h:65
Mix_Music * Music
Definition: C4MusicFile.h:82
virtual void SetVolume(int)=0
char FileName[_MAX_FNAME+1]
Definition: C4MusicFile.h:34