将音频文件拆分为任意大小的片段

Jot*_*ota 6 audio file-io split r

我有一个大的声音文件(150 MB),我想分成一些更容易管理的大小文件,比如说有5分钟音频的文件.显然,最后一段将是<= 5分钟,那没关系.有没有办法轻松完成这类任务?

可以使用以下链接下载用于此问题的小样本.mp3文件:download.linnrecords.com/test/mp3/recit.aspx.

这是我到目前为止所尝试的.我使用readMP3from 导入数据tuneR并将使用该cutw函数,但是没有找到使用它的有效方法.

library(tuneR)

sample<-readMP3("recit.mp3") 

# the file is only 9.04 seconds long (44.1 Hz, 16-bit, sterio)
# so, for this example we can cut it into 0.5 second intervals)
subsamp1<-cutw(sample, from=0, to=0.5, output="Wave")

# then I would have to do this for each interval up to:
subsampn<-cutw(sample, from=9, to=9.04, output="Wave") 
# where I have to explicitly state the maximum second (i.e. 9.04), 
# unless there is a way I don't know of to extract this information.
Run Code Online (Sandbox Code Playgroud)

当间隔变得比总文件长度小时,这种方法效率低.此外,sample是立体声,但是subsamp1是单声道的,如果可能的话,我宁愿不改变任何有关数据的内容.

在提高效率的方式中,我尝试向fromto参数输入向量,但是我得到了一个错误(见下文).尽管它已经奏效,但它并不是一个特别好的解决方案.有人知道使用R来解决这个问题的更优雅的方法吗?

cutw(subsamp1,from=seq(0,9,0.5),to=c(seq(0.5,9.0,0.5),9.04) 
# had to explicitly supply the max second (i.e. 9.04). 
# must be a better way to extract the maximum second

Error in wave[a:b, ] : subscript out of bounds
In addition: Warning messages:
1: In if (from > to) stop("'from' cannot be superior to 'to'") :
  the condition has length > 1 and only the first element will be used
2: In if (from == 0) { :
  the condition has length > 1 and only the first element will be used
3: In a:b : numerical expression has 19 elements: only the first used
Run Code Online (Sandbox Code Playgroud)

Jea*_*ams 2

我没有任何在 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
subsamps <- lapply(index, function(i) cutw(audio, f=freq, from=breaks[i], to=breaks[i+1]))
Run Code Online (Sandbox Code Playgroud)