MythTV  master
visualize.h
Go to the documentation of this file.
1 /*
2  visualize.h
3 
4  (c) 2003 Thor Sigvaldason and Isaac Richards
5  Closely based on code from mq3 by Brad Hughes
6 
7  Part of the mythTV project
8 
9  music visualizers
10 */
11 
12 #ifndef VISUALIZE_H
13 #define VISUALIZE_H
14 
15 // C++ headers
16 #include <vector>
17 
18 // Qt headers
19 #include <QCoreApplication>
20 #include <QVector>
21 
22 // MythTV headers
23 #include <libmyth/visual.h>
26 
27 // MythMusic headers
28 #include "constants.h"
29 
30 #include <complex>
31 extern "C" {
32  #include <libavutil/mem.h>
33  #include <libavcodec/avfft.h>
34 }
35 
36 #define SAMPLES_DEFAULT_SIZE 512
37 
38 class MainVisual;
39 
40 class VisualNode
41 {
42  public:
43  VisualNode(short *l, short *r, unsigned long n, std::chrono::milliseconds timecode)
44  : m_left(l), m_right(r), m_length(n), m_offset(timecode)
45  {
46  // left and right are allocated and then passed to this class
47  // the code that allocated left and right should give up all ownership
48  }
49 
51  {
52  delete [] m_left;
53  delete [] m_right;
54  }
55 
56  short *m_left {nullptr};
57  short *m_right {nullptr};
58  unsigned long m_length;
59  std::chrono::milliseconds m_offset;
60 };
61 
63 {
64  public:
65  explicit VisualBase(bool screensaverenable = false);
66  virtual ~VisualBase(void);
67 
68  // return true if the output should stop
69  virtual bool process( VisualNode *node ) = 0;
70 
71  // this is called on nodes that will not be displayed :: Not needed for most visualizations
72  // (i.e. between the displayed frames, if you need the whole audio stream)
73  virtual bool processUndisplayed( VisualNode */*node*/ )
74  {
75  return true; // By default this does nothing : Ignore the in-between chunks of audio data
76  };
77 
78  virtual bool draw( QPainter *, const QColor & ) = 0;
79  virtual void resize( const QSize &size ) = 0;
80  virtual void handleKeyPress(const QString &action) = 0;
81  virtual int getDesiredFPS(void) { return m_fps; }
82  // Override this if you need the potential of capturing more data than the default
83  virtual unsigned long getDesiredSamples(void) { return SAMPLES_DEFAULT_SIZE; }
84  static void drawWarning(QPainter *p, const QColor &back, QSize size, const QString& warning, int fontsize = 28);
85 
86  protected:
87  int m_fps {20};
88  bool m_xscreensaverenable {true};
89 };
90 
92 {
93  public:
95  virtual ~VisFactory() = default;
96  const VisFactory* next() const {return m_pNextVisFactory;}
97  virtual const QString &name(void) const = 0;
98  virtual VisualBase* create(MainVisual *parent, const QString &pluginName) const = 0;
99  virtual uint plugins(QStringList *list) const = 0;
100  static const VisFactory* VisFactories() {return g_pVisFactories;}
101  protected:
104 };
105 
106 #define RUBBERBAND false
107 #define TWOCOLOUR 0
108 
109 class StereoScope : public VisualBase
110 {
111  public:
112  StereoScope();
113  ~StereoScope() override = default;
114 
115  void resize( const QSize &size ) override; // VisualBase
116  bool process( VisualNode *node ) override; // VisualBase
117  bool draw( QPainter *p, const QColor &back ) override; // VisualBase
118  void handleKeyPress(const QString &action) override // VisualBase
119  {(void) action;}
120 
121  protected:
122  QColor m_startColor {Qt::green};
123  QColor m_targetColor {Qt::red};
124  std::vector<double> m_magnitudes {};
125  QSize m_size;
126  bool const m_rubberband {RUBBERBAND};
127  double const m_falloff {1.0};
128 };
129 
130 class MonoScope : public StereoScope
131 {
132  public:
133  MonoScope() = default;
134  ~MonoScope() override = default;
135 
136  bool process( VisualNode *node ) override; // StereoScope
137  bool draw( QPainter *p, const QColor &back ) override; // StereoScope
138 };
139 
140 class LogScale
141 {
142  public:
143  explicit LogScale(int maxscale = 0, int maxrange = 0);
144  ~LogScale();
145 
146  int scale() const { return m_s; }
147  int range() const { return m_r; }
148 
149  void setMax(int maxscale, int maxrange);
150 
151  int operator[](int index);
152 
153 
154  private:
155  int *m_indices {nullptr};
156  int m_s {0};
157  int m_r {0};
158 };
159 
160 class Spectrum : public VisualBase
161 {
162  // This class draws bars (up and down)
163  // based on the magnitudes at various
164  // frequencies in the audio data.
165 
166  public:
167  Spectrum();
168  ~Spectrum() override;
169 
170  void resize(const QSize &size) override; // VisualBase
171  bool process(VisualNode *node) override; // VisualBase
172  bool draw(QPainter *p, const QColor &back = Qt::black) override; // VisualBase
173  void handleKeyPress(const QString &action) override // VisualBase
174  {(void) action;}
175 
176  protected:
177  static inline double clamp(double cur, double max, double min);
178 
179  QColor m_startColor {Qt::blue};
180  QColor m_targetColor {Qt::red};
181  QVector<QRect> m_rects;
182  QVector<double> m_magnitudes;
183  QSize m_size;
185 
186  // Setup the "magical" audio data transformations
187  // provided by the Fast Fourier Transforms library
188  double m_scaleFactor {2.0};
189  double m_falloff {10.0};
191 
192  FFTComplex* m_dftL { nullptr };
193  FFTComplex* m_dftR { nullptr };
194  FFTContext* m_fftContextForward { nullptr };
195 };
196 
197 class Squares : public Spectrum
198 {
199  public:
200  Squares();
201  ~Squares() override = default;
202 
203  void resize (const QSize &newsize) override; // Spectrum
204  bool draw(QPainter *p, const QColor &back = Qt::black) override; // Spectrum
205  void handleKeyPress(const QString &action) override // Spectrum
206  {(void) action;}
207 
208  private:
209  void drawRect(QPainter *p, QRect *rect, int i, int c, int w, int h);
210  QSize m_actualSize {0,0};
211  int m_fakeHeight {0};
213 };
214 
215 class Piano : public VisualBase
216 {
217  // This class draws bars (up and down)
218  // based on the magnitudes at piano pitch
219  // frequencies in the audio data.
220 
221 #define PIANO_AUDIO_SIZE 4096
222 #define PIANO_N 88
223 
224 #define piano_audio float
225 #define goertzel_data float
226 
227 #define PIANO_RMS_NEGLIGIBLE .001
228 #define PIANO_SPECTRUM_SMOOTHING 0.95
229 #define PIANO_MIN_VOL (-10)
230 #define PIANO_KEYPRESS_TOO_LIGHT .2
231 
235 
236  // This keeps track of the samples processed for each note
237  // Low notes require a lot of samples to be correctly identified
238  // Higher ones are displayed quicker
241 
242  bool is_black_note; // These are painted on top of white notes, and have different colouring
243 };
244 
245  public:
246  Piano();
247  ~Piano() override;
248 
249  void resize(const QSize &size) override; // VisualBase
250 
251  bool process(VisualNode *node) override; // VisualBase
252 
253  // These functions are new, since we need to inspect all the data
254  bool processUndisplayed(VisualNode *node) override; // VisualBase
255  unsigned long getDesiredSamples(void) override; // VisualBase
256 
257  bool draw(QPainter *p, const QColor &back = Qt::black) override; // VisualBase
258  void handleKeyPress(const QString &action) override // VisualBase
259  {(void) action;}
260 
261  protected:
262  static inline double clamp(double cur, double max, double min);
263  bool process_all_types(VisualNode *node, bool this_will_be_displayed);
264  void zero_analysis(void);
265 
266  QColor m_whiteStartColor {245,245,245};
267  QColor m_whiteTargetColor {Qt::red};
268  QColor m_blackStartColor {10,10,10};
269  QColor m_blackTargetColor {Qt::red};
270 
271  std::vector<QRect> m_rects {};
272  QSize m_size;
273 
274  std::chrono::milliseconds m_offsetProcessed {0ms};
275 
278 
279  std::vector<double> m_magnitude {};
280 };
281 
282 class AlbumArt : public VisualBase
283 {
285 
286  public:
287  AlbumArt(void);
288  ~AlbumArt() override = default;
289 
290  void resize(const QSize &size) override; // VisualBase
291  bool process(VisualNode *node = nullptr) override; // VisualBase
292  bool draw(QPainter *p, const QColor &back = Qt::black) override; // VisualBase
293  void handleKeyPress(const QString &action) override; // VisualBase
294 
295  private:
296  bool needsUpdate(void);
297  void findFrontCover(void);
298  bool cycleImage(void);
299 
300  QSize m_size;
301  QSize m_cursize;
303  QImage m_image;
304 
306  QDateTime m_lastCycle;
307 };
308 
309 class Blank : public VisualBase
310 {
311  // This draws ... well ... nothing
312  public:
313  Blank();
314  ~Blank() override = default;
315 
316  void resize(const QSize &size) override; // VisualBase
317  bool process(VisualNode *node = nullptr) override; // VisualBase
318  bool draw(QPainter *p, const QColor &back = Qt::black) override; // VisualBase
319  void handleKeyPress(const QString &action) override // VisualBase
320  {(void) action;}
321 
322  private:
323  QSize m_size;
324 };
325 
326 #endif // __visualize_h
Squares::Squares
Squares()
Definition: visualize.cpp:822
VisualNode::~VisualNode
~VisualNode()
Definition: visualize.h:50
Piano::piano_key_data::samples_processed
int samples_processed
Definition: visualize.h:239
Blank::Blank
Blank()
Definition: visualize.cpp:1541
Spectrum::~Spectrum
~Spectrum() override
Definition: visualize.cpp:604
Piano::piano_key_data
Definition: visualize.h:232
Piano::m_magnitude
std::vector< double > m_magnitude
Definition: visualize.h:279
Piano::piano_key_data::samples_process_before_display_update
int samples_process_before_display_update
Definition: visualize.h:240
AlbumArt::findFrontCover
void findFrontCover(void)
Definition: visualize.cpp:1355
VisFactory::g_pVisFactories
static VisFactory * g_pVisFactories
Definition: visualize.h:102
Piano
Definition: visualize.h:215
AlbumArt::resize
void resize(const QSize &size) override
Definition: visualize.cpp:1403
Spectrum::m_scaleFactor
double m_scaleFactor
Definition: visualize.h:188
Piano::draw
bool draw(QPainter *p, const QColor &back=Qt::black) override
Definition: visualize.cpp:1217
back
static guint32 * back
Definition: goom_core.cpp:31
VisualNode
Definition: videovisual.h:25
Piano::processUndisplayed
bool processUndisplayed(VisualNode *node) override
Definition: visualize.cpp:1065
Piano::zero_analysis
void zero_analysis(void)
Definition: visualize.cpp:959
Piano::getDesiredSamples
unsigned long getDesiredSamples(void) override
Definition: visualize.cpp:1055
Spectrum::m_dftR
FFTComplex * m_dftR
Definition: visualize.h:193
Spectrum::process
bool process(VisualNode *node) override
Definition: visualize.cpp:649
VisualNode::VisualNode
VisualNode(short *l, short *r, unsigned long n, std::chrono::milliseconds timecode)
Definition: visualize.h:43
StereoScope
Definition: visualize.h:109
AlbumArt::m_image
QImage m_image
Definition: visualize.h:303
VisualBase::getDesiredSamples
virtual unsigned long getDesiredSamples(void)
Definition: visualize.h:83
VisFactory::VisFactory
VisFactory()
Definition: visualize.h:94
RUBBERBAND
#define RUBBERBAND
Definition: visualize.h:106
mythbaseexp.h
AlbumArt::Q_DECLARE_TR_FUNCTIONS
Q_DECLARE_TR_FUNCTIONS(AlbumArt)
LogScale::m_indices
int * m_indices
Definition: videovisualdefs.h:70
Blank::handleKeyPress
void handleKeyPress(const QString &action) override
Definition: visualize.h:319
LogScale::operator[]
int operator[](int index) const
Definition: videovisualdefs.h:63
LogScale::m_r
int m_r
Definition: videovisualdefs.h:72
AlbumArt
Definition: visualize.h:282
StereoScope::m_rubberband
const bool m_rubberband
Definition: visualize.h:126
Spectrum::m_magnitudes
QVector< double > m_magnitudes
Definition: visualize.h:182
VisualBase
Definition: visualize.h:62
Blank::draw
bool draw(QPainter *p, const QColor &back=Qt::black) override
Definition: visualize.cpp:1558
StereoScope::resize
void resize(const QSize &size) override
Definition: visualize.cpp:167
StereoScope::m_falloff
const double m_falloff
Definition: visualize.h:127
Spectrum::Spectrum
Spectrum()
Definition: visualize.cpp:592
Blank::m_size
QSize m_size
Definition: visualize.h:323
MusicMetadata
Definition: musicmetadata.h:80
StereoScope::process
bool process(VisualNode *node) override
Definition: visualize.cpp:177
LogScale::LogScale
LogScale(int maxscale=0, int maxrange=0)
Definition: videovisualdefs.h:9
StereoScope::m_size
QSize m_size
Definition: visualize.h:125
Piano::m_offsetProcessed
std::chrono::milliseconds m_offsetProcessed
Definition: visualize.h:274
MonoScope
Definition: visualize.h:130
VisualNode::m_left
short * m_left
Definition: videovisual.h:37
VisualBase::VisualBase
VisualBase(bool screensaverenable=false)
Definition: visualize.cpp:44
AlbumArt::m_currImageType
ImageType m_currImageType
Definition: visualize.h:302
SAMPLES_DEFAULT_SIZE
#define SAMPLES_DEFAULT_SIZE
Definition: visualize.h:36
Squares::handleKeyPress
void handleKeyPress(const QString &action) override
Definition: visualize.h:205
Spectrum::draw
bool draw(QPainter *p, const QColor &back=Qt::black) override
Definition: visualize.cpp:761
VisualNode::m_offset
std::chrono::milliseconds m_offset
Definition: videovisual.h:40
Spectrum::m_scale
LogScale m_scale
Definition: visualize.h:184
MonoScope::process
bool process(VisualNode *node) override
Definition: visualize.cpp:395
Spectrum::m_analyzerBarWidth
int m_analyzerBarWidth
Definition: visualize.h:190
Piano::m_whiteStartColor
QColor m_whiteStartColor
Definition: visualize.h:266
MonoScope::~MonoScope
~MonoScope() override=default
VisualBase::resize
virtual void resize(const QSize &size)=0
AlbumArt::draw
bool draw(QPainter *p, const QColor &back=Qt::black) override
Definition: visualize.cpp:1467
Piano::m_blackTargetColor
QColor m_blackTargetColor
Definition: visualize.h:269
Squares
Definition: visualize.h:197
StereoScope::draw
bool draw(QPainter *p, const QColor &back) override
Definition: visualize.cpp:295
Squares::drawRect
void drawRect(QPainter *p, QRect *rect, int i, int c, int w, int h)
Definition: visualize.cpp:834
hardwareprofile.config.p
p
Definition: config.py:33
VisualNode::m_right
short * m_right
Definition: videovisual.h:38
LogScale::range
int range() const
Definition: visualize.h:147
AlbumArt::~AlbumArt
~AlbumArt() override=default
StereoScope::~StereoScope
~StereoScope() override=default
AlbumArt::cycleImage
bool cycleImage(void)
Definition: visualize.cpp:1374
Spectrum::m_startColor
QColor m_startColor
Definition: visualize.h:179
VisFactory
Definition: visualize.h:91
AlbumArt::m_cursize
QSize m_cursize
Definition: visualize.h:301
LogScale::setMax
void setMax(int maxscale, int maxrange)
Definition: videovisualdefs.h:22
LogScale::scale
int scale() const
Definition: visualize.h:146
Piano::piano_key_data::q2
goertzel_data q2
Definition: visualize.h:233
AlbumArt::needsUpdate
bool needsUpdate(void)
Definition: visualize.cpp:1447
VisualBase::getDesiredFPS
virtual int getDesiredFPS(void)
Definition: visualize.h:81
AlbumArt::process
bool process(VisualNode *node=nullptr) override
Definition: visualize.cpp:1408
StereoScope::m_targetColor
QColor m_targetColor
Definition: visualize.h:123
Squares::m_fakeHeight
int m_fakeHeight
Definition: visualize.h:211
VisualBase::~VisualBase
virtual ~VisualBase(void)
Definition: visualize.cpp:51
goertzel_data
#define goertzel_data
Definition: visualize.h:225
VisualBase::draw
virtual bool draw(QPainter *, const QColor &)=0
Piano::resize
void resize(const QSize &size) override
Definition: visualize.cpp:975
StereoScope::m_magnitudes
std::vector< double > m_magnitudes
Definition: visualize.h:124
VisFactory::plugins
virtual uint plugins(QStringList *list) const =0
uint
unsigned int uint
Definition: compat.h:79
VisFactory::VisFactories
static const VisFactory * VisFactories()
Definition: visualize.h:100
AlbumArt::m_currentMetadata
MusicMetadata * m_currentMetadata
Definition: visualize.h:305
LogScale::m_s
int m_s
Definition: videovisualdefs.h:71
LogScale::~LogScale
~LogScale()
Definition: videovisualdefs.h:14
StereoScope::m_startColor
QColor m_startColor
Definition: visualize.h:122
Piano::piano_key_data::magnitude
goertzel_data magnitude
Definition: visualize.h:233
VisualBase::process
virtual bool process(VisualNode *node)=0
VisFactory::create
virtual VisualBase * create(MainVisual *parent, const QString &pluginName) const =0
Squares::m_actualSize
QSize m_actualSize
Definition: visualize.h:210
AlbumArt::m_lastCycle
QDateTime m_lastCycle
Definition: visualize.h:306
MainVisual
Definition: mainvisual.h:34
Piano::m_audioData
piano_audio * m_audioData
Definition: visualize.h:277
VisualBase::processUndisplayed
virtual bool processUndisplayed(VisualNode *)
Definition: visualize.h:73
IT_UNKNOWN
@ IT_UNKNOWN
Definition: musicmetadata.h:30
AlbumArt::handleKeyPress
void handleKeyPress(const QString &action) override
Definition: visualize.cpp:1413
VisFactory::~VisFactory
virtual ~VisFactory()=default
VisualNode::m_length
long m_length
Definition: videovisual.h:39
Squares::~Squares
~Squares() override=default
StereoScope::StereoScope
StereoScope()
Definition: visualize.cpp:162
VisualBase::m_fps
int m_fps
Definition: visualize.h:87
Spectrum::m_falloff
double m_falloff
Definition: visualize.h:189
Piano::piano_key_data::coeff
goertzel_data coeff
Definition: visualize.h:233
MonoScope::draw
bool draw(QPainter *p, const QColor &back) override
Definition: visualize.cpp:485
Piano::m_pianoData
piano_key_data * m_pianoData
Definition: visualize.h:276
Spectrum::resize
void resize(const QSize &size) override
Definition: visualize.cpp:611
Squares::draw
bool draw(QPainter *p, const QColor &back=Qt::black) override
Definition: visualize.cpp:868
VisualNode::m_length
unsigned long m_length
Definition: visualize.h:58
Spectrum::m_fftContextForward
FFTContext * m_fftContextForward
Definition: visualize.h:194
Piano::piano_key_data::is_black_note
bool is_black_note
Definition: visualize.h:242
Blank::~Blank
~Blank() override=default
constants.h
Piano::m_size
QSize m_size
Definition: visualize.h:272
AlbumArt::AlbumArt
AlbumArt(void)
Definition: visualize.cpp:1348
VisualBase::m_xscreensaverenable
bool m_xscreensaverenable
Definition: visualize.h:88
Squares::resize
void resize(const QSize &newsize) override
Definition: visualize.cpp:827
MonoScope::MonoScope
MonoScope()=default
VisFactory::name
virtual const QString & name(void) const =0
Spectrum::m_dftL
FFTComplex * m_dftL
Definition: visualize.h:192
Blank::resize
void resize(const QSize &size) override
Definition: visualize.cpp:1547
Piano::m_whiteTargetColor
QColor m_whiteTargetColor
Definition: visualize.h:267
VisualBase::handleKeyPress
virtual void handleKeyPress(const QString &action)=0
Piano::process
bool process(VisualNode *node) override
Definition: visualize.cpp:1071
VisFactory::m_pNextVisFactory
VisFactory * m_pNextVisFactory
Definition: visualize.h:103
build_compdb.action
action
Definition: build_compdb.py:9
Spectrum::handleKeyPress
void handleKeyPress(const QString &action) override
Definition: visualize.h:173
Blank
Definition: visualize.h:309
Spectrum
Definition: visualize.h:160
Blank::process
bool process(VisualNode *node=nullptr) override
Definition: visualize.cpp:1553
Piano::Piano
Piano()
Definition: visualize.cpp:905
Spectrum::m_rects
QVector< QRect > m_rects
Definition: visualize.h:181
Piano::clamp
static double clamp(double cur, double max, double min)
Definition: visualize.cpp:1208
Piano::~Piano
~Piano() override
Definition: visualize.cpp:951
Piano::piano_key_data::q1
goertzel_data q1
Definition: visualize.h:233
Piano::m_blackStartColor
QColor m_blackStartColor
Definition: visualize.h:268
Squares::m_numberOfSquares
int m_numberOfSquares
Definition: visualize.h:212
Spectrum::clamp
static double clamp(double cur, double max, double min)
Definition: visualize.cpp:752
LogScale
Definition: videovisualdefs.h:6
VisFactory::next
const VisFactory * next() const
Definition: visualize.h:96
AlbumArt::m_size
QSize m_size
Definition: visualize.h:300
ImageType
ImageType
Definition: musicmetadata.h:28
Piano::m_rects
std::vector< QRect > m_rects
Definition: visualize.h:271
musicmetadata.h
Piano::process_all_types
bool process_all_types(VisualNode *node, bool this_will_be_displayed)
Definition: visualize.cpp:1079
StereoScope::handleKeyPress
void handleKeyPress(const QString &action) override
Definition: visualize.h:118
Piano::piano_key_data::max_magnitude_seen
goertzel_data max_magnitude_seen
Definition: visualize.h:234
VisualBase::drawWarning
static void drawWarning(QPainter *p, const QColor &back, QSize size, const QString &warning, int fontsize=28)
Definition: visualize.cpp:63
Spectrum::m_targetColor
QColor m_targetColor
Definition: visualize.h:180
Spectrum::m_size
QSize m_size
Definition: visualize.h:183
piano_audio
#define piano_audio
Definition: visualize.h:224
Piano::handleKeyPress
void handleKeyPress(const QString &action) override
Definition: visualize.h:258
visual.h