Praat Script の書き方 フォルマントやピッチの計測に Script を用いると、計測点をマークした後に、自 動的に計測できる。 1. 変数とは、電卓で計算した値をメモリーに一時記憶するのと同じように、 計算の結果を保存しておく場所である。この変数は、中学生で習った数学 のxやyに当たる。しかし、xやyは、数式の右辺にも左辺にも使えるが、 コンピューターのプログラムでは、x= …というように使う。Praat Script で、 数式以外の時で使う時には、引用符で囲まなければならない。また、変数 の名前は英小文字から始めなければならない。
例:time_middle = (end_time – start_time ) /2 引用符不要 Get value at time... 1 'start_time' Hertz Linear 引用符必要
2. まず、普通に計測する方法から説明する。Praat の Objects で、スピーチ (例として、wav ファイルを使うが、aiff ファイルでも良い)を読み込む。 (Read from file …) 次に TextGrid を作る。TextGrid は、Interval Tier と Point Tier がある。(Tier は層)New ¥ Create Text Grid とすると、新ウィンドウ が開き、All tier names: Mary John bell, Which of these are point tiers?: bell と書 かれている。そのまま、OK をクリックすると、Mary と John という Interval Tier に、bell という Point Tier のある TextGrid が作成され、名前が、
Mary_John_bell となる。名前は、Rename…をクリックすると変えられる。 Interval Tier は、境界線を示し、Point Tier は、計測点などを示す。TextGrid の構成や名前は、目的に合わせて決める。ここでは、Interval Tier と Point Tier がそれぞれ1つの TextGrid を作成し、名前を wav ファイルと同じにする。
3. wav ファイルと TextGrid ファイル(Shift または、Ctrl を押しながら) を選択した後に、Scale Time をクリックする。TextGrid の元の長さは、1 秒だが、ScaleTime をクリックすることにより、wav ファイルと同じ長さに 調整することができる。その後、TextGrid に Boundary (境界線)やポイン トを入れる。また、境界線と境界線の間に、音素などの情報も入れられる。 Wav ファイルと TextGrid ファイルを1つに Collection ファイルとして保存 することもでき、また、別々に保存することもできる。
4. フォルマントの計測をする。まず、wav ファイル(Sound ファイル)を選 択し、Formants & LPC- ¥ To Formant (Burg)…をクリックすると、新しいウィ ンドウが開き、OK をクリックすると、Praat Objects のウィンドウに wav フ ァイルと同じ名前の Formant が現れる。Formant を選択し、Query ¥ Get value at time…をクリックすると、ある時間のフォルマントを計測できる。
5. Praat には、ここまで行ったことが、記録されている。Praat ¥ New Praat script で、untitled script が現れ、Edit ¥ Paste history をすると、次のように書 かれている。
Read from file... E:¥test¥c1.wav select Sound c1
To Formant (burg)... 0 5 5000 0.025 50 Get value at time... 1 0.5 Hertz Linear
(フォルマントの三番目の項目は、女性は、5500、男性は、5000 を一般的 に用いる。最初の1は、第1フォルマントを、0.5 は、時間が 0.5 秒である ことを示す。)
この Paste history を使うと script が比較的、簡単に書ける。
6. フォルマントの値を計測する時の時間は、TextGrid の interval の 中心や point で決めた点で計測することが多い。そこで、TextGrid の Interval の開始時間や終了時間、又は、point の示す時間を調べる。この時に、まず、 Praat Control で、Query で、時間を尋ねる。その後、Paste history を使うと、 次のようになる。
select TextGrid c1 Get start point... 1 2 Get end point... 1 2 Get time of point... 2 1
最初の行で TextGrid を select(選択)している。この選択を忘れるとエラー の原因となる。次の行の 1 は、1番目の Interval Tier で、2 は2番目の Interval を指す。4 行目の 2 は、2番目の Point Tier を指し、次の 1 は、最初の point を意味する。
その後、変数の名前を決める。最初の Tier の2番目の interval の開始時間 を time_start、終了時間を time_end 、2番目の Point Tier の最初の point の 時間を time_point とすると、次のようになる。
select TextGrid c1
time_end = Get end point... 1 2 time_point = Get time of point... 2 1
次に、interval の中央の時間を time_center とすると、 time_center = (time_start + time_end) /2
となる。。
ここで、interval の中央や、point のフォルマントを計測する。それぞれのフ ォルマントを f1_center、 f2_center、 f1_point、 f2_point とすると、 select Formant c4
f1_center = Get value at time... 1 ‘time_center’ Hertz Linear f2_center = Get value at time... 2 ‘time_center’ Hertz Linear f1_point = Get value at time... 1 ‘time_point’ Hertz Linear f2_point = Get value at time... 1 ‘time_point’ Hertz Linear
となる。これで、1つのファイルのフォルマントが計測できる。Query に あるコマンドを使うとある区間内の最大値や最小値も計測できる。また、 TextGrid に記入した内容(音素)なども、知ることができる。(Get label)
7. 次にあるディレクトリ(ファイルフォルダー)のすべての値を一度に計
測する方法に入る。まず、ディレクトリーにあるすべての wav ファイルの 名前を調べる。そのために、New ¥ Create Strings as file list をクリックする。
そうすると、Praat Objects に Strings fileList が現れるが、そのリストには、 wav ファイルのみの名前が記されている。まず、wav ファイルの数をしら べ、numberOfFiles という変数にする。
Create Strings as file list... fileList E:¥test¥*.wav select Strings fileList
numberOfFiles = Get number of strings
8. 次に、順番にファイルを読み込んでいくのだが、ここでループ(繰り返
し)を使うが、次のような1組のコマンドを使う。 for i to numberOfFiles
endfor
この空白部分で、色々なことを行う。
この i は、1から、ディレクトリーにあるファイルの数まで、変化するの で、次のような script で、順番にファイルを読みこむ。一般にループの中の 行は、下げる。
Create Strings as file list... flieList E:¥test¥*.wav select Strings fileList
numberOfFiles = Get number of strings for i to numberOfFiles
select Strings fileList file_name$ = Get string... i
Read from file... E:¥test¥'file_name$' endfor
ここで、文字型(string)変数、つまり、記憶する内容が、アルファベット で表わされる変数の名前は、$を最後につけなければならない。この script を実行すると、wav ファイルのみを読み込む。
9. 次に TextGrid ファイルを読み込むために、script を修整する。
Read from file… E:¥test¥’file_name$’ を実行した後には、wav ファイルが選 択されている。そこで、name$ = selected $(“Sound”) とすると、現在、選択 されたファイルの “wav”部分のない名前が name$ となる。そして、その後 ろに “TextGrid”を付け加えると TextGrid ファイルを読み込む。
Create Strings as file list... list E:¥test¥*.wav select Strings list
for i to numberOfFiles select Strings list
file_name$ = Get string... i
Read from file... E:¥test¥'file_name$' name$= selected$("Sound")
Read from file... E:¥test¥'name$'.TextGrid endfor
これで、wav と TextGrid の両方を読み込むことができる。
10. 5で行ったように、Sound ファイルから Formant ファイルを作るが、 最初に Sound ファイルを選択する。その後、Formant ファイルをつくる。 select Sound 'name$'
To Formant (burg)... 0 5 5000 0.025 50
この Formant ファイルの名前は、Sound ファイルと同じである。次に6で 行ったように、時間を決める。そして、フォルマントを計測する。
for i to numberOfFiles select Strings list
file_name$ = Get string... i
Read from file... E:¥test¥'file_name$' name$= selected$("Sound")
Read from file... E:¥test¥'name$'.TextGrid select Sound 'name$'
To Formant (burg)... 0 5 5000 0.025 50 select TextGrid 'name$'
time_point = Get time of point... 2 1 select Formant 'name$'
f1_center = Get value at time... 1 'time_center' Hertz Linear f2_center = Get value at time... 2 'time_center' Hertz Linear f1_point = Get value at time... 1 'time_point' Hertz Linear f2_point = Get value at time... 1 'time_point' Hertz Linear endfor
これで、フォルマントの計測ができたが、音声のない部分や TextGrid の範 囲以外のところで、フォルマントの計測を行うとエラーとなる。その場合 のために、下記のようなコマンドを入れることもある。
if (f1_point = undefined) | (f2_point = undefined) echo error: check 'name$'
endif
ここで、“echo”は、info ウィンドウに残すメッセージである。このメッセー ジでどのファイルが問題なのかチェックを行えるので便利である。
なお、ファイルが多い場合、メモリーが足りなくなる恐れがあるので、ル ープを終わる前に、Sound などを除去しておいた方が、よい。
select Sound 'name$' Remove
select TextGrid 'name$' Remove
select Formant 'name$' Remove endfor 11. データの入っているディレクトリー(フォルダー)や TextGrid の層のよ うによく変更する項目は、使用者に入力をしてもらうようにすると便利で ある。ここで、text は、空白のない文字型変数、integer は整数型変数のこ とである。他のタイプもある。(Help Scripting 6.1 参照)
form Measuring Formants text directory E:¥test integer tier_number 2 integer num_point 1
integer max_formant 5000 endform 整数型の変数は、'tier_number'のように、使用できるが、文字型変数には、“$” を最後に加え、'directory$'として、使用する。 12. 計測したフォルマントの値を保存するのに、info 又は Table を使う。 info を使う場合 ループの前に info を初期化した後に、printline で、書き込んでいく。各項目 の間にタブを入れる。 clearinfo
printline 'name$' 'tab$' 'f1_point' 'tab$'' f2_point' 結果は、次のようになる。 c1 358.60200552080215 358.60200552080215 c2 912.8032046621003 912.8032046621003 この結果は、info に表示されているので、コピーして、メモ帳に貼り付け て用いる。 13. 表(Table)を使うやり方は次のようである。ループの前に、表 の項目(列)について決める。 上記のように、ファイルの名前、F1 、F2 だけの場合は、3列、行は、ファ イルの数にする。次に列(項目)の名前をつける。この例では、表の名前 として、formant_table、項目の名前として、file_name、f1、f2 とする。 Create Table... formant_table numberOfFiles 3
Set column label (index)... 1 file_name Set column label (index)... 2 f1 Set column label (index)... 3 f2
これで、表ができた。次にループの中で、表にフォルマントの値などを記 入する。
select Table formant_table
Set string value... i file_name 'name$' Set numeric value... i f1 'f1_point' Set numeric value... i f2 'f2_point'
最後にループの終了後、Excel ファイルと保存する。 Write to text file... E:¥test¥formant_table.xls
この表は、エクセルファイルとして保存されているので、そのまま、分析 に用いることができる。結果は、次のようである。 なお、多数の interval (区間)のフォルマントなどを計測するときは、二重 のループを用いる。 14. Script を書く時に文末に不要な空白がないように気をつける必 要がある。空白があると、エラーとなる。また、新しい script を実行してい るときに、エラーが起きることは、よくある。 そのようなときに、どこま で、実行したか、変数の値が知りたいときに、echo は、便利である。 例えば、現在、何番目のループかを知りたいとき echo loop : 'i' というコマ ンドを挿入しておく。また、フォルマントの計測の部分で、echo measuring formant というように書いておくと、 エラーが起きた時に、それ以降の部 分のチェックを行えばよいことがわかる。また、#を使って日本語のコメン トは、入れることができる。 15. Script の例 Info ウィンドウを使った script # ディレクトリーなどの情報をインプット form Measuring Formants
text directory E:¥test integer tier_number 2 integer point_number 1 integer max_formant 5000 endform # info ウィンドウを刷新。ディレクトリーの wav ファイルのリストを作成 clearinfo
Create Strings as file list... list 'directory$'¥*.wav # ディレクトリーにあるファイルの数を調べる。 select Strings list
numberOfFiles = Get number of strings
# ループ リストに従って、wav ファイルを読み込む。
# 次に、拡張子を除いた名前を name$とする。次に TextGrid を読み込む for i to numberOfFiles
select Strings list
file_name$ = Get string... i
Read from file... 'directory$'¥'file_name$' name$= selected$("Sound")
# TextGrid を読み込み、point の時間情報を入手 Read from file... 'directory$'¥'name$'.TextGrid
time_point = Get time of point... 'tier_number' 'point_number'
# Formant ファイルを作成、その後、point におけるフォルマントを計測 select Sound 'name$'
To Formant (burg)... 0 5 max_formant 0.025 50
f1_point = Get value at time... 1 'time_point' Hertz Linear f2_point = Get value at time... 1 'time_point' Hertz Linear
# フォルマントの値が計測できない場合、エラーメッセージを出す if (f1_point = undefined) | (f2_point = undefined)| (time_start = undefined) echo error: check 'name$' F1: ‘f1_point’ F2: ‘f2_point’
endif
# info ウィンドウにフォルマントを書き込む printline 'name$' 'tab$' 'f1_point' 'tab$' 'f2_point'
# 使用済みのファイルを除去 select Sound 'name$' Remove
select TextGrid 'name$' Remove
select Formant 'name$' Remove
endfor
16. 表を使った Praat Script
# ディレクトリーなどの情報をインプット form Measuring Formants
text directory E:¥test integer tier_number 2 integer point_number 1 integer max_formant 5000 endform
# ディレクトリーのファイルのリストを作成、ファイル数を調べる。 Create Strings as file list... list 'directory$'¥*.wav
select Strings list
numberOfFiles = Get number of strings
# 表の作成 ファイル数の行で、3列の表とする。項目名を決定 Create Table without column names... formant_table numberOfFiles 3 Set column label (index)... 1 file_name
Set column label (index)... 2 f1 Set column label (index)... 3 f2
# ループ
for i to numberOfFiles select Strings list
file_name$ = Get string... i
Read from file... 'directory$'¥'file_name$' name$= selected$("Sound")
select Sound 'name$'
To Formant (burg)... 0 5 'max_formant' 0.025 50
# TextGrid を読み込み、point の時間を調べ、フォルマントを調べる。 Read from file... 'directory$'¥'name$'.TextGrid
time_point = Get time of point... 'tier_number' 'point_number'
select Formant 'name$'
f1_point = Get value at time... 1 'time_point' Hertz Linear f2_point = Get value at time... 2 'time_point' Hertz Linear
# フォルマントの値が不定の場合、エラーメッセージを出す。
if (f1_point = undefined) | (f2_point = undefined)| (time_start = undefined) echo error: check 'name$' F1: ‘f1_point’ F2: ‘f2_point’
endif
# 表に 情報を記入する select Table formant_table
Set string value... i file_name 'name$' Set numeric value... i f1 'f1_point' Set numeric value... i f2 'f2_point' # 使用済みのファイルの除去 select Sound 'name$' Remove
select TextGrid 'name$' Remove
select Formant 'name$' Remove
endfor
# 表を書きだす
select Table formant_table
Write to table file... 'directory$'¥formant_table.xls Copyright © Setsuko Shirai 2009