SDXFrameWork  0.13
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Sound.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/Loading.h>
7 
8 namespace SDX
9 {
13  class Sound
14  {
15  private:
16  Mix_Chunk* handle;
17  int leftPan = 255;
18  int rightPan = 255;
19 
20  double volume = 1.0;
21  int angle = 0;
22  int distance = -1;
23  public:
24  Sound(){}
25 
26  ~Sound(){}
27 
29  Sound(const char *ファイル名, double 音量 = 1.0)
30  {
31  Load(ファイル名);
32  }
33 
35  bool Load(const char *ファイル名, double 音量 = 1.0)
36  {
37  if (Loading::isLoading)
38  {
39  Loading::AddLoading([=]{ Load(ファイル名,音量); });
40  return true;
41  }
42 
43  if (handle != nullptr){ return false; }
44 
45  handle = Mix_LoadWAV(ファイル名);
46  if (!handle){ return false; }
47 
48  volume = std::min(音量,1.0);
49 
50  Mix_VolumeChunk(handle, int(volume * 128));
51  return true;
52  }
53 
55  bool Release()
56  {
57  if (handle == nullptr){ return false; }
58  Mix_FreeChunk(handle);
59  return true;
60  }
61 
63  Mix_Chunk* GetHandle() const
64  {
65  return this->handle;
66  }
67 
69  bool Play() const
70  {
71  //static int channel = 0;
72  //channel = (++channel) % 2;
73  Mix_PlayChannel(0, handle, 0);
74 
75  if (distance >= 0)
76  {
77  Mix_SetPosition(0, angle, distance);
78  }
79  else if (leftPan < 255 || rightPan < 255)
80  {
81  Mix_SetPanning(0, leftPan, rightPan);
82  }
83 
84  return true;
85  }
86 
88  bool SetVolume(double 音量)
89  {
90  volume = std::max(音量, 0.0);
91  volume = std::min(音量, 1.0);
92  Mix_VolumeChunk(handle, int(音量 * MIX_MAX_VOLUME));
93  return true;
94  }
95 
97  double GetVolume()
98  {
99  return volume;
100  }
101 
104  void SetPanning(double 左パン = 1.0, double 右パン = 1.0)
105  {
106  leftPan = std::min(int(左パン*255),255);
107  rightPan = std::min(int(右パン*255),255);
108  }
109 
115  void Set3DEffect(double 距離 = -1, double 角度 = 0)
116  {
117  distance = std::min((int)距離*255,255);
118  angle = int(角度 * 180 / PAI);//℃に変換
119  if (angle < 0)
120  {
121  angle = 360 -(std::abs(angle) % 360);
122  }
123  }
124 
127  static void SetMainVolume(double 音量)
128  {
129  //全チャンネルの音量を変更
130  Mix_Volume(-1,int(音量 * MIX_MAX_VOLUME));
131  }
132  };
133 }
const double PAI
円周率
Definition: SDX.h:26
効果音用音声を表すクラス.
Definition: Sound.h:13
static void SetMainVolume(double 音量)
全体の音量を0~1.0の範囲で設定.
Definition: Sound.h:127
Mix_Chunk * GetHandle() const
ハンドルを取得.
Definition: Sound.h:63
void Set3DEffect(double 距離=-1, double 角度=0)
3D音声再生効果を付ける.
Definition: Sound.h:115
double GetVolume()
音量を取得.
Definition: Sound.h:97
static void AddLoading(std::function< void(void)> &&読み込み関数)
非同期読み込み処理に追加.
Definition: Loading.h:96
Sound(const char *ファイル名, double 音量=1.0)
音声ファイルをメモリに読み込む.
Definition: Sound.h:29
bool Load(const char *ファイル名, double 音量=1.0)
音声ファイルをメモリに読み込む.
Definition: Sound.h:35
bool SetVolume(double 音量)
音量を0~1.0の範囲で設定.
Definition: Sound.h:88
bool Play() const
音声ファイルを再生.
Definition: Sound.h:69
bool Release()
音声ファイルをメモリから開放.
Definition: Sound.h:55
void SetPanning(double 左パン=1.0, double 右パン=1.0)
音声パンを設定.
Definition: Sound.h:104