Skip to content

Commit

Permalink
Audio formats
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Feb 9, 2024
1 parent 117a28d commit 2509b83
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
27 changes: 26 additions & 1 deletion modules/juce_python/bindings/ScriptJuceAudioFormatsBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,33 @@ using namespace py::literals;

// ============================================================================================

void registerJuceAudioFormatsBindings ([[maybe_unused]] py::module_& m)
void registerJuceAudioFormatsBindings (py::module_& m)
{
// ============================================================================================ juce::AudioFormatManager

py::class_<AudioFormat, PyAudioFormat> classAudioFormat (m, "AudioFormat");

classAudioFormat
.def (py::init<String, StringArray>())
.def (py::init<StringRef, StringRef>())
;

// ============================================================================================ juce::AudioFormatManager

py::class_<AudioFormatManager> classAudioFormatManager (m, "AudioFormatManager");

classAudioFormatManager
.def (py::init<>())
.def ("registerFormat", &AudioFormatManager::registerFormat)
.def ("registerBasicFormats", &AudioFormatManager::registerBasicFormats)
.def ("clearFormats", &AudioFormatManager::clearFormats)
.def ("getNumKnownFormats", &AudioFormatManager::getNumKnownFormats)
.def ("getKnownFormat", &AudioFormatManager::getKnownFormat, py::return_value_policy::reference)
.def ("findFormatForFileExtension", &AudioFormatManager::findFormatForFileExtension, py::return_value_policy::reference)
.def ("getDefaultFormat", &AudioFormatManager::getDefaultFormat, py::return_value_policy::reference)
.def ("getWildcardForAllFormats", &AudioFormatManager::getWildcardForAllFormats)
.def ("createReaderFor", py::overload_cast<const File&> (&AudioFormatManager::createReaderFor))
;
}

} // namespace popsicle::Bindings
97 changes: 97 additions & 0 deletions modules/juce_python/bindings/ScriptJuceAudioFormatsBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,101 @@ namespace popsicle::Bindings {

void registerJuceAudioFormatsBindings (pybind11::module_& m);

// =================================================================================================

struct PyAudioFormat : juce::AudioFormat
{
using juce::AudioFormat::AudioFormat;

PyAudioFormat (juce::String formatName, juce::StringArray fileExtensions)
: juce::AudioFormat (std::move (formatName), std::move (fileExtensions))
{
}

PyAudioFormat (juce::StringRef formatName, juce::StringRef fileExtensions)
: juce::AudioFormat (std::move (formatName), std::move (fileExtensions))
{
}

juce::StringArray getFileExtensions() const override
{
PYBIND11_OVERRIDE (juce::StringArray, juce::AudioFormat, getFileExtensions);
}

bool canHandleFile (const juce::File& fileToTest) override
{
PYBIND11_OVERRIDE_PURE (bool, juce::AudioFormat, canHandleFile, fileToTest);
}

juce::Array<int> getPossibleSampleRates() override
{
PYBIND11_OVERRIDE_PURE (juce::Array<int>, juce::AudioFormat, getPossibleSampleRates);
}

juce::Array<int> getPossibleBitDepths() override
{
PYBIND11_OVERRIDE_PURE (juce::Array<int>, juce::AudioFormat, getPossibleBitDepths);
}

bool canDoStereo() override
{
PYBIND11_OVERRIDE_PURE (bool, juce::AudioFormat, canDoStereo);
}

bool canDoMono() override
{
PYBIND11_OVERRIDE_PURE (bool, juce::AudioFormat, canDoMono);
}

bool isCompressed() override
{
PYBIND11_OVERRIDE (bool, juce::AudioFormat, isCompressed);
}

bool isChannelLayoutSupported (const juce::AudioChannelSet& channelSet) override
{
PYBIND11_OVERRIDE (bool, juce::AudioFormat, isChannelLayoutSupported, channelSet);
}

juce::StringArray getQualityOptions() override
{
PYBIND11_OVERRIDE (juce::StringArray, juce::AudioFormat, getQualityOptions);
}

juce::AudioFormatReader* createReaderFor (juce::InputStream* sourceStream, bool deleteStreamIfOpeningFails) override
{
PYBIND11_OVERRIDE_PURE (juce::AudioFormatReader*, juce::AudioFormat, createReaderFor, sourceStream, deleteStreamIfOpeningFails);
}

juce::MemoryMappedAudioFormatReader* createMemoryMappedReader (const juce::File& file) override
{
PYBIND11_OVERRIDE (juce::MemoryMappedAudioFormatReader*, juce::AudioFormat, createMemoryMappedReader, file);
}

juce::MemoryMappedAudioFormatReader* createMemoryMappedReader (juce::FileInputStream* fin) override
{
PYBIND11_OVERRIDE (juce::MemoryMappedAudioFormatReader*, juce::AudioFormat, createMemoryMappedReader, fin);
}

juce::AudioFormatWriter* createWriterFor (juce::OutputStream* streamToWriteTo,
double sampleRateToUse,
unsigned int numberOfChannels,
int bitsPerSample,
const juce::StringPairArray& metadataValues,
int qualityOptionIndex) override
{
PYBIND11_OVERRIDE_PURE (juce::AudioFormatWriter*, juce::AudioFormat, createWriterFor, streamToWriteTo, sampleRateToUse, numberOfChannels, bitsPerSample, metadataValues, qualityOptionIndex);
}

juce::AudioFormatWriter* createWriterFor (juce::OutputStream* streamToWriteTo,
double sampleRateToUse,
const juce::AudioChannelSet& channelLayout,
int bitsPerSample,
const juce::StringPairArray& metadataValues,
int qualityOptionIndex) override
{
PYBIND11_OVERRIDE_PURE (juce::AudioFormatWriter*, juce::AudioFormat, createWriterFor, streamToWriteTo, sampleRateToUse, channelLayout, bitsPerSample, metadataValues, qualityOptionIndex);
}
};

} // namespace popsicle::Bindings

0 comments on commit 2509b83

Please sign in to comment.