File: PlaybackController.h

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (339 lines) | stat: -rw-r--r-- 11,038 bytes parent folder | download
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/***************************************************************************
       PlaybackController.h  -  Interface for generic playback control
                             -------------------
    begin                : Nov 15 2000
    copyright            : (C) 2000 by Thomas Eschenbacher
    email                : Thomas Eschenbacher <Thomas.Eschenbacher@gmx.de>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef PLAYBACK_CONTROLLER_H
#define PLAYBACK_CONTROLLER_H

#include "config.h"
#include "libkwave_export.h"

#include <QtGlobal>
#include <QList>
#include <QMutex>
#include <QObject>

#include "libkwave/PlayBackParam.h"
#include "libkwave/Runnable.h"
#include "libkwave/Sample.h"
#include "libkwave/WorkerThread.h"

namespace Kwave
{

    class PlayBackDevice;
    class PlaybackDeviceFactory;
    class SignalManager;

    /**
     * Provides a generic interface for classes that can control playback
     * with start, stop, pause and continue. This class is intended to be used
     * or derived in a class that is able to control a playback device by
     * simply starting or stopping playback and can be easily used by some
     * other part of the program that has nothing to do directly with
     * playback. The playback control functions all start with "playback"
     * and are slots that are intended to be connected to some simple
     * gui elements like toolbar buttons or menu entries.
     *
     * This class internally manages the logic and handling of the
     * playback position.
     */
    class LIBKWAVE_EXPORT PlaybackController: public QObject,
                                            public Kwave::Runnable
    {
    Q_OBJECT

    public:

        /** Default constructor */
        explicit PlaybackController(Kwave::SignalManager &signal_manager);

        /** Destructor */
        ~PlaybackController() override;

    public:

        /** resets start, current position, loop, pause and running flag */
        void reset();

        /** returns the loop mode flag */
        bool loop() const;

        /** returns true if the playback is running */
        bool running() const;

        /** returns true if the playback is paused */
        bool paused() const;

        /** sets a new start position */
        void setStartPos(sample_index_t pos);

        /** sets a new end position */
        void setEndPos(sample_index_t pos);

        /** returns the position where the playback starts */
        sample_index_t startPos() const;

        /** returns the position where the playback ends */
        sample_index_t endPos() const;

        /** returns the current position of the playback pointer */
        sample_index_t currentPos() const;

        /**
         * Registers a PlaybackDeviceFactory
         */
        void registerPlaybackDeviceFactory(
            Kwave::PlaybackDeviceFactory *factory);

        /**
         * Unregisters a PlaybackDeviceFactory
         */
        void unregisterPlaybackDeviceFactory(
            Kwave::PlaybackDeviceFactory *factory);

        /**
         * Create a playback device matching the given playback method.
         *
         * @param method a playback_method_t (e.g. Pulse, ALSA, OSS...)
         * @return a new PlayBackDevice or 0 if failed
         */
        virtual Kwave::PlayBackDevice *createDevice(
            Kwave::playback_method_t method);

        /**
         * Creates, opens and initializes a playback device.
         *
         * @param tracks number of tracks,
         *               if negative use the setting of playback_params
         * @param playback_params points to a structure with playback
         *                        parameters. If null, the default parameters
         *                        of the current signal will be used
         * @return a pointer to an opened PlayBackDevice or null if failed
         * @see PlayBackDevice
         */
        Kwave::PlayBackDevice *openDevice(int tracks,
            const Kwave::PlayBackParam *playback_params);

        /**
         * Sets default playback parameters, for use next time playback
         * is started
         * @param params new playback parameters
         */
        void setDefaultParams(const Kwave::PlayBackParam &params);

        /**
         * Checks whether a playback method is supported and returns the
         * next best match if not.
         * @param method reference to a playback method, can be modified
         */
        void checkMethod(Kwave::playback_method_t &method);

    public slots:

        /**
         * (Re-)starts the playback. If playback has successfully been
         * started, the signal sigPlaybackStarted() will be emitted.
         */
        void playbackStart();

        /**
         * (Re-)starts the playback in loop mode (like with playbackStart().
         * Also emitts sigPlaybackStarted() if playback has successfully
         * been started.
         */
        void playbackLoop();

        /**
         * Pauses the playback. Causes sigPlaybackDone() to be emitted if
         * the current buffer has played out. The current playback pointer
         * will stay at it's current position.
         */
        void playbackPause();

        /**
         * Continues the playback at the position where it has been stopped
         * by the playbackPause() command. If the last playback pointer
         * has become invalid or is not available (less 0), this function
         * will do the same as playbackStart(). This also emits the
         * signal sigPlaybackStarted().
         */
        void playbackContinue();

        /**
         * Stopps playback / loop. Like playbackPause(), but resets the
         * playback pointer back to the start.
         */
        void playbackStop();

        /**
         * If playback is currently running, it will be paused and
         * then restarted with current track and time selection.
         */
        void reload();

        /** Seeks to a new position */
        void seekTo(sample_index_t pos);

        /** Called when the seek has finished */
        void seekDone(sample_index_t pos);

        /** Updates the current playback position */
        void updatePlaybackPos(sample_index_t pos);

        /** Updates the status if playback is done */
        void playbackDone();

    signals:

        /**
         * Signals that playback has started.
         */
        void sigPlaybackStarted();

        /**
         * Signals that playback has been paused.
         */
        void sigPlaybackPaused();

        /**
         * Signals that playback has stopped.
         */
        void sigPlaybackStopped();

        /**
         * Emits the current position of the playback pointer
         */
        void sigPlaybackPos(sample_index_t pos);

        /**
         * Emits the current position after a seek operation
         */
        void sigSeekDone(sample_index_t pos);

        /**
         * Signals that playback has stopped (sent from worker thread).
         */
        void sigDevicePlaybackDone();

        /** Emits the current playback position (from worker thread) */
        void sigDevicePlaybackPos(sample_index_t pos);

        /** Emitted after a successful seek operation (from worker thread)*/
        void sigDeviceSeekDone(sample_index_t pos);

    private slots:

        /**
         * Closes the playback device, deletes the instance of the
         * PlayBackDevice and sets m_device to 0.
         * @see m_device
         * @see PlayBackDevice
         */
        void closeDevice();

        /** updates the mixer matrix if the track selection has changed */
        void trackSelectionChanged();

    protected:

        /** wrapper for our run() function, called from worker thread */
        void run_wrapper(const QVariant &params) override;

    private:

        /** Starts playback device (and worker thread) */
        void startDevicePlayBack();

        /** Stops the playback device (and worker thread) */
        void stopDevicePlayBack();

    private:

        /** Reference to our signal manager */
        Kwave::SignalManager &m_signal_manager;

        /**
         * Thread that executes the run() member function.
         */
        Kwave::WorkerThread m_thread;

        /** The playback device used for playback */
        Kwave::PlayBackDevice *m_device;

        /** Mutex for locking access to the playback device */
        QMutex m_lock_device;

        /** the parameters used for playback */
        Kwave::PlayBackParam m_playback_params;

        /**
         * Mutex for locking access to members that control the playback
         * loop, like m_should_seek, m_seek_pos and m_mixer
         */
        QMutex m_lock_playback;

        /** if true, m_seek_pos is valid and a seek has been requested */
        bool m_should_seek;

        /** position to seek to */
        sample_index_t m_seek_pos;

        /** notification flag, true if the track selection has changed */
        bool m_track_selection_changed;

        /**
         * If true, we are in "reload" mode. In this mode the playback is
         * paused and continued without emitting a sigPlaybackDone. This
         * is useful if playback parameters or signal selection has changed
         * during playback.
         */
        bool m_reload_mode;

        /** if set to true, the playback will be done in loop mode */
        bool m_loop_mode;

        /** if true, playback is only paused and can be continued */
        bool m_paused;

        /** is set to true if the playback has been started */
        bool m_playing;

        /** the current play position */
        sample_index_t m_playback_position;

        /** the start position for playback */
        sample_index_t m_playback_start;

        /** the end position for playback */
        sample_index_t m_playback_end;

        /** Start of the selection when playback started */
        sample_index_t m_old_first;

        /** End of the selection when playback started */
        sample_index_t m_old_last;

        /** list of playback device factories */
        QList<Kwave::PlaybackDeviceFactory *> m_playback_factories;

    };
}

#endif /* PLAYBACK_CONTROLLER_H */

//***************************************************************************
//***************************************************************************