-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
base: master
Are you sure you want to change the base?
adding sample exporter #7627
Changes from 11 commits
1be7bb8
318d8b4
a9248c4
50d20a7
3d98c77
0c630d0
baab754
77cfaae
58f4e15
b25142f
61e7ca9
2bc670d
f87af45
071a002
0e34ec4
8bf5f0e
98cd250
02025eb
adba327
5c30c30
3566660
7c317b4
e19bb60
1ba312e
2021092
0452b7f
5228076
5fa60b3
5a0eeb7
96f9888
65ec0af
9fec78c
bc59f5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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())); | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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"), | ||
|
@@ -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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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
Doing so would likely also remove duplicated code in
AudioFileFlac
andAudioFileWave
.