Added R and bash scripts

This commit is contained in:
Samuel Ortion 2021-05-01 08:02:59 +02:00
parent 6b4531e4da
commit 833b2835be
3 changed files with 102 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
media/split/*.wav
media
.vscode
media/

65
expansion_x10.R Normal file
View File

@ -0,0 +1,65 @@
library(warbleR)
library(tuneR)
# Set the inputs directory
dir = "/home/ortion/records/"
indir = paste0(dir, 'raw')
outdir = paste0(dir, 'exp')
# 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))
}

33
split_5s.sh Executable file
View File

@ -0,0 +1,33 @@
#!/bin/bash
##########################################################
#
# Split wav sounds into 5s wav using ffmpeg
#
##########################################################
indir="/home/ortion/Documents/projects/time-expanseR/media/raw"
outdir="/home/ortion/Documents/projects/time-expanseR/media/split"
if [ ! -d $outdir ];
then
mkdir $outdir
fi
cd $indir
for item in `ls ./*.WAV`
do
audio_dur=`sox --i -D $item`
# Convert float to int
audio_dur=${audio_dur%.*}
echo "$item ($audio_dur)"
ss=0
to=5
for ss in `seq 0 5 $(( $audio_dur - 5 ))`
do
to=$(( $ss + 5 ))
echo $ss $to
ffmpeg -ss $ss -i "$indir/$item" -t $to -c copy "$outdir/$item-$ss.wav"
done
done