summaryrefslogtreecommitdiffstats
path: root/talk/po2talk
blob: 46fe1fe69b1834c4ec5f036024495a4cdaaa9b87 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#! /usr/bin/perl

use Getopt::Long;

sub help;
sub read_po;
sub join_id;
sub new_tmp_file;
sub cleanup;

GetOptions(
  'help' => \&help,
  'compress' => \$opt_compress,
);

END { cleanup }     
$SIG{INT} = \&cleanup;
$SIG{TERM} = \&cleanup;

my @tmp_files;

$espeak = "espeak";

$magic = 0x692741e8;

help if @ARGV != 2;

$po = read_po $ARGV[0];

$tmp_txt = new_tmp_file;
$tmp_wav = new_tmp_file;
$tmp_snd = new_tmp_file;

for $msg (@{$po}) {
  $id = join_id $msg, 'id';
  $str = join_id $msg, 'str';

  next unless $id ne "";

  eval '$id = "' . $id . '"';
  eval '$str = "' . $str . '"';

  $str = $id if $str eq "";

  if(!$snd{$str}) {
    open W, ">$tmp_txt";
    print W $str;
    close W;

    $punc = length($id) == 1 ? "--punc" : "";

    system "$espeak $punc -f $tmp_txt -w $tmp_wav";
    system "sox $tmp_wav -b -u -c 1 -r 16000 -t .wav $tmp_snd";
    if($opt_compress) {
      system "./sc $tmp_snd $tmp_snd";
    }

    open F, $tmp_snd;
    sysread F, $snd_buf, -s $tmp_snd;
    close F;

    $snd{$str} = $snd_buf;
  }

  $snd_id{$id} = $str;
}

$file_ofs = 0;
@snds = sort keys %snd;
@snd_ids = sort keys %snd_id;

for $snd_id (@snd_ids) {
  $txt_ofs{$snd_id} = $file_ofs;
  $file_ofs += length($snd_id) + 1;
}

for $snd (@snds) {
  $snd_ofs{$snd} = $file_ofs;
  $file_ofs += length($snd{$snd});
}

$head_size = 8 + 8 * @snd_ids;

open W, ">$ARGV[1]";

print W pack("VV", $magic, scalar(@snd_ids));
for $snd_id (@snd_ids) {
  print W pack("VV", ($txt_ofs{$snd_id} + $head_size, $snd_ofs{$snd_id{$snd_id}} + $head_size));
}
for $snd_id (@snd_ids) {
  print W $snd_id, "\x00";
}
for $snd (@snds) {
  print W $snd{$snd};
}

close W;


sub help
{
  print STDERR
  "Usage: po2talk [options] po_file talk_file\n" .
  "Run po file through espeak.\n";

  exit 0;
}


sub read_po
{
  local $_;
  my ($msg, $cnt, $id, $f);

  $cnt = 0;

  open F, $_[0];
  while(<F>) {
    s/^\s*|\s*$//g;
    next if $_ eq "";
    if(/^#,/) {
      s/^#,\s*//;
      for $f (split /\s*,\s*/) {
        ${$msg->[$cnt]{flags}{$f}} = 1 if $f ne "";
      }
      next
    }
    if(/^#(\s|$)/) { push @{$msg->[$cnt]{u_comment}}, $_ ; next }
    if(/^#/) { push @{$msg->[$cnt]{a_comment}}, $_; next }
    if(s/^msg(id\b|id_plural\b|str(\[\d+\])?)\s*//) {
      $id = $1;
      if($id eq 'id') {
        $msg->[$cnt]{line} = $. unless exists $msg->[$cnt]{$line};
        $cnt++;
      }
    }
    if($cnt && /^"(.*)"$/) {
      push @{$msg->[$cnt - 1]{$id}}, $1;
    }
    else {
      print STDERR "$_[0]:$.: invalid po format\n";
      return undef;
    }
  }
  close F;

  # trailing comments
  pop @$msg if @$msg > 0 && !$msg->[-1]{id};

  return $msg;
}


sub join_id
{
  my ($msg, $id);

  ($msg, $id) = @_;

  return join '', @{$msg->{$id}};
}


# create new temporary file
sub new_tmp_file
{
  local $_;

  chomp ($_ = `mktemp /tmp/po2talk.XXXXXXXXXX`);
  die "error: mktemp failed\n" if $?;

  push @tmp_files, $_;

  return $_;
}


# remove temporary files
sub cleanup
{
  local $_;

  for (@tmp_files) {
    next unless defined $_;
    unlink;
    $_ = undef;
  }

  undef @tmp_files;
}