Skip to content

Commit 465c94d

Browse files
committed
Added parameter validation and test cases for raw audio format options
1 parent b48cd30 commit 465c94d

File tree

11 files changed

+332
-144
lines changed

11 files changed

+332
-144
lines changed

src/AudioFileReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ AudioFileReader::AudioFileReader()
3535
AudioFileReader::~AudioFileReader()
3636
{
3737
}
38+
39+
//------------------------------------------------------------------------------

src/AudioFileReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class AudioProcessor;
3131
//------------------------------------------------------------------------------
3232

3333
class AudioFileReader
34-
{
34+
{
3535
public:
3636
AudioFileReader();
3737
virtual ~AudioFileReader();

src/FileFormat.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//------------------------------------------------------------------------------
22
//
3-
// Copyright 2023 BBC Research and Development
3+
// Copyright 2024 BBC Research and Development
44
//
55
// Author: Chris Needham
66
//
@@ -49,11 +49,11 @@ FileFormat fromString(const std::string& name)
4949
{ "ogg", FileFormat::Ogg },
5050
{ "oga", FileFormat::Ogg },
5151
{ "opus", FileFormat::Opus },
52+
{ "raw", FileFormat::Raw },
5253
{ "dat", FileFormat::Dat },
5354
{ "json", FileFormat::Json },
5455
{ "txt", FileFormat::Txt },
55-
{ "png", FileFormat::Png },
56-
{ "raw", FileFormat::Raw }
56+
{ "png", FileFormat::Png }
5757
};
5858

5959
const auto i = map.find(key);
@@ -95,6 +95,10 @@ std::string toString(FileFormat file_format)
9595
str = "opus";
9696
break;
9797

98+
case FileFormat::Raw:
99+
str = "raw";
100+
break;
101+
98102
case FileFormat::Dat:
99103
str = "dat";
100104
break;
@@ -111,10 +115,6 @@ std::string toString(FileFormat file_format)
111115
str = "png";
112116
break;
113117

114-
case FileFormat::Raw:
115-
str = "raw";
116-
break;
117-
118118
default:
119119
throwError("Unknown file format");
120120
break;

src/FileFormat.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//------------------------------------------------------------------------------
22
//
3-
// Copyright 2021 BBC Research and Development
3+
// Copyright 2024 BBC Research and Development
44
//
55
// Author: Chris Needham
66
//
@@ -39,11 +39,11 @@ namespace FileFormat {
3939
Flac,
4040
Ogg,
4141
Opus,
42+
Raw,
4243
Dat,
4344
Json,
4445
Txt,
45-
Png,
46-
Raw
46+
Png
4747
};
4848

4949
FileFormat fromString(const std::string& name);

src/OptionHandler.cpp

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//------------------------------------------------------------------------------
22
//
3-
// Copyright 2013-2023 BBC Research and Development
3+
// Copyright 2013-2024 BBC Research and Development
44
//
55
// Author: Chris Needham
66
//
@@ -51,50 +51,6 @@
5151

5252
//------------------------------------------------------------------------------
5353

54-
static std::string getFileExtension(const boost::filesystem::path& filename)
55-
{
56-
std::string extension = filename.extension().string();
57-
58-
// Remove leading "."
59-
if (!extension.empty()) {
60-
extension.erase(0, 1);
61-
}
62-
63-
return extension;
64-
}
65-
66-
//------------------------------------------------------------------------------
67-
68-
static FileFormat::FileFormat getFormatFromFileExtension(
69-
const boost::filesystem::path& filename)
70-
{
71-
return FileFormat::fromString(getFileExtension(filename));
72-
}
73-
74-
//------------------------------------------------------------------------------
75-
76-
static FileFormat::FileFormat getInputFormat(
77-
const Options& options,
78-
const boost::filesystem::path& filename)
79-
{
80-
return options.hasInputFormat() ?
81-
FileFormat::fromString(options.getInputFormat()) :
82-
getFormatFromFileExtension(filename);
83-
}
84-
85-
//------------------------------------------------------------------------------
86-
87-
static FileFormat::FileFormat getOutputFormat(
88-
const Options& options,
89-
const boost::filesystem::path& filename)
90-
{
91-
return options.hasOutputFormat() ?
92-
FileFormat::fromString(options.getOutputFormat()) :
93-
getFormatFromFileExtension(filename);
94-
}
95-
96-
//------------------------------------------------------------------------------
97-
9854
static std::unique_ptr<AudioFileReader> createAudioFileReader(
9955
const boost::filesystem::path& input_filename,
10056
const FileFormat::FileFormat input_format,
@@ -113,14 +69,13 @@ static std::unique_ptr<AudioFileReader> createAudioFileReader(
11369
}
11470
else if (input_format == FileFormat::Raw) {
11571
SndFileAudioFileReader* sndfile_audio_file_reader = new SndFileAudioFileReader;
72+
reader.reset(sndfile_audio_file_reader);
11673

11774
sndfile_audio_file_reader->configure(
11875
options.getRawAudioChannels(),
11976
options.getRawAudioSampleRate(),
12077
options.getRawAudioFormat()
12178
);
122-
123-
reader.reset(sndfile_audio_file_reader);
12479
}
12580
else {
12681
throwError("Unknown file type: %1%", input_filename);
@@ -184,7 +139,7 @@ static std::pair<bool, double> getDuration(
184139
const Options& options)
185140
{
186141
bool verbose = !options.getQuiet();
187-
142+
188143
std::unique_ptr<AudioFileReader> audio_file_reader(
189144
createAudioFileReader(input_filename, input_format, options)
190145
);
@@ -736,17 +691,17 @@ bool OptionHandler::run(const Options& options)
736691
bool success = true;
737692

738693
try {
739-
const boost::filesystem::path input_filename =
694+
const boost::filesystem::path& input_filename =
740695
options.getInputFilename();
741696

742-
const boost::filesystem::path output_filename =
697+
const boost::filesystem::path& output_filename =
743698
options.getOutputFilename();
744699

745700
const FileFormat::FileFormat input_format =
746-
getInputFormat(options, input_filename);
701+
options.getInputFormat();
747702

748703
const FileFormat::FileFormat output_format =
749-
getOutputFormat(options, output_filename);
704+
options.getOutputFormat();
750705

751706
if (shouldConvertAudioFormat(input_format, output_format)) {
752707
success = convertAudioFormat(

0 commit comments

Comments
 (0)