'Extract portions of wav file and save them in R
I have a large number of relatively large mono audio wav files each of about 2 GB that I want to annotate in Raven Pro (version 1.4). The files seem to be too large and Raven is not opening them. I can easily load and play them with any other application, this means to me that the files are OK, is just my computer/Raven having issues. I have written an R script that, based on details on a selection table, extracts one or more sections from the original large wav file and saves each section in a separate file which name is the original long file name with addition of an index. The script works perfectly well but when I open one of the section files in Raven I get the following message:
If I try to open the same files in Audacity or R or foobar200 or any other app, the file plays well. My code is:
library(readr)
library(lubridate)
library(tuneR)
library(tidyverse)
library(dplyr)
setwd(choose.dir())
select.table <-
as.data.frame(read_csv(file.choose(),
col_types = cols(
start = col_time(format = "%H:%M:%S"),
end = col_time(format = "%H:%M:%S")
)))
# convert hh:mm:ss to seconds to work with tuneR
select.table$start <- period_to_seconds(hms(select.table$start))
select.table$end <- period_to_seconds(hms(select.table$end))
colnames(select.table)[colnames(select.table) == "File"] <-
"sound.files"
# add a selection number
select.table <-
select.table %>% group_by(sound.files) %>% mutate(selec = row_number())
X <- select.table
#-----function to iterate through all the files in a folder and the elements in the selection table
cut.selections <- function(X, mar) {
cutFUN <- function(select.table, i, mar) {
r <-
tuneR::readWave(as.character(select.table$sound.files[i]), header = TRUE)
f <- r$sample.rate
t <- c(select.table$start[i] - mar, select.table$end[i] + mar)
mar1 <- mar
mar2 <- mar1 + select.table$end[i] - select.table$start[i]
if (t[1] < 0)
t[1] <- 0
if (t[2] > r$samples / f)
t[2] <- r$samples / f
# Cut wave
wvcut <-
tuneR::readWave(
as.character(select.table$sound.files[i]),
from = t[1],
to = t[2],
units = "seconds"
)
tuneR::writeWave(
object = wvcut,
filename = paste0(
gsub("\\.wav$", "", select.table$sound.files[i], ignore.case = TRUE),
"_sample_",
select.table$selec [i],
".wav"
)
)
}
out <- pbapply::pblapply(1:nrow(select.table), function(y)
cutFUN(X, i = y, mar = 0.05))
}
cut.selections(X = select.table, mar = 0.05)
I have also tried to use the savewav() function in the package seewave
instead of the writeWave()
function with:
savewav(
wvcut,
filename = paste0(
gsub("\\.wav$", "", select.table$sound.files[i], ignore.case = TRUE),
"_sample_",
select.table$selec [i],
".wav"
), channel=1
)
My input selection table looks like:
> select.table
File start end
1 6239.211010165347.wav 00:25:00 00:30:00
2 6239.211010165347.wav 01:30:00 01:35:00
3 6239.211010165347.wav 01:50:00 01:55:00
4 6239.211010165347.wav 06:58:42 07:03:42
5 6239.211010165347.wav 10:58:42 11:03:42
6 6239.211010165347.wav 12:03:42 12:08:42
7 6239.211010165347.wav 14:21:07 14:26:07
8 6239.211010165347.wav 14:46:07 14:51:07
9 6239.211010165347.wav 15:56:07 16:01:07
10 6239.211010165347.wav 17:16:07 17:21:07
11 6239.211010165347.wav 17:36:07 17:41:07
12 6239.210928165620.wav 01:55:00 02:00:00
13 6239.210928165620.wav 04:44:25 04:49:25
14 6239.210928165620.wav 05:49:25 05:54:25
15 6239.210928165620.wav 06:09:25 06:14:25
My processed selection table looks l.ike:
> select.table
# A tibble: 462 x 4
# Groups: sound.files [44]
sound.files start end selec
<chr> <dbl> <dbl> <int>
1 6239.211010165347.wav 1500 1800 1
2 6239.211010165347.wav 5400 5700 2
3 6239.211010165347.wav 6600 6900 3
4 6239.211010165347.wav 25122 25422 4
5 6239.211010165347.wav 39522 39822 5
6 6239.211010165347.wav 43422 43722 6
7 6239.211010165347.wav 51667 51967 7
8 6239.211010165347.wav 53167 53467 8
9 6239.211010165347.wav 57367 57667 9
10 6239.211010165347.wav 62167 62467 10
# ... with 452 more rows
What is causing the Raven problem?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|