1 /***************************************************************************
2  *   Copyright (C) 2008-2021 by Ilya Kotov                                 *
3  *   forkotov02@ya.ru                                                      *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20 #ifndef VISUAL_H
21 #define VISUAL_H
22 
23 #include <QMutex>
24 #include <QStringList>
25 #include <QWidget>
26 #include <QHash>
27 #include <stddef.h>
28 #include "qmmp_export.h"
29 
30 #define QMMP_VISUAL_NODE_SIZE 512 //samples
31 
32 class VisualFactory;
33 class VisualBuffer;
34 
35 /*! @brief The Visual class provides the base interface class of visualizations.
36  *  @author Ilya Kotov <forkotov02@ya.ru>
37  */
38 class QMMP_EXPORT Visual : public QWidget
39 {
40     Q_OBJECT
41 public:
42     /*!
43     * Object contsructor.
44     * @param parent Parent object.
45     * @param f Widget flags.
46     */
47     Visual(QWidget *parent, Qt::WindowFlags f = Qt::WindowFlags());
48     /*!
49      * Destructor.
50      */
51     virtual ~Visual();
52     /*!
53     * Returns a list of visual factories.
54     */
55     static QList<VisualFactory*> factories();
56     /*!
57      * Returns plugin file path.
58      * @param factory Visual plugin factory.
59      */
60     static QString file(const VisualFactory *factory);
61     /*!
62      * Sets whether the visual plugin is enabled.
63      * @param factory Visual plugin factory.
64      * @param enable Plugin enable state (\b true - enable, \b false - disable)
65      */
66     static void setEnabled(VisualFactory *factory, bool enable = true);
67     /*!
68      * Returns \b true if visual plugin is enabled, otherwise returns \b false
69      * @param factory Effect plugin factory.
70      */
71     static bool isEnabled(const VisualFactory *factory);
72     /*!
73      * Adds external visualization \b visual.
74      */
75     static void add(Visual*visual);
76     /*!
77      * Removes external visualization \b visual.
78      */
79     static void remove(Visual*);
80     /*!
81      * Prepares visual plugins for usage.
82      * @param parent Parent widget.
83      * @param receiver Receiver object.
84      * @param member A slot to receive changes of active visualizations list.
85      */
86     static void initialize(QWidget *parent, QObject *receiver = nullptr, const char *member = nullptr);
87     /*!
88      * Returns a pointer to a list of created visual objects.
89      */
90     static QList<Visual *> *visuals();
91     /*!
92      * Shows configuration dialog and updates settings automatically.
93      * @param factory Visual plugin factory.
94      * @param parent Parent widget.
95      */
96     static void showSettings(VisualFactory *factory, QWidget *parent);
97     /*!
98      * Adds data for visualization.
99      * @param pcm Audio data.
100      * @param samples Number of samples.
101      * @param channels Number of channels.
102      * @param ts Elapsed time (in milliseconds).
103      * @param delay Audio output delay.
104      */
105     static void addAudio(float *pcm, int samples, int channels, qint64 ts, qint64 delay);
106     /*!
107      * Clears visualization buffer.
108      */
109     static void clearBuffer();
110 
111 public slots:
112     /*!
113      * Starts visualization. Default implementation does nothing.
114      */
115     virtual void start();
116     /*!
117      * Stops visualization. Default implementation does nothing.
118      */
119     virtual void stop();
120 
121 signals:
122     /*!
123      * Emitted when visual widget is closed by user.
124      */
125     void closedByUser();
126 
127 protected:
128     /*!
129      * QWidget's close event. Reimplementation should call base function.
130      * @param event QCloseEvent insatance.
131      */
132     virtual void closeEvent (QCloseEvent *event) override;
133     /*!
134      * Takes visualization data. Caller should allocate \b QMMP_VISUAL_NODE_SIZE
135      * bytes for each channel. If buffer for right channel is not specified,
136      * this function will average data from left and right channels.
137      * @param left Left channel buffer.
138      * @param right Right channel buffer.
139      */
140     bool takeData(float *left, float *right = nullptr);
141 
142 private:
143     static QList<VisualFactory*> *m_factories;
144     static QHash <const VisualFactory*, QString> *m_files;
145     static void checkFactories();
146     static QList<Visual*>  m_visuals;
147     static QHash<VisualFactory*, Visual*> m_vis_map; //internal visualization
148     static QWidget *m_parentWidget;
149     static QObject *m_receiver;
150     static const char *m_member;
151     static VisualBuffer m_buffer;
152 };
153 
154 #endif
155