-
Notifications
You must be signed in to change notification settings - Fork 0
/
praatresynth.cpp
156 lines (134 loc) · 4.78 KB
/
praatresynth.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <vector>
#include "sndfile.h"
#include "Sound.h"
#include "Sound_to_Pitch.h"
#include "Pitch_to_PitchTier.h"
#include "RealTier.h"
#include "PitchTier.h"
#include "Pitch.h"
#include "LongSound.h"
#include "melder.h"
#include "Manipulation.h"
#include "NUMmachar.h"
#include "AmplitudeTier.h"
// command line parsing
#include <tclap/CmdLine.h>
using namespace std;
vector<pair<double, double>> pointPairsFromCSVFilename(string path)
{
ifstream theFile(path);
string str;
string delim = ",";
vector<pair<double, double>> newPoints;
while (std::getline(theFile, str))
{
double time = stod(str.substr(0, str.find(delim)));
double value = stod(str.substr(str.find(delim) + 1));
newPoints.push_back(make_pair(time, value));
}
theFile.close();
return newPoints;
}
void replaceTierWithPoints(RealTier tier, vector <pair<double, double>> points)
{
Collection_removeAllItems(tier->points);
for (pair<double, double> p : points)
{
RealTier_addPoint(tier, p.first, p.second);
}
}
AmplitudeTier amplitudeTierFromPoints(vector <pair<double, double>> points)
{
double timeStart = 0.0;
double timeEnd = 0.0;
for (pair<double, double> p : points) {
if (p.second > timeEnd) {
timeEnd = p.second;
}
}
AmplitudeTier tier = AmplitudeTier_create(timeStart, timeEnd);
replaceTierWithPoints((RealTier)tier, points);
return tier;
}
void initPraat()
{
// these things generally need to be called to initialize Praat's numeric libraries.
// They may not actually all be needed, but it's a safe bet to include them.
NUMmachar ();
NUMinit ();
NUMrandom_init();
Melder_alloc_init ();
Melder_message_init ();
}
int main(int argc, const char *argv[])
{
initPraat();
try
{
TCLAP::CmdLine cmd("Praat speech resynthesis", ' ', "0.1");
TCLAP::ValueArg<string> audioArg("i", "input-audio", "Input audio filename", true, "", "path");
TCLAP::ValueArg<string> outAudioArg("o", "output-audio", "Output audio filename", true, "", "path");
TCLAP::ValueArg<string> pitchCSVArg("p", "pitch", "Pitch CSV", false, "", "path");
TCLAP::ValueArg<string> durationCSVArg("d", "duration", "Duration CSV", false, "", "path");
TCLAP::ValueArg<string> amplitudeCSVArg("a", "amplitude", "Amplitude multiplier CSV", false, "", "path");
cmd.add(audioArg);
cmd.add(outAudioArg);
cmd.add(pitchCSVArg);
cmd.add(durationCSVArg);
cmd.add(amplitudeCSVArg);
cmd.parse(argc, argv);
// load input audio file
size_t size = audioArg.getValue().length() + 1;
wchar_t path[size];
mbstowcs(path, audioArg.getValue().c_str(), size);
MelderFile mf;
structMelderFile file2 = { 0 };
Melder_relativePathToFile(path, &file2);
mf = &file2;
// create Praat Sound and Manipulation
Sound snd = Sound_readFromSoundFile(mf);
Manipulation manip = Sound_to_Manipulation(snd, 0.01, 75, 600);
// load pitch CSV if it exists
string pitchCSVPath = pitchCSVArg.getValue();
if (pitchCSVPath != "")
{
vector<pair<double, double>> newPitchPoints = pointPairsFromCSVFilename(pitchCSVPath);
replaceTierWithPoints((RealTier)manip->pitch, newPitchPoints);
}
// load duration CSV if it exists
string durationCSVPath = durationCSVArg.getValue();
if (durationCSVPath != "")
{
vector<pair<double, double>> newDurationPoints = pointPairsFromCSVFilename(durationCSVPath);
replaceTierWithPoints((RealTier)manip->duration, newDurationPoints);
}
// load output audio file
size = outAudioArg.getValue().length() + 1;
wchar_t outPath[size];
mbstowcs(outPath, outAudioArg.getValue().c_str(), size);
Sound snd2 = Manipulation_to_Sound(manip, Manipulation_OVERLAPADD);
// load amplitude CSV if it exists
string amplitudeCSVPath = amplitudeCSVArg.getValue();
if (amplitudeCSVPath != "")
{
vector<pair<double, double>> newAmplitudePoints = pointPairsFromCSVFilename(amplitudeCSVPath);
AmplitudeTier ampTier = amplitudeTierFromPoints(newAmplitudePoints);
Sound_AmplitudeTier_multiply_inline(snd2, ampTier);
}
// output the file
MelderFile outFile;
structMelderFile outFile2 = { 0 };
Melder_relativePathToFile(outPath, &outFile2);
outFile = &outFile2;
Sound_writeToAudioFile(snd2, outFile, Melder_WAV, 16);
}
catch (TCLAP::ArgException &e)
{
cerr << "error: " << e.error() << " for arg " << e.argId() << endl;
}
return 0;
}