-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #680 from jpcima/importer
Import foreign instruments
- Loading branch information
Showing
12 changed files
with
272 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
|
||
// This code is part of the sfizz library and is licensed under a BSD 2-clause | ||
// license. You should have receive a LICENSE.md file along with the code. | ||
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz | ||
|
||
#include "ForeignInstrument.h" | ||
#include "foreign_instruments/AudioFile.h" | ||
|
||
namespace sfz { | ||
|
||
InstrumentFormatRegistry::InstrumentFormatRegistry() | ||
: formats_ { | ||
&AudioFileInstrumentFormat::getInstance(), | ||
} | ||
{ | ||
} | ||
|
||
InstrumentFormatRegistry& InstrumentFormatRegistry::getInstance() | ||
{ | ||
static InstrumentFormatRegistry registry; | ||
return registry; | ||
} | ||
|
||
const InstrumentFormat* InstrumentFormatRegistry::getMatchingFormat(const fs::path& path) const | ||
{ | ||
const InstrumentFormat* resultFormat = nullptr; | ||
|
||
for (const InstrumentFormat* currentFormat : formats_) { | ||
if (currentFormat->matchesFilePath(path)) { | ||
resultFormat = currentFormat; | ||
break; | ||
} | ||
} | ||
|
||
return resultFormat; | ||
} | ||
|
||
} // namespace sfz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
|
||
// This code is part of the sfizz library and is licensed under a BSD 2-clause | ||
// license. You should have receive a LICENSE.md file along with the code. | ||
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz | ||
|
||
#pragma once | ||
#include <ghc/fs_std.hpp> | ||
#include <string> | ||
#include <vector> | ||
#include <memory> | ||
|
||
namespace sfz { | ||
|
||
class InstrumentFormat; | ||
class InstrumentImporter; | ||
|
||
/** | ||
* Registry of known non-SFZ instrument formats | ||
*/ | ||
class InstrumentFormatRegistry { | ||
private: | ||
InstrumentFormatRegistry(); | ||
|
||
public: | ||
/** | ||
* @brief Get the single instance of the registry | ||
*/ | ||
static InstrumentFormatRegistry& getInstance(); | ||
|
||
/** | ||
* @brief Get a format which is able to handle a file which goes by the | ||
* given path name, or null otherwise. | ||
*/ | ||
const InstrumentFormat* getMatchingFormat(const fs::path& path) const; | ||
|
||
private: | ||
std::vector<const InstrumentFormat*> formats_; | ||
}; | ||
|
||
/** | ||
* Description of a non-SFZ instrument format | ||
*/ | ||
class InstrumentFormat { | ||
public: | ||
virtual ~InstrumentFormat() {} | ||
|
||
/** | ||
* @brief Get the name of the instrument format | ||
*/ | ||
virtual const char* name() const noexcept = 0; | ||
|
||
/** | ||
* @brief Checks whether this importer matches files of the given path | ||
* | ||
* This should check for a pattern like such as a file extension, but not | ||
* examine the contents of the file itself. | ||
*/ | ||
virtual bool matchesFilePath(const fs::path& path) const = 0; | ||
|
||
/** | ||
* @brief Create a new importer for instrument files of this format | ||
*/ | ||
virtual std::unique_ptr<InstrumentImporter> createImporter() const = 0; | ||
}; | ||
|
||
/** | ||
* Importer of non-SFZ instruments | ||
*/ | ||
class InstrumentImporter { | ||
public: | ||
/** | ||
* @brief Get the format that this importer converts from | ||
*/ | ||
virtual const InstrumentFormat* getFormat() const noexcept = 0; | ||
|
||
/** | ||
* @brief Process the file and convert to an equivalent SFZ string | ||
*/ | ||
virtual std::string convertToSfz(const fs::path& path) const = 0; | ||
}; | ||
|
||
} // namespace sfz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
|
||
// This code is part of the sfizz library and is licensed under a BSD 2-clause | ||
// license. You should have receive a LICENSE.md file along with the code. | ||
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz | ||
|
||
#include "AudioFile.h" | ||
#include <absl/strings/match.h> | ||
#include <absl/strings/string_view.h> | ||
#include <absl/memory/memory.h> | ||
#include <locale> | ||
#include <sstream> | ||
|
||
namespace sfz { | ||
|
||
static const char* kRecognizedAudioExtensions[] = { | ||
".wav", ".flac", ".ogg", ".mp3", ".aif", ".aiff", ".aifc", | ||
}; | ||
|
||
/// | ||
AudioFileInstrumentFormat& AudioFileInstrumentFormat::getInstance() | ||
{ | ||
static AudioFileInstrumentFormat format; | ||
return format; | ||
} | ||
|
||
const char* AudioFileInstrumentFormat::name() const noexcept | ||
{ | ||
return "Audio file"; | ||
} | ||
|
||
bool AudioFileInstrumentFormat::matchesFilePath(const fs::path& path) const | ||
{ | ||
const std::string ext = path.extension().u8string(); | ||
|
||
for (absl::string_view knownExt : kRecognizedAudioExtensions) { | ||
if (absl::EqualsIgnoreCase(ext, knownExt)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
std::unique_ptr<InstrumentImporter> AudioFileInstrumentFormat::createImporter() const | ||
{ | ||
return absl::make_unique<AudioFileInstrumentImporter>(); | ||
} | ||
|
||
/// | ||
std::string AudioFileInstrumentImporter::convertToSfz(const fs::path& path) const | ||
{ | ||
std::ostringstream os; | ||
os.imbue(std::locale::classic()); | ||
os << "<region>sample=" << path.filename().u8string(); | ||
return os.str(); | ||
} | ||
|
||
const InstrumentFormat* AudioFileInstrumentImporter::getFormat() const noexcept | ||
{ | ||
return &AudioFileInstrumentFormat::getInstance(); | ||
} | ||
|
||
} // namespace sfz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
|
||
// This code is part of the sfizz library and is licensed under a BSD 2-clause | ||
// license. You should have receive a LICENSE.md file along with the code. | ||
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz | ||
|
||
#pragma once | ||
#include "../ForeignInstrument.h" | ||
|
||
namespace sfz { | ||
|
||
class AudioFileInstrumentFormat : public InstrumentFormat { | ||
private: | ||
AudioFileInstrumentFormat() noexcept = default; | ||
|
||
public: | ||
static AudioFileInstrumentFormat& getInstance(); | ||
const char* name() const noexcept override; | ||
bool matchesFilePath(const fs::path& path) const override; | ||
std::unique_ptr<InstrumentImporter> createImporter() const override; | ||
}; | ||
|
||
/// | ||
class AudioFileInstrumentImporter : public InstrumentImporter { | ||
public: | ||
std::string convertToSfz(const fs::path& path) const override; | ||
const InstrumentFormat* getFormat() const noexcept override; | ||
}; | ||
|
||
} // namespace sfz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters