Load libraries

# install.packages(c("bioacoustics", "tuneR", "seewave", "dplyr", "tidyverse", "lubridate", "tools", "ggplot2", "ggpmisc", "plotly", "stringr", "monitoR", "pbapply", "foreach", "doParallel", "R.utils"), type="binary")
library(bioacoustics)
library(tuneR)
library(seewave)
library(dplyr)
library(tidyverse)
library(lubridate)
library(tools)
library(ggplot2)
library(ggpmisc)
library(plotly)
library(stringr)
library(foreach)
library(doParallel)
library(R.utils)
library(readxl)
library(pbapply)
library(monitoR)
ymd_hms = function(dt) {
  lubridate::ymd_hms(dt, tz="Pacific/Auckland")
}
dmy_hms = function(dt) {
  lubridate::dmy_hms(dt, tz="Pacific/Auckland")
}
options(stringsAsFactors = FALSE)

Load RFID log files

files = list.files("data/2019 RFID", pattern = "*.TXT$", recursive = TRUE, full.names=TRUE)
csvs = lapply(files, function(path) {
  csv = read.csv(path, header=FALSE, col.names = c("year", "month", "day", "hour","minute_sec", "id", "ext", "int", "msg"))
  csv$site = strsplit(strsplit(path, "/")[[1]][4], "_")[[1]][1]
  csv
})
rfid = do.call(rbind, csvs)
rm(csvs)
rfid$datetime = paste0(rfid$year, "-", rfid$month, "-", rfid$day, " ", rfid$hour, ":", rfid$minute_sec)
rfid$datetime = ymd_hms(rfid$datetime)
ggplot(rfid, aes(datetime)) +
  geom_freqpoly(binwidth = 60 * 60)

Reference spectrograms for playback files

html_tag_audio <- function(file, type = c("wav")) {
  type <- match.arg(type)
  htmltools::tags$h3(
    basename(file),
    htmltools::tags$audio(
      controls = "",
      htmltools::tags$source(
        src = file,
        type = glue::glue("audio/{type}", type = type)
      )
    )
  )
}
files = Sys.glob("./clips/PB*.wav")
PB_wavs = lapply(files, function(f) {
  wav = read_audio(f)
  wav_length = round(length(wav) / wav@samp.rate, 2)
  spectro(wav, main=paste0(basename(f), " ", wav_length, "s"), osc=TRUE)
  print(html_tag_audio(f))
  data.frame(filename=basename(f), wav_length)
})

PB0009zp.wav

PB0014zp.wav

PB0023.wav

PB0024.wav

PBB11up.wav

PB_wavs = do.call(rbind, PB_wavs)

Build a reference dataframe showing which wave files correspond to which times

read_BAR_metadata = function(files) {
  df = pblapply(files, function(f) {
    bits = strsplit(basename(f), "_")[[1]]
    start = ymd_hms(paste(bits[2], bits[3]))
    site = strsplit(f, "/")[[1]][4]
    try({
      wav_header = readWave(f, header=TRUE)
      duration = wav_header$samples / wav_header$sample.rate
      end = start + duration
      return(data.frame(filename = f, base_filename = basename(f), site = site, start = start, end = end, samples = wav_header$samples, sr = wav_header$sample.rate, duration=duration, interval = interval(start, end)))
    })
    NULL
  })
  df = do.call(rbind, df)
  df
}
if (file.exists("misc/BAR_timespans.csv")) {
  df = read.csv("misc/BAR_timespans.csv")
  df$start = ymd_hms(df$start)
  df$end = ymd_hms(df$end)
  df$interval = interval(df$start, df$end)
} else {
  files = list.files("data/2019 BAR recordings", pattern = "*.wav$", recursive = TRUE, full.names=TRUE)
  df = read_BAR_metadata(files)
  write.csv(df, "misc/BAR_timespans.csv", row.names = FALSE)
}
df[c("site", "interval")]

Select a few playback entries to check

PB_to_check = "PB0023.wav"
site_to_check = "NNK30"
filtered_rfid = filter(rfid, msg == PB_to_check & site == site_to_check)
filtered_df = filter(df, site == site_to_check)
filtered_rfid$corrected_datetime = filtered_rfid$datetime
filtered_rfid$match = lapply(1:nrow(filtered_rfid), function(i) {
  dt = filtered_rfid$corrected_datetime[i]
  site = filtered_rfid$site[i]
  indices = which(dt %within% filtered_df$interval)
  if (length(indices) == 0) indices = NA
  indices
})
print(paste("There are", nrow(filtered_rfid), PB_to_check, "playbacks, of which", sum(!is.na(filtered_rfid$match)), "have recordings"))
[1] "There are 307 PB0023.wav playbacks, of which 48 have recordings"
# A few randomly
datetimes_to_check = sort(sample(filtered_rfid$corrected_datetime[!is.na(filtered_rfid$match)], 5))
# Ones from the 13th
#datetimes_to_check = filtered_rfid$corrected_datetime[!is.na(filtered_rfid$match) & filtered_rfid$datetime > ymd_hms("2019-11-20 00:00:00") & filtered_rfid$datetime < ymd_hms("2019-11-20 23:59:59")]
#datetimes_to_check = filtered_rfid$corrected_datetime[!is.na(filtered_rfid$match) & filtered_rfid$corrected_datetime %within% interval(ymd_hms("2019-12-13 16:45:00"), ymd_hms("2019-12-13 17:13:00"))]
print(datetimes_to_check)
[1] "2019-12-13 15:04:16 NZDT" "2019-12-13 15:24:06 NZDT" "2019-12-13 16:38:35 NZDT" "2019-12-14 06:20:17 NZDT"
[5] "2019-12-14 06:39:53 NZDT"
#dt = ymd_hms("2019-12-13 16:45:04")
setWavPlayer("play") # OS dependent
checked_clips = lapply(datetimes_to_check, function(dt) {
  match = filtered_rfid$match[filtered_rfid$corrected_datetime == dt][[1]]
  f = filtered_df$filename[match]
  clip_start = time_length(dt - filtered_df$start[match], unit = "seconds")
  clip_end = clip_start + 1
  print(paste(f, dt, clip_start, clip_end))
  wav = read_audio(f, from = clip_start, to = clip_end)
  spectro(wav, main=dt, osc=TRUE)
  #play(wav)
  wav
})
[1] "data/2019 BAR recordings/NNK Kamahi nests/NNK30/NNK30_20191213_STUDY [-39.1072 176.8183]/NNK30_20191213_144041_SunriseToSunset [-39.1072 176.8183].wav 2019-12-13 15:04:16 1415 1416"
[1] "data/2019 BAR recordings/NNK Kamahi nests/NNK30/NNK30_20191213_STUDY [-39.1072 176.8183]/NNK30_20191213_144041_SunriseToSunset [-39.1072 176.8183].wav 2019-12-13 15:24:06 2605 2606"