I’m writing this plugin that at some point has recorded loops, stored in AudioSampleBuffers. I have built a save button, and when you click it and select a directory, the plugin will write all buffers with something in them out to .wav files in the selected directory. So far so good.
Now, I want the save button to become an export button that you can simply drag, a la Maschine. I have a feeling I need to build and implement some custom class for this that would inherit from Thread. I am trying with the following:
// FileExportThread.h
class FileExportThread : public Thread
{
public:
void giveAudioSampleBuffer (AudioSampleBuffer* newBufferptr);
void run() override;
class Listener
{
public:
virtual ~Listener() {}
virtual void fileWritten (String fileName) = 0;
};
private:
AudioSampleBuffer buffer;
};
// FileExportThread.cpp
#include "FileExportThread.h"
void FileExportThread::giveAudioSampleBuffer (AudioSampleBuffer *newBufferptr)
{
buffer = AudioSampleBuffer (*newBufferptr);
}
void FileExportThread::run()
{
if (buffer.getNumSamples() == 0)
{
listeners.call (&fileWritten, "");
exit (0);
}
WavAudioFormat waf;
std::unique_ptr<AudioFormatWriter> afw (waf.createWriterFor (new FileOutputStream (*new File (
File::getSpecialLocation (File::tempDirectory).getFullPathName() + "/filename.wav")
), sampleRate, kChannels, 16, StringPairArray(), 0));
afw->writeFromAudioSampleBuffer (buffer,
0,
buffer.getNumSamples());
afw->flush();
listeners.call (&fileWritten, "..filename.wav");
}
However, on both lines with listeners.call, XCode shows 'listeners' is a private member of 'juce::Thread'. Understandable. But I need to know from the main GUI thread of my plugin, when all 8 loops have been successfully written, to then call the DragAndDropContainer::performExternalDragDropOfFiles with them.
Am I better off using the Thread::launch()?
What is the supposed way to communicate from a juce::Thread back to the spawner that the task has been successfully executed (hopefully with result in a String)?
Is there a better, easier, or cleaner way to launch exactly 8 threads to start writing .wav files and get notified when (all) done?
