Skip to content

Instantly share code, notes, and snippets.

Created June 13, 2011 10:44
Show Gist options
  • Save anonymous/1022588 to your computer and use it in GitHub Desktop.
Save anonymous/1022588 to your computer and use it in GitHub Desktop.
[Perl/Win32] vt_jpn Sample
#!/usr/bin/perl
#
# vt_jpn Sample
# Platform Win32 ActivePerl 5.10.1 Build 1007
# 著作権は主張しません。内容は保証しません。使用者の責任で使って下さい
# でも使う前についまるを買うのがお勧め
#
# 2011-06-13 0.01 動作確認。エラー処理・ゴミ処理もしてない
#
use strict;
use Win32::API;
use Win32::GUI();
use Win32::GUI::Constants qw( WM_USER );
use constant VT_FILE_API_FMT_S16PCM => 0;
use constant VT_FILE_API_FMT_ALAW => 1;
use constant VT_FILE_API_FMT_MULAW => 2;
use constant VT_FILE_API_FMT_DADPCM => 3;
use constant VT_FILE_API_FMT_S16PCM_WAVE => 4;
use constant VT_FILE_API_FMT_U08PCM_WAVE => 5;
# use constant VT_FILE_API_FMT_IMA_WAVE => 6; # not supported
use constant VT_FILE_API_FMT_ALAW_WAVE => 7;
use constant VT_FILE_API_FMT_MULAW_WAVE => 8;
use constant VT_FILE_API_FMT_MULAW_AU => 9;
my($Config) = {
VOICES => {
'haruka' => {
id => 6,
dir => 'synthesis/Haruka/'
},
'show' => {
id => 1,
dir => 'synthesis/Show/',
},
},
};
my($vt_jpn) = {
VT_LOADTTS_JPN => Win32::API->new('vt_jpn', 'VT_LOADTTS_JPN', 'NIPP', 'I'),
VT_TextToFile_JPN => Win32::API->new('vt_jpn', 'VT_TextToFile_JPN', 'IPPIIIIIII', 'I'),
VT_PLAYTTS_JPN => Win32::API->new('vt_jpn', 'VT_PLAYTTS_JPN', 'INPIIIIIII', 'I'),
VT_UNLOADTTS_JPN => Win32::API->new('vt_jpn', 'VT_UNLOADTTS_JPN', 'I', 'V'),
};
MAIN: {
my($text) = '吾輩は猫である。名前はまだ無い。どこで生れたかとんと見当がつかぬ。';
# my($accel);
my($window);
my($label);
# $accel = Win32::GUI::AcceleratorTable->new(
# "Esc" => sub {
# return(-1);
# },
# );
$window = Win32::GUI::Window->new(
-name => "Window",
-title => "VT_Test",
-post => [0, 0],
-size => [100, 60],
# -accel => $accel
);
# $label = $window->AddLabel(
# -text => "Stop if Push ESC",
# -top => 10,
# -left => 10,
# );
$window->Hook(
WM_USER,
sub {
my($self) = shift;
my($wParam) = shift;
my($lParam) = shift;
my($type) = shift;
my($msgCode) = shift;
printf(
"WM_USER: W[%i] L[%i] T[%i] M[%i]\n",
$wParam,
$lParam,
$type,
$msgCode
);
# 仕様書ではwParamで通知(1で再生開始、0で変換終了)となっているが挙動不明
# '1' とは True の意味?? ※最初に1~2回 0 が来るのを除いて
# 実動作をみて lParam で終了を判定
if ($lParam < 0) {
return(-1);
}
}
);
# ファイルに保存
&text2wave(
'show',
'test.wav',
$text
);
# サウンドカードで再生
&text2speech(
'haruka',
$text,
$window
);
exit(0);
};
# サウンドカードで再生
sub text2speech {
my($speaker) = shift; # 話者名
my($text) = shift; # 合成文字列
my($window) = shift; # Win32::Window(終了待機用)
my($voice);
my($ret);
if (length($text) < 1) {
return;
}
$voice = $Config->{VOICES}->{$speaker};
$ret = $vt_jpn->{VT_LOADTTS_JPN}->Call(
0,
$voice->{id},
$voice->{dir},
0
);
print "VT_LOADTTS_JPN: $ret\n";
print "Whnd: ", $window->{-handle}, "\n";
$ret = $vt_jpn->{VT_PLAYTTS_JPN}->Call(
$window->{-handle},
WM_USER,
$text,
$voice->{id},
105, # pitch 50-200%
120, # speed 50-400%
-1, # volume 0-500%
300, # pause 0-65,535ms def:687ms
-1, # dictidx
-1 # texttype(未使用)
);
print "VT_PLAYTTS_JPN: $ret\n";
#$window->Show();
Win32::GUI::Dialog();
$ret = $vt_jpn->{VT_UNLOADTTS_JPN}->Call($voice->{id});
print "VT_UNLOADTTS_JPN: $ret\n";
print "End.\n";
}
# ファイルに保存
sub text2wave {
my($speaker) = shift; # 話者名
my($outFile) = shift; # 出力ファイル名
my($text) = shift; # 合成文字列
my($voice);
my($ret);
$voice = $Config->{VOICES}->{$speaker};
$ret = $vt_jpn->{VT_LOADTTS_JPN}->Call(
0,
$voice->{id},
$voice->{dir},
0
);
print "VT_LOADTTS_JPN: $ret\n";
$ret = $vt_jpn->{VT_TextToFile_JPN}->Call(
VT_FILE_API_FMT_S16PCM_WAVE,
$text,
$outFile,
$voice->{id},
-1, # pitch 50-200%
-1, # speed 50-400%
-1, # volume 0-500%
300, # pause 0-65,535ms def:687ms
-1, # dictidx
-1 # texttype(未使用)
);
print "VT_TextToFile_JPN: $ret\n";
$ret = $vt_jpn->{VT_UNLOADTTS_JPN}->Call($voice->{id});
print "VT_UNLOADTTS_JPN: $ret\n";
print "End.\n";
}
exit(1);
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment