Skip to content

Commit

Permalink
Fixes to leaking handles, documentation, and indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alice committed Sep 11, 2016
1 parent 47025e0 commit 24bc693
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/MagnumPlugins/AnyAudioImporter/AnyImporter.cpp
Expand Up @@ -54,7 +54,7 @@ void AnyImporter::doOpenFile(const std::string& filename) {
else if(Utility::String::endsWith(filename, ".wav"))
plugin = "WavAudioImporter";
else if(Utility::String::endsWith(filename, ".flac"))
plugin = "FlacAudioImporter";
plugin = "FlacAudioImporter";
else {
Error() << "Audio::AnyImporter::openFile(): cannot determine type of file" << filename;
return;
Expand Down
31 changes: 18 additions & 13 deletions src/MagnumPlugins/DrFlacAudioImporter/DrFlacImporter.cpp
Expand Up @@ -35,30 +35,33 @@

namespace Magnum { namespace Audio {

DrFlacImporter::DrFlacImporter() = default;
DrFlacImporter::DrFlacImporter() : _handle(nullptr) {}

DrFlacImporter::DrFlacImporter(PluginManager::AbstractManager& manager, std::string plugin): AbstractImporter(manager, std::move(plugin)) {}

DrFlacImporter::~DrFlacImporter()
{
doClose();
}

auto DrFlacImporter::doFeatures() const -> Features { return Feature::OpenData; }

bool DrFlacImporter::doIsOpened() const { return _data; }

void DrFlacImporter::doOpenData(Containers::ArrayView<const char> data) {

drflac* handle = drflac_open_memory(reinterpret_cast<const UnsignedByte*>(data.data()), data.size());
_handle = drflac_open_memory(reinterpret_cast<const UnsignedByte*>(data.data()), data.size());

if(handle == NULL) {
if(_handle == NULL) {
Error() << "Audio::DrFlacImporter::openData(): failed to open and decode FLAC data";
return;
}

_handle = (void*)handle;

int32_t* decodedData = handle->pDecodedSamples;
uint64_t samples = handle->totalSampleCount;
uint32_t frequency = handle->sampleRate;
uint8_t numChannels = handle->channels;
uint8_t bitsPerSample = handle->bitsPerSample;
int32_t* decodedData = _handle->pDecodedSamples;
uint64_t samples = _handle->totalSampleCount;
uint32_t frequency = _handle->sampleRate;
uint8_t numChannels = _handle->channels;
uint8_t bitsPerSample = _handle->bitsPerSample;

_frequency = frequency;

Expand All @@ -76,16 +79,18 @@ void DrFlacImporter::doOpenData(Containers::ArrayView<const char> data) {
Error() << "Audio::DrFlacImporter::openData(): unsupported channel count"
<< numChannels << "with" << bitsPerSample
<< "bits per sample";
doClose();
return;
}

_data = std::move(tempData);
return;
}

void DrFlacImporter::doClose() {
drflac_close((drflac*)_handle);
_handle = nullptr;
void DrFlacImporter::doClose() {
if(_handle) drflac_close((drflac*)_handle);

_handle = nullptr;
_data = nullptr;
}

Expand Down
16 changes: 9 additions & 7 deletions src/MagnumPlugins/DrFlacAudioImporter/DrFlacImporter.h
Expand Up @@ -52,10 +52,10 @@
namespace Magnum { namespace Audio {

/**
@brief OGG audio importer plugin using stb_vorbis
@brief FLAC audio importer plugin using dr_flac
Supports mono and stereo files with 16 bits per channel. The files are
imported with @ref Buffer::Format::Mono16 or @ref Buffer::Format::Stereo16,
Supports mono and stereo files with 8/16 bits per channel. The files are
imported with @ref Buffer::Format::Mono8/16 or @ref Buffer::Format::Stereo8/16,
respectively.
This plugin is built if `WITH_DRFLACAUDIOIMPORTER` is enabled when building
Expand All @@ -65,9 +65,8 @@ dependency of another plugin, you need to request `DrFlacAudioImporter`
component of `MagnumPlugins` package in CMake and link to
`MagnumPlugins::DrFlacAudioImporter`.
This plugins provides `VorbisAudioImporter`, but note that this plugin doesn't
have complete support for all format quirks and the performance might be worse
than when using plugin dedicated for given format.
This plugins provides `FlacAudioImporter`, but note that this plugin doesn't
handle CRC checks, corrupt or perverse FLAC streams, or broadcast streams.
See @ref building-plugins, @ref cmake-plugins and @ref plugins for more
information.
Expand All @@ -80,6 +79,9 @@ class MAGNUM_DRFLACAUDIOIMPORTER_EXPORT DrFlacImporter: public AbstractImporter
/** @brief Plugin manager constructor */
explicit DrFlacImporter(PluginManager::AbstractManager& manager, std::string plugin);

/** @brief Destructor */
~DrFlacImporter();

private:
MAGNUM_DRFLACAUDIOIMPORTER_LOCAL Features doFeatures() const override;
MAGNUM_DRFLACAUDIOIMPORTER_LOCAL bool doIsOpened() const override;
Expand All @@ -93,7 +95,7 @@ class MAGNUM_DRFLACAUDIOIMPORTER_EXPORT DrFlacImporter: public AbstractImporter
Containers::Array<char> _data;
Buffer::Format _format;
UnsignedInt _frequency;
void* _handle;
struct drflac* _handle;
};

}}
Expand Down

0 comments on commit 24bc693

Please sign in to comment.