summaryrefslogtreecommitdiff
path: root/include/nw4r/snd/snd_Voice.h
blob: 3c43d7416ac6734f9ba0155508277ebba52b30fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#ifndef NW4R_SND_VOICE_H
#define NW4R_SND_VOICE_H

/*******************************************************************************
 * headers
 */

#include "common.h"

#include "nw4r/snd/snd_adpcm.h" // AdpcmLoopParam
#include "nw4r/snd/snd_AxVoice.h"
#include "nw4r/snd/snd_global.h"
#include "nw4r/snd/snd_DisposeCallbackManager.h" // DisposeCallback

#include "nw4r/ut/ut_LinkList.h"

/*******************************************************************************
 * types
 */

// forward declarations
namespace nw4r { namespace snd { namespace detail { struct WaveInfo; }}}

/*******************************************************************************
 * classes and functions
 */

namespace nw4r { namespace snd { namespace detail
{
	// [R89JEL]:/bin/RVL/Debug/mainD.elf:.debug::0x2b832
	// NOTE: different from ketteiban: no remote fields
	class Voice : public DisposeCallback
	{
	// enums
	public:
		// [R89JEL]:/bin/RVL/Debug/mainD.elf:.debug::0x2b6c2
		enum VoiceCallbackStatus
		{
			CALLBACK_STATUS_FINISH_WAVE,
			CALLBACK_STATUS_CANCEL,
			CALLBACK_STATUS_DROP_VOICE,
			CALLBACK_STATUS_DROP_DSP,
		};

		enum VoiceSyncFlag {
			SYNC_AX_SRC_INITIAL = (1 << 0),
			SYNC_AX_VOICE = (1 << 1),
			SYNC_AX_SRC = (1 << 2),
			SYNC_AX_VE = (1 << 3),
			SYNC_AX_MIX = (1 << 4),
			SYNC_AX_LPF = (1 << 5),
			SYNC_AX_REMOTE = (1 << 7),
			SYNC_AX_BIQUAD = (1 << 8),
		};

	// typedefs
	public:
		typedef ut::LinkList<Voice, 0xfc> LinkList;

		typedef void Callback(Voice *dropVoice, VoiceCallbackStatus status,
		                      void *callbackData);

	// methods
	public:
		// cdtors
		Voice();
		virtual ~Voice();

		// virtual function ordering
		// vtable DisposeCallback
		virtual void InvalidateData(void const *, void const *) {}
		virtual void InvalidateWaveData(void const *start, void const *end);

		// methods
		void Setup(WaveInfo const &waveParam, u32 startOffset);

		int GetPriority() const { return mPriority; }

		void SetVoiceOutParam(int voiceOutIndex,
		                      VoiceOutParam const &voiceOutParam);
		void SetVoiceOutParamPitchDisableFlag(bool flag)
		{
			mVoiceOutParamPitchDisableFlag = flag;
		}
		void SetRemoteFilter(int filter);
		void SetBiquadFilter(int type, f32 value);
		void SetPriority(int priority);
		void SetPan(f32 pan);
		void SetSurroundPan(f32 surroundPan);
		void SetLpfFreq(f32 lpfFreq);
		void SetOutputLine(int flag);
		void SetMainOutVolume(f32 volume);
		void SetMainSend(f32 send);
		void SetFxSend(AuxBus bus, f32 send);
		void SetRemoteOutVolume(int remote, f32 volume);
		void SetPitch(f32 pitch);
		void SetVolume(f32 volume);
		void SetVeVolume(f32 targetVolume, f32 initVolume);
		void SetPanMode(PanMode panMode);
		void SetPanCurve(PanCurve panCurve);

		bool IsActive() const { return mAxVoice[0][0] != nullptr; }
		bool IsPlayFinished() const
		{
			return IsActive() && mAxVoice[0][0]->IsPlayFinished();
		}
		bool IsRun() const
		{
			return IsActive() && mAxVoice[0][0]->IsRun();
		}
		SampleFormat GetFormat() const;
		u32 GetCurrentPlayingSample() const;
		int GetPhysicalVoiceCount() const
		{
			return mChannelCount * mVoiceOutCount;
		}

		void SetVoiceType(AxVoice::VoiceType type);
		void SetLoopFlag(bool loopFlag);
		void SetLoopStart(int channelIndex, void const *baseAddress,
		                  u32 samples);
		void SetLoopEnd(int channelIndex, void const *baseAddress, u32 samples);
		void SetAdpcmLoop(int channelIndex, AdpcmLoopParam const *param);

		bool Acquire(int channelCount, int voiceOutCount, int priority,
		             Callback *callback, void *callbackData);
		void Free();

		void InitParam(int channelCount, int voiceOutCount, Callback *callback,
		               void *callbackData);

		void Start();
		void Pause(bool flag);
		void Stop();
		void StopAtPoint(int channelIndex, void const *baseAddress,
		                 u32 samples);

	private:
		void RunAllAxVoice();
		void StopAllAxVoice();
		void SyncAxVoice();
		void StopFinished();

		void Calc();
		void CalcAxSrc(bool initialUpdate);
		void CalcAxVe();
		bool CalcAxMix();
		void CalcAxLpf();
		void CalcAxBiquadFilter();
		void CalcAxRemoteFilter();

		void CalcMixParam(int channelIndex, int voiceOutIndex,
		                  AxVoice::MixParam *mix,
		                  AxVoice::RemoteMixParam *rmtmix);

		void Update();
		void UpdateVoicesPriority();

		void ResetDelta();

		void TransformDpl2Pan(f32 *outPan, f32 *outSurroundPan, f32 inPan,
		                      f32 inSurroundPan);

		static void AxVoiceCallbackFunc(AxVoice *dropVoice,
		                                AxVoice::AxVoiceCallbackStatus status,
		                                void *callbackData);

	// static members
	public:
		static f32 const SURROUND_ATTENUATED_DB;
		static u32 const VOICE_PRIORITY_RELEASE;
		static u32 const VOICE_PRIORITY_ALLOC;
		static u32 const VOICE_PRIORITY_USE;
		static int const VOICE_PRIORITY_FREE;
		static u32 const VOICE_PRIORITY_NODROP;
		static u32 const VOICE_PRIORITY_MAX = 31; // just going to put it here
		static int const PRIORITY_RELEASE;
		static int const PRIORITY_NODROP;
		static int const PRIORITY_MIN = 0;
		static int const PRIORITY_MAX = 255;
		static int const REMOTE_FILTER_MAX = 127;
		static int const REMOTE_FILTER_MIN = 0;
		static f32 const SEND_MAX;
		static f32 const SEND_MIN;
		static f32 const BIQUAD_VALUE_MAX;
		static f32 const BIQUAD_VALUE_MIN;
		static f32 const CUTOFF_FREQ_MAX;
		static f32 const CUTOFF_FREQ_MIN;
		static f32 const SPAN_CENTER;
		static f32 const SPAN_REAR;
		static f32 const SPAN_FRONT;
		static f32 const PAN_CENTER;
		static f32 const PAN_RIGHT;
		static f32 const PAN_LEFT;
		static f32 const VOLUME_MAX;
		static f32 const VOLUME_MIN;
		static int const CHANNEL_MAX = 2;
		static int const UPDATE_BIQUAD = 1 << 8;
		static int const UPDATE_REMOTE_FILTER = 1 << 7;
		// 1 << 6 skipped
		static int const UPDATE_LPF = 1 << 5;
		static int const UPDATE_MIX = 1 << 4;
		static int const UPDATE_VE = 1 << 3;
		static int const UPDATE_SRC = 1 << 2;
		static int const UPDATE_PAUSE = 1 << 1;
		static int const UPDATE_START = 1 << 0;

	// members
	private:
		/* base DisposeCallback */							// size 0x0c, offset 0x00
		AxVoice				*mAxVoice[CHANNEL_MAX][4];		// size 0x20, offset 0x0c
		VoiceOutParam		mVoiceOutParam[4];				// size 0x60, offset 0x2c
		int					mChannelCount;					// size 0x04, offset 0x8c
		int					mVoiceOutCount;					// size 0x04, offset 0x90
		Callback			*mCallback;						// size 0x04, offset 0x94
		void				*mCallbackData;					// size 0x04, offset 0x98
		bool				mActiveFlag;					// size 0x01, offset 0x9c
		bool				mStartFlag;						// size 0x01, offset 0x9d
		bool				mStartedFlag;					// size 0x01, offset 0x9e
		bool				mPauseFlag;						// size 0x01, offset 0x9f
		bool				mPausingFlag;					// size 0x01, offset 0xa0
		bool				mVoiceOutParamPitchDisableFlag;	// size 0x01, offset 0xa1
		u16				mSyncFlag;						// size 0x02, offset 0xa2
		u8					mRemoteFilter;					// size 0x01, offset 0xa4
		u8					mBiquadType;					// size 0x01, offset 0xa5
		/* 2 bytes padding */
		int					mPriority;						// size 0x04, offset 0xa8
		f32					mPan;							// size 0x04, offset 0xac
		f32					mSurroundPan;					// size 0x04, offset 0xb0
		f32					mLpfFreq;						// size 0x04, offset 0xb4
		f32					mBiquadValue;					// size 0x04, offset 0xb8
		int					mOutputLineFlag;				// size 0x04, offset 0xbc
		f32					mMainOutVolume;					// size 0x04, offset 0xc0
		f32					mMainSend;						// size 0x04, offset 0xc4
		f32					mFxSend[AUX_BUS_NUM];			// size 0x0c, offset 0xc8
		f32					mRemoteOutVolume[4];			// size 0x10, offset 0xd4 TODO following sizes wrong 
		f32					mPitch;							// size 0x04, offset 0xd4
		f32					mVolume;						// size 0x04, offset 0xd8
		f32					mVeInitVolume;					// size 0x04, offset 0xdc
		f32					mVeTargetVolume;				// size 0x04, offset 0xe0
		PanMode				mPanMode;						// size 0x04, offset 0xe4
		PanCurve			mPanCurve;						// size 0x04, offset 0xe8
	public:
		ut::LinkListNode	mLinkNode;						// size 0x08, offset 0xec

	// friends
	private:
		friend class VoiceManager;
	}; // size 0xf4
}}} // namespace nw4r::snd::detail

#endif // NW4R_SND_VOICE_H