Last active
September 28, 2016 05:52
-
-
Save tomoyanonymous/0158715d18b9f8083d392be25a0797fb to your computer and use it in GitHub Desktop.
sourceフォルダに48kHzのwavファイルを放り込むとdestにラウドネスノーマライズされて出てくるやつ(ITU−R BS1770,1771準拠)/ Loudness normalize 48kHz wav files from 'source' into 'dest'(based on ITU−R BS1770,1771)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tuneR) | |
library(signal) | |
if(!is.na(commandArgs(T)[1])){ | |
isPrenormalize = commandArgs(T)[1] == "-normalize" | |
}else{ | |
isPrenormalize=FALSE | |
} | |
print(paste("Pre-normalize: " ,isPrenormalize)) | |
prefilter1 <- Arma(c(1.53512485958697,-2.69169618940638,1.19839281085285),c(1,-1.69065929318241,0.73248077421585)) | |
prefilter2 <- Arma(c(1.0,-2.0,1.0),c(1,-1.99004745483398,0.99007225036621)) | |
meansq <- function(array){ | |
return(mean(array^2)) | |
} | |
apply_kfilter <- function(array){ | |
applied <- filter(prefilter1,array) | |
applied <- filter(prefilter2,applied) | |
return(applied) | |
} | |
measureloudness <- function(filename ,prenormalize=TRUE){ | |
print(paste("measuring loudness of ",filename,"...")) | |
src_path <- paste('source/',filename,sep = "") | |
wavobj <- readWave(src_path) | |
print(wavobj) | |
if(wavobj@samp.rate!=48000){ | |
print("SR isn't 48000.") | |
return(FALSE); | |
} | |
norm <- normalize(wavobj,unit = "1",pcm = FALSE,rescale = prenormalize) | |
left <- norm@left | |
left<- apply_kfilter(left) | |
meansq_left <-meansq(left) | |
if(wavobj@stereo){ | |
right <- norm@right | |
right <- apply_kfilter(right) | |
meansq_right <-meansq(right) | |
loudness <- meansq_left+meansq_right | |
}else{ | |
loudness <- meansq_left | |
} | |
lkfs <- -0.691 + 10*log(loudness,10) | |
print(paste(lkfs,"LKFS")) | |
return(lkfs) | |
} | |
dbtoa <- function(db){ | |
return(10^(db/20)) | |
} | |
applygain <- function(filename,loudness,min_loudness,prenormalize=TRUE){ | |
print(paste("normalizing " , filename,"...")) | |
g_difference <- loudness- min_loudness | |
print(paste("gain diffrence : " , g_difference)) | |
src_path <- paste('source/',filename,sep = "") | |
wavobj <- readWave(src_path) | |
# print(wavobj) | |
norm <- normalize(wavobj,unit = "1",pcm = FALSE,rescale = prenormalize) | |
g <- dbtoa(-g_difference) | |
print(paste("gain coefficient is ",g)) | |
newleft <- as.integer(norm@left * g * (2^23-1)) | |
if(norm@stereo){ | |
newright<- as.integer(norm@right * g * (2^23-1)) | |
newwav <- Wave(left =newleft,right=newright,samp.rate = 48000,bit = 24,pcm = TRUE) | |
writeWave(newwav,paste("dest/",filename,sep = "")) | |
print(paste("wrote ",filename,".")) | |
}else{ | |
newwav <- Wave(left =newleft,samp.rate = 48000,bit = 24,pcm = TRUE) | |
writeWave(newwav,paste("dest/",filename,sep = "")) | |
print(paste("wrote ",filename,".")) | |
} | |
} | |
# start | |
files <- list.files('source','*.wav') | |
lkfsarray <- c() | |
for (f in files){ | |
lkfs <- measureloudness(f,isPrenormalize) | |
lkfsarray <-c(lkfsarray,lkfs) | |
} | |
frame <- data.frame(FILE = files,LOUDNESS= lkfsarray) | |
print(frame) | |
minlkfs <- min(lkfsarray) | |
print(paste("minimum loudness is ",minlkfs,"LKFS")) | |
for(i in 1: length(files)){ | |
applygain(frame$FILE[i],frame$LOUDNESS[i],minlkfs,isPrenormalize) | |
} | |
print("end all sequence") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment