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