summaryrefslogtreecommitdiffstats
path: root/src/engine/sync/basesyncablelistener.cpp
blob: d799f39dc1d007f33a3ad4c5a735d8dfc4515587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "engine/sync/basesyncablelistener.h"

#include <QMetaType>

#include "engine/sync/internalclock.h"

static const char* kInternalClockGroup = "[InternalClock]";

BaseSyncableListener::BaseSyncableListener(ConfigObject<ConfigValue>* pConfig)
        : m_pConfig(pConfig),
          m_pInternalClock(new InternalClock(kInternalClockGroup, this)),
          m_pMasterSyncable(NULL) {
    qRegisterMetaType<SyncMode>("SyncMode");
    m_pInternalClock->setMasterBpm(124.0);
}

BaseSyncableListener::~BaseSyncableListener() {
    // We use the slider value because that is never set to 0.0.
    m_pConfig->set(ConfigKey("[InternalClock]", "bpm"), ConfigValue(
        m_pInternalClock->getBpm()));
    delete m_pInternalClock;
}

void BaseSyncableListener::addSyncableDeck(Syncable* pSyncable) {
    if (m_syncables.contains(pSyncable)) {
        qDebug() << "BaseSyncableListener: already has" << pSyncable;
        return;
    }
    m_syncables.append(pSyncable);
}

void BaseSyncableListener::onCallbackStart(int sampleRate, int bufferSize) {
    m_pInternalClock->onCallbackStart(sampleRate, bufferSize);
}

void BaseSyncableListener::onCallbackEnd(int sampleRate, int bufferSize) {
    m_pInternalClock->onCallbackEnd(sampleRate, bufferSize);
}

EngineChannel* BaseSyncableListener::getMaster() const {
    return m_pMasterSyncable ? m_pMasterSyncable->getChannel() : NULL;
}

Syncable* BaseSyncableListener::getSyncableForGroup(const QString& group) {
    foreach (Syncable* pSyncable, m_syncables) {
        if (pSyncable->getGroup() == group) {
            return pSyncable;
        }
    }
    return NULL;
}

bool BaseSyncableListener::syncDeckExists() const {
    foreach (const Syncable* pSyncable, m_syncables) {
        if (pSyncable->getSyncMode() != SYNC_NONE && pSyncable->getBaseBpm() > 0) {
            return true;
        }
    }
    return false;
}

int BaseSyncableListener::playingSyncDeckCount() const {
    int playing_sync_decks = 0;

    foreach (const Syncable* pSyncable, m_syncables) {
        SyncMode sync_mode = pSyncable->getSyncMode();
        if (sync_mode == SYNC_NONE) {
            continue;
        }

        if (pSyncable->isPlaying()) {
            ++playing_sync_decks;
        }
    }

    return playing_sync_decks;
}

double BaseSyncableListener::masterBpm() const {
    if (m_pMasterSyncable) {
        return m_pMasterSyncable->getBpm();
    }
    return m_pInternalClock->getBpm();
}

double BaseSyncableListener::masterBeatDistance() const {
    if (m_pMasterSyncable) {
        return m_pMasterSyncable->getBeatDistance();
    }
    return m_pInternalClock->getBeatDistance();
}

double BaseSyncableListener::masterBaseBpm() const {
    if (m_pMasterSyncable) {
        return m_pMasterSyncable->getBaseBpm();
    }
    return m_pInternalClock->getBaseBpm();
}

void BaseSyncableListener::setMasterBpm(Syncable* pSource, double bpm) {
    if (pSource != m_pInternalClock) {
        m_pInternalClock->setMasterBpm(bpm);
    }
    foreach (Syncable* pSyncable, m_syncables) {
        if (pSyncable == pSource ||
                pSyncable->getSyncMode() == SYNC_NONE) {
            continue;
        }
        pSyncable->setMasterBpm(bpm);
    }
}

void BaseSyncableListener::setMasterInstantaneousBpm(Syncable* pSource, double bpm) {
    if (pSource != m_pInternalClock) {
        m_pInternalClock->setInstantaneousBpm(bpm);
    }
    foreach (Syncable* pSyncable, m_syncables) {
        if (pSyncable == pSource ||
                pSyncable->getSyncMode() == SYNC_NONE) {
            continue;
        }
        pSyncable->setInstantaneousBpm(bpm);
    }
}

void BaseSyncableListener::setMasterBaseBpm(Syncable* pSource, double bpm) {
    if (pSource != m_pInternalClock) {
        m_pInternalClock->setMasterBaseBpm(bpm);
    }
    foreach (Syncable* pSyncable, m_syncables) {
        if (pSyncable == pSource ||
                pSyncable->getSyncMode() == SYNC_NONE) {
            continue;
        }
        pSyncable->setMasterBaseBpm(bpm);
    }
}

void BaseSyncableListener::setMasterBeatDistance(Syncable* pSource, double beat_distance) {
    if (pSource != m_pInternalClock) {
        m_pInternalClock->setMasterBeatDistance(beat_distance);
    }
    foreach (Syncable* pSyncable, m_syncables) {
        if (pSyncable == pSource ||
                pSyncable->getSyncMode() == SYNC_NONE) {
            continue;
        }
        pSyncable->setMasterBeatDistance(beat_distance);
    }
}

void BaseSyncableListener::setMasterParams(Syncable* pSource, double beat_distance,
                                           double base_bpm, double bpm) {
    if (pSource != m_pInternalClock) {
        m_pInternalClock->setMasterParams(beat_distance, base_bpm, bpm);
    }
    foreach (Syncable* pSyncable, m_syncables) {
        if (pSyncable == pSource ||
                pSyncable->getSyncMode() == SYNC_NONE) {
            continue;
        }
        pSyncable->setMasterParams(beat_distance, base_bpm, bpm);
    }
}

void BaseSyncableListener::checkUniquePlayingSyncable() {
    int playing_sync_decks = 0;
    Syncable* unique_syncable = NULL;
    foreach (Syncable* pSyncable, m_syncables) {
        SyncMode sync_mode = pSyncable->getSyncMode();
        if (sync_mode == SYNC_NONE) {
            continue;
        }

        if (pSyncable->isPlaying()) {
            if (playing_sync_decks > 0) {
                return;
            }
            unique_syncable = pSyncable;
            ++playing_sync_decks;
        }
    }
    if (playing_sync_decks == 1) {
        unique_syncable->notifyOnlyPlayingSyncable();
    }
}