Skip to content

Commit

Permalink
[namespace juce] Modernize the library by removing the old JuceHeader…
Browse files Browse the repository at this point in the history
….h, and switching to the new include style. Also, ditch the using namespace juce which leads to a big change but makes refactoring later easier.
  • Loading branch information
christofmuc committed Dec 20, 2022
1 parent 768efa3 commit eface45
Show file tree
Hide file tree
Showing 38 changed files with 309 additions and 345 deletions.
40 changes: 20 additions & 20 deletions AudioRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

const float kSignalThreshold = 0.001f; // Signals below this value are considered noise (don't trigger recording)

AudioRecorder::AudioRecorder(File directory, std::string const& baseFileName, RecordingType recordingType) :
AudioRecorder::AudioRecorder(juce::File directory, std::string const& baseFileName, RecordingType recordingType) :
samplesWritten_(0), directory_(directory), baseFileName_(baseFileName), recordingType_(recordingType), writer_(nullptr), automaticRecordFromSilenceToSilence_(false), silenceDuration_(0)
{
thread_ = std::make_unique<TimeSliceThread>("RecorderDiskWriter");
thread_ = std::make_unique<juce::TimeSliceThread>("RecorderDiskWriter");
thread_->startThread();
}

Expand All @@ -47,7 +47,7 @@ AudioRecorder::~AudioRecorder()
void AudioRecorder::startRecording(std::string const& filename, bool fromSilenceToSilence, std::function<void()> onSilence)
{
automaticRecordFromSilenceToSilence_ = fromSilenceToSilence;
activeFile_ = File(filename);
activeFile_ = juce::File(filename);
onSilence_ = onSilence;
if (isRecording()) {
// Stop current recorder first
Expand Down Expand Up @@ -76,9 +76,9 @@ bool AudioRecorder::hasDetectedSignal() const
return isRecording() && hasFoundStart_;
}

RelativeTime AudioRecorder::getElapsedTime() const
juce::RelativeTime AudioRecorder::getElapsedTime() const
{
return RelativeTime(samplesWritten_ / (double) lastSampleRate_);
return juce::RelativeTime(samplesWritten_ / (double) lastSampleRate_);
}

juce::String AudioRecorder::getFilename() const
Expand All @@ -101,19 +101,19 @@ void AudioRecorder::updateChannelInfo(int sampleRate, int numChannels)
writeThread_.reset();

// Create the audio format writer
std::unique_ptr<AudioFormat> audioFormat;
std::unique_ptr<juce::AudioFormat> audioFormat;
std::string fileExtension;
switch (recordingType_) {
case RecordingType::WAV:
audioFormat = std::make_unique<WavAudioFormat>();
audioFormat = std::make_unique<juce::WavAudioFormat>();
fileExtension = ".wav";
break;
case RecordingType::FLAC:
audioFormat = std::make_unique<FlacAudioFormat>();
audioFormat = std::make_unique<juce::FlacAudioFormat>();
fileExtension = ".flac";
break;
case RecordingType::AIFF:
audioFormat = std::make_unique<AiffAudioFormat>();
audioFormat = std::make_unique<juce::AiffAudioFormat>();
fileExtension = ".aiff";
break;
}
Expand All @@ -125,7 +125,7 @@ void AudioRecorder::updateChannelInfo(int sampleRate, int numChannels)
if (allowed == bitDepthRequested) bitsOk = true;
if (!bitsOk) {
jassert(false);
SimpleLogger::instance()->postMessage("Error: trying to create a file with a bit depth that is not supported by the format: " + String(bitDepthRequested));
SimpleLogger::instance()->postMessage("Error: trying to create a file with a bit depth that is not supported by the format: " + juce::String(bitDepthRequested));
return;
}

Expand All @@ -134,7 +134,7 @@ void AudioRecorder::updateChannelInfo(int sampleRate, int numChannels)
if (rate == sampleRate) rateOk = true;
if (!rateOk) {
jassert(false);
SimpleLogger::instance()->postMessage("Error: trying to create a file with a sample rate that is not supported by the format: " + String(sampleRate));
SimpleLogger::instance()->postMessage("Error: trying to create a file with a sample rate that is not supported by the format: " + juce::String(sampleRate));
return;
}

Expand Down Expand Up @@ -169,7 +169,7 @@ void AudioRecorder::updateChannelInfo(int sampleRate, int numChannels)
}*/

// Set up a new audio file to write to
startTime_ = Time::getCurrentTime();
startTime_ = juce::Time::getCurrentTime();
if (activeFile_.getFullPathName().isEmpty()) {
return;
// activeFile_ = directory_.getNonexistentChildFile(String(baseFileName_) + startTime_.formatted("-%Y-%m-%d-%H-%M-%S"), fileExtension, false);
Expand All @@ -178,10 +178,10 @@ void AudioRecorder::updateChannelInfo(int sampleRate, int numChannels)
SimpleLogger::instance()->postMessage("Overwriting file " + activeFile_.getFullPathName());
activeFile_.deleteFile();
}
OutputStream* outStream = new FileOutputStream(activeFile_, 16384);
juce::OutputStream* outStream = new juce::FileOutputStream(activeFile_, 16384);

// Create the writer based on the format and file
StringPairArray metaData;
juce::StringPairArray metaData;
writer_ = audioFormat->createWriterFor(outStream, sampleRate, (unsigned) numChannels, bitDepthRequested, metaData, 1 /* unused by wav */);
if (!writer_) {
jassert(false);
Expand All @@ -191,7 +191,7 @@ void AudioRecorder::updateChannelInfo(int sampleRate, int numChannels)
}

// Finally, create the new writer associating it with the background thread
writeThread_ = std::make_unique<AudioFormatWriter::ThreadedWriter>(writer_, *thread_, 16384);
writeThread_ = std::make_unique<juce::AudioFormatWriter::ThreadedWriter>(writer_, *thread_, 16384);
samplesWritten_ = 0;
}

Expand All @@ -203,7 +203,7 @@ void AudioRecorder::saveBlock(const float* const* data, int numSamples)
// TODO - need a smarter strategy than that
SimpleLogger::instance()->postMessage("Ups, FIFO full and can't write block to disk, lost it!");
}
samplesWritten_ += (uint64) numSamples;
samplesWritten_ += (juce::uint64) numSamples;
}
}

Expand All @@ -212,7 +212,7 @@ juce::File AudioRecorder::getDirectory() const
return directory_;
}

void AudioRecorder::setDirectory(File& directory)
void AudioRecorder::setDirectory(juce::File& directory)
{
// Stop writing if any
writeThread_.reset();
Expand All @@ -228,7 +228,7 @@ void AudioRecorder::audioDeviceIOCallbackWithContext (const float* const* inputC
float* const* outputChannelData,
int numOutputChannels,
int numSamples,
const AudioIODeviceCallbackContext& context)
const juce::AudioIODeviceCallbackContext& context)
{
ignoreUnused(context);
#endif
Expand Down Expand Up @@ -279,7 +279,7 @@ void AudioRecorder::audioDeviceIOCallbackWithContext (const float* const* inputC
if (automaticRecordFromSilenceToSilence_ && silenceDuration_ > 0.01 * lastSampleRate_) {
// End of story
writeThread_.reset();
MessageManager::callAsync([this]() { onSilence_(); });
juce::MessageManager::callAsync([this]() { onSilence_(); });
hasFoundStart_ = false;
}
}
Expand All @@ -289,7 +289,7 @@ void AudioRecorder::audioDeviceIOCallbackWithContext (const float* const* inputC
}
}

void AudioRecorder::audioDeviceAboutToStart(AudioIODevice* device)
void AudioRecorder::audioDeviceAboutToStart(juce::AudioIODevice* device)
{
auto inputChannelMask = device->getActiveInputChannels();
hasFoundStart_ = false;
Expand Down
35 changes: 18 additions & 17 deletions AudioRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

#pragma once

#include "JuceHeader.h"
#include <juce_audio_devices/juce_audio_devices.h>
#include <juce_audio_formats/juce_audio_formats.h>

enum class RecordingType
{
Expand All @@ -34,24 +35,24 @@ enum class RecordingType
};


class AudioRecorder : public AudioIODeviceCallback {
class AudioRecorder : public juce::AudioIODeviceCallback {
public:
AudioRecorder(File directory, std::string const& baseFileName, RecordingType recordingType);
AudioRecorder(juce::File directory, std::string const& baseFileName, RecordingType recordingType);
virtual ~AudioRecorder() override;

void startRecording(std::string const& filename, bool fromSilenceToSilence, std::function<void()> onSilence);
void stopRecording();
bool isRecording() const;
bool hasDetectedSignal() const;

RelativeTime getElapsedTime() const;
String getFilename() const;
File getFile() const;
juce::RelativeTime getElapsedTime() const;
juce::String getFilename() const;
juce::File getFile() const;

void updateChannelInfo(int sampleRate, int numChannels);

File getDirectory() const;
void setDirectory(File& directory);
juce::File getDirectory() const;
void setDirectory(juce::File& directory);

#if JUCE_VERSION < 0x070000
virtual void audioDeviceIOCallback(const float** inputChannelData, int numInputChannels, float** outputChannelData, int numOutputChannels, int numSamples) override;
Expand All @@ -61,23 +62,23 @@ class AudioRecorder : public AudioIODeviceCallback {
float* const* outputChannelData,
int numOutputChannels,
int numSamples,
const AudioIODeviceCallbackContext& context) override;
const juce::AudioIODeviceCallbackContext& context) override;
#endif
virtual void audioDeviceAboutToStart(AudioIODevice* device) override;
virtual void audioDeviceAboutToStart(juce::AudioIODevice* device) override;
virtual void audioDeviceStopped() override;

private:
void saveBlock(const float* const* data, int numSamples);

Time startTime_;
uint64 samplesWritten_;
File activeFile_;
File directory_;
juce::Time startTime_;
juce::uint64 samplesWritten_;
juce::File activeFile_;
juce::File directory_;
std::string baseFileName_;
RecordingType recordingType_;
AudioFormatWriter* writer_;
std::unique_ptr<TimeSliceThread> thread_;
std::unique_ptr<AudioFormatWriter::ThreadedWriter> writeThread_;
juce::AudioFormatWriter* writer_;
std::unique_ptr<juce::TimeSliceThread> thread_;
std::unique_ptr<juce::AudioFormatWriter::ThreadedWriter> writeThread_;

int lastSampleRate_;
int lastNumChannels_;
Expand Down
31 changes: 16 additions & 15 deletions Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,51 +34,51 @@ Data& Data::instance()
return instance_;
}

const juce::var Data::getProperty(const Identifier& name)
const juce::var Data::getProperty(const juce::Identifier& name)
{
return instance().get().getProperty(name);
}

const juce::var Data::getEphemeralProperty(const Identifier& name)
const juce::var Data::getEphemeralProperty(const juce::Identifier& name)
{
return instance().getEphemeral().getProperty(name);
}

const juce::var Data::getRuntimeProperty(const Identifier& name)
const juce::var Data::getRuntimeProperty(const juce::Identifier& name)
{
return instance().getRuntime().getProperty(name);
}

juce::Value Data::getPropertyAsValue(const Identifier& name)
juce::Value Data::getPropertyAsValue(const juce::Identifier& name)
{
return instance().get().getPropertyAsValue(name, nullptr);
}

juce::Value Data::getEphemeralPropertyAsValue(const Identifier& name)
juce::Value Data::getEphemeralPropertyAsValue(const juce::Identifier& name)
{
return instance().getEphemeral().getPropertyAsValue(name, nullptr);
}

juce::Value Data::getRuntimePropertyAsValue(const Identifier& name)
juce::Value Data::getRuntimePropertyAsValue(const juce::Identifier& name)
{
return instance().getRuntime().getPropertyAsValue(name, nullptr);
}

void Data::ensurePropertyExists(const Identifier &name, var defaultValue)
void Data::ensurePropertyExists(const juce::Identifier& name, juce::var defaultValue)
{
if (!instance_.get().hasProperty(name)) {
instance_.get().setProperty(name, defaultValue, nullptr);
}
}

void Data::ensureEphemeralPropertyExists(const Identifier &name, var defaultValue)
void Data::ensureEphemeralPropertyExists(const juce::Identifier& name, juce::var defaultValue)
{
if (!instance_.getEphemeral().hasProperty(name)) {
instance_.getEphemeral().setProperty(name, defaultValue, nullptr);
}
}

void Data::ensureRuntimePropertyExists(const Identifier &name, var defaultValue)
void Data::ensureRuntimePropertyExists(const juce::Identifier& name, juce::var defaultValue)
{
if (!instance_.getRuntime().hasProperty(name)) {
instance_.getRuntime().setProperty(name, defaultValue, nullptr);
Expand All @@ -87,14 +87,15 @@ void Data::ensureRuntimePropertyExists(const Identifier &name, var defaultValue)

void Data::reset()
{
instance_.tree_ = std::make_unique<ValueTree>(Identifier("Setup"));
instance_.ephemeralTree_ = std::make_unique<ValueTree>(Identifier("Ephemeral"));
instance_.tree_ = std::make_unique<juce::ValueTree>(juce::Identifier("Setup"));
instance_.ephemeralTree_ = std::make_unique<juce::ValueTree>(juce::Identifier("Ephemeral"));
}

Data::Data()
: tree_{std::make_unique<ValueTree>(Identifier("Setup"))}
, ephemeralTree_{std::make_unique<ValueTree>(Identifier("Ephemeral"))}
, runtimeTree_{std::make_unique<ValueTree>(Identifier("EphemeralSurvivingReset"))}
:
tree_ { std::make_unique<juce::ValueTree>(juce::Identifier("Setup")) }, ephemeralTree_ { std::make_unique<juce::ValueTree>(juce::Identifier("Ephemeral")) }, runtimeTree_ {
std::make_unique<juce::ValueTree>(juce::Identifier("EphemeralSurvivingReset"))
}
{}

juce::ValueTree& Data::get()
Expand All @@ -117,7 +118,7 @@ void Data::initializeFromSettings()
auto xmlDoc = Settings::instance().get("ClientSettings", "");
auto topLevel = juce::parseXML(xmlDoc);
if (topLevel) {
*tree_ = ValueTree::fromXml(*topLevel);
*tree_ = juce::ValueTree::fromXml(*topLevel);
}
}

Expand Down
33 changes: 17 additions & 16 deletions Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,27 @@

#pragma once

#include "JuceHeader.h"
#include <juce_core/juce_core.h>
#include <juce_data_structures/juce_data_structures.h>

class Data {
public:
static Data& instance();

static const var getProperty(const Identifier& name); // Convenience function for instance().get().getProperty()
static const var getEphemeralProperty(const Identifier& name);
static const var getRuntimeProperty(const Identifier& name);
static Value getPropertyAsValue(const Identifier& name); // Convenience function for instance().get().getPropertyAsValue()
static Value getEphemeralPropertyAsValue(const Identifier& name);
static Value getRuntimePropertyAsValue(const Identifier& name);
static void ensurePropertyExists(const Identifier &name, var defaultValue);
static void ensureEphemeralPropertyExists(const Identifier &name, var defaultValue);
static void ensureRuntimePropertyExists(const Identifier &name, var defaultValue);
static const juce::var getProperty(const juce::Identifier& name); // Convenience function for instance().get().getProperty()
static const juce::var getEphemeralProperty(const juce::Identifier& name);
static const juce::var getRuntimeProperty(const juce::Identifier& name);
static juce::Value getPropertyAsValue(const juce::Identifier& name); // Convenience function for instance().get().getPropertyAsValue()
static juce::Value getEphemeralPropertyAsValue(const juce::Identifier& name);
static juce::Value getRuntimePropertyAsValue(const juce::Identifier& name);
static void ensurePropertyExists(const juce::Identifier& name, juce::var defaultValue);
static void ensureEphemeralPropertyExists(const juce::Identifier& name, juce::var defaultValue);
static void ensureRuntimePropertyExists(const juce::Identifier& name, juce::var defaultValue);
static void reset();

ValueTree& get();
ValueTree& getEphemeral();
ValueTree& getRuntime();
juce::ValueTree& get();
juce::ValueTree& getEphemeral();
juce::ValueTree& getRuntime();

void initializeFromSettings();
void saveToSettings();
Expand All @@ -53,7 +54,7 @@ class Data {

static Data instance_;

std::unique_ptr<ValueTree> tree_;
std::unique_ptr<ValueTree> ephemeralTree_;
std::unique_ptr<ValueTree> runtimeTree_;
std::unique_ptr<juce::ValueTree> tree_;
std::unique_ptr<juce::ValueTree> ephemeralTree_;
std::unique_ptr<juce::ValueTree> runtimeTree_;
};
4 changes: 2 additions & 2 deletions DebounceTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

#pragma once

#include "JuceHeader.h"
#include <juce_events/juce_events.h>

class DebounceTimer : public Timer {
class DebounceTimer : public juce::Timer {
public:
void callDebounced(std::function<void()> action, int milliseconds);

Expand Down
Loading

0 comments on commit eface45

Please sign in to comment.