对于我的工作,我需要分析大型.wav文件(> 208 MB),并且我使用R包seewave和tuneR。我使用readWave函数将每个文件以30秒的方式放入R环境中:
tr1_1 = readWave("TR1_edit.WAV", from = 0, to = 0.5, units = "minutes")
tr1_2= readWave("TR1_edit.WAV", from = 0.5, to = 1, units = "minutes")
tr1_3= readWave("TR1_edit.WAV", from = 1, to = 1.5, units = "minutes")
tr1_4= readWave("TR1_edit.WAV", from = 1.5, to = 2, units = "minutes")
tr1_5= readWave("TR1_edit.WAV", from = 2, to = 2.5, units = "minutes")
等等。这种方法有效,但效率不高或漂亮。有没有办法更有效地导入和拆分大型.wav类文件?
答案 0 :(得分:2)
如果您同时将所有这些内容加载到内存中,而不是顺序变量名称,则应使用列表。
tr1 = list()
duration = 0.5
start_times = seq(0, 2, by = duration)
for (i in seq_along(start_times)) {
tr1[[i]] = readWave('TR1_edit.WAV',
from = start_times[i],
to = start_times[i] + duration,
units = 'minutes')
}
这与you should use a list of data frames的原因相同,而不是按顺序命名数据框。
您可以轻松地将此包装到一个函数中,该函数将WAV
文件的名称作为输入,从元数据中获取其长度,并在30秒(或参数化参数)段中导入它,并返回细分列表。
答案 1 :(得分:0)
@Gregor和@AkselA感谢您的意见。 for循环解决方案的最大问题是我正在使用的wave文件大小不一,所以我最终会在结果列表中找到空白元素。我当前的解决方案导入整个文件,然后从那里将其分成30多个部分:
duration = 1.44e6
tr1 <- readWave("TR1_edit.wav", from = 0, to = 1, units = "minutes")
tr1 <- as.matrix(tr1@left)
tr1 <- cbind(tr1, (rep(1:(length(tr1)/duration), each = duration)))
tr1 <- lapply(split(tr1[,1],tr1[,2]),matrix, ncol = 1)
从那里我可以使用mapply将向量返回到wave类
w <- function(s){
Wave(s, right = numeric(0), samp.rate = 48000, bit = 16, pcm = TRUE)
}
tr1 <- mapply(w, tr1)