Skip to content

Commit 7b0f7dc

Browse files
committed
add x.wav handler
Former-commit-id: 67870de
1 parent eb1deaa commit 7b0f7dc

File tree

2 files changed

+62
-53
lines changed

2 files changed

+62
-53
lines changed

src/FileIO/AudioModelIO.cpp

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
#include "Utils/LOG.h"
1717
#include "Utils/exception.h"
1818

19-
AudioModelIO::AudioModelIO(std::filesystem::path Path, lessAudioModel audioModel) : RootFilePath(std::move(Path)), _audioModel(audioModel) {
19+
AudioModelIO::AudioModelIO(std::filesystem::path Path, lessAudioModel audioModel) : in_file_path(std::move(Path)), _audioModel(audioModel) {
20+
root_file_path = in_file_path;
2021
GenerateFilePath();
2122
}
2223

23-
AudioModelIO::AudioModelIO(std::filesystem::path Path) : RootFilePath(std::move(Path)) {
24+
AudioModelIO::AudioModelIO(std::filesystem::path Path) : in_file_path(std::move(Path)) {
25+
root_file_path = in_file_path;
2426
GenerateFilePath();
2527
}
2628

@@ -36,7 +38,7 @@ AudioModelIO::~AudioModelIO() {
3638
}
3739

3840
[[maybe_unused]] void AudioModelIO::SetFilePath(const std::filesystem::path &Path) {
39-
RootFilePath = Path;
41+
root_file_path = Path;
4042
GenerateFilePath();
4143
}
4244

@@ -49,13 +51,13 @@ AudioModelIO::~AudioModelIO() {
4951
}
5052

5153
[[maybe_unused]] std::filesystem::path AudioModelIO::GetFilePath() {
52-
return RootFilePath;
54+
return root_file_path;
5355
}
5456

5557
void AudioModelIO::GenerateFilePath() {
56-
F0FilePath = RootFilePath.replace_extension(F0FileExt);
57-
SPFilePath = RootFilePath.replace_extension(SPFileExt);
58-
APFilePath = RootFilePath.replace_extension(APFileExt);
58+
f0_file_path = root_file_path.replace_extension(f0_file_ext);
59+
sp_file_path = root_file_path.replace_extension(sp_file_ext);
60+
ap_file_path = root_file_path.replace_extension(ap_file_ext);
5961
}
6062

6163
void AudioModelIO::SaveAudioModel() {
@@ -122,35 +124,35 @@ int AudioModelIO::CheckHeader(FILE *fp, const char *text) {
122124
}
123125

124126
void AudioModelIO::WriteF0() {
125-
FILE *fp = fopen(F0FilePath.string().c_str(), "wb");
127+
FILE *fp = fopen(f0_file_path.string().c_str(), "wb");
126128
if (nullptr == fp)
127-
throw file_open_error(F0FilePath.string());
129+
throw file_open_error(f0_file_path.string());
128130

129131
// Header
130-
fwrite(F0Header, 1, 4, fp);
132+
fwrite(f0_header, 1, 4, fp);
131133

132134
// Parameters
133-
WriteOneParameter(fp, F0LengthHeader, _audioModel.f0_length);
134-
WriteOneParameter(fp, FramePeridoHeader, _audioModel.frame_period);
135-
WriteOneParameter(fp, XLHeader, _audioModel.x_length);
135+
WriteOneParameter(fp, f0_length_header, _audioModel.f0_length);
136+
WriteOneParameter(fp, frame_period_header, _audioModel.frame_period);
137+
WriteOneParameter(fp, x_length_header, _audioModel.x_length);
136138

137139
// Data
138140
fwrite(_audioModel.f0, 8, _audioModel.f0_length, fp);
139141
fclose(fp);
140142
}
141143

142144
void AudioModelIO::WriteSP() {
143-
FILE *fp = fopen(SPFilePath.string().c_str(), "wb");
145+
FILE *fp = fopen(sp_file_path.string().c_str(), "wb");
144146
if (nullptr == fp)
145-
throw file_open_error(SPFilePath.string());
147+
throw file_open_error(sp_file_path.string());
146148
// Header
147-
fwrite(SPHeader, 1, 4, fp);
149+
fwrite(sp_header, 1, 4, fp);
148150

149151
// Parameters
150-
WriteOneParameter(fp, F0LengthHeader, _audioModel.f0_length);
151-
WriteOneParameter(fp, FramePeridoHeader, _audioModel.frame_period);
152-
WriteOneParameter(fp, FFTSizeHeader, _audioModel.fft_size);
153-
WriteOneParameter(fp, FSHeader, _audioModel.fs);
152+
WriteOneParameter(fp, f0_length_header, _audioModel.f0_length);
153+
WriteOneParameter(fp, frame_period_header, _audioModel.frame_period);
154+
WriteOneParameter(fp, fft_size_header, _audioModel.fft_size);
155+
WriteOneParameter(fp, fs_header, _audioModel.fs);
154156

155157
// Data
156158
for (int i = 0; i < _audioModel.f0_length; ++i)
@@ -159,18 +161,18 @@ void AudioModelIO::WriteSP() {
159161
}
160162

161163
void AudioModelIO::WriteAP() {
162-
FILE *fp = fopen(APFilePath.string().c_str(), "wb");
164+
FILE *fp = fopen(ap_file_path.string().c_str(), "wb");
163165
if (nullptr == fp)
164-
throw file_open_error(APFilePath.string());
166+
throw file_open_error(ap_file_path.string());
165167

166168
// Header
167-
fwrite(APHeader, 1, 4, fp);
169+
fwrite(ap_header, 1, 4, fp);
168170

169171
// Parameters
170-
WriteOneParameter(fp, F0LengthHeader, _audioModel.f0_length);
171-
WriteOneParameter(fp, FramePeridoHeader, _audioModel.frame_period);
172-
WriteOneParameter(fp, FFTSizeHeader, _audioModel.fft_size);
173-
WriteOneParameter(fp, FSHeader, _audioModel.fs);
172+
WriteOneParameter(fp, f0_length_header, _audioModel.f0_length);
173+
WriteOneParameter(fp, frame_period_header, _audioModel.frame_period);
174+
WriteOneParameter(fp, fft_size_header, _audioModel.fft_size);
175+
WriteOneParameter(fp, fs_header, _audioModel.fs);
174176

175177
// Data
176178
for (int i = 0; i < _audioModel.f0_length; ++i)
@@ -179,12 +181,12 @@ void AudioModelIO::WriteAP() {
179181
}
180182

181183
void AudioModelIO::ReadF0() {
182-
FILE *fp = fopen(F0FilePath.string().c_str(), "rb");
184+
FILE *fp = fopen(f0_file_path.string().c_str(), "rb");
183185
if (nullptr == fp)
184-
throw file_open_error(F0FilePath.string());
186+
throw file_open_error(f0_file_path.string());
185187
// Header
186188
try {
187-
CheckHeader(fp, F0Header);
189+
CheckHeader(fp, f0_header);
188190
} catch (header_check_error &error) {
189191
YALL_ERROR_ << error.what();
190192
}
@@ -215,13 +217,13 @@ void AudioModelIO::ReadF0() {
215217
}
216218

217219
void AudioModelIO::ReadSP() {
218-
FILE *fp = fopen(SPFilePath.string().c_str(), "rb");
220+
FILE *fp = fopen(sp_file_path.string().c_str(), "rb");
219221
if (nullptr == fp)
220-
throw file_open_error(SPFilePath.string());
222+
throw file_open_error(sp_file_path.string());
221223

222224
// Header
223225
try {
224-
CheckHeader(fp, SPHeader);
226+
CheckHeader(fp, sp_header);
225227
} catch (header_check_error &error) {
226228
YALL_ERROR_ << error.what();
227229
}
@@ -242,13 +244,13 @@ void AudioModelIO::ReadSP() {
242244
}
243245

244246
void AudioModelIO::ReadAP() {
245-
FILE *fp = fopen(APFilePath.string().c_str(), "rb");
247+
FILE *fp = fopen(ap_file_path.string().c_str(), "rb");
246248
if (nullptr == fp)
247-
throw file_open_error(APFilePath.string());
249+
throw file_open_error(ap_file_path.string());
248250

249251
// Header
250252
try {
251-
CheckHeader(fp, APHeader);
253+
CheckHeader(fp, ap_header);
252254
} catch (header_check_error &error) {
253255
YALL_ERROR_ << error.what();
254256
}
@@ -277,8 +279,14 @@ bool AudioModelIO::CheckAudioModelFile(const std::filesystem::path &path) {
277279
}
278280

279281
bool AudioModelIO::CheckAudioModel() {
280-
YALL_DEBUG_ << "Check AudioModel File...";
281-
if (!CheckAudioModelFile(F0FilePath) || !CheckAudioModelFile(SPFilePath) || !CheckAudioModelFile(APFilePath)) {
282+
YALL_DEBUG_ << "Check AudioModel File: " + in_file_path.string();
283+
284+
// x.wav handler
285+
if (in_file_path.stem() == std::filesystem::path("x")){
286+
return true;
287+
}
288+
289+
if (!CheckAudioModelFile(f0_file_path) || !CheckAudioModelFile(sp_file_path) || !CheckAudioModelFile(ap_file_path)) {
282290
YALL_DEBUG_ << "Audio Model NOT Exist.";
283291
return false;
284292
} else {

src/FileIO/AudioModelIO.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,24 @@ class AudioModelIO {
4949

5050
private:
5151
lessAudioModel _audioModel{};
52-
std::filesystem::path RootFilePath{};
53-
std::filesystem::path F0FilePath{};
54-
std::filesystem::path SPFilePath{};
55-
std::filesystem::path APFilePath{};
52+
std::filesystem::path root_file_path{};
53+
std::filesystem::path in_file_path{};
54+
std::filesystem::path f0_file_path{};
55+
std::filesystem::path sp_file_path{};
56+
std::filesystem::path ap_file_path{};
5657

5758
protected:
58-
const std::string F0FileExt = "lessaudiof0";
59-
const std::string SPFileExt = "lessaudiosp";
60-
const std::string APFileExt = "lessaudioap";
61-
const char F0LengthHeader[5] = {'F', '0', 'L', ' ', '\0'}; // number of samples (int)
62-
const char FramePeridoHeader[5] = {'F', 'P', ' ', ' ', '\0'}; // frame perido (double)
63-
const char FFTSizeHeader[5] = {'F', 'F', 'T', ' ', '\0'}; // FFT size (int)
64-
const char XLHeader[5] = {'X', 'L', ' ', ' ', '\0'}; // x_length (int)
65-
const char FSHeader[5] = {'F', 'S', ' ', ' ', '\0'}; // frame per sample (int)
66-
const char F0Header[5] = {'5', '4', '0', '0', '\0'}; // F0
67-
const char SPHeader[5] = {'5', '4', '0', '1', '\0'}; // SP
68-
const char APHeader[5] = {'5', '4', '0', '2', '\0'}; // AP
59+
const std::string f0_file_ext = "lessaudiof0";
60+
const std::string sp_file_ext = "lessaudiosp";
61+
const std::string ap_file_ext = "lessaudioap";
62+
const char f0_length_header[5] = {'F', '0', 'L', ' ', '\0'}; // number of samples (int)
63+
const char frame_period_header[5] = {'F', 'P', ' ', ' ', '\0'}; // frame perido (double)
64+
const char fft_size_header[5] = {'F', 'F', 'T', ' ', '\0'}; // FFT size (int)
65+
const char x_length_header[5] = {'X', 'L', ' ', ' ', '\0'}; // x_length (int)
66+
const char fs_header[5] = {'F', 'S', ' ', ' ', '\0'}; // frame per sample (int)
67+
const char f0_header[5] = {'5', '4', '0', '0', '\0'}; // F0
68+
const char sp_header[5] = {'5', '4', '0', '1', '\0'}; // SP
69+
const char ap_header[5] = {'5', '4', '0', '2', '\0'}; // AP
6970

7071
private:
7172
void WriteAudioContent();

0 commit comments

Comments
 (0)