|
| 1 | +// SPDX-FileCopyrightText: Team OpenVPI |
| 2 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 3 | + |
| 4 | +#include "AudioClipImporter.h" |
| 5 | +#include "AudioClipImporter_p.h" |
| 6 | + |
| 7 | +#include <cmath> |
| 8 | + |
| 9 | +#include <QDir> |
| 10 | +#include <QFileInfo> |
| 11 | +#include <QLoggingCategory> |
| 12 | + |
| 13 | +#include <CoreApi/filelocker.h> |
| 14 | + |
| 15 | +#include <SVSCraftCore/MusicTime.h> |
| 16 | +#include <SVSCraftCore/MusicTimeline.h> |
| 17 | + |
| 18 | +#include <TalcsFormat/FormatEntry.h> |
| 19 | +#include <TalcsFormat/FormatManager.h> |
| 20 | + |
| 21 | +#include <dspxmodelORM/ClipSequence.h> |
| 22 | +#include <dspxmodelORM/Model.h> |
| 23 | +#include <dspxmodelORM/Track.h> |
| 24 | + |
| 25 | +#include <coreplugin/DspxDocument.h> |
| 26 | +#include <coreplugin/ProjectDocumentContext.h> |
| 27 | +#include <coreplugin/ProjectTimeline.h> |
| 28 | +#include <coreplugin/ProjectWindowInterface.h> |
| 29 | + |
| 30 | +#include <transactional/TransactionController.h> |
| 31 | + |
| 32 | +#include <audio/GlobalAudioContext.h> |
| 33 | +#include <audio/internal/HashHelper.h> |
| 34 | +#include <audio/internal/ProjectAudioAddOn.h> |
| 35 | + |
| 36 | +namespace Audio { |
| 37 | + |
| 38 | + Q_STATIC_LOGGING_CATEGORY(lcAudioClipImporter, "diffscope.audio.audioclipimporter") |
| 39 | + |
| 40 | + AudioClipImporter::AudioClipImporter(Core::ProjectWindowInterface *windowHandle, QObject *parent) |
| 41 | + : QObject(parent), d_ptr(new AudioClipImporterPrivate) { |
| 42 | + Q_D(AudioClipImporter); |
| 43 | + d->q_ptr = this; |
| 44 | + d->windowHandle = windowHandle; |
| 45 | + d->document = windowHandle ? windowHandle->projectDocumentContext()->document() : nullptr; |
| 46 | + } |
| 47 | + |
| 48 | + AudioClipImporter::~AudioClipImporter() = default; |
| 49 | + |
| 50 | + bool AudioClipImporter::openFile(const QString &fileName) { |
| 51 | + Q_D(AudioClipImporter); |
| 52 | + const QPointer<AudioClipImporter> guard(this); |
| 53 | + d->io.reset(); |
| 54 | + d->fileName.clear(); |
| 55 | + if (!d->windowHandle || !d->document || !QFileInfo(fileName).isFile()) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + const auto manager = GlobalAudioContext::formatManager(); |
| 60 | + auto entries = manager->entries(); |
| 61 | + if (auto hintedEntry = manager->hintFromExtension(QFileInfo(fileName).suffix().toLower())) { |
| 62 | + entries.removeOne(hintedEntry); |
| 63 | + entries.prepend(hintedEntry); |
| 64 | + } |
| 65 | + for (auto entry : entries) { |
| 66 | + QVariant userData; |
| 67 | + std::unique_ptr<talcs::AbstractAudioFormatIO> io(entry->getFormatOpen(fileName, userData, d->windowHandle->invisibleCentralWidget())); |
| 68 | + if (!guard || !d->windowHandle || !d->document || d->windowHandle->projectDocumentContext()->document() != d->document) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + if (io) { |
| 72 | + return setFile(fileName, io.release(), userData, QString::fromLatin1(entry->metaObject()->className())); |
| 73 | + } |
| 74 | + } |
| 75 | + qCWarning(lcAudioClipImporter) << "Failed to open audio file" << fileName; |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + bool AudioClipImporter::setFile(const QString &fileName, talcs::AbstractAudioFormatIO *io, const QVariant &userData, const QString &entryClassName) { |
| 80 | + Q_D(AudioClipImporter); |
| 81 | + d->io.reset(io); |
| 82 | + d->fileName.clear(); |
| 83 | + if (!d->windowHandle || !d->document || !d->io || !d->io->open(talcs::AbstractAudioFormatIO::Read)) { |
| 84 | + d->io.reset(); |
| 85 | + return false; |
| 86 | + } |
| 87 | + const auto sampleRate = d->io->sampleRate(); |
| 88 | + const auto length = d->io->length(); |
| 89 | + if (!std::isfinite(sampleRate) || sampleRate <= 0 || length < 0 || d->io->channelCount() <= 0) { |
| 90 | + qCWarning(lcAudioClipImporter) << "Invalid audio metadata" << fileName; |
| 91 | + d->io.reset(); |
| 92 | + return false; |
| 93 | + } |
| 94 | + d->path = createAudioPath(d->windowHandle, fileName, userData, entryClassName); |
| 95 | + if (d->path.digest.isEmpty()) { |
| 96 | + qCWarning(lcAudioClipImporter) << "Failed to calculate audio file digest" << fileName; |
| 97 | + d->io.reset(); |
| 98 | + return false; |
| 99 | + } |
| 100 | + d->durationMsec = static_cast<double>(length) * 1000.0 / sampleRate; |
| 101 | + d->fileName = QFileInfo(fileName).absoluteFilePath(); |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + talcs::AbstractAudioFormatIO *AudioClipImporter::audioFormatIO() const { |
| 106 | + Q_D(const AudioClipImporter); |
| 107 | + return d->io.get(); |
| 108 | + } |
| 109 | + |
| 110 | + QString AudioClipImporter::fileName() const { |
| 111 | + Q_D(const AudioClipImporter); |
| 112 | + return d->fileName; |
| 113 | + } |
| 114 | + |
| 115 | + dspx::AudioClip *AudioClipImporter::insertClip(dspx::Track *track, int position, const QString &name) { |
| 116 | + Q_D(AudioClipImporter); |
| 117 | + if (!d->windowHandle || !d->document || !d->windowHandle->projectTimeline() || d->windowHandle->projectDocumentContext()->document() != d->document || !d->io || !track || track->model() != d->document->model() || !track->trackList() || !d->document->transactionController()->isExclusiveToTransaction()) { |
| 118 | + return nullptr; |
| 119 | + } |
| 120 | + if (!(d->io->openMode() & talcs::AbstractAudioFormatIO::Read) && !d->io->open(talcs::AbstractAudioFormatIO::Read)) { |
| 121 | + return nullptr; |
| 122 | + } |
| 123 | + d->io->seek(0); |
| 124 | + position = qMax(0, position); |
| 125 | + const auto timeline = d->windowHandle->projectTimeline()->musicTimeline(); |
| 126 | + const auto positionMsec = timeline->create(0, 0, position).millisecond(); |
| 127 | + const auto endPosition = timeline->create(positionMsec + d->durationMsec).totalTick(); |
| 128 | + const auto model = d->document->model(); |
| 129 | + updateAudioPathLocation(d->windowHandle, &d->path, d->fileName); |
| 130 | + auto clip = model->createAudioClip(); |
| 131 | + clip->setName(name); |
| 132 | + clip->setPath(d->path); |
| 133 | + clip->setClipStart(0); |
| 134 | + clip->setClipLength(qMax(1, endPosition - position)); |
| 135 | + clip->setPosition(position); |
| 136 | + clip->setGain(1); |
| 137 | + |
| 138 | + auto projectAudioAddOn = Internal::ProjectAudioAddOn::of(d->windowHandle); |
| 139 | + if (projectAudioAddOn) { |
| 140 | + projectAudioAddOn->addAudioClipCache(clip, d->io.release()); |
| 141 | + } |
| 142 | + if (!track->clips()->insertItem(clip)) { |
| 143 | + if (projectAudioAddOn) { |
| 144 | + d->io.reset(projectAudioAddOn->takeAudioClipCache(clip)); |
| 145 | + } |
| 146 | + model->destroyItem(clip); |
| 147 | + return nullptr; |
| 148 | + } |
| 149 | + d->io.reset(); |
| 150 | + d->fileName.clear(); |
| 151 | + return clip; |
| 152 | + } |
| 153 | + |
| 154 | + dspx::AudioPathInfo AudioClipImporter::createAudioPath(Core::ProjectWindowInterface *windowHandle, const QString &fileName, const QVariant &userData, const QString &entryClassName) { |
| 155 | + dspx::AudioPathInfo path; |
| 156 | + updateAudioPathLocation(windowHandle, &path, fileName); |
| 157 | + path.formatEntryClassName = entryClassName; |
| 158 | + path.userData = userData; |
| 159 | + path.digest = Internal::HashHelper::digest(QFileInfo(fileName).absoluteFilePath()); |
| 160 | + return path; |
| 161 | + } |
| 162 | + |
| 163 | + void AudioClipImporter::updateAudioPathLocation(Core::ProjectWindowInterface *windowHandle, dspx::AudioPathInfo *path, const QString &fileName) { |
| 164 | + if (!path) { |
| 165 | + return; |
| 166 | + } |
| 167 | + const QFileInfo fileInfo(fileName); |
| 168 | + path->absoluteDir = fileInfo.absolutePath(); |
| 169 | + path->relativeDir = {}; |
| 170 | + path->fileName = fileInfo.fileName(); |
| 171 | + |
| 172 | + const auto context = windowHandle ? windowHandle->projectDocumentContext() : nullptr; |
| 173 | + const auto fileLocker = context ? context->fileLocker() : nullptr; |
| 174 | + const auto projectPath = fileLocker ? fileLocker->path() : QString(); |
| 175 | + if (!projectPath.isEmpty()) { |
| 176 | + const QDir projectDir(QFileInfo(projectPath).absolutePath()); |
| 177 | + const auto relativePath = projectDir.relativeFilePath(fileInfo.absoluteFilePath()); |
| 178 | + if (relativePath == "." || (!relativePath.startsWith("../") && relativePath != ".." && !QDir::isAbsolutePath(relativePath))) { |
| 179 | + path->relativeDir = projectDir.relativeFilePath(fileInfo.absolutePath()); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | +} |
| 185 | + |
| 186 | +#include "moc_AudioClipImporter.cpp" |
0 commit comments