我有一个包含四个通道的多通道 WAV 文件,我只需要提取其中一个通道(比如说通道 1),以便声音生态学能够计算指数。如何使用 R 读取 WAV 文件,仅提取第一个通道,并将其另存为新的 WAV 文件,供 soundecology 用于索引计算?
我正在使用 Audacity 手动擦除这些通道,以便我可以计算生态声学指数
tuneR::channel()
函数可用于从多通道wav文件中提取通道。
由于原始帖子不包含可重现的示例,我们将从互联网上下载一个示例多声道 WAV 文件,阅读它并提取一个声道。
然后我们将编写输出文件并用 WAV 文件播放器进行测试,并播放文件以确认输出有效并播放预期的声音。
# download an example multichannel wav file
download.file("https://www.jensign.com/bdp95/7dot1voiced/7dot1voiced.zip",
"./data/7dot1voiced.zip",mode="wb")
unzip("./data/7dot1voiced.zip",exdir="./data/unzip")
根据 jensign.com 上的文档 下载的 zip 文件包含一个具有六个通道的 wav 5.1 文件和一个具有 8 个通道的 7.1 文件。我们将读取 5.1 文件,并期望它包含 6 个通道。
library(tuneR)
# read WAV file as multichannel
aFile <- readWave("./data/unzip/Nums_5dot1_24_48000.wav",toWaveMC = TRUE)
# calculate # of channels
nchannel(aFile)
这里我们确认文件包含六个通道。
> nchannel(aFile)
[1] 6
我们提取第一个频道并打印其频道信息。
# extract channel 1
channel1 <- aFile[,1]
# print object info
channel1
注意打印显示提取的对象中只有一个通道。
> channel1
WaveMC Object
Number of Samples: 434386
Duration (seconds): 9.05
Samplingrate (Hertz): 48000
Number of channels: 1
PCM (integer format): TRUE
Bit (8/16/24/32/64): 24
最后,我们将通道写入输出文件,并在 R 外部播放保存的文件以确认它包含预期的内容。
# write to file
writeWave(channel1,"./data/channel1.wav")
# when the output file is played it will say "one" for the first channel, since
# each channel contains a voice saying the channel number
我们可以提取不同的渠道并证明该过程适用于其他渠道。在这里,我们将提取通道 3,保存生成的文件,并确认我们听到一个声音说“三”。
# extract channel 3
channel3 <- aFile[,3]
# print object info
channel3
我们看到
channel3
对象有一个通道。
> channel3
WaveMC Object
Number of Samples: 434386
Duration (seconds): 9.05
Samplingrate (Hertz): 48000
Number of channels: 1
PCM (integer format): TRUE
Bit (8/16/24/32/64): 24
现在我们将通道数据写入输出 WAV 文件。
# write to file
writeWave(channel3,"./data/channel3.wav")
收听输出文件作为练习留给读者。