1 //=============================================================================
2 //  MuseScore
3 //  Music Composition & Notation
4 //
5 //  Copyright (C) 2002-2012 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 __MSYNTHESIZER_H__
14 #define __MSYNTHESIZER_H__
15 
16 #include <atomic>
17 #include "effects/effect.h"
18 #include "libmscore/synthesizerstate.h"
19 
20 namespace Ms {
21 
22 struct MidiPatch;
23 class NPlayEvent;
24 class Synthesizer;
25 class Effect;
26 class Xml;
27 
28 //---------------------------------------------------------
29 //   MasterSynthesizer
30 //    hosts several synthesizers
31 //---------------------------------------------------------
32 
33 class MasterSynthesizer : public QObject {
34       Q_OBJECT
35 
36       float _gain             { defaultGain };
37       float _boost            { 10.0  };     // +20dB
38       double _masterTuning    { 440.0 };
39 
40       int _dynamicsMethod     { 1 };      // Default dynamics method
41       int _ccToUse            { 1 };      // CC2
42 
43       static constexpr double MUTE = 0.00;            // for gain to decibels conversion
44       static constexpr double MAX = 10.00;            // for gain to decibels conversion
45       static constexpr double N = 20.0;               // for gain to decibels conversion
46 
47    public:
48       static const int MAX_BUFFERSIZE = 8192;
49       static const int MAX_EFFECTS = 2;
50       static constexpr float defaultGain = 0.1f;  // -20dB
51 
52    private:
53       std::atomic<bool> lock1      { false };
54       std::atomic<bool> lock2      { true  };
55       std::vector<Synthesizer*> _synthesizer;
56       std::vector<Effect*> _effectList[MAX_EFFECTS];
57       Effect* _effect[MAX_EFFECTS]  { nullptr, nullptr };
58 
59       float _sampleRate;
60 
61       float effect1Buffer[MAX_BUFFERSIZE];
62       float effect2Buffer[MAX_BUFFERSIZE];
63       int indexOfEffect(int ab, const QString& name);
64       float convertGainToDecibels(float gain) const;
65 
66    public slots:
sfChanged()67       void sfChanged() { emit soundFontChanged(); }
68       void setGain(float f);
69 
70    signals:
71       void soundFontChanged();
72       void gainChanged(float);
73 
74    public:
75       MasterSynthesizer();
76       ~MasterSynthesizer();
77       void registerSynthesizer(Synthesizer*);
78 
79       void init();
80 
sampleRate()81       float sampleRate()            { return _sampleRate; }
82       void setSampleRate(float val);
83 
84       void process(unsigned, float*);
85       void play(const NPlayEvent&, unsigned);
86 
87       void setMasterTuning(double val);
masterTuning()88       double masterTuning() const      { return _masterTuning; }
89 
90       int index(const QString&) const;
91       QString name(unsigned) const;
92 
93       QList<MidiPatch*> getPatchInfo() const;
94       MidiPatch* getPatchInfo(QString synti, int bank, int program);
95 
96       SynthesizerState state() const;
97       bool setState(const SynthesizerState&);
98 
99       Synthesizer* synthesizer(const QString& name);
effectList(int ab)100       const std::vector<Effect*>& effectList(int ab) const { return _effectList[ab]; }
synthesizer()101       const std::vector<Synthesizer*> synthesizer() const { return _synthesizer; }
102       bool hasSoundFontsLoaded() const;
103       void registerEffect(int ab, Effect*);
104 
105       void reset();
106       void allSoundsOff(int channel);
107       void allNotesOff(int channel);
108 
109       void setEffect(int ab, int idx);
110       Effect* effect(int ab);
111       int indexOfEffect(int ab);
112 
gain()113       float gain() const     { return _gain; }
114       float gainAsDecibels() const;
115       float defaultGainAsDecibels;
116       float minGainAsDecibels = -80;
117       float maxGainAsDecibels = 0;
118 
119       void setGainAsDecibels(float decibelValue);
boost()120       float boost() const    { return _boost; }
setBoost(float v)121       void setBoost(float v) { _boost = v; }
122 
dynamicsMethod()123       int dynamicsMethod() const          { return _dynamicsMethod; }
setDynamicsMethod(int val)124       void setDynamicsMethod(int val)     { _dynamicsMethod = val; }
ccToUseIndex()125       int ccToUseIndex() const            { return _ccToUse; }    // NOTE: this doesn't return a CC number, but returns an index instead
setCcToUseIndex(int val)126       void setCcToUseIndex(int val)       { _ccToUse = val; }
127 
128       bool storeState();
129       };
130 
131 }
132 #endif
133 
134