Lib
QOLを高める
sound.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "util.h"
4 #include "file.h"
5 #include <xaudio2.h>
6 #include <vorbis/vorbisfile.h>
7 #include <unordered_map>
8 
9 namespace yappy {
11 namespace sound {
12 
13 // 3MiB
14 const uint32_t SoundFileSizeMax = 3 * 1024 * 1024;
15 const uint32_t SoundEffectPlayMax = 64;
16 
17 const uint32_t BgmOvReadSize = 4096;
18 const uint32_t BgmBufferSize = 4096 * 16;
19 const uint32_t BgmBufferCount = 2;
20 
21 #pragma region Deleters
22 struct hmmioDeleter {
23  using pointer = HMMIO;
24  void operator()(HMMIO hMmio)
25  {
26  ::mmioClose(hMmio, 0);
27  }
28 };
29 
30 struct voiceDeleter {
31  void operator()(IXAudio2Voice *pv)
32  {
33  pv->DestroyVoice();
34  }
35 };
36 
38  void operator()(OggVorbis_File *ovf)
39  {
40  ov_clear(ovf);
41  }
42 };
43 #pragma endregion
44 
45 struct SoundEffect : private util::noncopyable {
46  WAVEFORMATEX format;
48  SoundEffect() = default;
49  ~SoundEffect() = default;
50 };
51 
52 struct Bgm : private util::noncopyable {
53  using OggFilePtr = std::unique_ptr<OggVorbis_File, oggFileDeleter>;
54 
55  Bgm(file::Bytes &&ovFileBin);
56  ~Bgm() = default;
57 
58  OggVorbis_File *ovFp() const { return m_ovFp.get(); }
59 
60 private:
61  file::Bytes m_ovFileBin;
62  uint32_t m_readPos;
63  OggVorbis_File m_ovFile;
64  // for public interface (auto close pointer to m_ovFile)
65  OggFilePtr m_ovFp;
66  // for OggVorbis_File callback (datasource == this)
67  static size_t read(void *ptr, size_t size, size_t nmemb, void *datasource);
68  static int seek(void *datasource, int64_t offset, int whence);
69  static long tell(void *datasource);
70  static int close(void *datasource);
71 };
72 
73 class XAudio2 : private util::noncopyable {
74 public:
75  using SeResource = const SoundEffect;
76  using SeResourcePtr = std::shared_ptr<SeResource>;
77  using BgmResource = Bgm;
78  using BgmResourcePtr = std::shared_ptr<BgmResource>;
79 
80  XAudio2();
81  ~XAudio2();
82 
83  void processFrame();
84 
85  // Sound Effect
86  SeResourcePtr loadSoundEffect(const wchar_t *path);
87  void playSoundEffect(const SeResourcePtr &se);
88  bool isPlayingAnySoundEffect() const;
89  void stopAllSoundEffect();
90 
91  // BGM
92  BgmResourcePtr loadBgm(const wchar_t *path);
93  void playBgm(const BgmResourcePtr &bgm);
94  void stopBgm();
95 
96 private:
97  using IXAudio2Ptr = util::ComPtr<IXAudio2>;
98  using SourceVoicePtr = std::unique_ptr<IXAudio2SourceVoice, voiceDeleter>;
99  using MasterVoicePtr = std::unique_ptr<IXAudio2MasteringVoice, voiceDeleter>;
100 
101  util::CoInitialize m_coInit;
102  IXAudio2Ptr m_pIXAudio;
103  MasterVoicePtr m_pMasterVoice;
104 
105  // Sound Effect
106  // keep reference to playing buffer in resource
107  using PyaingSeElem = std::tuple<SeResourcePtr, SourceVoicePtr>;
108  std::array<PyaingSeElem, SoundEffectPlayMax> m_playingSeList;
109 
110  PyaingSeElem *findFreeSeEntry();
111  void processFrameSe();
112 
113  // BGM
114  // raw wave buffer, which must be deleted after m_pBgmVoice
115  std::unique_ptr<char[]> m_pBgmBuffer;
116  uint32_t m_writePos;
117  // play m_pBgmBuffer at another thread
118  SourceVoicePtr m_pBgmVoice;
119  // keep reference to resource struct
120  BgmResourcePtr m_playingBgm;
121 
122  void processFrameBgm();
123 };
124 
125 } // namespace sound
126 } // namespace yappy
Noncopyable class.
Definition: util.h:24
std::unique_ptr< OggVorbis_File, oggFileDeleter > OggFilePtr
Definition: sound.h:53
std::shared_ptr< BgmResource > BgmResourcePtr
Definition: sound.h:78
void operator()(HMMIO hMmio)
Definition: sound.h:24
const uint32_t SoundFileSizeMax
Definition: sound.h:14
sh をピクセル座標からUV座標に offset
Definition: Memo.txt:71
Definition: config.cpp:6
Auto CoInitializeEx() and CoUninitialize() class.
Definition: util.h:127
std::shared_ptr< SeResource > SeResourcePtr
Definition: sound.h:76
Utilities.
const uint32_t SoundEffectPlayMax
Definition: sound.h:15
std::vector< uint8_t > Bytes
File byte sequence. Vector of uint8_t.
Definition: file.h:25
const uint32_t BgmBufferSize
Definition: sound.h:18
const uint32_t BgmBufferCount
Definition: sound.h:19
OggVorbis_File * ovFp() const
Definition: sound.h:58
const uint32_t BgmOvReadSize
Definition: sound.h:17
void operator()(IXAudio2Voice *pv)
Definition: sound.h:31
std::unique_ptr< T, ComDeleter > ComPtr
unique_ptr of IUnknown with ComDeleter.
Definition: util.h:88
void operator()(OggVorbis_File *ovf)
Definition: sound.h:38
WAVEFORMATEX format
Definition: sound.h:46
file::Bytes samples
Definition: sound.h:47