SDXFrameWork  0.10
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Music.h
1 //Copyright © 2014 SDXFramework
2 //[License]GNU Affero General Public License, version 3
3 //[Contact]http://sourceforge.jp/projects/dxframework/
4 #pragma once
5 #include <Multimedia/SDX.h>
6 #include <Multimedia/Sound.h>
7 #include <chrono>
8 
9 namespace SDX
10 {
14  class Music
15  {
16  friend class System;
17  private:
18  static Music *active;
19  static Music *next;
20  static bool nextLoop;
21  static bool nextRestart;
22 
23  Mix_Music* handle = nullptr;
24  std::string fileName;
25  int volume;
26 
27  int fadeInTime = 0;
28  int fadeOutTime = 0;
29 
30  std::chrono::system_clock::time_point startTime;//再生開始時刻
31  double restartPosition = 0;
32 
35  static void Finished()
36  {
37  //ここでは再生処理等をしてはいけない
38  //途中で終了した場合
39  auto diff = std::chrono::system_clock::now() - active->startTime;
40  active->restartPosition += (double)std::chrono::duration_cast<std::chrono::milliseconds>(diff).count() / 1000;
41  active = nullptr;
42  }
43 
44  public:
45  Music(){};
46 
47  ~Music(){}
48 
50  Music(const char *ファイル名, double 音量 = 1.0)
51  {
52  Music::Load(ファイル名, 音量);
53  }
54 
57  bool Load(const char *ファイル名, double 音量 = 1.0)
58  {
59  if (Loading::isLoading)
60  {
61  Loading::AddLoading([=]{ Load(ファイル名, 音量); });
62  return true;
63  }
64 
65  if (handle != nullptr){ return false; }
66 
67  fileName = ファイル名;
68  handle = Mix_LoadMUS(ファイル名);
69  volume = int(音量 * 255);
70 
71  return true;
72  }
73 
75  bool Destroy()
76  {
77  if (handle == nullptr){ return false; }
78 
79  Mix_FreeMusic(handle);
80  return true;
81  }
82 
85  bool Play( bool ループ再生フラグ = true )
86  {
87  next = nullptr;
88  if (handle == nullptr){ return false; }
89 
90  //現在再生中のBGMにfadeOutTimeが設定されている場合
91  if (active && active->fadeOutTime > 0)
92  {
93  next = this;
94  nextLoop = ループ再生フラグ;
95  nextRestart = false;
96  Stop();
97  return true;
98  }
99 
100  int isLoop = ( 1 - ループ再生フラグ * 2);
101 
102  if (fadeInTime <= 0)
103  {
104  Mix_PlayMusic(handle, isLoop );
105  }
106  else
107  {
108  Mix_FadeInMusic(handle, isLoop , fadeInTime);
109  }
110 
111  startTime = std::chrono::system_clock::now();
112  Mix_VolumeMusic(volume / 2);
113  active = this;
114 
115  return true;
116  }
117 
120  bool Restart(bool ループ再生フラグ = true)
121  {
122  next = nullptr;
123  if (handle == nullptr){ return false; }
124 
125  //現在再生中のBGMにfadeOutTimeが設定されている場合
126  if (active && active->fadeOutTime > 0)
127  {
128  next = this;
129  nextLoop = ループ再生フラグ;
130  nextRestart = true;
131  Stop();
132  return true;
133  }
134  else if ( active )
135  {
136  //止めてから再生する
137  Stop();
138  }
139 
140  int isLoop = ( 1 - ループ再生フラグ * 2);
141 
142  if (fadeInTime <= 0)
143  {
144  Mix_PlayMusic(handle, isLoop);
145  if (restartPosition > 0)
146  {
147  Mix_SetMusicPosition(restartPosition);
148  }
149  }
150  else
151  {
152  Mix_FadeInMusicPos(handle, isLoop, fadeInTime, restartPosition);
153  }
154 
155  startTime = std::chrono::system_clock::now();
156  Mix_VolumeMusic(volume / 2);
157  active = this;
158 
159  return true;
160  }
161 
163  void SetVolume(double 音量)
164  {
165  volume = int(音量 * 255);
166  }
167 
170  void SetFadeInTime(int フェードイン時間)
171  {
172  fadeInTime = std::max(0,フェードイン時間);
173  }
174 
177  void SetFadeOutTime(int フェードアウト時間)
178  {
179  fadeOutTime = std::max(0,フェードアウト時間);
180  }
181 
184  static bool Check()
185  {
186  //何故かこの関数だけ成功時は1
187  return (Mix_PlayingMusic() == 1);
188  }
189 
192  static bool Stop()
193  {
194  if (!Check()){ return false; }
195 
196  if (active->fadeOutTime <= 0)
197  {
198  Mix_HaltMusic();
199  }
200  else
201  {
202  Mix_FadeOutMusic( active->fadeOutTime );
203  }
204 
205  return true;
206  }
207 
210  static void ChangeVolume(double 音量)
211  {
212  Mix_VolumeMusic(int(音量 * 255));
213  }
214 
217  static bool Update()
218  {
219  if (!active && next)
220  {
221  if (nextRestart)
222  {
223  return next->Restart(nextLoop);
224  }
225  else
226  {
227  return next->Play(nextLoop);
228  }
229  }
230  return false;
231  }
232 
233  };
234 }
void SetFadeInTime(int フェードイン時間)
再生時のフェードイン時間を設定[ミリ秒].
Definition: Music.h:170
void SetFadeOutTime(int フェードアウト時間)
停止時のフェードアウト時間を設定[ミリ秒].
Definition: Music.h:177
bool Destroy()
音声ファイルを解放.
Definition: Music.h:75
ライブラリの初期化やシステム的な処理を行う関数群.
Definition: System.h:13
bool Play(bool ループ再生フラグ=true)
音声ファイルを先頭から再生.
Definition: Music.h:85
static bool Stop()
再生中のMusicを停止.
Definition: Music.h:192
bool Load(const char *ファイル名, double 音量=1.0)
音声ファイルを登録.
Definition: Music.h:57
static void AddLoading(std::function< void(void)> &&読み込み関数)
非同期読み込み処理に追加.
Definition: Loading.h:96
BGM用音声を表すクラス.
Definition: Music.h:14
static bool Update()
fadeOut付きで終了した後に次Musicを再生するための処理.
Definition: Music.h:217
static void ChangeVolume(double 音量)
再生中の音量を変更.
Definition: Music.h:210
bool Restart(bool ループ再生フラグ=true)
前回停止した位置から再生.
Definition: Music.h:120
void SetVolume(double 音量)
0~1.0で音量を設定.
Definition: Music.h:163
static bool Check()
再生中か確認.
Definition: Music.h:184
Music(const char *ファイル名, double 音量=1.0)
音声ファイルを登録.
Definition: Music.h:50