Loading...
Searching...
No Matches
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 <mutex>
15#include <verdigris>
16
17class QGraphicsView;
18namespace Media::Sound
19{
20class LayerView;
21struct WaveformComputerImpl;
22
24{
25 std::shared_ptr<AudioFile> file;
26 double zoom{};
27 double tempo_ratio{};
28
29 QSizeF layerSize{};
30 double devicePixelRatio{};
31 double view_x0{};
32 double view_xmax{};
33
34 TimeVal startOffset{};
35 TimeVal loopDuration{};
36 bool loops{};
37
38 bool colors{};
39
43 friend bool operator==(const WaveformRequest&, const WaveformRequest&) = default;
44};
45
47{
48 enum Mode
49 {
50 RMS,
51 Mean,
52 Sample
53 } mode{};
54 double zoom{};
55
56 int x0{};
57 int xf{};
58};
59
62static constexpr int64_t maxWaveformPixels = 3840ll * 2160 * 3;
63
64struct SCORE_PLUGIN_MEDIA_EXPORT WaveformComputer : public QObject
65{
66 W_OBJECT(WaveformComputer)
67public:
68 WaveformComputer(bool threaded = true);
70
71 void stop();
72
77 void claim(const QVector<QImage*>& img);
78
79public:
80 void recompute(WaveformRequest req)
81 E_SIGNAL(SCORE_PLUGIN_MEDIA_EXPORT, recompute, req);
82
83 void ready(QVector<QImage*> img, ComputedWaveform wf)
84 E_SIGNAL(SCORE_PLUGIN_MEDIA_EXPORT, ready, img, wf);
85
86private:
87 friend struct WaveformComputerImpl;
88
89 void on_recompute(WaveformRequest&& req, int64_t n);
90 void timerEvent(QTimerEvent* event) override;
92 void deliver(QVector<QImage*> img, ComputedWaveform wf);
93
94 std::mutex m_inflightMutex;
95 QVector<QImage*> m_inflight;
96
97 std::atomic_int64_t m_redraw_count = std::numeric_limits<int64_t>::lowest();
98 std::chrono::steady_clock::time_point last_render = {};
99
101 std::chrono::steady_clock::duration m_lastRenderDuration{};
102
103 WaveformRequest m_currentRequest;
104
105 int64_t m_n{};
106 int64_t m_processed_n{-1};
107
108 std::shared_ptr<AudioFile> m_currentFile;
109 Media::AudioFile::ViewHandle m_currentView;
110 std::atomic_bool m_abort{};
111};
112
113}
114
115inline QDataStream& operator<<(QDataStream& i, const Media::Sound::WaveformRequest& sel)
116{
117 SCORE_ABORT;
118 return i;
119}
120inline QDataStream& operator>>(QDataStream& i, Media::Sound::WaveformRequest& sel)
121{
122 SCORE_ABORT;
123 return i;
124}
125inline QDataStream& operator<<(QDataStream& i, const Media::Sound::ComputedWaveform& sel)
126{
127 SCORE_ABORT;
128 return i;
129}
130inline QDataStream& operator>>(QDataStream& i, Media::Sound::ComputedWaveform& sel)
131{
132 SCORE_ABORT;
133 return i;
134}
135Q_DECLARE_METATYPE(Media::Sound::WaveformRequest)
136W_REGISTER_ARGTYPE(Media::Sound::WaveformRequest)
137Q_DECLARE_METATYPE(Media::Sound::ComputedWaveform)
138W_REGISTER_ARGTYPE(Media::Sound::ComputedWaveform)
139W_REGISTER_ARGTYPE(QVector<QImage>)
140Q_DECLARE_METATYPE(QVector<QImage*>)
141W_REGISTER_ARGTYPE(QVector<QImage*>)
Definition MediaFileHandle.hpp:222
Definition WaveformComputer.hpp:47
Definition WaveformComputer.hpp:65
Definition WaveformComputer.cpp:75
Definition WaveformComputer.hpp:24
friend bool operator==(const WaveformRequest &, const WaveformRequest &)=default
Definition TimeValue.hpp:21