Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding sample exporter #7627

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1be7bb8
LmmsExporterSample_adding_class
szeli1 Dec 18, 2024
318d8b4
CMakeLists_adding_new_class_target
szeli1 Dec 19, 2024
a9248c4
LmmsExporterSample_fixing_printf
szeli1 Dec 19, 2024
50d20a7
SampleClip_implementing_static_exporter
szeli1 Dec 19, 2024
3d98c77
SampleClipView_implementing_exporting_context_menu_option
szeli1 Dec 19, 2024
0c630d0
LmmsExporterSample_fixing_typo
szeli1 Dec 19, 2024
baab754
SamleClipView_fixing_typo
szeli1 Dec 19, 2024
77cfaae
LmmsExporterSample_removing_blank_line
szeli1 Dec 19, 2024
58f4e15
LmmsExporterSample_removing_LMMS_EXPORT
szeli1 Dec 19, 2024
b25142f
LmmsExporterSample_removing_ifdef
szeli1 Dec 19, 2024
61e7ca9
LmmsExporterSample_removing_more_blank_lines
szeli1 Dec 19, 2024
2bc670d
LmmsMassExporter_renaming
szeli1 Jan 4, 2025
f87af45
LmmsMassExporter_moving_sndfile_code_to_new_class
szeli1 Jan 4, 2025
071a002
CMakeLists_updating_class_names
szeli1 Jan 4, 2025
0e34ec4
SampleClip_updating_class_names
szeli1 Jan 4, 2025
8bf5f0e
SampleClipView_updating_class_names
szeli1 Jan 4, 2025
98cd250
LmmsMassExporter_removing_include_QFile
szeli1 Jan 4, 2025
02025eb
LmmsMassExporter_implementing_callback
szeli1 Jan 4, 2025
adba327
SampleClip_implementing_sample_reloading
szeli1 Jan 4, 2025
5c30c30
SampleClipView_updating_names
szeli1 Jan 4, 2025
3566660
Use FileDialog::getSaveFileName
michaelgregorius Jan 4, 2025
7c317b4
LmmsMassExporter_moving_flac_exporter
szeli1 Jan 5, 2025
e19bb60
FlacExporter_moving_to_new_file
szeli1 Jan 5, 2025
1ba312e
LmmsMassExporter_updating_description
szeli1 Jan 5, 2025
2021092
Merge remote-tracking branch 'origin/sample_export' into sample_export
szeli1 Jan 5, 2025
0452b7f
CMakeLists_adding_FlacExporter_cmake_target
szeli1 Jan 5, 2025
5228076
LmmsMassExporter_removing_blank_line
szeli1 Jan 5, 2025
5fa60b3
Save file with user selected name
michaelgregorius Jan 6, 2025
5a0eeb7
LmmsMassExporter_renaming_to_ThreadedExportManager
szeli1 Jan 8, 2025
96f9888
CMakeLists_renaming_class
szeli1 Jan 8, 2025
65ec0af
SampleClipView_removing_LmmsMassExporter_include
szeli1 Jan 8, 2025
9fec78c
SampleClip_renaming_class
szeli1 Jan 8, 2025
bc59f5f
FlacExporter_adding_sync_function_before_destructing
szeli1 Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions include/LmmsExporterSample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* LmmsExporterSample.h - exports .flac files outside of AudioEngine
*
* Copyright (c) 2024 - 2025 szeli1 <TODO/at/gmail/dot.com>
*
* This file is part of LMMS - https://lmms.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/

#ifndef LMMS_LMMS_EXPORTER_SAMPLE_H
#define LMMS_LMMS_EXPORTER_SAMPLE_H

#include <memory>
#include <sndfile.h>
#include <thread>
#include <vector>
#include <mutex>

#include <QString>

namespace lmms
{

class SampleBuffer;

class LmmsExporterSample
{
public:
LmmsExporterSample();
~LmmsExporterSample();

//! outputLocationAndName: should include path and name, could include ".flac"
void startExporting(const QString& outputLocationAndName, std::shared_ptr<const SampleBuffer> buffer);
void stopExporting();

//void writeBuffer(const SampleFrame* _ab, fpp_t const frames);

private:
static void threadedExportFunction(LmmsExporterSample* thisExporter, volatile bool* abortExport);

void stopThread();
bool openFile(const QString& outputLocationAndName, std::shared_ptr<const SampleBuffer> buffer);
void exportBuffer(std::shared_ptr<const SampleBuffer> buffer);
void closeFile();

std::vector<std::pair<QString, std::shared_ptr<const SampleBuffer>>> m_buffers;

volatile bool m_abortExport;
bool m_isThreadRunning;
std::mutex m_readMutex;
std::thread* m_thread;

SNDFILE* m_fileDescriptor;
};

} // namespace lmms

#endif // LMMS_LMMS_EXPORTER_SAMPLE_H
3 changes: 3 additions & 0 deletions include/SampleClip.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace lmms
{

class LmmsExporterSample;
class SampleBuffer;

namespace gui
Expand Down Expand Up @@ -93,6 +94,8 @@ public slots:
Sample m_sample;
BoolModel m_recordModel;
bool m_isPlaying;

static std::unique_ptr<LmmsExporterSample> s_sampleExporter;

friend class gui::SampleClipView;

Expand Down
1 change: 1 addition & 0 deletions include/SampleClipView.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public slots:
void updateSample();
void reverseSample();
void setAutomationGhost();
void exportSampleBuffer();



Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ set(LMMS_SRCS
core/LadspaManager.cpp
core/LfoController.cpp
core/LinkedModelGroups.cpp
core/LmmsExporterSample.cpp
core/LocklessAllocator.cpp
core/MeterModel.cpp
core/Metronome.cpp
Expand Down
179 changes: 179 additions & 0 deletions src/core/LmmsExporterSample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* LmmsExporterSample.cpp - exports .flac files outside of AudioEngine
*
* Copyright (c) 2024 szeli1 <TODO/at/gmail/dot.com>
*
* This file is part of LMMS - https://lmms.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/

#include <sndfile.h>

#include <QFile>
#include <QFileInfo>

#include "LmmsExporterSample.h"

#include "SampleBuffer.h"

namespace lmms
{

LmmsExporterSample::LmmsExporterSample() :
m_abortExport(false),
m_isThreadRunning(false),
m_readMutex(),
m_thread(nullptr),
m_fileDescriptor(NULL)
{}

LmmsExporterSample::~LmmsExporterSample()
{
stopExporting();
}


void LmmsExporterSample::startExporting(const QString& outputLocationAndName, std::shared_ptr<const SampleBuffer> buffer)
{
QString filteredName(QFileInfo(outputLocationAndName).absolutePath() + "/" + QFileInfo(outputLocationAndName).baseName()+ ".flac");
m_readMutex.lock();
m_buffers.push_back(std::make_pair(filteredName, buffer));
m_readMutex.unlock();

if (m_isThreadRunning == false)
{
stopExporting();
m_isThreadRunning = true;
m_thread = new std::thread(&LmmsExporterSample::threadedExportFunction, this, &m_abortExport);
}
}

void LmmsExporterSample::stopExporting()
{
if (m_thread != nullptr)
{
if (m_isThreadRunning == true)
{
m_abortExport = true;
}
m_thread->join();
delete m_thread;
m_thread = nullptr;
m_isThreadRunning = false;
m_abortExport = false;
}
}


void LmmsExporterSample::threadedExportFunction(LmmsExporterSample* thisExporter, volatile bool* abortExport)
{
thisExporter->m_isThreadRunning = true;

while (*abortExport == false)
{
std::pair<QString, std::shared_ptr<const SampleBuffer>> curBuffer = std::make_pair(QString(""), nullptr);
thisExporter->m_readMutex.lock();
bool shouldExit = thisExporter->m_buffers.size() <= 0;
if (shouldExit == false)
{
curBuffer = thisExporter->m_buffers[thisExporter->m_buffers.size() - 1];
}
thisExporter->m_readMutex.unlock();
if (shouldExit) { break; }

bool success = thisExporter->openFile(curBuffer.first, curBuffer.second);
if (success)
{
thisExporter->exportBuffer(curBuffer.second);
thisExporter->closeFile();
}

thisExporter->m_buffers.pop_back();
}

thisExporter->m_isThreadRunning = false;
}

bool LmmsExporterSample::openFile(const QString& outputLocationAndName, std::shared_ptr<const SampleBuffer> buffer)
{
bool success = true;
QFile targetFile(outputLocationAndName);
if (targetFile.exists() == false)
{
// creating new file
success = targetFile.open(QIODevice::WriteOnly);
if (success == false) { return false; }
targetFile.close();
}

constexpr int channelCount = 2;
SF_INFO exportInfo;

memset(&exportInfo, 0, sizeof(exportInfo));
exportInfo.frames = static_cast<sf_count_t>(buffer->size() * channelCount);
exportInfo.samplerate = buffer->sampleRate();
exportInfo.channels = channelCount;
exportInfo.format = (SF_FORMAT_FLAC | SF_FORMAT_PCM_24);

QByteArray characters = outputLocationAndName.toUtf8();
m_fileDescriptor = sf_open(characters.data(), SFM_WRITE, &exportInfo);

success = m_fileDescriptor != NULL;

if (success == false)
{
printf("LmmsExporterSample sf_open error\n");
}

return success;
}

void LmmsExporterSample::exportBuffer(std::shared_ptr<const SampleBuffer> buffer)
{
if (m_fileDescriptor == NULL) { return; }
constexpr size_t channelCount = 2;
// multiply by 2 since there is 2 channels
std::vector<float> outputBuffer(buffer->size() * channelCount);
size_t i = 0;
for (auto it = buffer->begin(); it != buffer->end(); ++it)
{
outputBuffer[i] = static_cast<float>(it->left());
outputBuffer[i + 1] = static_cast<float>(it->right());
outputBuffer[i] = outputBuffer[i] > 1.0f ? 1.0f : outputBuffer[i] < -1.0f ? -1.0f : outputBuffer[i];
outputBuffer[i + 1] = outputBuffer[i + 1] > 1.0f ? 1.0f : outputBuffer[i + 1] < -1.0f ? -1.0f : outputBuffer[i + 1];
i = i + channelCount;
}
size_t count = sf_writef_float(m_fileDescriptor, outputBuffer.data(), static_cast<sf_count_t>(buffer->size()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO all the sndfile related code should be moved into a helper class that is concerned with writing LMMS buffers into files in various formats using sndfile. It could be an RAII class which

  • takes a filename and the export format in its constructor and opens the file using sndfile
  • provides a method to push LMMS buffers which are then written into the file
  • in the destructor closes the file.

Doing so would likely also remove duplicated code in AudioFileFlac and AudioFileWave.

if (count != buffer->size())
{
printf("LmmsExporterSample sf_writef_float error\n");
}
}

void LmmsExporterSample::closeFile()
{
if (m_fileDescriptor == NULL) { return; }
int success = sf_close(m_fileDescriptor);
if (success != 0)
{
printf("LmmsExporterSample sf_close error\n");
}
m_fileDescriptor = NULL;
}

} // namespace lmms
5 changes: 5 additions & 0 deletions src/core/SampleClip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <QDomElement>
#include <QFileInfo>

#include "LmmsExporterSample.h"
#include "PathUtil.h"
#include "SampleBuffer.h"
#include "SampleClipView.h"
Expand All @@ -37,6 +38,9 @@
namespace lmms
{

std::unique_ptr<LmmsExporterSample> SampleClip::s_sampleExporter = std::make_unique<LmmsExporterSample>();


SampleClip::SampleClip(Track* _track, Sample sample, bool isPlaying)
: Clip(_track)
, m_sample(std::move(sample))
Expand Down Expand Up @@ -79,6 +83,7 @@ SampleClip::SampleClip(Track* _track, Sample sample, bool isPlaying)
setAutoResize( false );
break;
}

updateTrackClips();
}

Expand Down
31 changes: 31 additions & 0 deletions src/gui/clips/SampleClipView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "GuiApplication.h"
#include "AutomationEditor.h"
#include "embed.h"
#include "FileDialog.h"
#include "LmmsExporterSample.h"
#include "PathUtil.h"
#include "SampleClip.h"
#include "SampleLoader.h"
Expand Down Expand Up @@ -81,6 +83,13 @@ void SampleClipView::constructContextMenu(QMenu* cm)
tr( "Set/clear record" ),
m_clip, SLOT(toggleRecord()));*/

cm->addAction(
embed::getIconPixmap("flip_x"),
tr("Export sample buffer"),
this,
SLOT(exportSampleBuffer())
);

cm->addAction(
embed::getIconPixmap("flip_x"),
tr("Reverse sample"),
Expand Down Expand Up @@ -376,5 +385,27 @@ bool SampleClipView::splitClip( const TimePos pos )
else { return false; }
}

void SampleClipView::exportSampleBuffer()
{
auto openFileDialog = FileDialog(nullptr, tr("Export audio file"), QString(), tr("Audio files (*.wav *.flac *.ogg *.mp3);;WAV (*.wav);;FLAC (*.flac);;OGG (*.ogg);;MP3 (*.mp3)"));


if (openFileDialog.exec() == QDialog::Accepted)
{
QStringList curSelectedFiles(openFileDialog.selectedFiles());
if (curSelectedFiles.isEmpty() == false)
szeli1 marked this conversation as resolved.
Show resolved Hide resolved
{
QString outputFileName = curSelectedFiles.first();
michaelgregorius marked this conversation as resolved.
Show resolved Hide resolved
if (outputFileName.isEmpty()) { return; }
if (outputFileName.endsWith(".flac") == false)
michaelgregorius marked this conversation as resolved.
Show resolved Hide resolved
{
outputFileName = PathUtil::stripPrefix(outputFileName) + ".flac";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly you let the users choose different file types but then save it as a Flac anyway. This is very confusing in my opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the exporter class only supports .flac and this was agreed to in discord. I can still change that, but it will add unreasonable complexity in my view.

}

m_clip->s_sampleExporter->startExporting(outputFileName, m_clip->m_sample.buffer());
michaelgregorius marked this conversation as resolved.
Show resolved Hide resolved
}
}
}


} // namespace lmms::gui
Loading