-
-
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 28 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,53 @@ | ||
/* | ||
* FlacExporter.h - exports .flac files outside of AudioEngine | ||
* | ||
* Copyright (c) 2024 - 2025 szeli1 | ||
* | ||
* 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_FLAC_EXPORTER_H | ||
#define LMMS_FLAC_EXPORTER_H | ||
|
||
#include <sndfile.h> | ||
|
||
#include <QString> | ||
|
||
namespace lmms | ||
{ | ||
|
||
class SampleFrame; | ||
|
||
class FlacExporter | ||
{ | ||
public: | ||
FlacExporter(int sampleRate, int bitDepth, const QString& outputLocationAndName); | ||
~FlacExporter(); | ||
|
||
void writeThisBuffer(const SampleFrame* samples, size_t sampleCount); | ||
bool getIsSuccesful() const; | ||
|
||
private: | ||
bool m_isSuccesful; | ||
SNDFILE* m_fileDescriptor; | ||
}; | ||
|
||
} // namespace lmms | ||
|
||
#endif // LMMS_FLAC_EXPORTER_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* LmmsMassExporter.h - exports files in .flac format on an other thread | ||
* | ||
* Copyright (c) 2024 - 2025 szeli1 | ||
* | ||
* 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_MASS_EXPORTER_H | ||
#define LMMS_LMMS_MASS_EXPORTER_H | ||
|
||
#include <atomic> | ||
#include <memory> | ||
#include <thread> | ||
#include <tuple> | ||
#include <vector> | ||
#include <mutex> | ||
|
||
#include <sndfile.h> | ||
|
||
#include <QString> | ||
|
||
namespace lmms | ||
{ | ||
|
||
class SampleBuffer; | ||
|
||
typedef void (*callbackFn)(void*); | ||
|
||
class LmmsMassExporter | ||
{ | ||
public: | ||
LmmsMassExporter(); | ||
~LmmsMassExporter(); | ||
|
||
//! outputLocationAndName: should include path and name, could include ".flac" | ||
void startExporting(const QString& outputLocationAndName, std::shared_ptr<const SampleBuffer> buffer, callbackFn callbackFunction = nullptr, void* callbackObject = nullptr); | ||
void stopExporting(); | ||
|
||
//void writeBuffer(const SampleFrame* _ab, fpp_t const frames); | ||
|
||
private: | ||
static void threadedExportFunction(LmmsMassExporter* thisExporter, volatile std::atomic<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::tuple<QString, std::shared_ptr<const SampleBuffer>, callbackFn, void*>> m_buffers; | ||
|
||
volatile std::atomic<bool> m_abortExport; | ||
bool m_isThreadRunning; | ||
std::mutex m_readMutex; | ||
std::thread* m_thread; | ||
}; | ||
|
||
} // namespace lmms | ||
|
||
#endif // LMMS_LMMS_MASS_EXPORTER_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* LmmsMassExporter.cpp - exports files in .flac format on an other thread | ||
* | ||
* Copyright (c) 2024 - 2025 szeli1 | ||
* | ||
* 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 <QFileInfo> | ||
|
||
#include "LmmsMassExporter.h" | ||
|
||
#include "FlacExporter.h" | ||
#include "SampleBuffer.h" | ||
|
||
namespace lmms | ||
{ | ||
|
||
LmmsMassExporter::LmmsMassExporter() : | ||
m_abortExport(false), | ||
m_isThreadRunning(false), | ||
m_readMutex(), | ||
m_thread(nullptr) | ||
{} | ||
|
||
LmmsMassExporter::~LmmsMassExporter() | ||
{ | ||
stopExporting(); | ||
} | ||
|
||
|
||
void LmmsMassExporter::startExporting(const QString& outputLocationAndName, std::shared_ptr<const SampleBuffer> buffer, callbackFn callbackFunction, void* callbackObject) | ||
{ | ||
m_readMutex.lock(); | ||
m_buffers.push_back(std::make_tuple(outputLocationAndName, buffer, callbackFunction, callbackObject)); | ||
m_readMutex.unlock(); | ||
|
||
if (m_isThreadRunning == false) | ||
{ | ||
stopExporting(); | ||
m_isThreadRunning = true; | ||
m_thread = new std::thread(&LmmsMassExporter::threadedExportFunction, this, &m_abortExport); | ||
} | ||
} | ||
|
||
void LmmsMassExporter::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 LmmsMassExporter::threadedExportFunction(LmmsMassExporter* thisExporter, volatile std::atomic<bool>* abortExport) | ||
{ | ||
thisExporter->m_isThreadRunning = true; | ||
|
||
while (*abortExport == false) | ||
{ | ||
std::tuple<QString, std::shared_ptr<const SampleBuffer>, callbackFn, void*> curBuffer = std::make_tuple(QString(""), nullptr, nullptr, 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_buffers.pop_back(); | ||
} | ||
thisExporter->m_readMutex.unlock(); | ||
if (shouldExit) { break; } | ||
|
||
// important new scope | ||
{ | ||
FlacExporter exporter(std::get<1>(curBuffer)->sampleRate(), 24, std::get<0>(curBuffer)); | ||
if (exporter.getIsSuccesful()) | ||
{ | ||
exporter.writeThisBuffer(std::get<1>(curBuffer)->data(), std::get<1>(curBuffer)->size()); | ||
} | ||
} | ||
|
||
if (std::get<2>(curBuffer)) | ||
{ | ||
// calling callback funcion | ||
std::get<2>(curBuffer)(std::get<3>(curBuffer)); | ||
} | ||
} | ||
|
||
thisExporter->m_isThreadRunning = false; | ||
} | ||
|
||
} // namespace lmms |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
#include <QDomElement> | ||
#include <QFileInfo> | ||
|
||
#include "LmmsMassExporter.h" | ||
#include "PathUtil.h" | ||
#include "SampleBuffer.h" | ||
#include "SampleClipView.h" | ||
|
@@ -37,10 +38,14 @@ | |
namespace lmms | ||
{ | ||
|
||
SampleClip::SampleClip(Track* _track, Sample sample, bool isPlaying) | ||
: Clip(_track) | ||
, m_sample(std::move(sample)) | ||
, m_isPlaying(false) | ||
std::unique_ptr<LmmsMassExporter> SampleClip::s_sampleExporter = std::make_unique<LmmsMassExporter>(); | ||
|
||
|
||
SampleClip::SampleClip(Track* _track, Sample sample, bool isPlaying) : | ||
Clip(_track), | ||
m_sample(std::move(sample)), | ||
m_isPlaying(false), | ||
m_exportedSampleName("") | ||
{ | ||
saveJournallingState( false ); | ||
setSampleFile( "" ); | ||
|
@@ -79,6 +84,7 @@ SampleClip::SampleClip(Track* _track, Sample sample, bool isPlaying) | |
setAutoResize( false ); | ||
break; | ||
} | ||
|
||
updateTrackClips(); | ||
} | ||
|
||
|
@@ -336,5 +342,19 @@ gui::ClipView * SampleClip::createView( gui::TrackView * _tv ) | |
return new gui::SampleClipView( this, _tv ); | ||
} | ||
|
||
void SampleClip::exportSampleBuffer(const QString& fileName) | ||
{ | ||
m_exportedSampleName = fileName; | ||
s_sampleExporter->startExporting(fileName, m_sample.buffer(), &SampleClip::exportSampleBufferCallback, this); | ||
} | ||
|
||
void SampleClip::exportSampleBufferCallback(void* thisObject) | ||
{ | ||
if (thisObject == nullptr) { return; } | ||
SampleClip* castedThis = static_cast<SampleClip*>(thisObject); | ||
castedThis->setSampleFile(castedThis->m_exportedSampleName); | ||
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. Now that I have thought a bit more about it I do not think that replacing the sample with the one we have just exported is a good idea. Let's say you have a project with all the samples contained in the project. You now want to export the sample to a different folder outside of the project just to save it. In this case the project file would now point to a file outside of the project folder which might not be the intended effect. IMO the export feature should simply export the file to wherever the users want. If users want to replace the sample with the exported one it should either be an option in the export dialog (initially disabled) or they should explicitly replace the samples themselves after the export. Doing it as it is currently done will likely lead to "chaos". |
||
castedThis->m_exportedSampleName.clear(); | ||
} | ||
|
||
|
||
} // namespace lmms |
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.
Some remarks about the name of the class:
lmms
namespace so there is not need to prefix it with "Lmms".ThreadedExporter
might be more fitting.Edit: I have just noticed the vector. Perhaps something like
ExportManager
orThreadedExportManager
might be better as a name then.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 like
ThreadedExportManager
, I will rename.