Skip to content

Instantly share code, notes, and snippets.

@shigemur
Created December 1, 2014 06:58
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 shigemur/7304945bb938c41741f9 to your computer and use it in GitHub Desktop.
Save shigemur/7304945bb938c41741f9 to your computer and use it in GitHub Desktop.
ColdFusion から VoiceText Web API をHTTPリクエストし、音声合成ファイルを作成します (4行目のapi_key変数に取得したAPIキーを指定して下さい)。
<cfprocessingdirective pageencoding="utf-8" />
<cfscript>
//APIキーを指定
api_key="ここに取得したAPIキーを入力してください";
//入力フォーム用パラメーター
qSpeaker = QueryNew('val,disp', 'varchar,varchar',[["show:0","ショウ(男性)"],["haruka:1","ハルカ(女性)"],["hikari:1","ヒカリ(女性)"],["takeru:1","タケル(男性)"],["santa:1","サンタクロース"],["bear:1","凶暴なクマ"]]); //スピーカー(val 話者名:感情有効(1)無効(0), disp:説明)
qEmotion = QueryNew('val,disp', 'varchar,varchar',[["happiness","喜"],["anger","怒"],["sadness","悲"]]); //感情カテゴリ(val:感情, disp:説明)
qEmotion_level = QueryNew('val,disp', 'integer,varchar',[[1,""],[2,"高"]]); //感情レベル(val:感情, disp:説明)
lPitch="50,200,100"; //音の高低(最小, 最大, 初期値)
lSpeed="50,400,100"; // 話す速度(最小, 最大, 初期値)
lVolume="50,200,100"; // 音量(最小, 最大, 初期値)
</cfscript>
<!--- 入力フォーム --->
<cfform preservedata="true">
<cftextarea name="ipt_text" cols="40" rows="5">ここにテキストを入力します(200文字以内)</cftextarea><br>
話者選択:<cfselect name="speaker" query="qSpeaker" value="val" display="disp" /><br>
感情選択:<cfselect name="emotion" query="qEmotion" value="val" display="disp" queryPosition="below" ><option value="">未選択</option></cfselect>
感情レベル:<cfselect name="emotion_level" query="qEmotion_level" value="val" display="disp" /> (ハルカ、ヒカリ、タケル、サンタクロース、凶暴なクマ のみ有効)<br>
音の高低:<cfinput type="text" name="pitch" size="3" required="true" range="#ListGetAt(lPitch,1)#,#ListGetAt(lPitch,2)#" value="#ListGetAt(lPitch,3)#" validate="integer" > (50~200% 整数のみ)<br>
話す速度:<cfinput type="text" name="speed" size="3" required="true" range="#ListGetAt(lSpeed,1)#,#ListGetAt(lSpeed,2)#" value="#ListGetAt(lSpeed,3)#" validate="integer" > (50~400% 整数のみ)<br>
音量:<cfinput type="text" name="volume" size="3" required="true" range="#ListGetAt(lVolume,1)#,#ListGetAt(lVolume,2)#" value="#ListGetAt(lVolume,3)#" validate="integer" > (50~200% 整数のみ)<br>
<cfinput type="submit" name="submit" value="音声合成ファイルの作成">
</cfform>
<!--- 音声合成・ダウンロード処理 --->
<cfif IsDefined("Form.submit")>
<cfset tempvfsDirName = "cfhttp_result">
<cfset tempFile = CreateUUID()>
<cfif not DirectoryExists("ram:///#tempvfsDirName#")>
<cfdirectory action="create" directory="ram:///#tempvfsDirName#">
</cfif>
<cfhttp url="https://api.voicetext.jp/v1/tts" username="#api_key#" password="" method="post" path="ram:///#tempvfsDirName#" file="#tempFile#">
<cfhttpparam type="formfield" name="text" value="#Form.ipt_text#">
<cfhttpparam type="formfield" name="speaker" value="#ListFirst(Form.speaker,':')#">
<cfif ListLast(Form.speaker, ':') AND Form.emotion NEQ "">
<cfhttpparam type="formfield" name="emotion" value="#Form.emotion#">
<cfhttpparam type="formfield" name="emotion_level" value="#Form.emotion_level#">
</cfif>
<cfhttpparam type="formfield" name="pitch" value="#Form.pitch#">
<cfhttpparam type="formfield" name="speed" value="#Form.speed#">
<cfhttpparam type="formfield" name="volume" value="#Form.volume#">
</cfhttp>
<cfif cfhttp.ResponseHeader.Status_Code EQ "200">
<cfheader name="Content-Disposition" value="attachment; filename=createdVoice.wav"/>
<cfcontent type="audio/wav" file="ram:///#tempvfsDirName#/#tempFile#" deletefile="true"/>
<cfelse>
音声合成処理でエラーが発生しました
</cfif>
</cfif>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment