Skip to content

Instantly share code, notes, and snippets.

@aetos382
Last active February 6, 2016 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aetos382/434657cf4530815a8434 to your computer and use it in GitHub Desktop.
Save aetos382/434657cf4530815a8434 to your computer and use it in GitHub Desktop.
https://gist.github.com/aetos382/e737286380e99cfaf9e8 を無駄に関数化してみたっす
function Play-SpeechText {
[CmdletBinding(
DefaultParameterSetName = 'Default',
ConfirmImpact = [System.Management.Automation.ConfirmIMpact]::Medium)]
param(
[Parameter(Mandatory)]
[string] $ApiKey,
[Parameter(Mandatory, Position = 0)]
[string] $Text,
[ValidateSet('show', 'haruka', 'hikari', 'takeru', 'santa', 'bear')]
[string] $Speaker = 'show',
[ValidateSet('happiness', 'sadness', 'anger')]
[string] $Emotion,
[ValidateRange(1, 4)]
[int] $EmotionLevel = 2,
[ValidateRange(50, 200)]
[int] $Pitch = 100,
[ValidateRange(50, 400)]
[int] $Speed = 100,
[ValidateRange(50, 200)]
[int] $Volume = 100,
[Parameter(ParameterSetName = 'OutToFile', Mandatory)]
[string] $OutFile,
[Parameter(ParameterSetName = 'OutToFile')]
[ValidateSet('wav', 'ogg', 'aac')]
[string] $Format = 'wav')
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("${ApiKey}:"))
$headers = @{
Authorization = "Basic $token"
}
$body = @{
text = $Text
speaker = $Speaker
pitch = $Pitch
speed = $Speed
volume = $Volume
}
if ($PSBoundParameters.ContainsKey('Emotion')) {
$body.emotion = $Emotion
$body.emotion_level = $EmotionLevel
}
$params = @{}
$outToFile = $PSCmdlet.ParameterSetName -eq 'OutToFile'
if ($outToFile)
{
$params.OutFile = $OutFile
$body.format = $Format
}
$response = Invoke-WebRequest -Uri 'https://api.voicetext.jp/v1/tts' -Method Post -Body $body -Headers $headers @params
if (!$outToFile) {
$player = New-Object 'System.Media.SoundPlayer' $response.RawContentStream
$player.PlaySync()
$player.Dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment