Skip to content

Commit

Permalink
Merge pull request #24 from agateau/save-improvements
Browse files Browse the repository at this point in the history
Save improvements
  • Loading branch information
agateau authored Jun 19, 2024
2 parents 56e8156 + c66dd3e commit 11f396b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
33 changes: 27 additions & 6 deletions src/core/SoundIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMetaProperty>
#include <QSaveFile>
#include <QUrl>
#include <QtEndian>

Expand Down Expand Up @@ -115,19 +117,38 @@ Result loadSfxr(Sound* sound, QIODevice* device) {

Result save(const Sound* sound, const QUrl& url) {
QString path = url.path();
QFile file(path);
QSaveFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
auto message = QCoreApplication::translate("SoundIO", "Cannot write file.");
return Result::createError(message);
}
QString ext = path.section(".", -1);

QString ext = QFileInfo(path).suffix();
if (ext.isEmpty()) {
auto message = QCoreApplication::translate("SoundIO", "No file format provided.");
return Result::createError(message);
}

Result result;
if (ext == "sfxr") {
return saveSfxr(sound, &file);
result = saveSfxr(sound, &file);
} else if (ext == "sfxj") {
return saveSfxj(sound, &file);
result = saveSfxj(sound, &file);
} else {
auto message =
QCoreApplication::translate("SoundIO", "Cannot save to format \"%1\".").arg(ext);
return Result::createError(message);
}
if (!result.isOk()) {
return result;
}

if (!file.commit()) {
auto message = QCoreApplication::translate("SoundIO", "Failed to save file: %1.")
.arg(file.errorString());
return Result::createError(message);
}
auto message = QCoreApplication::translate("SoundIO", "Cannot save to format \"%1\".").arg(ext);
return Result::createError(message);
return {};
}

Result saveSfxr(const Sound* sound, QIODevice* device) {
Expand Down
21 changes: 8 additions & 13 deletions src/core/WavSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include "Sound.h"
#include "Synthesizer.h"

#include <QFile>
#include <QSaveFile>
#include <QUrl>
#include <QtEndian>

class WavExportStrategy : public Synthesizer::SynthStrategy {
public:
int file_sampleswritten;
int file_sampleswritten = 0;
qreal filesample = 0.0f;
int fileacc = 0;
int wav_bits = 16;
Expand All @@ -18,13 +18,7 @@ class WavExportStrategy : public Synthesizer::SynthStrategy {
~WavExportStrategy() {
}

bool open(const QString& path) {
auto file = std::make_unique<QFile>(path);
if (!file->open(QIODevice::WriteOnly)) {
return false;
}
mDevice = std::move(file);
return true;
explicit WavExportStrategy(QIODevice* device) : mDevice(device) {
}

qint64 fwrite(const void* ptr, size_t size) {
Expand Down Expand Up @@ -58,7 +52,7 @@ class WavExportStrategy : public Synthesizer::SynthStrategy {
void write(qreal sample) override;

private:
std::unique_ptr<QIODevice> mDevice;
QIODevice* mDevice;
};

void WavExportStrategy::write(qreal ssample) {
Expand Down Expand Up @@ -87,10 +81,11 @@ WavSaver::WavSaver(QObject* parent) : BaseWavSaver(parent) {

bool WavSaver::save(Sound* sound, const QUrl& url) {
QString path = url.path();
WavExportStrategy wav;
if (!wav.open(path)) {
QSaveFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
return false;
}
WavExportStrategy wav(&file);
wav.wav_bits = bits();
wav.wav_freq = frequency();

Expand Down Expand Up @@ -132,5 +127,5 @@ bool WavSaver::save(Sound* sound, const QUrl& url) {
quint64 dataChunkSize = wav.file_sampleswritten * wav.wav_bits / 8;
wav.fwriteUInt32(dataChunkSize); // chunk size (data)

return true;
return file.commit();
}
2 changes: 2 additions & 0 deletions src/ui/FileActions.qml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ ColumnLayout {
id: saveFileDialog
title: qsTr("Save sound")
selectExisting: false
defaultSuffix: "sfxj"
nameFilters: [
qsTr("SFXR JSON (*.sfxj)") + " (*.sfxj)",
qsTr("SFXR Binary (from the original SFXR app) (*.sfxr)") + " (*.sfxr)",
Expand Down Expand Up @@ -116,6 +117,7 @@ ColumnLayout {
FileDialog {
id: exportWavFileDialog
selectExisting: false
defaultSuffix: "wav"
title: qsTr("Export as WAV")
nameFilters: [qsTr("Wav files") + " (*.wav)",
qsTr("All files") + " (*)"]
Expand Down

0 comments on commit 11f396b

Please sign in to comment.