Fix audio align. Related to #71 and #73

parent 235df2d3
Pipeline #2237 passed with stage
in 21 minutes and 15 seconds
......@@ -90,6 +90,7 @@ int AudioCorrelation::getShift(int childIndex) const
int indexOffset = (int)m_correlations.at(childIndex)->maxIndex();
indexOffset -= (int)m_children.at(childIndex)->envelope().size();
indexOffset += (int)m_children.at(childIndex)->offset();
return indexOffset;
}
......
......@@ -24,20 +24,22 @@ AudioEnvelope::AudioEnvelope(const QString &binId, int clipId, size_t offset, si
: m_offset(offset)
, m_clipId(clipId)
, m_startpos(startPos)
, m_length(length)
{
std::shared_ptr<ProjectClip> clip = pCore->bin()->getBinClip(binId);
m_envelopeSize = clip->frameDuration();
m_producer = clip->cloneProducer();
if (length > 2000) {
// Analyse on timeline clip zone only
m_offset = 0;
m_producer->set_in_and_out((int) offset, (int) (offset + length));
}
m_envelopeSize = m_producer->get_playtime();
m_producer->set("set.test_image", 1);
connect(&m_watcher, &QFutureWatcherBase::finished, this, [this] { envelopeReady(this); });
if (!m_producer || !m_producer->is_valid()) {
qCDebug(KDENLIVE_LOG) << "// Cannot create envelope for producer: " << binId;
} else {
m_info = std::make_unique<AudioInfo>(m_producer);
if (length > 0) {
Q_ASSERT(length + m_offset <= m_envelopeSize);
m_envelopeSize = length;
}
}
}
......@@ -80,6 +82,11 @@ const AudioEnvelope::AudioSummary &AudioEnvelope::audioSummary()
return *m_audioSummary.constBegin();
}
const size_t AudioEnvelope::offset()
{
return m_offset;
}
const std::vector<qint64> &AudioEnvelope::envelope()
{
// Blocks until the summary is available.
......@@ -99,7 +106,7 @@ AudioEnvelope::AudioSummary AudioEnvelope::loadAndNormalizeEnvelope() const
QTime t;
t.start();
m_producer->seek((int)m_offset);
m_producer->seek(0);
for (size_t i = 0; i < summary.audioAmplitudes.size(); ++i) {
std::unique_ptr<Mlt::Frame> frame(m_producer->get_frame((int)i));
qint64 position = mlt_frame_get_position(frame->get_frame());
......
......@@ -55,6 +55,8 @@ public:
QImage drawEnvelope();
const size_t offset();
void dumpInfo();
int clipId() const;
......@@ -92,10 +94,9 @@ private:
QFutureWatcher<AudioSummary> m_watcher;
QFuture<AudioSummary> m_audioSummary;
const size_t m_offset;
size_t m_offset;
const int m_clipId;
const size_t m_startpos;
const size_t m_length;
size_t m_envelopeSize;
signals:
......
......@@ -1544,7 +1544,7 @@ void TimelineController::setAudioRef(int clipId)
std::unique_ptr<AudioEnvelope> envelope(new AudioEnvelope(getClipBinId(clipId), clipId));
m_audioCorrelator.reset(new AudioCorrelation(std::move(envelope)));
connect(m_audioCorrelator.get(), &AudioCorrelation::gotAudioAlignData, [&](int cid, int shift) {
int pos = m_model->getClipPosition(m_audioRef) + shift + m_model->getClipIn(m_audioRef);
int pos = m_model->getClipPosition(m_audioRef) + shift - m_model->getClipIn(m_audioRef);
bool result = m_model->requestClipMove(cid, m_model->getClipTrackId(cid), pos, true, true);
if (!result) {
pCore->displayMessage(i18n("Cannot move clip to frame %1.", (pos + shift)), InformationMessage, 500);
......@@ -1580,7 +1580,7 @@ void TimelineController::alignAudio(int clipId)
}
}
// Perform audio calculation
AudioEnvelope *envelope = new AudioEnvelope(getClipBinId(clipId), clipId, (size_t)m_model->getClipIn(clipId), (size_t)m_model->getClipPlaytime(clipId),
AudioEnvelope *envelope = new AudioEnvelope(otherBinId, clipId, (size_t)m_model->getClipIn(clipId), (size_t)m_model->getClipPlaytime(clipId),
(size_t)m_model->getClipPosition(clipId));
m_audioCorrelator->addChild(envelope);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment