Last active
August 29, 2015 14:04
-
-
Save drilldev/65fa132940b7d0e1077a to your computer and use it in GitHub Desktop.
Powershellから VoiceText Web APIをたたいて、しゃべらせるスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
VoiceText Web APIを利用してしゃべる。(https://cloud.voicetext.jp/webapi) | |
.DESCRIPTION | |
Invoke-RestMethod を使って、.wavファイルデータを取得して再生する。 | |
.EXAMPLE | |
posh-VoiceText.ps1 -text "たけやぶやけた" -speaker show | |
# License : MIT | |
# http://mollifier.mit-license.org/ | |
#> | |
[CmdletBinding()] | |
Param( | |
[parameter(Mandatory=$true,ValueFromPipeline=$True, | |
HelpMessage="【必須】合成するテキスト。200文字以内")] | |
[ValidateLength(1, 200)] | |
[String]$text | |
,[parameter( | |
HelpMessage="【必須】話者名。いずれかを指定。show (男性),haruka (女性),hikari (女性),takeru (男性), santa (サンタクロース),bear (凶暴なクマ)")] | |
[ValidateSet('show', 'haruka','hikari', 'takeru', 'santa', 'bear')] | |
[String]$speaker='show' | |
,[parameter( | |
HelpMessage="感情カテゴリ。いずれかを指定。happiness 喜,anger 怒,sadness 悲")] | |
[ValidateSet('happiness', 'anger','sadness')] | |
[String]$emotion | |
,[parameter( | |
HelpMessage="感情レベル。1~2で数値が大きいほど感情が強くなる")] | |
[ValidateRange(1,2)] | |
[Int]$emotion_level | |
,[parameter(HelpMessage="音の高低")] | |
[ValidateRange(50, 200)] | |
[Int]$pitch | |
,[parameter(HelpMessage="話す速度")] | |
[ValidateRange(50, 400)] | |
[Int]$speed | |
,[parameter(HelpMessage="音量")] | |
[ValidateRange(50, 200)] | |
[Int]$volume | |
,[parameter( | |
HelpMessage="出力するWaveファイル名")] | |
[String]$OutFile | |
,[parameter(HelpMessage="再生しません")] | |
[switch]$NoPlay | |
,[parameter(HelpMessage="テキストを返します")] | |
[switch]$PassThru | |
) | |
Function base64_encode { | |
param($str) | |
$byte = [Text.Encoding]::UTF8.GetBytes( $str ) | |
return [Convert]::ToBase64String( $byte ) | |
} | |
Function msPlayerSync { | |
param($wavfile) | |
$ms = New-Object Media.SoundPlayer( (Resolve-Path $wavfile) ) | |
$ms.PlaySync() | |
} | |
# main | |
if($Env:VOICETEXT_API_KEY -eq $NULL){ # APIキーがセットされてない | |
$apikey = Read-Host "YOUR_API_KEY" | |
$Env:VOICETEXT_API_KEY = $apikey | |
} | |
$endpoint = "https://api.voicetext.jp/v1/tts" | |
$hdr = @{} | |
$hdr["Authorization"] = "Basic " + (base64_encode ( $Env:VOICETEXT_API_KEY + ":" )) | |
$rest_body = @{} | |
$rest_body["text"] = $text | |
$rest_body["speaker"] = $speaker | |
if ($pitch -ne 0){ $rest_body["pitch"] = $pitch } | |
if ($speed -ne 0){ $rest_body["speed"] = $speed } | |
if ($volume -ne 0){ $rest_body["volume"] = $volume } | |
if ($emotion -ne ""){ $rest_body["emotion"] = $emotion } | |
if ($emotion_level -ne 0){ $rest_body["emotion_level"] = $emotion_level } | |
#Write-Debug (ConvertTo-Json $rest_body ) | |
if($OutFile -eq "") { | |
$wavfile = [IO.Path]::GetTempFileName() + ".wav" | |
}else{ | |
$wavfile = $OutFile | |
} | |
$r = Invoke-RestMethod -Uri $endpoint -Method POST -Headers $hdr ` | |
-BODY $rest_body -OutFile $wavfile | |
if($r -eq $NULL){ | |
# ResponseCode -eq 200 | |
if(-not ($NoPlay)){ | |
msPlayerSync $wavfile|Out-Null | |
} | |
if($OutFile -eq "") { | |
del $wavfile | |
} | |
}else{ | |
# ResponseCode -ne 200 not return wav file. | |
Write-Debug (ConvertTo-Json $r ) | |
} | |
if ($PassThru) { | |
return $text | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment