Skip to content

Commit e913c21

Browse files
committed
Added a few more tests and fixed logic errors
1 parent 1797e5a commit e913c21

7 files changed

Lines changed: 66 additions & 89 deletions

File tree

libpiper/src/main/utils/main_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void parseArgsLogic(int argc, char *argv[], RunConfig &runConfig) {
7474
runConfig.outputType = OUTPUT_FILE;
7575
runConfig.outputPath = std::filesystem::path(filePath);
7676
}
77-
} else if (arg == "-d" || arg == "--output_dir" || arg == "output-dir") {
77+
} else if (arg == "-d" || arg == "--output_dir" || arg == "--output-dir") {
7878
ensureArg(argc, argv, i);
7979
runConfig.outputType = OUTPUT_DIRECTORY;
8080
runConfig.outputPath = std::filesystem::path(argv[++i]);

libpiper/src/main/utils/process.cpp

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,15 @@
1212
using json = nlohmann::json;
1313

1414
void processInputStream(piper::RunConfig &runConfig, piper_synthesizer *piper, piper_synthesize_options *options) {
15-
std::unique_ptr<piper_synthesize_options> default_options;
16-
if (!options) {
17-
default_options = std::make_unique<piper_synthesize_options>(
18-
piper_default_synthesize_options(piper));
19-
options = default_options.get();
20-
}
21-
22-
// Speaker ID
23-
if (runConfig.speakerId) {
24-
options->speaker_id = runConfig.speakerId.value();
25-
}
26-
27-
// Scales
28-
if (runConfig.noiseScale) {
29-
options->noise_scale = runConfig.noiseScale.value();
30-
}
31-
32-
if (runConfig.lengthScale) {
33-
options->length_scale = runConfig.lengthScale.value();
34-
}
35-
36-
if (runConfig.noiseW) {
37-
options->noise_w_scale = runConfig.noiseW.value();
38-
}
39-
15+
4016
std::string line;
4117
while (getline(std::cin, line)) {
18+
if (line.empty()) {
19+
continue;
20+
}
4221

22+
piper_synthesize_options local_options = *options;
4323
auto outputType = runConfig.outputType;
44-
auto speakerId = options->speaker_id;
4524
std::optional<std::filesystem::path> maybeOutputPath = runConfig.outputPath;
4625

4726
if (runConfig.jsonInput) {
@@ -60,7 +39,7 @@ void processInputStream(piper::RunConfig &runConfig, piper_synthesizer *piper, p
6039

6140
if (lineRoot.contains("speaker_id")) {
6241
// Override speaker id
63-
options->speaker_id =
42+
local_options.speaker_id =
6443
lineRoot["speaker_id"].get<int>();
6544
}
6645
}
@@ -79,7 +58,7 @@ void processInputStream(piper::RunConfig &runConfig, piper_synthesizer *piper, p
7958

8059
// Output audio to automatically-named WAV file in a directory
8160
std::ofstream audioFile(outputPath.string(), std::ios::binary);
82-
textToWavFile(runConfig, piper, options, line.c_str(), audioFile);
61+
textToWavFile(piper, &local_options, line.c_str(), audioFile);
8362
std::cout << outputPath.string() << std::endl;
8463
} else if (outputType == piper::OUTPUT_FILE) {
8564
if (!maybeOutputPath || maybeOutputPath->empty()) {
@@ -101,15 +80,11 @@ void processInputStream(piper::RunConfig &runConfig, piper_synthesizer *piper, p
10180

10281
// Output audio to WAV file
10382
std::ofstream audioFile(outputPath.string(), std::ios::binary);
104-
textToWavFile(runConfig, piper, options, line.c_str(), audioFile);
83+
textToWavFile(piper, &local_options, line.c_str(), audioFile);
10584
std::cout << outputPath.string() << std::endl;
10685
} else if (outputType == piper::OUTPUT_STDOUT) {
10786
// Output WAV to stdout
108-
textToWavFile(runConfig, piper, options, line.c_str(), std::cout);
87+
textToWavFile(piper, &local_options, line.c_str(), std::cout);
10988
}
110-
111-
// Restore config (--json-input)
112-
options->speaker_id = speakerId;
113-
11489
} // for each line
11590
}

libpiper/src/main/utils/process.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "main_utils.hpp"
55
#include "piper.h"
66

7-
void processInputStream(piper::RunConfig &runConfig, piper_synthesizer *piper, piper_synthesize_options *options);
7+
void processInputStream(piper::RunConfig &runConfig,
8+
piper_synthesizer *piper,
9+
piper_synthesize_options *options);
810

911
#endif // PIPER_PROCESS_HPP

libpiper/src/main/utils/wavfile.cpp

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,10 @@
22
#include "wav_headers.hpp"
33
#include <iostream>
44

5-
void textToWavFile(piper::RunConfig &runConfig, piper_synthesizer *piper, piper_synthesize_options *options, const char *string, std::ostream &stream) {
6-
std::unique_ptr<piper_synthesize_options> default_options;
7-
if (!options) {
8-
default_options = std::make_unique<piper_synthesize_options>(
9-
piper_default_synthesize_options(piper));
10-
options = default_options.get();
11-
}
12-
13-
// Speaker ID
14-
if (runConfig.speakerId) {
15-
options->speaker_id = runConfig.speakerId.value();
16-
}
17-
18-
// Scales
19-
if (runConfig.noiseScale) {
20-
options->noise_scale = runConfig.noiseScale.value();
21-
}
22-
23-
if (runConfig.lengthScale) {
24-
options->length_scale = runConfig.lengthScale.value();
25-
}
26-
27-
if (runConfig.noiseW) {
28-
options->noise_w_scale = runConfig.noiseW.value();
29-
}
30-
5+
void textToWavFile(piper_synthesizer *piper,
6+
piper_synthesize_options *options,
7+
const char *string,
8+
std::ostream &stream) {
319
piper_synthesize_start(piper,
3210
string,
3311
options /* NULL for defaults */);

libpiper/src/main/utils/wavfile.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "main_utils.hpp"
66
#include <ostream>
77

8-
void textToWavFile(piper::RunConfig &runConfig, piper_synthesizer *piper, piper_synthesize_options *options, const char *string, std::ostream &file);
8+
void textToWavFile(piper_synthesizer *piper, piper_synthesize_options *options,
9+
const char *string, std::ostream &file);
910

1011
#endif // WAVFILE_H_

libpiper/tests/test_process.cpp

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,19 @@ namespace {
1414
// RAII class to redirect stdin for tests
1515
class StdinRedirect {
1616
public:
17-
StdinRedirect(const std::string &input) {
18-
// Create a temporary file with the input
19-
std::filesystem::path tempPath =
20-
std::filesystem::temp_directory_path() / "stdin.txt";
21-
std::ofstream tempFile(tempPath);
22-
tempFile << input;
23-
tempFile.close();
24-
25-
// Redirect stdin
26-
old_stdin = freopen(tempPath.c_str(), "r", stdin);
17+
StdinRedirect(const std::string &input) : old_cin(std::cin.rdbuf()) {
18+
input_buffer << input;
19+
std::cin.rdbuf(input_buffer.rdbuf());
2720
}
2821

2922
~StdinRedirect() {
3023
// Restore stdin and clean up
31-
if (old_stdin) {
32-
freopen(old_stdin_path, "r", stdin);
33-
fclose(old_stdin);
34-
}
35-
std::filesystem::remove(std::filesystem::temp_directory_path() /
36-
"stdin.txt");
24+
std::cin.rdbuf(old_cin);
3725
}
3826

3927
private:
40-
FILE *old_stdin = nullptr;
41-
// This is platform-specific, but works for this test case
42-
const char *old_stdin_path = "/dev/tty";
28+
std::streambuf *old_cin;
29+
std::stringstream input_buffer;
4330
};
4431
} // namespace
4532

@@ -68,15 +55,17 @@ piper_synthesizer *ProcessTest::synth = nullptr;
6855
TEST_F(ProcessTest, ProcessInputStreamText) {
6956
piper::RunConfig runConfig;
7057
runConfig.outputType = piper::OUTPUT_STDOUT;
71-
runConfig.speakerId = 0;
58+
59+
piper_synthesize_options options = piper_default_synthesize_options(synth);
60+
options.speaker_id = 0;
7261

7362
StdinRedirect redirect("This is a test.");
7463

7564
// Redirect cout to check output
7665
std::stringstream cout_buffer;
7766
std::streambuf *old_cout = std::cout.rdbuf(cout_buffer.rdbuf());
7867

79-
processInputStream(runConfig, synth, nullptr);
68+
processInputStream(runConfig, synth, &options);
8069

8170
std::cout.rdbuf(old_cout); // Restore cout
8271

@@ -86,18 +75,50 @@ TEST_F(ProcessTest, ProcessInputStreamText) {
8675
ASSERT_EQ(audio_data.substr(0, 4), "RIFF");
8776
}
8877

78+
TEST_F(ProcessTest, ProcessInputStreamFileOutput) {
79+
piper::RunConfig runConfig;
80+
auto outputPath = std::filesystem::temp_directory_path() / "test.wav";
81+
runConfig.outputPath = outputPath;
82+
runConfig.outputType = piper::OUTPUT_FILE;
83+
84+
piper_synthesize_options options = piper_default_synthesize_options(synth);
85+
options.speaker_id = 0;
86+
87+
StdinRedirect redirect("This is a test for file output.");
88+
89+
// Redirect cout to capture the output path
90+
std::stringstream cout_buffer;
91+
std::streambuf* old_cout = std::cout.rdbuf(cout_buffer.rdbuf());
92+
93+
processInputStream(runConfig, synth, &options);
94+
95+
std::cout.rdbuf(old_cout); // Restore cout
96+
97+
// Verify the output file was created and has content
98+
ASSERT_TRUE(std::filesystem::exists(outputPath));
99+
std::ifstream audio_file(outputPath, std::ios::binary | std::ios::ate);
100+
ASSERT_TRUE(audio_file.is_open());
101+
std::streamsize size = audio_file.tellg();
102+
ASSERT_GT(size, 44); // Should have a 44-byte header plus some audio data
103+
104+
// Clean up the created file
105+
std::filesystem::remove(outputPath);
106+
}
107+
89108
TEST_F(ProcessTest, ProcessInputStreamJson) {
90109
piper::RunConfig runConfig;
91110
runConfig.outputType = piper::OUTPUT_STDOUT;
92111
runConfig.jsonInput = true;
93-
runConfig.speakerId = 0;
112+
113+
piper_synthesize_options options = piper_default_synthesize_options(synth);
114+
options.speaker_id = 0;
94115

95116
StdinRedirect redirect("{\"text\": \"This is a JSON test.\"}");
96117

97118
std::stringstream cout_buffer;
98119
std::streambuf *old_cout = std::cout.rdbuf(cout_buffer.rdbuf());
99120

100-
processInputStream(runConfig, synth, nullptr);
121+
processInputStream(runConfig, synth, &options);
101122

102123
std::cout.rdbuf(old_cout);
103124

libpiper/tests/test_wavfile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ piper_synthesizer *WavFileTest::synth = nullptr;
3030

3131
TEST_F(WavFileTest, TextToWavFile) {
3232
std::stringstream audio_stream;
33-
piper::RunConfig runConfig;
34-
runConfig.speakerId = 0;
33+
piper_synthesize_options options = piper_default_synthesize_options(synth);
34+
options.speaker_id = 0;
3535

36-
textToWavFile(runConfig, synth, nullptr, "This is a test.", audio_stream);
36+
textToWavFile(synth, &options, "This is a test.", audio_stream);
3737

3838
std::string audio_data = audio_stream.str();
3939
// Should have a 44-byte header plus some audio data

0 commit comments

Comments
 (0)