Skip to content
Snippets Groups Projects
Commit 278fcbe8 authored by Peter Smit's avatar Peter Smit
Browse files

Make wav-copy accept both xspecifiers and xfilenames

In scripts such as perturb-speed and perturb-volume scp lines are
tranformed into piped command with the appropropriate sox command. The
case that the scp file has file offsets was not handled. This commit
both generalizes the wav-copy command to work also on xfilenames and
fixes the two perturb scripts to use this command in case of file
offsets.
parent 5b7fd74b
No related branches found
No related tags found
No related merge requests found
......@@ -38,8 +38,12 @@ scale_high = 2.0
for line in sys.stdin.readlines():
if len(line.strip()) == 0:
continue
# Handle three cases of rxfilenames appropriately; 'input piped command', 'file offset' and 'filename'
if line.strip()[-1] == '|':
print '{0} sox --vol {1} -t wav - -t wav - |'.format(line.strip(), random.uniform(scale_low, scale_high))
elif re.search(':[0-9]+$', line.strip()) is not None:
parts = line.split()
print '{id} wav-copy {wav} - | sox --vol {vol} -t wav - -t wav - |'.format(id = parts[0], wav=' '.join(parts[1:]), vol = random.uniform(scale_low, scale_high))
else:
parts = line.split()
print '{id} sox --vol {vol} -t wav {wav} -t wav - |'.format(id = parts[0], wav=' '.join(parts[1:]), vol = random.uniform(scale_low, scale_high))
......
......@@ -74,9 +74,11 @@ if [ -f $srcdir/segments ]; then
'{printf("%s %s %.2f %.2f\n", $1, $2, $3/factor, $4/factor);}' >$destdir/segments
utils/apply_map.pl -f 1 $destdir/reco_map <$srcdir/wav.scp | sed 's/| *$/ |/' | \
# Handle three cases of rxfilenames appropriately; "input piped command", "file offset" and "filename"
awk -v factor=$factor \
'{wid=$1; $1=""; if ($NF=="|") {print wid $_ " sox -t wav - -t wav - speed " factor " |"}
else {print wid " sox -t wav" $_ " -t wav - speed " factor " |"}}' > $destdir/wav.scp
else if (match($0, /:[0-9]+$/)) {print wid " wav-copy" $_ " - | sox -t wav - -t wav - speed " factor " |" }
else {print wid " sox -t wav" $_ " -t wav - speed " factor " |"}}' > $destdir/wav.scp
if [ -f $srcdir/reco2file_and_channel ]; then
utils/apply_map.pl -f 1 $destdir/reco_map <$srcdir/reco2file_and_channel >$destdir/reco2file_and_channel
fi
......@@ -85,8 +87,10 @@ if [ -f $srcdir/segments ]; then
else # no segments->wav indexed by utterance.
if [ -f $srcdir/wav.scp ]; then
utils/apply_map.pl -f 1 $destdir/utt_map <$srcdir/wav.scp | sed 's/| *$/ |/' | \
# Handle three cases of rxfilenames appropriately; "input piped command", "file offset" and "filename"
awk -v factor=$factor \
'{wid=$1; $1=""; if ($NF=="|") {print wid $_ " sox -t wav - -t wav - speed " factor " |"}
else if (match($0, /:[0-9]+$/)) {print wid " wav-copy" $_ " - | sox -t wav - -t wav - speed " factor " |" }
else {print wid " sox -t wav" $_ " -t wav - speed " factor " |"}}' > $destdir/wav.scp
fi
fi
......
// featbin/wav-copy.cc
// Copyright 2013-2014 Daniel Povey
// 2016 Aalto University (author: Peter Smit)
// See ../../COPYING for clarification regarding multiple authors
//
......@@ -26,12 +27,14 @@ int main(int argc, char *argv[]) {
try {
using namespace kaldi;
const char *usage =
"Copy archives of wave files\n"
"Copy wave file or archives of wave files\n"
"\n"
"Usage: wav-copy [options...] <wav-rspecifier> <wav-rspecifier>\n"
"Usage: wav-copy [options] <wav-rspecifier> <wav-wspecifier>\n"
" or: wav-copy [options] <wav-rxfilename> <wav-wxfilename>\n"
"e.g. wav-copy scp:wav.scp ark:-\n"
" wav-copy wav.ark:123456 -\n"
"See also: wav-to-duration extract-segments\n";
ParseOptions po(usage);
po.Read(argc, argv);
......@@ -41,20 +44,43 @@ int main(int argc, char *argv[]) {
exit(1);
}
std::string wav_rspecifier = po.GetArg(1),
wav_wspecifier = po.GetArg(2);
std::string wav_in_fn = po.GetArg(1),
wav_out_fn = po.GetArg(2);
bool in_is_rspecifier = (ClassifyRspecifier(wav_in_fn, NULL, NULL)
!= kNoRspecifier),
out_is_wspecifier = (ClassifyWspecifier(wav_out_fn, NULL, NULL, NULL)
!= kNoWspecifier);
if (in_is_rspecifier != out_is_wspecifier)
KALDI_ERR << "Cannot mix archives with regular files";
if (in_is_rspecifier) {
int32 num_done = 0;
int32 num_done = 0;
SequentialTableReader<WaveHolder> wav_reader(wav_rspecifier);
TableWriter<WaveHolder> wav_writer(wav_wspecifier);
SequentialTableReader<WaveHolder> wav_reader(wav_in_fn);
TableWriter<WaveHolder> wav_writer(wav_out_fn);
for (; !wav_reader.Done(); wav_reader.Next()) {
wav_writer.Write(wav_reader.Key(), wav_reader.Value());
num_done++;
for (; !wav_reader.Done(); wav_reader.Next()) {
wav_writer.Write(wav_reader.Key(), wav_reader.Value());
num_done++;
}
KALDI_LOG << "Copied " << num_done << " wave files";
return (num_done != 0 ? 0 : 1);
} else {
bool binary = true;
Input ki(wav_in_fn, &binary);
Output ko(wav_out_fn, binary, false);
WaveHolder wh;
if (!wh.Read(ki.Stream())) {
KALDI_ERR << "Read failure from "
<< PrintableRxfilename(wav_in_fn);
}
if (!WaveHolder::Write(ko.Stream(), true, wh.Value())) {
KALDI_ERR << "Write failure to "
<< PrintableWxfilename(wav_out_fn);
}
}
KALDI_LOG << "Copied " << num_done << " wave files";
return (num_done != 0 ? 0 : 1);
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment