Skip to content

Commit

Permalink
added state flags to Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkorsukov committed Feb 21, 2025
1 parent 40d8cee commit 2d3e740
Show file tree
Hide file tree
Showing 26 changed files with 145 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/appshell/view/preferences/generalpreferencesmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ void GeneralPreferencesModel::checkUpdateForCurrentLanguage()

m_languageUpdateProgress = languagesService()->update(languageCode);

m_languageUpdateProgress.progressChanged.onReceive(this, [this](int64_t current, int64_t total, const std::string& status) {
m_languageUpdateProgress.progressed().onReceive(this, [this](int64_t current, int64_t total, const std::string& status) {
emit receivingUpdateForCurrentLanguage(current, total, QString::fromStdString(status));
});

m_languageUpdateProgress.finished.onReceive(this, [this, languageCode](const ProgressResult& res) {
m_languageUpdateProgress.finished().onReceive(this, [this, languageCode](const ProgressResult& res) {
if (res.ret.code() == static_cast<int>(Err::AlreadyUpToDate)) {
QString msg = muse::qtrc("appshell/preferences", "Your version of %1 is up to date.")
.arg(languagesService()->language(languageCode).name);
Expand Down
2 changes: 1 addition & 1 deletion src/converter/api/converterapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool ConverterApi::batch(const QString& outDir, const QString& job, const QStrin
}

muse::ProgressPtr progress = std::make_shared<muse::Progress>();
progress->progressChanged.onReceive(this, [progressCallback](int64_t current, int64_t total, const std::string& title) {
progress->progressed().onReceive(this, [progressCallback](int64_t current, int64_t total, const std::string& title) {
QCoreApplication::processEvents();
if (progressCallback.isCallable()) {
QJSValueList args;
Expand Down
8 changes: 4 additions & 4 deletions src/converter/internal/convertercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ Ret ConverterController::batchConvert(const muse::io::path_t& batchJobFile, cons
TRACEFUNC;

if (progress) {
progress->started.notify();
progress->start();
}

RetVal<BatchJob> batchJob = parseBatchJob(batchJobFile);
if (!batchJob.ret) {
LOGE() << "failed parse batch job file, err: " << batchJob.ret.toString();
if (progress) {
progress->finished.send(ProgressResult(batchJob.ret));
progress->finish(ProgressResult(batchJob.ret));
}
return batchJob.ret;
}
Expand All @@ -70,7 +70,7 @@ Ret ConverterController::batchConvert(const muse::io::path_t& batchJobFile, cons
for (const Job& job : batchJob.val) {
if (progress) {
++current;
progress->progressChanged.send(current, total, job.in.toStdString());
progress->progress(current, total, job.in.toStdString());
}

Ret ret = fileConvert(job.in, job.out, stylePath, forceMode, soundProfile, extensionUri, job.transposeOptions);
Expand All @@ -88,7 +88,7 @@ Ret ConverterController::batchConvert(const muse::io::path_t& batchJobFile, cons
}

if (progress) {
progress->finished.send(ProgressResult(ret));
progress->finish(ProgressResult(ret));
}

return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/framework/audio/internal/encoders/flacencoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bool FlacEncoder::init(const io::path_t& path, const SoundTrackFormat& format, c
m_format = format;

m_flac = new FlacHandler([this](int64_t current, int64_t total){
m_progress.progressChanged.send(current, total, "");
m_progress.progress(current, total, "");
});

if (!m_flac->set_verify(true)
Expand Down
6 changes: 3 additions & 3 deletions src/framework/audio/internal/encoders/mp3encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ size_t Mp3Encoder::requiredOutputBufferSize(samples_t totalSamplesNumber) const

size_t Mp3Encoder::encode(samples_t samplesPerChannel, const float* input)
{
m_progress.progressChanged.send(0, 100, "");
m_progress.progress(0, 100, "");

int encodedBytes = lame_encode_buffer_interleaved_ieee_float(m_handler->flags, input, samplesPerChannel,
m_outputBuffer.data(),
static_cast<int>(m_outputBuffer.size()));

m_progress.progressChanged.send(50, 100, "");
m_progress.progress(50, 100, "");
size_t result = std::fwrite(m_outputBuffer.data(), sizeof(unsigned char), encodedBytes, m_fileStream);
m_progress.progressChanged.send(100, 100, "");
m_progress.progress(100, 100, "");

return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/framework/audio/internal/encoders/oggencoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ using namespace muse::audio::encode;

size_t OggEncoder::encode(samples_t samplesPerChannel, const float* input)
{
m_progress.progressChanged.send(0, 100, "");
m_progress.progress(0, 100, "");
int code = ope_encoder_write_float(m_opusEncoder, input, samplesPerChannel + m_format.sampleRate * 2);
m_progress.progressChanged.send(100, 100, "");
m_progress.progress(100, 100, "");

return code == OPE_OK ? samplesPerChannel : 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/framework/audio/internal/encoders/wavencoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ size_t WavEncoder::encode(samples_t samplesPerChannel, const float* input)
}

if (progressValues.contains(sampleIdx)) {
m_progress.progressChanged.send(sampleIdx, total, "");
m_progress.progress(sampleIdx, total, "");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/framework/audio/internal/soundtracks/soundtrackwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ SoundTrackWriter::SoundTrackWriter(const io::path_t& destination, const SoundTra
}

m_encoderPtr->init(destination, format, totalSamplesNumber);
m_encoderPtr->progress().progressChanged.onReceive(this, [this](int64_t current, int64_t total, std::string) {
m_encoderPtr->progress().progressed().onReceive(this, [this](int64_t current, int64_t total, std::string) {
sendStepProgress(ENCODE_STEP, current, total);
});
}
Expand Down Expand Up @@ -179,5 +179,5 @@ void SoundTrackWriter::sendStepProgress(int step, int64_t current, int64_t total
int stepRange = step == PREPARE_STEP ? 80 : 20;
int stepProgressStart = step == PREPARE_STEP ? 0 : 80;
int stepCurrentProgress = stepProgressStart + ((current * 100 / total) * stepRange) / 100;
m_progress.progressChanged.send(stepCurrentProgress, 100, "");
m_progress.progress(stepCurrentProgress, 100, "");
}
4 changes: 2 additions & 2 deletions src/framework/audio/internal/worker/audiooutputhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ Promise<bool> AudioOutputHandler::saveSoundTrack(const TrackSequenceId sequenceI
m_saveSoundTracksWritersMap[sequenceId] = writer;

Progress progress = saveSoundTrackProgress(sequenceId);
writer->progress().progressChanged.onReceive(this, [&progress](int64_t current, int64_t total, std::string title) {
progress.progressChanged.send(current, total, title);
writer->progress().progressed().onReceive(this, [&progress](int64_t current, int64_t total, std::string title) {
progress.progress(current, total, title);
});

Ret ret = writer->write();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void RegisterAudioPluginsScenario::init()
{
TRACEFUNC;

m_progress.cancelRequested.onNotify(this, [this]() {
m_progress.canceled().onNotify(this, [this]() {
m_aborted = true;
});

Expand Down Expand Up @@ -83,7 +83,7 @@ void RegisterAudioPluginsScenario::processPluginsRegistration(const io::paths_t&
}

m_aborted = false;
m_progress.started.notify();
m_progress.start();

std::string appPath = globalConfiguration()->appBinPath().toStdString();
int64_t pluginCount = static_cast<int64_t>(pluginPaths.size());
Expand All @@ -96,7 +96,7 @@ void RegisterAudioPluginsScenario::processPluginsRegistration(const io::paths_t&
const io::path_t& pluginPath = pluginPaths[i];
std::string pluginPathStr = pluginPath.toStdString();

m_progress.progressChanged.send(i, pluginCount, io::filename(pluginPath).toStdString());
m_progress.progress(i, pluginCount, io::filename(pluginPath).toStdString());
qApp->processEvents();

LOGD() << "--register-audio-plugin " << pluginPathStr;
Expand All @@ -110,7 +110,7 @@ void RegisterAudioPluginsScenario::processPluginsRegistration(const io::paths_t&
}
}

m_progress.finished.send(muse::make_ok());
m_progress.finish(muse::make_ok());
}

Ret RegisterAudioPluginsScenario::registerPlugin(const io::path_t& pluginPath)
Expand Down
8 changes: 4 additions & 4 deletions src/framework/cloud/audiocom/audiocomservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ ProgressPtr AudioComService::uploadAudio(QIODevice& audioData, const QString& au
ProgressPtr progress = std::make_shared<Progress>();

INetworkManagerPtr manager = networkManagerCreator()->makeNetworkManager();
manager->progress().progressChanged.onReceive(this, [progress](int64_t current, int64_t total, const std::string& message) {
progress->progressChanged.send(current, total, message);
manager->progress().progressed().onReceive(this, [progress](int64_t current, int64_t total, const std::string& message) {
progress->progress(current, total, message);
});

auto createAudioCallback = [this, manager, &audioData, title, audioFormat, existingUrl, visibility, replaceExisting]() {
Expand All @@ -219,7 +219,7 @@ ProgressPtr AudioComService::uploadAudio(QIODevice& audioData, const QString& au
};

async::Async::call(this, [this, progress, createAudioCallback, uploadCallback]() {
progress->started.notify();
progress->start();

ProgressResult result;

Expand All @@ -241,7 +241,7 @@ ProgressPtr AudioComService::uploadAudio(QIODevice& audioData, const QString& au
m_currentUploadingAudioId.clear();
m_currentUploadingAudioInfo = {};

progress->finished.send(result);
progress->finish(result);
});

return progress;
Expand Down
24 changes: 12 additions & 12 deletions src/framework/cloud/musescorecom/musescorecomservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,19 @@ ProgressPtr MuseScoreComService::downloadScore(int scoreId, QIODevice& scoreData
ProgressPtr progress = std::make_shared<Progress>();

INetworkManagerPtr manager = networkManagerCreator()->makeNetworkManager();
manager->progress().progressChanged.onReceive(this, [progress](int64_t current, int64_t total, const std::string& message) {
progress->progressChanged.send(current, total, message);
manager->progress().progressed().onReceive(this, [progress](int64_t current, int64_t total, const std::string& message) {
progress->progress(current, total, message);
});

async::Async::call(this, [this, manager, scoreId, &scoreData, hash, secret, progress]() {
progress->started.notify();
progress->start();

ProgressResult result;
result.ret = executeRequest([this, manager, scoreId, &scoreData, hash, secret]() {
return doDownloadScore(manager, scoreId, scoreData, hash, secret);
});

progress->finished.send(result);
progress->finish(result);
});

return progress;
Expand Down Expand Up @@ -363,8 +363,8 @@ ProgressPtr MuseScoreComService::uploadScore(QIODevice& scoreData, const QString
ProgressPtr progress = std::make_shared<Progress>();

INetworkManagerPtr manager = networkManagerCreator()->makeNetworkManager();
manager->progress().progressChanged.onReceive(this, [progress](int64_t current, int64_t total, const std::string& message) {
progress->progressChanged.send(current, total, message);
manager->progress().progressed().onReceive(this, [progress](int64_t current, int64_t total, const std::string& message) {
progress->progress(current, total, message);
});

std::shared_ptr<ValMap> scoreUrlMap = std::make_shared<ValMap>();
Expand All @@ -377,13 +377,13 @@ ProgressPtr MuseScoreComService::uploadScore(QIODevice& scoreData, const QString
};

async::Async::call(this, [this, progress, uploadCallback, scoreUrlMap]() {
progress->started.notify();
progress->start();

ProgressResult result;
result.ret = executeRequest(uploadCallback);
result.val = Val(*scoreUrlMap);

progress->finished.send(result);
progress->finish(result);
});

return progress;
Expand All @@ -394,18 +394,18 @@ ProgressPtr MuseScoreComService::uploadAudio(QIODevice& audioData, const QString
ProgressPtr progress = std::make_shared<Progress>();

INetworkManagerPtr manager = networkManagerCreator()->makeNetworkManager();
manager->progress().progressChanged.onReceive(this, [progress](int64_t current, int64_t total, const std::string& message) {
progress->progressChanged.send(current, total, message);
manager->progress().progressed().onReceive(this, [progress](int64_t current, int64_t total, const std::string& message) {
progress->progress(current, total, message);
});

auto uploadCallback = [this, manager, &audioData, audioFormat, sourceUrl]() {
return doUploadAudio(manager, audioData, audioFormat, sourceUrl);
};

async::Async::call(this, [this, progress, uploadCallback]() {
progress->started.notify();
progress->start();
Ret ret = executeRequest(uploadCallback);
progress->finished.send(ret);
progress->finish(ret);
});

return progress;
Expand Down
51 changes: 42 additions & 9 deletions src/framework/global/progress.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,54 @@
namespace muse {
using ProgressResult = RetVal<Val>;

struct Progress
class Progress
{
// Communication from the creator of the progress to the clients
async::Notification started;
async::Channel<int64_t /*current*/, int64_t /*total*/, std::string /*title*/> progressChanged;
async::Channel<ProgressResult> finished;
public:

// Communication from the clients to the creator of the progress that it should cancel its work
async::Notification cancelRequested;
// start
void start()
{
m_isStarted = true;
m_started.notify();
}

async::Notification& started() { return m_started; }
bool isStarted() const { return m_isStarted; }

// progress
void progress(int64_t current, int64_t total, const std::string& msg) { m_progressed.send(current, total, msg); }
async::Channel<int64_t /*current*/, int64_t /*total*/, std::string /*title*/>& progressed()
{
return m_progressed;
}

// finish or cancel
void finish(const ProgressResult& res)
{
m_isStarted = false;
m_finished.send(res);
}

async::Channel<ProgressResult>& finished() { return m_finished; }

void cancel()
{
cancelRequested.notify();
finished.send(make_ret(Ret::Code::Cancel));
m_isCanceled = true;
m_canceled.notify();
finish(make_ret(Ret::Code::Cancel));
}

async::Notification& canceled() { return m_canceled; }
bool isCanceled() const { return m_isCanceled; }

private:
async::Notification m_started;
async::Channel<ProgressResult> m_finished;
async::Notification m_canceled;
bool m_isStarted = false;
bool m_isCanceled = false;

async::Channel<int64_t /*current*/, int64_t /*total*/, std::string /*title*/> m_progressed;
};

using ProgressPtr = std::shared_ptr<Progress>;
Expand Down
Loading

0 comments on commit 2d3e740

Please sign in to comment.