| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::Naver::TTS; | 
| 2 |  |  |  |  |  |  | $WebService::Naver::TTS::VERSION = 'v0.0.2'; | 
| 3 | 2 |  |  | 2 |  | 65261 | use utf8; | 
|  | 2 |  |  |  |  | 27 |  | 
|  | 2 |  |  |  |  | 13 |  | 
| 4 | 2 |  |  | 2 |  | 59 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 33 |  | 
| 5 | 2 |  |  | 2 |  | 7 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 47 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 951 | use HTTP::Tiny; | 
|  | 2 |  |  |  |  | 84035 |  | 
|  | 2 |  |  |  |  | 95 |  | 
| 8 | 2 |  |  | 2 |  | 1288 | use Path::Tiny; | 
|  | 2 |  |  |  |  | 25035 |  | 
|  | 2 |  |  |  |  | 763 |  | 
| 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 | 81 | 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, %tmp_opts?) | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | my $mp3 = $client->tts('안녕하세요'); | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | C<$mp3> is L obj. | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | C<$mp3> is C if failed. | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | C<%tmp_opts> is L options. | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =over | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =item * | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | DIR => $dir | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =item * | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | SUFFIX => '.dir' | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =item * | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | TMPDIR => 1 | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | default is C<1> | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =back | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =cut | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | our $URL = "https://openapi.naver.com/v1/voice/tts.bin"; | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | sub tts { | 
| 147 | 0 |  |  | 0 | 1 |  | my ( $self, $text, %tmp_opts ) = @_; | 
| 148 | 0 | 0 |  |  |  |  | return unless $text; | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 0 |  |  |  |  |  | my $res = $self->{http}->post_form( $URL, { speaker => $self->{speaker}, speed => $self->{speed}, text => $text } ); | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 0 | 0 |  |  |  |  | die "Failed to convert text($text) to speech file: $res->{reason}\n" unless $res->{success}; | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 0 |  |  |  |  |  | my $temp = Path::Tiny->tempfile(%tmp_opts); | 
| 155 | 0 |  |  |  |  |  | $temp->spew_raw( $res->{content} ); | 
| 156 | 0 |  |  |  |  |  | return $temp; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | __END__ |