1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Audacityでラベル付き音声ファイルを切り出す

Posted at

Audacityを利用してWAVファイルをラベル毎に切り出します

やったこと

Audacityでラベルトラックを作成し、WAVデータの時間半以後とにラベリングを行い、このラベル毎にWAVファイルを切り出しました。

バックグラウンド

長時間の録音データから、機械学習向けラベル付き音声ファイルを作成するためです。

Audacity

ラベル付け

音声ファイルの特定範囲を選択します。
image.png
編修−>ラベル−>選択範囲にラベルをつける
image.png
ラベルを入力
image.png
ラベルの書き出し
ファイル−>書き出し−>ラベルの書き出し
ファイルは Start(TAB)Stop(Tab)Labelのフォーマット

LabelTrack.txt
0.529306        0.923964        PING
1.569346        1.843286        PING2
2.553670        2.836895        PING

これをpython でラベル毎に切り出します。

切り出しには audiosegmentを使用
python3 cut [入力WAVファイル] [LabelTrack.txt]
出力は
入力WAVファイル_[LABEL]_[通し番号].wav
でラベル毎に切り出されて書き出されます。
同時にmetadata.csvとして
ファイル名,label
ファイルが書き出されます。

cut.py
# -*- coding: utf-8 -*-
from pydub import AudioSegment
import sys


def main():
    args = sys.argv
    infile=args[1]
    auxfilename = args[2]
    metaFilename="metadata.csv"
# Reading wav
    sound = AudioSegment.from_file(infile, format="wav")
    fn = infile.split(".")
# Reading AUX
    auxfile = open( auxfilename , 'r')
    metaFile = open( metaFilename , 'w' )
    metaFile.write( "filename,class\n" )
    auxlines = auxfile.readlines()
    auxfile.close()
    fileNum=0
    for auxline in auxlines[0:]:
        items = auxline[:-1].split('    ')
        start = float( items[0] )*1000.0
        end = float( items[1] ) *1000.0
#        start = int( float( items[0] ) * 1000 )
#        end = int( float( items[1] ) * 1000 )
        className = items[2]
        print( start , end )
        sound[ start:end ].export( fn[0]+"_"+className+"_"+str(fileNum)+".wav" , format="wav" )
        metaFile.write( fn[0]+"_"+className+"_"+str(fileNum)+".wav"+","+className +"\n")
        fileNum=fileNum+1
    metaFile.close()



if __name__ == "__main__":
    main()
1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?