从一个 S4 对象输出许多 .wavs,该对象由一个大的 (350 mb) .wav 切割成 5 秒的片段
Outputting many .wavs from an S4 object made from a large (350 mb) .wav cut into 5 second segments
使用此处找到的 R 代码:
Split an audio file into pieces of an arbitrary size
我想将我的音频分割成 5 秒,然后将它们全部导出为 .wavs。使用上面的代码后,我能够得到一个包含 2564 个元素的 S4 对象,这些元素是波浪,每个波浪有 6 个槽。
我希望能够将其中的每一个都保存为 .wav,但我有点迷茫。到目前为止,这是我的代码。
# Calling the packages
library(seewave)
library(audio)
library(tuneR)
# Load audio wave into object
Rec12234 <- readWave("012234.wav")
# Make sure the file loaded correctly - should show sample rate, etc.
head(Rec12234)
#Set frequency
freq <- 16000
# Set the length
totlen <- length(Rec12234)
#Set the duration
totsec <- totlen/freq
# How long each sample is (in seconds)
seglen <- 5
#Defining the break points
breaks <- unique(c(seq(0, totsec, seglen), totsec))
index <- 1:(length(breaks)-1)
#Splitting the file
items <- lapply(index, function(i) Rec12234[(breaks[i]*freq):(breaks[i+1]*freq)])
我对编码和 R 还很陌生,如果答案很简单,我深表歉意!
感谢您的帮助!
您的开局不错。实际上,你只需要 tuneR
包就可以做你想做的事。我更喜欢使用 Wave 对象的插槽来获取我的信息。这样你就可以处理具有不同采样率的文件等
library(tuneR)
# Load audio wave into object
Rec12234 <- readWave("012234.wav")
# Make sure the file loaded correctly - should show sample rate, etc.
head(Rec12234)
#Set frequency
freq <- Rec12234@samp.rate
# Set the length
totlen <- length(Rec12234@left) # 1 channel default is left
#Set the duration
totsec <- totlen/freq
# How long each sample is (in seconds)
seglen <- 5
#Defining the break points
breaks <- unique(c(seq(0, totsec, seglen), totsec))
index <- 1:(length(breaks)-1)
#Splitting the file
items <- lapply(index, function(i) Rec12234@left[(breaks[i]*freq):(breaks[i+1]*freq)])
for (i in 1:length(items)) {
wavName <- paste('m',i,'.wav',sep='') # file name
temp <- Wave(items[i], samp.rate=freq, bit=16) # item into Wave object
writeWave(temp, wavName) # write the file
}
使用此处找到的 R 代码:
Split an audio file into pieces of an arbitrary size
我想将我的音频分割成 5 秒,然后将它们全部导出为 .wavs。使用上面的代码后,我能够得到一个包含 2564 个元素的 S4 对象,这些元素是波浪,每个波浪有 6 个槽。
我希望能够将其中的每一个都保存为 .wav,但我有点迷茫。到目前为止,这是我的代码。
# Calling the packages
library(seewave)
library(audio)
library(tuneR)
# Load audio wave into object
Rec12234 <- readWave("012234.wav")
# Make sure the file loaded correctly - should show sample rate, etc.
head(Rec12234)
#Set frequency
freq <- 16000
# Set the length
totlen <- length(Rec12234)
#Set the duration
totsec <- totlen/freq
# How long each sample is (in seconds)
seglen <- 5
#Defining the break points
breaks <- unique(c(seq(0, totsec, seglen), totsec))
index <- 1:(length(breaks)-1)
#Splitting the file
items <- lapply(index, function(i) Rec12234[(breaks[i]*freq):(breaks[i+1]*freq)])
我对编码和 R 还很陌生,如果答案很简单,我深表歉意!
感谢您的帮助!
您的开局不错。实际上,你只需要 tuneR
包就可以做你想做的事。我更喜欢使用 Wave 对象的插槽来获取我的信息。这样你就可以处理具有不同采样率的文件等
library(tuneR)
# Load audio wave into object
Rec12234 <- readWave("012234.wav")
# Make sure the file loaded correctly - should show sample rate, etc.
head(Rec12234)
#Set frequency
freq <- Rec12234@samp.rate
# Set the length
totlen <- length(Rec12234@left) # 1 channel default is left
#Set the duration
totsec <- totlen/freq
# How long each sample is (in seconds)
seglen <- 5
#Defining the break points
breaks <- unique(c(seq(0, totsec, seglen), totsec))
index <- 1:(length(breaks)-1)
#Splitting the file
items <- lapply(index, function(i) Rec12234@left[(breaks[i]*freq):(breaks[i+1]*freq)])
for (i in 1:length(items)) {
wavName <- paste('m',i,'.wav',sep='') # file name
temp <- Wave(items[i], samp.rate=freq, bit=16) # item into Wave object
writeWave(temp, wavName) # write the file
}