line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Naver::TTS; |
2
|
|
|
|
|
|
|
$WebService::Naver::TTS::VERSION = 'v0.0.1'; |
3
|
2
|
|
|
2
|
|
27408
|
use utf8; |
|
2
|
|
|
|
|
15
|
|
|
2
|
|
|
|
|
9
|
|
4
|
2
|
|
|
2
|
|
50
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
40
|
|
5
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
42
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
989
|
use HTTP::Tiny; |
|
2
|
|
|
|
|
68621
|
|
|
2
|
|
|
|
|
71
|
|
8
|
2
|
|
|
2
|
|
1330
|
use Path::Tiny; |
|
2
|
|
|
|
|
18386
|
|
|
2
|
|
|
|
|
513
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=encoding utf8 |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
WebService::Naver::TTS - Perl interface to Naver TTS API |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $client = WebService::Naver::TTS->new(id => 'xxxx', secret => 'xxxx'); |
19
|
|
|
|
|
|
|
my $mp3 = $client->tts('안녕하세요'); # $mp3 is Path::Tiny object |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 METHODS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head2 new( id => $id, secret => $secret, \%options ) |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
L |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $client = WebService::Naver::TTS->new(id => $client_id, secret => $client_secret); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head3 \%options |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=over |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=item C |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
See L"speaker($speaker)"> |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item C |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Interger value between C<-5> and C<5>. |
40
|
|
|
|
|
|
|
C<0> is default. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
-5 ~ 5 사이 정수로 -5면 0.5배 빠른, 5면 0.5배 느린, 0이면 정상 속도의 목소리로 합성 |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=back |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub new { |
49
|
1
|
|
|
1
|
1
|
16
|
my ( $class, %args ) = @_; |
50
|
1
|
50
|
|
|
|
5
|
return unless $args{id}; |
51
|
0
|
0
|
|
|
|
|
return unless $args{secret}; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $self = { |
54
|
|
|
|
|
|
|
key => $args{id}, |
55
|
|
|
|
|
|
|
secret => $args{secret}, |
56
|
|
|
|
|
|
|
speaker => $args{speaker} || 'mijin', |
57
|
|
|
|
|
|
|
speed => $args{speed} // 0, |
58
|
|
|
|
|
|
|
http => HTTP::Tiny->new( |
59
|
|
|
|
|
|
|
default_headers => { |
60
|
|
|
|
|
|
|
agent => 'WebService::Naver::TTS - Perl interface to Naver TTS API', |
61
|
|
|
|
|
|
|
'X-Naver-Client-Id' => $args{id}, |
62
|
|
|
|
|
|
|
'X-Naver-Client-Secret' => $args{secret}, |
63
|
|
|
|
|
|
|
} |
64
|
0
|
|
0
|
|
|
|
), |
|
|
|
0
|
|
|
|
|
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
bless $self, $class; |
68
|
0
|
|
|
|
|
|
return $self; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head2 speaker($speaker) |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=over |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=item * |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
B 미진(한국어, 여성) - default |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=item * |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
B 진호(한국어, 남성) |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item * |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
B 클라라(영어, 여성) |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item * |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
B 매튜(영어, 남성) |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=item * |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
B 유리(일본어, 여성) |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item * |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
B 신지(일본어, 남성) |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=item * |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
B 메이메이(중국어, 여성) |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=back |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub speaker { |
108
|
0
|
|
|
0
|
1
|
|
my ( $self, $speaker ) = @_; |
109
|
0
|
0
|
|
|
|
|
return unless $speaker; |
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
$self->{speaker} = $speaker; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 tts($text) |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
my $mp3 = $client->tts('안녕하세요'); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
C<$mp3> is L obj. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
C<$mp3> is C if failed. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
our $URL = "https://openapi.naver.com/v1/voice/tts.bin"; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub tts { |
127
|
0
|
|
|
0
|
1
|
|
my ( $self, $text ) = @_; |
128
|
0
|
0
|
|
|
|
|
return unless $text; |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
my $res = $self->{http}->post_form( $URL, { speaker => $self->{speaker}, speed => $self->{speed}, text => $text } ); |
131
|
|
|
|
|
|
|
|
132
|
0
|
0
|
|
|
|
|
die "Failed to convert text($text) to speech file: $res->{reason}\n" unless $res->{success}; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
my $temp = Path::Tiny->tempfile; |
135
|
0
|
|
|
|
|
|
$temp->spew_raw( $res->{content} ); |
136
|
0
|
|
|
|
|
|
return $temp; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
__END__ |