wavelength 1.0.0
Loading...
Searching...
No Matches
synth.h
Go to the documentation of this file.
1#pragma once
2
3#include <libsd.h>
4
6#include "synth_s/channel.h"
7#include "synth_s/midievent.h"
8#include "synth_s/voice.h"
9
19class Synth {
20public:
21 static constexpr int kNumChannels = 16;
22 static constexpr int kNumCores = 2;
23 static constexpr int kVoicesPerCore = 24;
24 static constexpr int kMaxRetiredVoices = 50;
25
33 static void TickThread();
34
45 static int ProcessMidiMessage(unsigned char status, unsigned char data1, unsigned char data2);
46
53 static void ProcessDueEvents();
54
64 static int QueueTimedEvents(const MidiEvent *events, unsigned int count);
65
76 static int ProcessMidiStream(const unsigned char *data, unsigned int size);
77
85 static void SetMono(bool mono);
86
94 static void SetSurround(bool enabled);
95
103 static void ResetAllControllers(unsigned char channel);
104
111 static void Init();
112
119 static void ResetSpu();
120
131 static int
132 CombinePan(unsigned char channelPan, unsigned char programPan, unsigned char samplePan);
133
142 static bool ChannelHasProgram(unsigned char channel);
143
153 static void NoteOn(unsigned char note, unsigned char velocity, unsigned char channel);
154
163 static void NoteOff(unsigned char note, unsigned char channel);
164
173 static void SelectProgram(unsigned short channel, unsigned short program);
174
183 static void ProgramChange(unsigned char channel, unsigned short program);
184
193 static void BankSelect(unsigned char channel, unsigned short bank);
194
202 static void UpdateChannelVolume(unsigned char channel);
203
212 static void SetPan(unsigned char channel, unsigned char pan);
213
222 static void SetVolume(unsigned char channel, unsigned char volume);
223
232 static void SetExpression(unsigned char channel, unsigned char expression);
233
243 static void SetPitchBend(unsigned char channel, signed char semitones, signed char cents);
244
254 static int LoadBankHeader(const unsigned char *data, unsigned short bank);
255
266 static int LoadPrograms(const unsigned char *data, short size, unsigned short bank);
267
279 static int LoadSampleDescs(const unsigned char *data, short size, unsigned short bank);
280
291 static int LoadSamples(const unsigned char *data, short size, unsigned short bank);
292
302 static int TransferDone(int channel, void *data);
303
311 static int EffectsApplied();
312
324 static int
325 TransferSampleData(unsigned char *data, int size, unsigned int offset, unsigned short bank);
326
336 static int SetBankSpuLayout(const unsigned char *data);
337
346 static int SetBankLoaded(unsigned short bank);
347
356 static int UnloadBank(unsigned short bank);
357
366 static void SetPriority(unsigned char channel, unsigned char priority);
367
376 static void SetDetune(unsigned char channel, unsigned char detune);
377
386 static void SetStereo(unsigned char channel, unsigned char stereo);
387
396 static void SetMonophonic(unsigned char channel, unsigned char monophonic);
397
407 static void
408 SetControllerEnable(unsigned char channel, unsigned char controller, unsigned char value);
409
418 static void SetTranspose(unsigned char channel, signed char semitones);
419
428 static void SetBusMode(unsigned char channel, unsigned char busMode);
429
438 static void SetBus(unsigned char channel, unsigned char bus);
439
447 static void AllNotesOff(unsigned char channel);
448
456 static void EffectThread();
457
468 static void SetEffectParam(unsigned char channel, unsigned char value, unsigned char param);
469
478 static void CommitKeys();
479
486 static void ClearKeyMasks();
487
488 static void *sNotifyAddress;
489 static int sNotifyPending;
490 static void *sEffectNotifyAddress;
492 static int sEffectThreadId;
493 static unsigned char sInitFlag;
494
496
497private:
506 static void RetireVoice(const Voice *voice);
507
514 static void UpdateVibrato();
515
522 static void UpdateVoiceStates();
523
534 static Voice *AllocVoice(int *core, int *voice, signed char bus);
535
544 static void KeyOn(unsigned int core0Mask, unsigned int core1Mask);
545
554 static void KeyOff(unsigned int core0Mask, unsigned int core1Mask);
555
566 static void SetVoiceMix(int core, int voice, int mode);
567
574 static void CommitVoiceMix();
575
582 static void CommitEffects();
583
590 static void ApplyEffects();
591
602 static void
603 SetVoiceBus(int core, int voice, const BankSampleDesc *sampleDesc, const Channel *channel);
604
605 static int sTickCount;
606 static Voice sVoices[kNumCores][kVoicesPerCore];
607 static int sRetiredVoiceCount;
608 static Voice sRetiredVoices[kMaxRetiredVoices];
609
610 static unsigned char sVoiceMixDirty;
611 static unsigned char sEffectsDirty;
612 static unsigned char sMono;
613 static unsigned char sSurroundDisabled;
614 static unsigned char sEffectsApplying;
615 static int sVibratoTick;
616 static unsigned int sNoteSerial;
617 static int sTransferBusy;
618 static int sTransferCancel;
619 static int sVoicesInUse[kNumCores];
620 static unsigned int sKeyOnMask[kNumCores];
621 static unsigned int sKeyOffMask[kNumCores];
622 static unsigned int sVoiceMixDryLeft[kNumCores];
623 static unsigned int sVoiceMixDryRight[kNumCores];
624 static unsigned int sVoiceMixWetLeft[kNumCores];
625 static unsigned int sVoiceMixWetRight[kNumCores];
626 static sceSdEffectAttr sEffectAttr[kNumCores];
627};
A sample descriptor loaded into a bank, linked to the sample it plays.
Definition banksampledesc.h:12
The state of one MIDI channel.
Definition channel.h:20
The MIDI synthesiser that drives the SPU2 voices.
Definition synth.h:19
static void SetStereo(unsigned char channel, unsigned char stereo)
Set a channel's stereo switch.
static void NoteOn(unsigned char note, unsigned char velocity, unsigned char channel)
Start a note on every descriptor of the channel's program whose key range covers it.
static constexpr int kNumCores
Definition synth.h:22
static void SetVolume(unsigned char channel, unsigned char volume)
Set a channel's volume.
static constexpr int kNumChannels
Definition synth.h:21
static void SetPitchBend(unsigned char channel, signed char semitones, signed char cents)
Set a channel's pitch bend.
static int sNotifyPending
Definition synth.h:489
static void * sEffectNotifyAddress
Definition synth.h:490
static void SetTranspose(unsigned char channel, signed char semitones)
Set a channel's transposition.
static void Init()
Reset the banks, channels, and voices, then the SPU2.
static constexpr int kVoicesPerCore
Definition synth.h:23
static void SetPriority(unsigned char channel, unsigned char priority)
Set a channel's voice priority.
static void SetMonophonic(unsigned char channel, unsigned char monophonic)
Set a channel's monophonic switch.
static unsigned char sInitFlag
Definition synth.h:493
static void SetBusMode(unsigned char channel, unsigned char busMode)
Set a channel's dry and reverb mix for later notes.
static Channel sChannels[kNumChannels]
Definition synth.h:495
static int TransferSampleData(unsigned char *data, int size, unsigned int offset, unsigned short bank)
Start a waveform transfer into a bank's SPU2 area.
static void SetControllerEnable(unsigned char channel, unsigned char controller, unsigned char value)
Switch the live update of one controller on or off for a channel.
static void CommitKeys()
Key off and then key on every voice the current batch collected.
static int UnloadBank(unsigned short bank)
Silence a bank's voices, detach its channels, and empty it.
static int LoadBankHeader(const unsigned char *data, unsigned short bank)
Load a bank's header.
static void ResetSpu()
Initialise the SPU2: the mixer, the reverb areas and settings, and the voice mix.
static void SetDetune(unsigned char channel, unsigned char detune)
Set a channel's detune switch.
static int sEffectThreadId
Definition synth.h:492
static void TickThread()
The tick thread.
static void ProgramChange(unsigned char channel, unsigned short program)
Act on a program change unless the channel already plays that program number.
static void SelectProgram(unsigned short channel, unsigned short program)
Point a channel at the program of its bank with a program number.
static int EffectsApplied()
Notify the EE at the next tick that a reverb update ended, when the EE requested it.
static int TransferDone(int channel, void *data)
Notify the EE at the next tick that a waveform transfer ended.
static void SetExpression(unsigned char channel, unsigned char expression)
Set a channel's expression.
static void SetBus(unsigned char channel, unsigned char bus)
Set a channel's preferred core for later notes.
static void SetSurround(bool enabled)
Allow or forbid the surround inversion of the descriptors that request it.
static void SetEffectParam(unsigned char channel, unsigned char value, unsigned char param)
Set one reverb setting of a core from a controller.
static constexpr int kMaxRetiredVoices
Definition synth.h:24
static int SetBankLoaded(unsigned short bank)
Mark a bank loaded.
static void SetPan(unsigned char channel, unsigned char pan)
Set a channel's pan.
static bool ChannelHasProgram(unsigned char channel)
Report whether a channel has a program it can play.
static void ProcessDueEvents()
Act on every queued MIDI message that is due, as one batch of key changes.
static void AllNotesOff(unsigned char channel)
Release a channel's notes and forget every retired voice.
static int QueueTimedEvents(const MidiEvent *events, unsigned int count)
Queue MIDI messages stamped with delays from now.
static int LoadSampleDescs(const unsigned char *data, short size, unsigned short bank)
Load a bank's sample descriptors, each record preceded by a size word.
static void NoteOff(unsigned char note, unsigned char channel)
Stop the oldest sounding instance of a note.
static int sEffectNotifyPending
Definition synth.h:491
static void BankSelect(unsigned char channel, unsigned short bank)
Point a channel at a loaded bank, retaining its program number.
static int LoadPrograms(const unsigned char *data, short size, unsigned short bank)
Load a bank's programs, each record preceded by a size word.
static void UpdateChannelVolume(unsigned char channel)
Rewrite the volume registers of a channel's sounding voices.
static int ProcessMidiStream(const unsigned char *data, unsigned int size)
Act on a MIDI byte stream with running status.
static void SetMono(bool mono)
Select mono output.
static int LoadSamples(const unsigned char *data, short size, unsigned short bank)
Load a bank's samples, each record preceded by a size word.
static void ClearKeyMasks()
Start a batch of key changes.
static int ProcessMidiMessage(unsigned char status, unsigned char data1, unsigned char data2)
Act on one MIDI channel message.
static void * sNotifyAddress
Definition synth.h:488
static int CombinePan(unsigned char channelPan, unsigned char programPan, unsigned char samplePan)
Combine three pan offsets around the centre, clamped to zero to 127.
static int SetBankSpuLayout(const unsigned char *data)
Place the banks' SPU2 areas one after another.
static void EffectThread()
The reverb thread.
static void ResetAllControllers(unsigned char channel)
Restore a channel's controllers to their defaults, except its monophonic switch.
One SPU2 voice and the note it plays.
Definition voice.h:20
One MIDI channel message as the EE sends it in a timed batch.
Definition midievent.h:9