1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SOUND_H
4#define ENGINE_SOUND_H
5
6#include <engine/kernel.h>
7#include <engine/storage.h>
8
9class ISound : public IInterface
10{
11 MACRO_INTERFACE("sound")
12public:
13 enum
14 {
15 FLAG_LOOP = 1 << 0,
16 FLAG_POS = 1 << 1,
17 FLAG_NO_PANNING = 1 << 2,
18 FLAG_PREVIEW = 1 << 3,
19 FLAG_ALL = FLAG_LOOP | FLAG_POS | FLAG_NO_PANNING | FLAG_PREVIEW,
20 };
21
22 enum
23 {
24 SHAPE_CIRCLE,
25 SHAPE_RECTANGLE,
26 };
27
28 // unused
29 struct CSampleHandle
30 {
31 int m_SampleId;
32 };
33
34 struct CVoiceShapeCircle
35 {
36 float m_Radius;
37 };
38
39 struct CVoiceShapeRectangle
40 {
41 float m_Width;
42 float m_Height;
43 };
44
45 class CVoiceHandle
46 {
47 friend class ISound;
48 int m_Id;
49 int m_Age;
50
51 public:
52 CVoiceHandle() :
53 m_Id(-1), m_Age(-1)
54 {
55 }
56
57 bool IsValid() const { return (Id() >= 0) && (Age() >= 0); }
58 int Id() const { return m_Id; }
59 int Age() const { return m_Age; }
60
61 bool operator==(const CVoiceHandle &Other) const { return m_Id == Other.m_Id && m_Age == Other.m_Age; }
62 };
63
64 virtual bool IsSoundEnabled() = 0;
65
66 virtual int LoadOpus(const char *pFilename, int StorageType = IStorage::TYPE_ALL) = 0;
67 virtual int LoadWV(const char *pFilename, int StorageType = IStorage::TYPE_ALL) = 0;
68 virtual int LoadOpusFromMem(const void *pData, unsigned DataSize, bool FromEditor = false) = 0;
69 virtual int LoadWVFromMem(const void *pData, unsigned DataSize, bool FromEditor = false) = 0;
70 virtual void UnloadSample(int SampleId) = 0;
71
72 virtual float GetSampleTotalTime(int SampleId) = 0; // in s
73 virtual float GetSampleCurrentTime(int SampleId) = 0; // in s
74 virtual void SetSampleCurrentTime(int SampleId, float Time) = 0;
75
76 virtual void SetChannel(int ChannelId, float Volume, float Panning) = 0;
77 virtual void SetListenerPos(float x, float y) = 0;
78
79 virtual void SetVoiceVolume(CVoiceHandle Voice, float Volume) = 0;
80 virtual void SetVoiceFalloff(CVoiceHandle Voice, float Falloff) = 0;
81 virtual void SetVoiceLocation(CVoiceHandle Voice, float x, float y) = 0;
82 virtual void SetVoiceTimeOffset(CVoiceHandle Voice, float TimeOffset) = 0; // in s
83
84 virtual void SetVoiceCircle(CVoiceHandle Voice, float Radius) = 0;
85 virtual void SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height) = 0;
86
87 virtual CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, float x, float y) = 0;
88 virtual CVoiceHandle Play(int ChannelId, int SampleId, int Flags) = 0;
89 virtual void Pause(int SampleId) = 0;
90 virtual void Stop(int SampleId) = 0;
91 virtual void StopAll() = 0;
92 virtual void StopVoice(CVoiceHandle Voice) = 0;
93 virtual bool IsPlaying(int SampleId) = 0;
94
95 virtual int MixingRate() const = 0;
96 virtual void Mix(short *pFinalOut, unsigned Frames) = 0;
97
98 // useful for thread synchronization
99 virtual void PauseAudioDevice() = 0;
100 virtual void UnpauseAudioDevice() = 0;
101
102protected:
103 inline CVoiceHandle CreateVoiceHandle(int Index, int Age)
104 {
105 CVoiceHandle Voice;
106 Voice.m_Id = Index;
107 Voice.m_Age = Age;
108 return Voice;
109 }
110};
111
112class IEngineSound : public ISound
113{
114 MACRO_INTERFACE("enginesound")
115public:
116 virtual int Init() = 0;
117 virtual int Update() = 0;
118 virtual void Shutdown() override = 0;
119};
120
121extern IEngineSound *CreateEngineSound();
122
123#endif
124