-
-
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
Open
szeli1
wants to merge
34
commits into
LMMS:master
Choose a base branch
from
szeli1:sample_export
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
adding sample exporter #7627
Changes from 11 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
1be7bb8
LmmsExporterSample_adding_class
szeli1 318d8b4
CMakeLists_adding_new_class_target
szeli1 a9248c4
LmmsExporterSample_fixing_printf
szeli1 50d20a7
SampleClip_implementing_static_exporter
szeli1 3d98c77
SampleClipView_implementing_exporting_context_menu_option
szeli1 0c630d0
LmmsExporterSample_fixing_typo
szeli1 baab754
SamleClipView_fixing_typo
szeli1 77cfaae
LmmsExporterSample_removing_blank_line
szeli1 58f4e15
LmmsExporterSample_removing_LMMS_EXPORT
szeli1 b25142f
LmmsExporterSample_removing_ifdef
szeli1 61e7ca9
LmmsExporterSample_removing_more_blank_lines
szeli1 2bc670d
LmmsMassExporter_renaming
szeli1 f87af45
LmmsMassExporter_moving_sndfile_code_to_new_class
szeli1 071a002
CMakeLists_updating_class_names
szeli1 0e34ec4
SampleClip_updating_class_names
szeli1 8bf5f0e
SampleClipView_updating_class_names
szeli1 98cd250
LmmsMassExporter_removing_include_QFile
szeli1 02025eb
LmmsMassExporter_implementing_callback
szeli1 adba327
SampleClip_implementing_sample_reloading
szeli1 5c30c30
SampleClipView_updating_names
szeli1 3566660
Use FileDialog::getSaveFileName
michaelgregorius 7c317b4
LmmsMassExporter_moving_flac_exporter
szeli1 e19bb60
FlacExporter_moving_to_new_file
szeli1 1ba312e
LmmsMassExporter_updating_description
szeli1 2021092
Merge remote-tracking branch 'origin/sample_export' into sample_export
szeli1 0452b7f
CMakeLists_adding_FlacExporter_cmake_target
szeli1 5228076
LmmsMassExporter_removing_blank_line
szeli1 5fa60b3
Save file with user selected name
michaelgregorius 5a0eeb7
LmmsMassExporter_renaming_to_ThreadedExportManager
szeli1 96f9888
CMakeLists_renaming_class
szeli1 65ec0af
SampleClipView_removing_LmmsMassExporter_include
szeli1 9fec78c
SampleClip_renaming_class
szeli1 bc59f5f
FlacExporter_adding_sync_function_before_destructing
szeli1 5437d00
SampleClip_removing_export_callback
szeli1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
.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.
I would agree to Michael, but I don't understand (yet?) why you couldn't just useEdit: I see, becauseAudioFileFlac
directly.AudioFileFlac
usesAudioEngine
, though that reference is barely used - it looks like the FLAC class only ever uses it for to get the FPP (and only the base classes take pointers toAudioEngine
which are not used for FLAC though). Is this the only reason not to useAudioFileFlac
?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.
Yes. It was decided to leave the audio engine code alone, #5990 removed audio engine from
AudioFileFlac
, but it was closed because this is a better solution.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.
I guess you mean #7452 ? I think it's an interesting way to do this. Any reason why you think this is better?