-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59cb1e8
commit d2da606
Showing
3 changed files
with
70 additions
and
1 deletion.
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,60 @@ | ||
#pragma once | ||
|
||
#include <juce_audio_formats/juce_audio_formats.h> | ||
#include <juce_core/juce_core.h> | ||
|
||
namespace ta | ||
{ | ||
|
||
struct AudioFileConverterOptions | ||
{ | ||
juce::File source{}; | ||
juce::File destination{}; | ||
}; | ||
|
||
struct AudioFileConverterResult | ||
{ | ||
bool success{false}; | ||
juce::String errorMessage{}; | ||
}; | ||
|
||
struct AudioFileConverter | ||
{ | ||
using Options = AudioFileConverterOptions; | ||
using Result = AudioFileConverterResult; | ||
|
||
AudioFileConverter(); | ||
|
||
template<typename DestinationFormat> | ||
auto convert(Options const& options) -> Result; | ||
|
||
private: | ||
juce::AudioFormatManager _formatManager; | ||
}; | ||
|
||
inline AudioFileConverter::AudioFileConverter() { _formatManager.registerBasicFormats(); } | ||
|
||
template<typename DestinationFormat> | ||
inline auto AudioFileConverter::convert(Options const& options) -> Result | ||
{ | ||
auto reader = std::unique_ptr<juce::AudioFormatReader>(_formatManager.createReaderFor(options.source)); | ||
if (reader == nullptr) { return Result{false, "Invalid file"}; } | ||
|
||
DestinationFormat format{}; | ||
auto const sr = reader->sampleRate; | ||
auto const ch = reader->numChannels; | ||
auto const bits = reader->bitsPerSample; | ||
|
||
auto out = std::make_unique<juce::FileOutputStream>(options.destination); | ||
auto writer = std::unique_ptr<juce::AudioFormatWriter>(format.createWriterFor(out.get(), sr, ch, bits, {}, 0)); | ||
|
||
if (writer == nullptr) { return Result{false, "Failed to convert to wav"}; } | ||
out.release(); // writer took ownership | ||
|
||
writer->writeFromAudioReader(*reader, 0, reader->lengthInSamples); | ||
writer->flush(); | ||
|
||
return Result{true, {}}; | ||
} | ||
|
||
} // namespace ta |
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