line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Naver::TTS; |
2
|
|
|
|
|
|
|
$WebService::Naver::TTS::VERSION = 'v0.0.3'; |
3
|
2
|
|
|
2
|
|
118347
|
use utf8; |
|
2
|
|
|
|
|
29
|
|
|
2
|
|
|
|
|
16
|
|
4
|
2
|
|
|
2
|
|
77
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
52
|
|
5
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
69
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
1640
|
use HTTP::Tiny; |
|
2
|
|
|
|
|
110352
|
|
|
2
|
|
|
|
|
99
|
|
8
|
2
|
|
|
2
|
|
1749
|
use Path::Tiny; |
|
2
|
|
|
|
|
31510
|
|
|
2
|
|
|
|
|
812
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=encoding utf8 |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
WebService::Naver::TTS - Perl interface to Naver TTS API |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Clova Speech Synthesis(CSS) |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $client = WebService::Naver::TTS->new(id => 'xxxx', secret => 'xxxx'); |
21
|
|
|
|
|
|
|
my $mp3 = $client->tts('안녕하세요'); # $mp3 is Path::Tiny object |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 METHODS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head2 new( id => $id, secret => $secret, \%options ) |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
L |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $client = WebService::Naver::TTS->new(id => $client_id, secret => $client_secret); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head3 \%options |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=over |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=item C |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
See L"speaker($speaker)"> |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=item C |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Interger value between C<-5> and C<5>. |
42
|
|
|
|
|
|
|
C<0> is default. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
-5 ~ 5 사이 정수로 -5면 0.5배 빠른, 5면 0.5배 느린, 0이면 정상 속도의 목소리로 합성 |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=back |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub new { |
51
|
1
|
|
|
1
|
1
|
173
|
my ( $class, %args ) = @_; |
52
|
1
|
50
|
|
|
|
7
|
return unless $args{id}; |
53
|
0
|
0
|
|
|
|
|
return unless $args{secret}; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $self = { |
56
|
|
|
|
|
|
|
key => $args{id}, |
57
|
|
|
|
|
|
|
secret => $args{secret}, |
58
|
|
|
|
|
|
|
speaker => $args{speaker} || 'mijin', |
59
|
|
|
|
|
|
|
speed => $args{speed} // 0, |
60
|
|
|
|
|
|
|
http => HTTP::Tiny->new( |
61
|
|
|
|
|
|
|
default_headers => { |
62
|
|
|
|
|
|
|
agent => 'WebService::Naver::TTS - Perl interface to Naver Clova Speech Synthesis API', |
63
|
|
|
|
|
|
|
'X-NCP-APIGW-API-KEY-ID' => $args{id}, |
64
|
|
|
|
|
|
|
'X-NCP-APIGW-API-KEY' => $args{secret}, |
65
|
|
|
|
|
|
|
} |
66
|
0
|
|
0
|
|
|
|
), |
|
|
|
0
|
|
|
|
|
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
bless $self, $class; |
70
|
0
|
|
|
|
|
|
return $self; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 speaker($speaker) |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=over |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=item * |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
B 미진(한국어, 여성) - default |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item * |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
B 진호(한국어, 남성) |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item * |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
B 클라라(영어, 여성) |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=item * |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
B 매튜(영어, 남성) |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item * |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
B 유리(일본어, 여성) |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item * |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
B 신지(일본어, 남성) |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=item * |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
B 메이메이(중국어, 여성) |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item * |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
B 중국어, 남성 |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item * |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
B 스페인어, 남성 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item * |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
B 스페인어, 여성 |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=back |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub speaker { |
122
|
0
|
|
|
0
|
1
|
|
my ( $self, $speaker ) = @_; |
123
|
0
|
0
|
|
|
|
|
return unless $speaker; |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
$self->{speaker} = $speaker; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 tts($text, %tmp_opts?) |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $mp3 = $client->tts('안녕하세요'); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
C<$text> 음성 합성할 문장. UTF-8 인코딩된 텍스트만 지원합니다. CSS API 는 최대 5000 자의 텍스트까지 음성 합성을 지원합니다. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
C<$mp3> is L obj. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
C<$mp3> is C if failed. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
C<%tmp_opts> is L options. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=over |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item * |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
DIR => $dir |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item * |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
SUFFIX => '.dir' |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
TMPDIR => 1 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
default is C<1> |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=back |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
our $URL = "https://naveropenapi.apigw.ntruss.com/voice/v1/tts"; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub tts { |
163
|
0
|
|
|
0
|
1
|
|
my ( $self, $text, %tmp_opts ) = @_; |
164
|
0
|
0
|
|
|
|
|
return unless $text; |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
my $res = $self->{http}->post_form( $URL, { speaker => $self->{speaker}, speed => $self->{speed}, text => $text } ); |
167
|
|
|
|
|
|
|
|
168
|
0
|
0
|
|
|
|
|
die "Failed to convert text($text) to speech file: $res->{reason}\n\n$res->{content}\n" unless $res->{success}; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my $temp = Path::Tiny->tempfile(%tmp_opts); |
171
|
0
|
|
|
|
|
|
$temp->spew_raw( $res->{content} ); |
172
|
0
|
|
|
|
|
|
return $temp; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
__END__ |