Krotos Modules 3
Loading...
Searching...
No Matches
SignalSaver.h
Go to the documentation of this file.
1namespace krotos
2{
3 //==============================================================================
7 class SignalSaver : public AsyncUpdater
8 {
9 public:
10 SignalSaver(const String& path, int numChannels, int64 numSamples, double sampleRate)
11 : m_numChannels(numChannels), m_length(numSamples), m_counter(0), m_file(path)
12 {
13 // The file path must end in .wav
14 jassert(path.contains(".wav"));
15
16 WavAudioFormat format;
17 m_writer.reset(format.createWriterFor(new FileOutputStream(m_file), sampleRate,
18 static_cast<unsigned int>(numChannels), 16, {}, 0));
19 }
20
21 void process(AudioBuffer<float> buffer)
22 {
23 jassert(buffer.getNumChannels() >= m_numChannels);
24 process(buffer.getArrayOfReadPointers(), buffer.getNumSamples());
25 }
26
27 void process(const float* const* buffer, int blockSize)
28 {
29 if (m_counter == -1)
30 return;
31
32 int numSamplesToWrite = (m_counter + int64(blockSize) < m_length) ? blockSize : int(m_length - m_counter);
33 m_writer->writeFromFloatArrays(buffer, m_numChannels, numSamplesToWrite);
34 m_counter += numSamplesToWrite;
35
36 if (m_counter >= m_length)
37 {
38 triggerAsyncUpdate();
39 m_counter = -1;
40 }
41 }
42
43 void handleAsyncUpdate() override { m_writer.reset(nullptr); }
44
45 private:
46 const int m_numChannels;
47 const int64 m_length;
48 int64 m_counter;
49
50 const File m_file;
51 std::unique_ptr<AudioFormatWriter> m_writer;
52
53 std::vector<float> m_data;
54
55 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SignalSaver)
56 };
57} // namespace krotos
Add this to an AudioProcessor to save a signal as a .wav for analysis.
Definition SignalSaver.h:8
const int64 m_length
Definition SignalSaver.h:47
std::vector< float > m_data
Definition SignalSaver.h:53
std::unique_ptr< AudioFormatWriter > m_writer
Definition SignalSaver.h:51
void process(AudioBuffer< float > buffer)
Definition SignalSaver.h:21
void handleAsyncUpdate() override
Definition SignalSaver.h:43
const File m_file
Definition SignalSaver.h:50
int64 m_counter
Definition SignalSaver.h:48
SignalSaver(const String &path, int numChannels, int64 numSamples, double sampleRate)
Definition SignalSaver.h:10
const int m_numChannels
Definition SignalSaver.h:46
void process(const float *const *buffer, int blockSize)
Definition SignalSaver.h:27
Definition AirAbsorptionFilter.cpp:2