~shanx-shashank/mixxx/effects_ladspa

« back to all changes in this revision

Viewing changes to mixxx/src/engine/callbackcontrolmanager.h

  • Committer: shanxS
  • Date: 2013-06-16 07:42:19 UTC
  • Revision ID: shanx.shashank@gmail.com-20130616074219-wszmk0slwfa1z61q
Init Repository.
Starting with GUI of lp:~shanx-shashank/mixxx/effects_parametricEq as base

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef CALLBACKCONTROLMANAGER_H
 
2
#define CALLBACKCONTROLMANAGER_H
 
3
 
 
4
#include <QSet>
 
5
#include <QObject>
 
6
#include <QScopedPointer>
 
7
#include <QTime>
 
8
 
 
9
#include "util/fifo.h"
 
10
#include "configobject.h"
 
11
#include "controlobject.h"
 
12
 
 
13
class CallbackControlManager;
 
14
 
 
15
// Struct to describe a pending update between the callback controls and
 
16
// ControlObject system.
 
17
struct ControlUpdate {
 
18
    ControlUpdate()
 
19
            : time(0),
 
20
              value(0) {
 
21
    }
 
22
    ControlUpdate(double time, double value)
 
23
            : time(time),
 
24
              value(value) {
 
25
    }
 
26
    double time;
 
27
    double value;
 
28
};
 
29
 
 
30
// A ControlUpdate and the ControlObject that it applies to.
 
31
struct CallbackControlMessage {
 
32
    ControlObject* control;
 
33
    ControlUpdate update;
 
34
};
 
35
 
 
36
// ControlWatcher is a class that listens to a ControlObject, receives its
 
37
// valueChanged() signals in an arbitrary thread, and emits a controlUpdated()
 
38
// signal that the CallbackControlManager receives.
 
39
class ControlWatcher : public QObject {
 
40
    Q_OBJECT
 
41
  public:
 
42
    ControlWatcher(ControlObject* pControl);
 
43
    virtual ~ControlWatcher();
 
44
  signals:
 
45
    void controlUpdated(ControlObject* pControl, double value);
 
46
  private slots:
 
47
    void slotValueChanged(double v);
 
48
  private:
 
49
    ControlObject* m_pControl;
 
50
};
 
51
 
 
52
// CallbackControl is a callback-safe wrapper around a ControlObject. Its
 
53
// valueChanged() signals are emitted from the callback thread at the start of
 
54
// the callback. Mutations of the CallbackControl are delivered to the wrapped
 
55
// ControlObject after the callback is complete via an EngineWorker.
 
56
class CallbackControl : public QObject {
 
57
    Q_OBJECT
 
58
  public:
 
59
    // Create a CallbackControl wrapper around pControl. Takes ownership of
 
60
    // pControl.
 
61
    CallbackControl(CallbackControlManager* pControlManager,
 
62
                    ControlObject* pControl, int bufferLength);
 
63
    virtual ~CallbackControl();
 
64
 
 
65
    // Get access to the internal control. WARNING: Touching this control is NOT
 
66
    // callback-safe.
 
67
    ControlObject* getControl() const {
 
68
        return m_control.data();
 
69
    }
 
70
 
 
71
    ConfigKey getKey() const {
 
72
        return m_control->getKey();
 
73
    }
 
74
 
 
75
    inline double get() const {
 
76
        return m_value.value;
 
77
    }
 
78
    void set(double value);
 
79
    void add(double value);
 
80
    void sub(double value);
 
81
 
 
82
    // Receive update from CallbackControlManager
 
83
    void receiveUpdate(ControlUpdate update);
 
84
 
 
85
  signals:
 
86
    // Callback-safe signal that a control value is changed. Always emitted from
 
87
    // the callback thread. WARNING: always connect to this signal with
 
88
    // Qt::DirectConnection or the slot WILL NOT be callback-safe.
 
89
    void valueChanged(double value);
 
90
    void valueChanged(double value, double time);
 
91
    void valueChangedFromEngine(double value);
 
92
    void valueChangedFromEngine(double value, double time);
 
93
    void controlUpdatedFromCallback(CallbackControl* pControl, double value);
 
94
 
 
95
  private:
 
96
    ControlUpdate m_value;
 
97
    FIFO<ControlUpdate> m_updates;
 
98
    QScopedPointer<ControlObject> m_control;
 
99
    ControlWatcher m_controlWatcher;
 
100
};
 
101
 
 
102
class CallbackControlManager : public QObject {
 
103
    Q_OBJECT
 
104
  public:
 
105
    explicit CallbackControlManager();
 
106
    virtual ~CallbackControlManager();
 
107
 
 
108
    // Add a control to be managed by CallbackControlManager. Returns a
 
109
    // CallbackControl. MUST ONLY BE CALLED DURING MIXXX INITIALIZATION.
 
110
    CallbackControl* addControl(ControlObject* pControl, int bufferLength);
 
111
 
 
112
    // Get a CallbackControl for the given ControlObject
 
113
    CallbackControl* getControl(ConfigKey key);
 
114
 
 
115
    void callbackProcessIncomingUpdates();
 
116
    void callbackProcessOutgoingUpdates();
 
117
    void processOutgoingUpdates();
 
118
 
 
119
  public slots:
 
120
    // Thread Safe but NOT callback-safe. Indicates that the control 'pControl'
 
121
    // was just updated to value 'value'
 
122
    void slotControlUpdated(ControlObject* pControl, double value);
 
123
 
 
124
    // Callback-safe.
 
125
    void slotControlUpdatedFromCallback(CallbackControl* pControl, double value);
 
126
 
 
127
  private:
 
128
    ////////////////////////////////////////////////////////////////////////////
 
129
    // The following may only be called/touched while m_incomingUpdateMutex is
 
130
    // held.
 
131
    ////////////////////////////////////////////////////////////////////////////
 
132
 
 
133
    double getTime() {
 
134
        // So stupid, but we can't trust QTime to give us accurate long-term
 
135
        // times since if somebody changes the system clock while running Mixxx,
 
136
        // the results is basically undefined. Restart it every time we get the
 
137
        // time and keep a tally.
 
138
        double elapsed = m_timer.restart();
 
139
        m_lastTimeValue += elapsed;
 
140
        return m_lastTimeValue;
 
141
    }
 
142
    QMutex m_incomingUpdateMutex;
 
143
    FIFO<CallbackControlMessage> m_incomingUpdateFifo;
 
144
 
 
145
 
 
146
    // TODO(XXX) Switch to QElapsedTimer once we require Qt >=4.7
 
147
    QTime m_timer;
 
148
    double m_lastTimeValue;
 
149
 
 
150
    ////////////////////////////////////////////////////////////////////////////
 
151
    // The following may only be touched from within the callback
 
152
    ////////////////////////////////////////////////////////////////////////////
 
153
 
 
154
    QHash<ConfigKey, CallbackControl*> m_callbackControls;
 
155
    QSet<ConfigKey> m_updatedControls;
 
156
    FIFO<CallbackControlMessage> m_outgoingUpdateFifo;
 
157
};
 
158
 
 
159
#endif /* CALLBACKCONTROLMANAGER_H */