MuseScore  3.4
Music composition and notation
zerberus.h
Go to the documentation of this file.
1 //=============================================================================
2 // Zerberus
3 // Zample player
4 //
5 // Copyright (C) 2013 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //=============================================================================
12 
13 #ifndef __ZERBERUS_H__
14 #define __ZERBERUS_H__
15 
16 #include <math.h>
17 #include <atomic>
18 // #include <mutex>
19 #include <list>
20 #include <memory>
21 #include <queue>
22 
24 #include "synthesizer/event.h"
25 #include "synthesizer/midipatch.h"
26 #include "voice.h"
27 
28 
29 class Channel;
30 class ZInstrument;
31 enum class Trigger : char;
32 
33 static const int MAX_VOICES = 512;
34 static const int MAX_CHANNELS = 256;
35 static const int MAX_TRIGGER = 512;
36 
37 //---------------------------------------------------------
38 // VoiceFifo
39 //---------------------------------------------------------
40 
41 class VoiceFifo {
42  std::queue<Voice*> buffer;
43  std::vector< std::unique_ptr<Voice> > voices;
44 
45  public:
47  voices.resize(MAX_VOICES);
48  }
49 
50  void init(Zerberus* z) {
51  for (int i = 0; i < MAX_VOICES; ++i) {
52  voices.push_back(std::unique_ptr<Voice>(new Voice(z)));
53  buffer.push(voices.back().get());
54  }
55  }
56 
57  void push(Voice* v) {
58  buffer.push(v);
59  }
60 
61  Voice* pop() {
62  Q_ASSERT(!buffer.empty());
63  Voice* v = buffer.front();
64  buffer.pop();
65  return v;
66  }
67 
68  bool empty() const { return buffer.empty(); }
69  };
70 
71 //---------------------------------------------------------
72 // Zerberus
73 //---------------------------------------------------------
74 
75 class Zerberus : public Ms::Synthesizer {
76  static bool initialized;
77  static std::list<ZInstrument*> globalInstruments;
78  QList<Ms::MidiPatch*> patches;
79 
80  double _masterTuning = 440.0;
81  std::atomic<bool> busy;
82 
83  std::list<ZInstrument*> instruments;
84  Channel* _channel[MAX_CHANNELS];
85 
86  int allocatedVoices = 0;
88  Voice* activeVoices = 0;
89  int _loadProgress = 0;
90  bool _loadWasCanceled = false;
91 
92  QMutex mutex;
93 
94  void programChange(int channel, int program);
95  void trigger(Channel*, int key, int velo, Trigger, int cc, int ccVal, double durSinceNoteOn);
96  void processNoteOff(Channel*, int pitch);
97  void processNoteOn(Channel* cp, int key, int velo);
98 
99  public:
100  Zerberus();
101  ~Zerberus();
102 
103  virtual void process(unsigned frames, float*, float*, float*);
104  virtual void play(const Ms::PlayEvent& event);
105 
106  bool loadInstrument(const QString&);
107 
108  ZInstrument* instrument(int program) const;
109  Voice* getActiveVoices() { return activeVoices; }
110  Channel* channel(int n) { return _channel[n]; }
111  int loadProgress() { return _loadProgress; }
112  void setLoadProgress(int val) { _loadProgress = val; }
113  bool loadWasCanceled() { return _loadWasCanceled; }
114  void setLoadWasCanceled(bool status) { _loadWasCanceled = status; }
115 
116  virtual void setMasterTuning(double val) { _masterTuning = val; }
117  virtual double masterTuning() const { return _masterTuning; }
118 
119  double ct2hz(double c) const { return pow(2.0, (c-6900.0) / 1200.0) * masterTuning(); }
120 
121  virtual const char* name() const;
122 
123  virtual Ms::SynthesizerGroup state() const;
124  virtual bool setState(const Ms::SynthesizerGroup&);
125 
126  virtual void allSoundsOff(int channel);
127  virtual void allNotesOff(int channel);
128 
129  virtual bool addSoundFont(const QString&);
130  virtual bool removeSoundFont(const QString&);
131  virtual bool loadSoundFonts(const QStringList&);
132  virtual bool removeSoundFonts(const QStringList& fileNames);
133  QStringList soundFonts() const;
134  std::vector<Ms::SoundFontInfo> soundFontsInfo() const override;
135 
136  virtual const QList<Ms::MidiPatch*>& getPatchInfo() const override { return patches; }
137 
138  void updatePatchList();
139 
140  virtual Ms::SynthesizerGui* gui();
141  static QFileInfoList sfzFiles();
142  };
143 
144 #endif
145 
Definition: instrument.h:30
QMutex mutex
Definition: zerberus.h:92
VoiceFifo freeVoices
Definition: zerberus.h:87
std::vector< std::unique_ptr< Voice > > voices
Definition: zerberus.h:43
virtual double masterTuning() const
Definition: zerberus.h:117
Voice * getActiveVoices()
Definition: zerberus.h:109
virtual void setMasterTuning(double val)
Definition: zerberus.h:116
Definition: synthesizer.h:41
void push(Voice *v)
Definition: zerberus.h:57
static bool initialized
Definition: zerberus.h:76
Definition: voice.h:118
virtual const QList< Ms::MidiPatch * > & getPatchInfo() const override
Definition: zerberus.h:136
Definition: zerberus.h:75
bool loadWasCanceled()
Definition: zerberus.h:113
void setLoadProgress(int val)
Definition: zerberus.h:112
Definition: synthesizergui.h:24
int loadProgress()
Definition: zerberus.h:111
std::atomic< bool > busy
Definition: zerberus.h:81
Channel * channel(int n)
Definition: zerberus.h:110
Trigger
Definition: zone.h:23
double ct2hz(double c) const
Definition: zerberus.h:119
Definition: zerberus.h:41
void setLoadWasCanceled(bool status)
Definition: zerberus.h:114
bool empty() const
Definition: zerberus.h:68
QList< Ms::MidiPatch * > patches
Definition: zerberus.h:78
Definition: synthesizerstate.h:40
std::queue< Voice * > buffer
Definition: zerberus.h:42
Voice * pop()
Definition: zerberus.h:61
void init(Zerberus *z)
Definition: zerberus.h:50
std::list< ZInstrument * > instruments
Definition: zerberus.h:83
VoiceFifo()
Definition: zerberus.h:46
Definition: event.h:219
static std::list< ZInstrument * > globalInstruments
Definition: zerberus.h:77
Definition: channel.h:23