wavelength 1.0.0
Loading...
Searching...
No Matches
synthps2.h
Go to the documentation of this file.
1#pragma once
2
3#include <list>
4#include <memory>
5#include <stddef.h>
6#include <vector>
7
8#include "os/bufstream.h"
9#include "os/mem.h"
10#include "os/string.h"
11#include "synth/lfo.h"
12#include "synth/onepolefilter.h"
13#include "synth/queuedjob.h"
14#include "synth/softfxfilter.h"
15#include "synth/synth.h"
16
17class BankFileIO;
18
28class SynthPS2 : public Synth {
29public:
31 static constexpr int kNumBankSlots = 7;
32
34 static constexpr int kNumChannels = 16;
35
37 static constexpr int kCommandBufferSize = 0x10000;
38
45 void *operator new(size_t nSize) {
46 return PoolMemAlloc(static_cast<int>(nSize), "SynthPs2", 0);
47 }
48
54 void operator delete(void *pBlock) {
55 PoolMemFree(pBlock);
56 }
57
68
75 ~SynthPS2() override;
76
84 int Init() override;
85
93 int Terminate() override;
94
101 void ResetSpu() override;
102
111 int SendPackedMessage(unsigned int nPacked) override;
112
128 int SendMessage(unsigned char nByte0,
129 unsigned char nByte1,
130 unsigned char nByte2,
131 int nByte3) override;
132
139 void UpdateTime() override;
140
150 int Poll() override;
151
160 return mStream;
161 }
162
170 void SetStream(StreamPlayer *pStream) override;
171
180 void SendStreamData(const void *pData, int nSize) override;
181
189 int GetStreamStatus() override;
190
200 int GetStreamFormat(int *pnRate, int *pnBlock) override;
201
210 int Partition(const std::vector<int> &blocks) override;
211
220 int GetBankBlockCount(const String &file) override;
221
230 bool IsBankLoaded(unsigned short nSlot) override;
231
240 int IsBankPresent(unsigned short nSlot) override;
241
252 int LoadBank(unsigned short nSlot, const String &file, bool bSkipPending) override;
253
265 int UnloadBank(unsigned short nSlot) override;
266
274 void SetSurround(bool bSurround) override;
275
283 void SetMono(bool bMono) override;
284
292 void SetLag(float fLag) override;
293
301 float GetLag() override;
302
312 void SetSoftFxSweep(float fPosition) override;
313
321 void SetSoftFxModulation(float fValue) override;
322
333 void SetSoftFxFilter(const SoftFxFilter &filter) override;
334
342 int EnableSoftFx() override;
343
351 int DisableSoftFx() override;
352
360 void SetOutputLevel(float fLevel) override;
361
369 float GetOutputLevel() override;
370
378 void SetSoftFxInput(int nInput) override;
379
387 void SetBusToCoreFx(int nOn) override;
388
396 void SetBusToSoftFx(int nOn) override;
397
405 void SendMessages(const std::vector<unsigned int> &messages) override;
406
415 void QueueCallback(Callback pfnCallback, int nArg) override;
416
417private:
419 enum Command {
420 kCommandInit = 100,
421 kCommandTerminate = 101,
422 kCommandSetBankLayout = 102,
423 kCommandSetBankLoaded = 103,
424 kCommandUnloadBank = 104,
425 kCommandTransferSamples = 105,
426 kCommandLoadBankHeader = 106,
427 kCommandLoadPrograms = 107,
428 kCommandLoadSampleDescs = 108,
429 kCommandLoadSamples = 109,
430 kCommandQueueEvents = 110,
431 kCommandConcatenation = 113,
432 kCommandMono = 114,
433 kCommandSurround = 115,
434 kCommandResetSpu = 116,
435 };
436
438 enum SoftFxCommand {
439 kSoftFxCommandInit = 100,
440 kSoftFxCommandStart = 102,
441 kSoftFxCommandStop = 103,
450 };
451
453 enum LoadState {
454 kLoadIdle = 0,
455 kLoadBankHeader = 1,
456 kFindPrograms = 2,
457 kLoadPrograms = 3,
458 kFindSamples = 4,
459 kLoadSampleDescs = 5,
460 kOpenAudioData = 6,
461 kLoadSamples = 7,
462 kFindSampleDescs = 8,
463 kTransferAudioData = 9,
464 kMarkLoaded = 10,
465 kFinishLoad = 11,
466 kAbortLoad = 12,
467 kNumLoadStates = 13,
468 };
469
471 struct BankDataCommand {
472 int mBank;
473 int mOffset;
474 };
475
477 union CommandBuffer {
478 BankDataCommand mLastHeader;
479 char mBytes[kCommandBufferSize];
480 };
481
483 static constexpr int kNumTaps = SoftFxFilter::kNumTaps;
484
486 struct SoftFxTap {
487 int mDelay;
488 int mGain;
489 };
490
492 struct SoftFxParams {
493 int mMode;
494 int mCutoff;
495 int mLevel;
496 int mFeedback;
497 SoftFxTap mTaps[kNumTaps];
498 };
499
507 int ReadStreamStatus();
508
515 static void ClearStreamStatus();
516
526 void SendCommand(int nCommand, const void *pData, int nSize);
527
535 int InitIop();
536
544 void UpdateVolumes();
545
553 bool IsRpcBusy();
554
562 bool IsSoftFxRpcBusy();
563
571 bool IsTransferDone();
572
579 void SetTransferPending();
580
588 bool IsCoreFxDone();
589
596 void SetCoreFxPending();
597
605 int PollBankLoad();
606
615 int SendBankLoaded(unsigned short nSlot);
616
625 int SendUnloadBank(unsigned short nSlot);
626
636 int StartBankLoad(unsigned short nSlot, const String &file);
637
649 int SendBankData(const std::vector<unsigned char> &data, int nBank, int nCommand, int nOffset);
650
660 int SendBankHeader(const std::vector<unsigned char> &data, int nBank);
661
671 int SendPrograms(const std::vector<unsigned char> &data, int nBank);
672
682 int SendSampleDescs(const std::vector<unsigned char> &data, int nBank);
683
693 int SendSamples(const std::vector<unsigned char> &data, int nBank);
694
705 int SendSampleData(const std::vector<unsigned char> &data, int nOffset, int nBank) {
706 SetTransferPending();
707 (void)SendBankData(data, nBank, kCommandTransferSamples, nOffset);
708 return 0;
709 }
710
717 void ProcessJobs();
718
726 void AdjustDelays(int nBias);
727
736 int FlushMidi();
737
745 void SendSoftFxEnable(int nEnable);
746
755 int SendPartition(const std::vector<int> &blocks);
756
763 void PollStream();
764
773 void UpdateSoftFx();
774
782 void SendMessageList(const std::vector<unsigned int> &messages);
783
791 void SetSweepFromController(unsigned char nValue);
792
800 void SetLevelFromController(unsigned char nValue);
801
810 bool HasPendingLoad(unsigned short nSlot);
811
812 std::vector<bool> mBankLoaded;
813 std::list<std::unique_ptr<QueuedJob>> mJobs;
814 int mLoadSlot;
815 std::vector<unsigned char> mChunk;
816 std::unique_ptr<BankFileIO> mBankFile;
817 int mLoadState;
818 std::vector<unsigned int> mMidiQueue;
819 int mInitialized;
820 float mLagMs;
821 float mNowMs;
822 float mLastMs;
823 int mTimeUpdated;
824 std::unique_ptr<BufStream> mCommands;
825 std::unique_ptr<BufStream> mSoftFxCommands;
826 alignas(64) CommandBuffer mCommandBuffer;
827 CommandBuffer mSoftFxBuffer;
828 StreamPlayer *mStream;
829 int mSoftFxEnabled;
830 float mSoftFxSweep;
831 float mSoftFxModulation;
832 float mOutputLevel;
833 SoftFxFilter mFilter;
834 Lfo mTapOscillators[kNumTaps];
835 std::vector<OnePoleFilter> mChannelVolumes;
836 int mGotAudioData;
837 int mMoreAudioData;
838 int mAudioDataOffset;
839 std::vector<unsigned char> mAudioData;
840};
Reader of a bank file and the audio data file beside it.
Definition bankfileio.h:36
Stream of sound data read from a file and handed to the sound output a run at a time.
Definition streamplayer.h:13
Growable text buffer that is also a PrnStream.
Definition string.h:19
static constexpr int kNumBankSlots
The number of bank slots of the synthesiser module.
Definition synthps2.h:31
int GetStreamStatus() override
Report the room the software effect has for stream data.
int IsBankPresent(unsigned short nSlot) override
Report whether a slot has a bank or a load into it is waiting.
void SetBusToSoftFx(int nOn) override
Do nothing.
void SendMessages(const std::vector< unsigned int > &messages) override
Queue a job that sends packed messages to set up the hardware effects.
bool IsBankLoaded(unsigned short nSlot) override
Report whether a slot has a bank and no load into it is waiting.
int EnableSoftFx() override
Queue a job that turns the software effects on.
~SynthPS2() override
Release the command buffers.
void SetSoftFxModulation(float fValue) override
Set the second control of the software effect.
void SendStreamData(const void *pData, int nSize) override
Queue a run of stream data for the software effect and clear the room it reported.
int DisableSoftFx() override
Queue a job that turns the software effects off.
int GetBankBlockCount(const String &file) override
Report the size of the audio data file of a bank in 64-byte blocks, rounded up.
void UpdateTime() override
Read the system clock and shorten the delays of the queued messages by the time passed.
int Poll() override
Run the frame's work and send the command buffers to the IOP modules.
SynthPS2()
Build the output with every bank slot empty and the command buffers ready.
int SendPackedMessage(unsigned int nPacked) override
Send one packed message.
void SetSoftFxInput(int nInput) override
Choose the mixer input of the sound processor that the software effect captures.
float GetLag() override
Report the lag of the output.
void SetSoftFxFilter(const SoftFxFilter &filter) override
Select the software effect and record its settings.
int Partition(const std::vector< int > &blocks) override
Queue the division of the sound memory among the bank slots.
int Terminate() override
Stop the synthesiser module.
int GetStreamFormat(int *pnRate, int *pnBlock) override
Report the format of the stream data.
void SetMono(bool bMono) override
Choose mono output for the synthesiser and the stream.
void ResetSpu() override
Queue a reset of the sound processor.
void SetBusToCoreFx(int nOn) override
Set whether the voices feed the hardware effects.
void SetSurround(bool bSurround) override
Choose whether the synthesiser may invert a channel for surround decoders.
int UnloadBank(unsigned short nSlot) override
Empty a slot, or cancel the load into it.
void SetOutputLevel(float fLevel) override
Set the volume of the input the software effect captures.
float GetOutputLevel() override
Report the level SetOutputLevel() set.
void SetStream(StreamPlayer *pStream) override
Set the stream the output plays, and end the stream the software effect played.
int LoadBank(unsigned short nSlot, const String &file, bool bSkipPending) override
Queue the load of a bank file into a slot.
void SetSoftFxSweep(float fPosition) override
Set the position of the sweep of a sweeping software filter.
static constexpr int kNumChannels
The number of MIDI channels, each with a smoothed volume.
Definition synthps2.h:34
void QueueCallback(Callback pfnCallback, int nArg) override
Queue a job that calls a routine once the jobs before it have run.
static constexpr int kCommandBufferSize
The size of each command buffer in bytes.
Definition synthps2.h:37
int Init() override
Read the lag, bind to the IOP modules, and start them.
StreamPlayer * GetStream() override
Report the stream SetStream() set.
Definition synthps2.h:159
int SendMessage(unsigned char nByte0, unsigned char nByte1, unsigned char nByte2, int nByte3) override
Send one message, or act on it at once.
void SetLag(float fLag) override
Set the lag of the output.
The MIDI synthesiser that drives the SPU2 voices.
Definition synth.h:19
void(* Callback)(int nArg)
Routine a queued callback job calls with its argument.
Definition synth.h:439
void * PoolMemAlloc(int nSize, const char *pszTag, int nAlign)
Allocate a block from the pool heaps and bill it to a tag.
void PoolMemFree(void *pBlock)
Return a block PoolMemAlloc() handed out to the pool heap that owns it.
SoftFxCommand
RPC function numbers.
Definition softfx.h:14
@ kSoftFxCommandStart
Definition softfx.h:17
@ kSoftFxCommandRouteVoices
Definition softfx.h:25
@ kSoftFxCommandConcatenation
Definition softfx.h:23
@ kSoftFxCommandSetVolume
Definition softfx.h:26
@ kSoftFxCommandStreamData
Definition softfx.h:20
@ kSoftFxCommandSetParams
Definition softfx.h:22
@ kSoftFxCommandInit
Definition softfx.h:15
@ kSoftFxCommandRouteInput
Definition softfx.h:24
@ kSoftFxCommandSetEffect
Definition softfx.h:21
@ kSoftFxCommandSetMonoStream
Definition softfx.h:27
@ kSoftFxCommandStop
Definition softfx.h:18
Settings of the software effect, read from the filter entry of the first effect bus.
Definition softfxfilter.h:9
static constexpr int kNumTaps
The number of delay taps of a kTypeChorus effect.
Definition softfxfilter.h:26