2016-11-06 112 views
0

我有幾個語音文件,我需要剪切聲音文件的某個部分,從0.21毫秒到0.45毫秒。下面的腳本將選擇從0.21毫秒到0.45毫秒的聲音片段並保存。我想從語音文件中剪切片段,然後在沒有它的情況下保存它。我應該在「選擇結束選擇到最近的過零點」並改變「寫入選定的聲音......」後添加另一行,但我不確定具體是什麼。praat - 刪除段

form Files 
    sentence InputDir ./ 
endform 

createDirectory ("output") 
Create Strings as file list... list 'inputDir$'*.wav 
numberOfFiles = Get number of strings 

for ifile to numberOfFiles 

    select Strings list 
    fileName$ = Get string... ifile 
    Read from file... 'inputDir$''fileName$' 
    sound_name$ = selected$ ("Sound") 

     select Sound 'sound_name$' 
     Edit 
     editor Sound 'sound_name$' 
     Select... 0.21 0.45 
     Move start of selection to nearest zero crossing 
     Move end of selection to nearest zero crossing 
     Write selected sound to WAV file... ./output/'fileName$' 
     endeditor 

    select all 
    minus Strings list 
    Remove 

endfor 

select all 
Remove 

回答

0

我希望削減從語音文件的段,然後保存它,沒有它

目前尚不清楚你想要刪除的片段做什麼。你想把它編輯出來嗎(如縮短聲音的總持續時間?)或簡單地將其靜音(如將樣本包含在零中?)。

無論哪種方式,您都不需要打開聲音編輯器(帶有聲波和聲譜圖的窗口)。下面我用一些替代方法複製了腳本(並更新了語法)。

form Files 
    sentence Input_dir ./ 
    positive Start 0.21 
    positive End 0.45 
endform 

createDirectory("output") 
list = Create Strings as file list: "list", input_dir$ + "*.wav" 
numberOfFiles = Get number of strings 

for ifile to numberOfFiles 
    selectObject: list 
    fileName$ = Get string: ifile 
    sound = Read from file: input_dir$ + fileName$ 
    sound_name$ = selected$("Sound") 

    # Find zero crossings 
    start = Get nearest zero crossing: 1, start 
    end = Get nearest zero crossing: 1, end 
    sound_end = Get total duration 

    # Cut the selected part out, shortening the sound 
    # by extracting the part before and after and concatenating 
    before = Extract part: 0, start, "rectangular", 1, "no" 
    selectObject: sound 
    after = Extract part: end, sound_end, "rectangular", 1, "no" 
    selectObject: before, after 
    new = Concatenate 
    removeObject: before, after 

    ## Or, if you want to set the selected part to zero 
    ## (since we already have the zero crossings) 
    # new = Set part to zero: start, end, "at exactly these times" 

    ## Or, if what you want is to get only the selected part 
    ## and not the original sound 
    # new = Extract part: start, end, "rectangular", 1, "no" 

    # Either way, the new, modified sound is selected 
    # and its ID is stored in `new` 

    Save as WAV file: "./output/" + fileName$ 

    # I prefer a less shotgunny way to remove unwanted objects 
    removeObject: sound, new 
endfor 

removeObject: list