gpt4 book ai didi

r - 如何在无声的地方分割音频文件

转载 作者:行者123 更新时间:2023-12-02 23:38:44 25 4
gpt4 key购买 nike

如下代码可以拆分wav.file,但设置剪切音频文件的持续时间有一个限制。请问R中是否有任何解决方案可以在无声的地方分割音频文件?

 library(seewave)
#your audio file (using example file from seewave package)
data(tico)
audio <- tico
#the frequency of your audio file
freq <- 22050
# the length and duration of your audio file
totlen <- length(audio)
totsec <- totlen/freq

# the duration that you want to chop the file into
seglen <- 0.5

# defining the break points
breaks <- unique(c(seq(0, totsec, seglen), totsec))
index <- 1:(length(breaks)-1)
# a list of all the segments
lapply(index, function(i) audio[(breaks[i]*freq):(breaks[i+1]*freq)])
# the above final line is the only difference between this code and the
# code provided by @Jean V. Adams

最佳答案

seewave包包含timer函数,该函数可检测静音和信号在哪里。需要一些注意才能使阈值,平滑度和最小持续时间正确,但这是一个示例。

    library(seewave)
# make some example data
sine <- synth(cf=1000, f = 22050, d = 2, output = 'Wave')
sinewithsilence <- addsilw(wave = sine, at = 'middle', output = 'Wave', d = 0.5)
testsignal1 <- pastew(sinewithsilence, sinewithsilence, output = 'Wave')
testsignal2 <- addsilw(wave = testsignal1, at = 'end', output = 'Wave', d = 0.5)

# choose one wav to split - uncomment as necessary
#signal <- tico
#signal <- testsignal1
signal <- testsignal2
totsec <- length(signal@left)/signal@samp.rate
periods <- timer(signal, plot = T, threshold = 1, msmooth = c(100,0), dmin=0.1)

starts <- periods$s.start
ends <- periods$s.end
if (length(starts) > length(ends)) {
ends <- c(ends, totsec)
}
periods.df <- data.frame(starts, ends)

splits = lapply(1:nrow(periods.df), function(i) cutw(signal, from = periods.df$starts[i], to = periods.df$ends[i], output = 'Wave'))

> str(splits)
List of 3
$ :Formal class 'Wave' [package "tuneR"] with 6 slots
.. ..@ left : num [1:22132] 0 0.281 0.54 0.754 0.909 ...
.. ..@ right : num(0)
.. ..@ stereo : logi FALSE
.. ..@ samp.rate: num 22050
.. ..@ bit : num 16
.. ..@ pcm : logi TRUE
$ :Formal class 'Wave' [package "tuneR"] with 6 slots
.. ..@ left : num [1:44265] 0 0 0 0 0 0 0 0 0 0 ...
.. ..@ right : num(0)
.. ..@ stereo : logi FALSE
.. ..@ samp.rate: num 22050
.. ..@ bit : num 16
.. ..@ pcm : logi TRUE
$ :Formal class 'Wave' [package "tuneR"] with 6 slots
.. ..@ left : num [1:22133] -0.997 -0.979 -0.883 -0.715 -0.49 ...
.. ..@ right : num(0)
.. ..@ stereo : logi FALSE
.. ..@ samp.rate: num 22050
.. ..@ bit : num 16
.. ..@ pcm : logi TRUE

关于r - 如何在无声的地方分割音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41803993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com