|
| 1 | +#include "AudioThumbnailWaveformThumbnail.h" |
| 2 | + |
| 3 | +#include <SVSCraftCore/MusicTime.h> |
| 4 | +#include <SVSCraftCore/MusicTimeline.h> |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | +#include <cmath> |
| 8 | +#include <utility> |
| 9 | + |
| 10 | +#include <dspxmodel/AudioClip.h> |
| 11 | +#include <dspxmodel/Clip.h> |
| 12 | +#include <dspxmodel/ClipTime.h> |
| 13 | +#include <dspxmodel/Model.h> |
| 14 | +#include <dspxmodel/Tempo.h> |
| 15 | +#include <dspxmodel/TempoSequence.h> |
| 16 | +#include <dspxmodel/Timeline.h> |
| 17 | + |
| 18 | +#include <coreplugin/DspxDocument.h> |
| 19 | +#include <coreplugin/ProjectDocumentContext.h> |
| 20 | +#include <coreplugin/ProjectTimeline.h> |
| 21 | +#include <coreplugin/ProjectWindowInterface.h> |
| 22 | + |
| 23 | +#include <audiovisualizer/internal/AudioMipmapAddOn.h> |
| 24 | + |
| 25 | +namespace AudioVisualizer::Internal { |
| 26 | + |
| 27 | + AudioThumbnailWaveformThumbnail::AudioThumbnailWaveformThumbnail(QQuickItem *parent) : WaveformThumbnail(parent) { |
| 28 | + } |
| 29 | + |
| 30 | + AudioThumbnailWaveformThumbnail::~AudioThumbnailWaveformThumbnail() { |
| 31 | + resetMapping(); |
| 32 | + } |
| 33 | + |
| 34 | + dspx::AudioClip *AudioThumbnailWaveformThumbnail::audioClip() const { |
| 35 | + return m_audioClip; |
| 36 | + } |
| 37 | + |
| 38 | + void AudioThumbnailWaveformThumbnail::setAudioClip(dspx::AudioClip *audioClip) { |
| 39 | + if (m_audioClip == audioClip) { |
| 40 | + return; |
| 41 | + } |
| 42 | + resetMapping(); |
| 43 | + m_audioClip = audioClip; |
| 44 | + reconnectProjectWindowInterface(); |
| 45 | + reconnectAudioClip(); |
| 46 | + updateMapping(); |
| 47 | + updateWaveformGeometry(); |
| 48 | + Q_EMIT audioClipChanged(); |
| 49 | + } |
| 50 | + |
| 51 | + Core::ProjectWindowInterface *AudioThumbnailWaveformThumbnail::projectWindowInterface() const { |
| 52 | + return m_projectWindowInterface; |
| 53 | + } |
| 54 | + |
| 55 | + void AudioThumbnailWaveformThumbnail::setProjectWindowInterface(Core::ProjectWindowInterface *projectWindowInterface) { |
| 56 | + if (m_projectWindowInterface == projectWindowInterface) { |
| 57 | + return; |
| 58 | + } |
| 59 | + resetMapping(); |
| 60 | + m_projectWindowInterface = projectWindowInterface; |
| 61 | + reconnectAudioClip(); |
| 62 | + reconnectProjectWindowInterface(); |
| 63 | + updateMapping(); |
| 64 | + updateWaveformGeometry(); |
| 65 | + Q_EMIT projectWindowInterfaceChanged(); |
| 66 | + } |
| 67 | + |
| 68 | + double AudioThumbnailWaveformThumbnail::viewportOffset() const { |
| 69 | + return m_viewportOffset; |
| 70 | + } |
| 71 | + |
| 72 | + void AudioThumbnailWaveformThumbnail::setViewportOffset(double viewportOffset) { |
| 73 | + if (qFuzzyCompare(m_viewportOffset, viewportOffset)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + m_viewportOffset = viewportOffset; |
| 77 | + updateWaveformGeometry(); |
| 78 | + Q_EMIT viewportOffsetChanged(); |
| 79 | + } |
| 80 | + |
| 81 | + double AudioThumbnailWaveformThumbnail::viewportLength() const { |
| 82 | + return m_viewportLength; |
| 83 | + } |
| 84 | + |
| 85 | + void AudioThumbnailWaveformThumbnail::setViewportLength(double viewportLength) { |
| 86 | + if (qFuzzyCompare(m_viewportLength, viewportLength)) { |
| 87 | + return; |
| 88 | + } |
| 89 | + m_viewportLength = viewportLength; |
| 90 | + updateWaveformGeometry(); |
| 91 | + Q_EMIT viewportLengthChanged(); |
| 92 | + } |
| 93 | + |
| 94 | + double AudioThumbnailWaveformThumbnail::sampleRate() const { |
| 95 | + return m_sampleRate; |
| 96 | + } |
| 97 | + |
| 98 | + void AudioThumbnailWaveformThumbnail::setSampleRate(double sampleRate) { |
| 99 | + if (qFuzzyCompare(m_sampleRate, sampleRate)) { |
| 100 | + return; |
| 101 | + } |
| 102 | + m_sampleRate = sampleRate; |
| 103 | + updateWaveformGeometry(); |
| 104 | + Q_EMIT sampleRateChanged(); |
| 105 | + } |
| 106 | + |
| 107 | + void AudioThumbnailWaveformThumbnail::resetMapping() { |
| 108 | + if (m_mipmapAddOn && m_audioClip) { |
| 109 | + m_mipmapAddOn->unmapThumbnailVIew(m_audioClip, this); |
| 110 | + } |
| 111 | + m_mipmapAddOn.clear(); |
| 112 | + for (const auto &connection : std::as_const(m_audioClipConnections)) { |
| 113 | + disconnect(connection); |
| 114 | + } |
| 115 | + m_audioClipConnections.clear(); |
| 116 | + for (const auto &connection : std::as_const(m_projectWindowInterfaceConnections)) { |
| 117 | + disconnect(connection); |
| 118 | + } |
| 119 | + m_projectWindowInterfaceConnections.clear(); |
| 120 | + } |
| 121 | + |
| 122 | + void AudioThumbnailWaveformThumbnail::updateMapping() { |
| 123 | + if (!m_audioClip || !m_projectWindowInterface) { |
| 124 | + clearWaveformGeometry(); |
| 125 | + return; |
| 126 | + } |
| 127 | + m_mipmapAddOn = AudioMipmapAddOn::of(m_projectWindowInterface); |
| 128 | + if (m_mipmapAddOn) { |
| 129 | + m_mipmapAddOn->mapThumbnailView(m_audioClip, this); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + void AudioThumbnailWaveformThumbnail::reconnectAudioClip() { |
| 134 | + if (!m_audioClip) { |
| 135 | + return; |
| 136 | + } |
| 137 | + m_audioClipConnections.append(connect(m_audioClip, &dspx::Clip::positionChanged, this, [this] { |
| 138 | + updateWaveformGeometry(); |
| 139 | + })); |
| 140 | + m_audioClipConnections.append(connect(m_audioClip, &dspx::Clip::lengthChanged, this, [this] { |
| 141 | + updateWaveformGeometry(); |
| 142 | + })); |
| 143 | + m_audioClipConnections.append(connect(m_audioClip, &QObject::destroyed, this, [this] { |
| 144 | + m_audioClip.clear(); |
| 145 | + resetMapping(); |
| 146 | + clearWaveformGeometry(); |
| 147 | + Q_EMIT audioClipChanged(); |
| 148 | + })); |
| 149 | + } |
| 150 | + |
| 151 | + void AudioThumbnailWaveformThumbnail::reconnectProjectWindowInterface() { |
| 152 | + if (!m_projectWindowInterface) { |
| 153 | + return; |
| 154 | + } |
| 155 | + auto timeline = m_projectWindowInterface->projectTimeline() ? m_projectWindowInterface->projectTimeline()->musicTimeline() : nullptr; |
| 156 | + if (timeline) { |
| 157 | + m_projectWindowInterfaceConnections.append(connect(timeline, &SVS::MusicTimeline::tempiChanged, this, [this] { |
| 158 | + updateWaveformGeometry(); |
| 159 | + })); |
| 160 | + } |
| 161 | + m_projectWindowInterfaceConnections.append(connect(m_projectWindowInterface, &QObject::destroyed, this, [this] { |
| 162 | + m_projectWindowInterface.clear(); |
| 163 | + resetMapping(); |
| 164 | + clearWaveformGeometry(); |
| 165 | + Q_EMIT projectWindowInterfaceChanged(); |
| 166 | + })); |
| 167 | + } |
| 168 | + |
| 169 | + void AudioThumbnailWaveformThumbnail::updateWaveformGeometry() { |
| 170 | + if (!m_audioClip || !m_projectWindowInterface || m_sampleRate <= 0.0 || m_viewportLength <= 0.0) { |
| 171 | + clearWaveformGeometry(); |
| 172 | + return; |
| 173 | + } |
| 174 | + if (!m_projectWindowInterface->projectTimeline() || !m_projectWindowInterface->projectTimeline()->musicTimeline()) { |
| 175 | + clearWaveformGeometry(); |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + const auto time = m_audioClip->time(); |
| 180 | + if (!time || time->clipLen() <= 0) { |
| 181 | + clearWaveformGeometry(); |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + const double clipPosition = m_audioClip->position(); |
| 186 | + const double clipStart = time->clipStart(); |
| 187 | + const double clipLength = time->clipLen(); |
| 188 | + const double clipEnd = clipPosition + clipLength; |
| 189 | + const double viewportStart = clipPosition + m_viewportOffset; |
| 190 | + const double viewportEnd = viewportStart + m_viewportLength; |
| 191 | + const double sectionStart = qMax(clipPosition, viewportStart); |
| 192 | + const double sectionEnd = qMin(clipEnd, viewportEnd); |
| 193 | + if (sectionEnd <= sectionStart) { |
| 194 | + clearWaveformGeometry(); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + const double audioZeroPosition = clipPosition - clipStart; |
| 199 | + setWaveformOffset(tickRangeToSamples(audioZeroPosition, sectionStart)); |
| 200 | + |
| 201 | + QList<double> sectionBoundaries{sectionStart}; |
| 202 | + auto projectDocumentContext = m_projectWindowInterface->projectDocumentContext(); |
| 203 | + auto document = projectDocumentContext ? projectDocumentContext->document() : nullptr; |
| 204 | + auto model = document ? document->model() : nullptr; |
| 205 | + auto tempoSequence = model && model->timeline() ? model->timeline()->tempos() : nullptr; |
| 206 | + if (tempoSequence) { |
| 207 | + const auto sliceStart = qMax(0, static_cast<int>(std::floor(sectionStart))); |
| 208 | + const auto sliceLength = qMax(0, static_cast<int>(std::ceil(sectionEnd)) - sliceStart + 1); |
| 209 | + const auto tempos = tempoSequence->slice(sliceStart, sliceLength); |
| 210 | + for (auto tempo : tempos) { |
| 211 | + if (!tempo) { |
| 212 | + continue; |
| 213 | + } |
| 214 | + const double tempoPosition = tempo->pos(); |
| 215 | + if (tempoPosition > sectionStart && tempoPosition < sectionEnd) { |
| 216 | + sectionBoundaries.append(tempoPosition); |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + sectionBoundaries.append(sectionEnd); |
| 221 | + std::sort(sectionBoundaries.begin(), sectionBoundaries.end()); |
| 222 | + |
| 223 | + QList<SVS::WaveformThumbnailSection> sections; |
| 224 | + sections.reserve(sectionBoundaries.size() > 1 ? sectionBoundaries.size() - 1 : 0); |
| 225 | + for (int i = 0; i + 1 < sectionBoundaries.size(); ++i) { |
| 226 | + const auto start = sectionBoundaries.at(i); |
| 227 | + const auto end = sectionBoundaries.at(i + 1); |
| 228 | + if (end <= start) { |
| 229 | + continue; |
| 230 | + } |
| 231 | + sections.append({ |
| 232 | + (start - clipPosition) / clipLength, |
| 233 | + (end - clipPosition) / clipLength, |
| 234 | + tickRangeToSamples(start, end), |
| 235 | + }); |
| 236 | + } |
| 237 | + setWaveformSections(sections); |
| 238 | + } |
| 239 | + |
| 240 | + void AudioThumbnailWaveformThumbnail::clearWaveformGeometry() { |
| 241 | + setWaveformOffset(0.0); |
| 242 | + setWaveformSections({}); |
| 243 | + } |
| 244 | + |
| 245 | + double AudioThumbnailWaveformThumbnail::tickToMillisecond(double tick) const { |
| 246 | + if (!m_projectWindowInterface || !m_projectWindowInterface->projectTimeline()) { |
| 247 | + return 0.0; |
| 248 | + } |
| 249 | + auto timeline = m_projectWindowInterface->projectTimeline()->musicTimeline(); |
| 250 | + if (!timeline) { |
| 251 | + return 0.0; |
| 252 | + } |
| 253 | + |
| 254 | + const double boundedTick = qMax(0.0, tick); |
| 255 | + const auto wholeTick = static_cast<int>(std::floor(boundedTick)); |
| 256 | + auto millisecond = timeline->create(0, 0, wholeTick).millisecond(); |
| 257 | + const auto fraction = boundedTick - wholeTick; |
| 258 | + if (!qFuzzyIsNull(fraction)) { |
| 259 | + millisecond += fraction * 60.0 * 1000.0 / (timeline->ticksPerQuarterNote() * timeline->tempoAt(wholeTick)); |
| 260 | + } |
| 261 | + return millisecond; |
| 262 | + } |
| 263 | + |
| 264 | + double AudioThumbnailWaveformThumbnail::tickRangeToSamples(double startTick, double endTick) const { |
| 265 | + if (endTick <= startTick || m_sampleRate <= 0.0) { |
| 266 | + return 0.0; |
| 267 | + } |
| 268 | + return (tickToMillisecond(endTick) - tickToMillisecond(startTick)) * m_sampleRate / 1000.0; |
| 269 | + } |
| 270 | + |
| 271 | +} |
| 272 | + |
| 273 | +#include "moc_AudioThumbnailWaveformThumbnail.cpp" |
0 commit comments