time-expanseR/expansion_x10.R

73 lines
2.0 KiB
R

#!/usr/bin/Rscrypt
library(warbleR)
# Set the inputs directory
dir = "/home/ortion/records/"
indir = paste0(dir, 'split')
outdir = paste0(dir, 'exp')
# Get user options
indir = getOptions(option.object, "i")
outdir = getOptions(option.object, "o")
print(indir, outdir)
# Set name prefix
car = "721035"
year = 2021
pass = 0
point = "Z2"
prefix = paste0("Car", car, "-", year, "-", "Pass", pass, "-", point, "-")
# Create the output directory
dir.create(outdir)
# Create a list of ".WAV" files
wav_list=list.files(indir,"*.WAV")
# To handle quite frequent crashes in mp32wav function
wav_list=list.files(indir, pattern=".wav$")
mp3_list=list.files(indir, pattern=".mp3$")
while(length(wav_list)<length(mp3_list))
{
Sys.time()
try(mp32wav())
Sys.time()
wav_list=list.files(indir,pattern=".wav$")
mp3_list=list.files(indir,pattern=".mp3$")
print(paste(length(wav_list),length(mp3_list),sep="/"))
}
# Split into 5 seconds files and apply X10 time expansion
for (j in 1:length(wav_list))
{
dur=0
if (is.null(wav_list[j]) || is.na(file.size(wav_list[j]) || file.size(wav_list[j]) == 0)) {
next
}
else if(file.size(wav_list[j]) > 50000)
{
wav_tmp = readWave(wav_list[j])
dur = duration(wav_tmp)
if (dur > 0)
{
for(k in 1:ceiling(dur/5))
{
left_channel_tmp = cutw(channel(wav_tmp,which="left"),from=(k-1)*5,to=min(dur,k*5),output="Wave")
left_channel_tmp = normalize(left_channel_tmp,level=0.3)
suf = formatC(x = k, width = 3, flag = "0") #create a suffix as 001, 000
savewav(left_channel_tmp,filename=paste0(SplitDir,substr(wav_list[j],1,nchar(wav_list[j])-4),"_",suf,".wav"))
if (length(wav_tmp@right) > 0){
right_channel_tmp = cutw(channel(wav_tmp,which="right"),from=(k-1)*5,to=min(dur,k*5),output="Wave",normalize= "16")
right_channel_tmp = normalize(right_channel_tmp,level=0.3)
savewav(right_channel_tmp,filename = paste0(SplitDir,substr(wav_list[j],1,nchar(wav_list[j])-4),"_",suf,".wav"))
}
}
}
}
print(paste(j,wav_list[j],dur))
}