WaveformComputer.hpp
1 #pragma once
2 #include <Media/AudioArray.hpp>
3 #include <Media/MediaFileHandle.hpp>
4 
5 #include <score/tools/Debug.hpp>
6 
7 #include <QImage>
8 #include <QObject>
9 #include <QVector>
10 
11 #include <score_plugin_media_export.h>
12 
13 #include <memory>
14 #include <verdigris>
15 
16 class QGraphicsView;
17 namespace Media::Sound
18 {
19 class LayerView;
20 struct WaveformComputerImpl;
21 
23 {
24  std::shared_ptr<AudioFile> file;
25  double zoom{};
26  double tempo_ratio{};
27 
28  QSizeF layerSize{};
29  double devicePixelRatio{};
30  double view_x0{};
31  double view_xmax{};
32 
33  TimeVal startOffset{};
34  TimeVal loopDuration{};
35  bool loops{};
36 
37  bool colors{};
38 };
39 
41 {
42  enum Mode
43  {
44  RMS,
45  Mean,
46  Sample
47  } mode{};
48  double zoom{};
49 
50  int x0{};
51  int xf{};
52 };
53 
54 struct SCORE_PLUGIN_MEDIA_EXPORT WaveformComputer : public QObject
55 {
56  W_OBJECT(WaveformComputer)
57 public:
58  WaveformComputer(bool threaded = true);
60 
61  void stop();
62 
63 public:
64  void recompute(WaveformRequest req)
65  E_SIGNAL(SCORE_PLUGIN_MEDIA_EXPORT, recompute, req);
66 
67  void ready(QVector<QImage*> img, ComputedWaveform wf)
68  E_SIGNAL(SCORE_PLUGIN_MEDIA_EXPORT, ready, img, wf);
69 
70 private:
71  friend struct WaveformComputerImpl;
72 
73  void on_recompute(WaveformRequest&& req, int64_t n);
74  void timerEvent(QTimerEvent* event) override;
75 
76  std::atomic_int64_t m_redraw_count = std::numeric_limits<int64_t>::lowest();
77  std::chrono::steady_clock::time_point last_request = std::chrono::steady_clock::now();
78  std::chrono::steady_clock::time_point last_render = {};
79  bool m_forceRedraw = false;
80 
81  WaveformRequest m_currentRequest;
82 
83  int64_t m_n{};
84  int64_t m_processed_n{-1};
85 
86  std::shared_ptr<AudioFile> m_currentFile;
87  Media::AudioFile::ViewHandle m_currentView;
88  std::atomic_bool m_abort{};
89 };
90 
91 }
92 
93 inline QDataStream& operator<<(QDataStream& i, const Media::Sound::WaveformRequest& sel)
94 {
95  SCORE_ABORT;
96  return i;
97 }
98 inline QDataStream& operator>>(QDataStream& i, Media::Sound::WaveformRequest& sel)
99 {
100  SCORE_ABORT;
101  return i;
102 }
103 inline QDataStream& operator<<(QDataStream& i, const Media::Sound::ComputedWaveform& sel)
104 {
105  SCORE_ABORT;
106  return i;
107 }
108 inline QDataStream& operator>>(QDataStream& i, Media::Sound::ComputedWaveform& sel)
109 {
110  SCORE_ABORT;
111  return i;
112 }
113 Q_DECLARE_METATYPE(Media::Sound::WaveformRequest)
114 W_REGISTER_ARGTYPE(Media::Sound::WaveformRequest)
115 Q_DECLARE_METATYPE(Media::Sound::ComputedWaveform)
116 W_REGISTER_ARGTYPE(Media::Sound::ComputedWaveform)
117 W_REGISTER_ARGTYPE(QVector<QImage>)
118 Q_DECLARE_METATYPE(QVector<QImage*>)
119 W_REGISTER_ARGTYPE(QVector<QImage*>)
Definition: MediaFileHandle.hpp:211
Definition: WaveformComputer.hpp:41
Definition: WaveformComputer.hpp:55
Definition: WaveformComputer.cpp:51
Definition: WaveformComputer.hpp:23
Definition: TimeValue.hpp:21