Skip to main content
added 1 character in body
Source Link
WMXZ
  • 7.6k
  • 1
  • 10
  • 35

Edit: In case of NaN NaN response, you could check what went wrong with decoding the time stamp. I did it by extactedextracted the decoding code from seewave into local script and added some print statement in the function

Edit: In case of NaN NaN response, you could check what went wrong with decoding the time stamp. I did it by extacted the decoding code from seewave into local script and added some print statement in the function

Edit: In case of NaN NaN response, you could check what went wrong with decoding the time stamp. I did it by extracted the decoding code from seewave into local script and added some print statement in the function

added 538 characters in body
Source Link
WMXZ
  • 7.6k
  • 1
  • 10
  • 35
################################################################################
##                                AUDIOMOTH
################################################################################

audiomoth <- function(x,                  # a character vector, not a Wave object
                      tz = ""             # a character vector defining a time zone specification, see as.POSIXct(), argument 'tz'
)
{
  ## INPUT
  if (!is.character(x)) stop("'x' should be of mode character.")
  
  ## PREPARE RESULTS
  options(stringsAsFactors = FALSE)
  res <- data.frame(year = numeric(0), month = numeric(0), day = numeric(0),
                    hour = numeric(0), min = numeric(0), sec = numeric(0),
                    time = numeric(0))
  N <- length(x)  ## number of file names    
  ## LOOP
  for (i in 1:N)
  { tmp <- x[i]
    n <- nchar(tmp)
    ## check if .WAV or .wav file
    extension <- substr(tmp, start = n-3, stop = n)
    if(extension != ".wav" & extension != ".WAV"){warning(paste("File '", tmp, "' is not a '.wav' file", sep=""))}
    else{
      if(nchar(tmp)==12)   ## hexadecimal format
      {
        hex <- unlist(strsplit(tmp, extension))
        num <- strtoi(hex, base = 16)
        time <- as.POSIXct(num, tz=tz, origin = "1970-01-01")  ## POSIXct as in songmeter()
        year <- as.numeric(format(time, "%Y"))
        month <- as.numeric(format(time, "%m"))
        day <- as.numeric(format(time, "%d"))
        hour <- as.numeric(format(time, "%H"))
        min <- as.numeric(format(time, "%M"))
        sec <- as.numeric(format(time, "%S"))
      }
      else if(nchar(tmp)==19)  ## YYYYMMDD_HHMMSS.wav format
      { print(tmp)
        year <- substr(tmp, start = 1, stop = 4)
        month <- substr(tmp, start = 5, stop = 6)
        day <- substr(tmp, start = 7, stop = 8)
        hour <- substr(tmp, start = 10, stop = 11)
        min <- substr(tmp, start = 12, stop = 13)
        sec <- substr(tmp, start = 14, stop = 15)
        time <- strptime(paste(year,month,day,hour,min,sec), "%Y%m%d%H%M%S")
      }
      res <- rbind(res, data.frame(year, month, day, hour, min, sec, time))
    }
  }
  return(res)    
  options(stringsAsFactors = TRUE)
}
dir <- "F:/WavFiles"
print(audiomoth(dir(dir, pattern="[wav]$|[WAV]$")))

You see what lts passeswhich will in my case result to the decoding routine. Maybe your 'debugging' printout gives an indication. It could be that some (sub)

> source("C:/Users/zimme/Documents/R/Scripts/do_lts_2.R")
[1] "20111208_203052.wav"
[1] "20111208_203122.wav"
  year month day hour min sec                time
1 2011    12  08   20  30  52 2011-12-08 20:30:52
2 2011    12  08   20  31  22 2011-12-08 20:31:22

Obviously directory remaining, etc.path to data needs to be adapted

audiomoth <- function(x,                  # a character vector, not a Wave object
                      tz = ""             # a character vector defining a time zone specification, see as.POSIXct(), argument 'tz'
)
{
  ## INPUT
  if (!is.character(x)) stop("'x' should be of mode character.")
  
  ## PREPARE RESULTS
  options(stringsAsFactors = FALSE)
  res <- data.frame(year = numeric(0), month = numeric(0), day = numeric(0),
                    hour = numeric(0), min = numeric(0), sec = numeric(0),
                    time = numeric(0))
  N <- length(x)  ## number of file names    
  ## LOOP
  for (i in 1:N)
  { tmp <- x[i]
    n <- nchar(tmp)
    ## check if .WAV or .wav file
    extension <- substr(tmp, start = n-3, stop = n)
    if(extension != ".wav" & extension != ".WAV"){warning(paste("File '", tmp, "' is not a '.wav' file", sep=""))}
    else{
      if(nchar(tmp)==12)   ## hexadecimal format
      {
        hex <- unlist(strsplit(tmp, extension))
        num <- strtoi(hex, base = 16)
        time <- as.POSIXct(num, tz=tz, origin = "1970-01-01")  ## POSIXct as in songmeter()
        year <- as.numeric(format(time, "%Y"))
        month <- as.numeric(format(time, "%m"))
        day <- as.numeric(format(time, "%d"))
        hour <- as.numeric(format(time, "%H"))
        min <- as.numeric(format(time, "%M"))
        sec <- as.numeric(format(time, "%S"))
      }
      else if(nchar(tmp)==19)  ## YYYYMMDD_HHMMSS.wav format
      { print(tmp)
        year <- substr(tmp, start = 1, stop = 4)
        month <- substr(tmp, start = 5, stop = 6)
        day <- substr(tmp, start = 7, stop = 8)
        hour <- substr(tmp, start = 10, stop = 11)
        min <- substr(tmp, start = 12, stop = 13)
        sec <- substr(tmp, start = 14, stop = 15)
        time <- strptime(paste(year,month,day,hour,min,sec), "%Y%m%d%H%M%S")
      }
      res <- rbind(res, data.frame(year, month, day, hour, min, sec, time))
    }
  }
  return(res)    
  options(stringsAsFactors = TRUE)
}

You see what lts passes to the decoding routine. Maybe your 'debugging' printout gives an indication. It could be that some (sub) directory remaining, etc.

################################################################################
##                                AUDIOMOTH
################################################################################

audiomoth <- function(x,                  # a character vector, not a Wave object
                      tz = ""             # a character vector defining a time zone specification, see as.POSIXct(), argument 'tz'
)
{
  ## INPUT
  if (!is.character(x)) stop("'x' should be of mode character.")
  
  ## PREPARE RESULTS
  options(stringsAsFactors = FALSE)
  res <- data.frame(year = numeric(0), month = numeric(0), day = numeric(0),
                    hour = numeric(0), min = numeric(0), sec = numeric(0),
                    time = numeric(0))
  N <- length(x)  ## number of file names    
  ## LOOP
  for (i in 1:N)
  { tmp <- x[i]
    n <- nchar(tmp)
    ## check if .WAV or .wav file
    extension <- substr(tmp, start = n-3, stop = n)
    if(extension != ".wav" & extension != ".WAV"){warning(paste("File '", tmp, "' is not a '.wav' file", sep=""))}
    else{
      if(nchar(tmp)==12)   ## hexadecimal format
      {
        hex <- unlist(strsplit(tmp, extension))
        num <- strtoi(hex, base = 16)
        time <- as.POSIXct(num, tz=tz, origin = "1970-01-01")  ## POSIXct as in songmeter()
        year <- as.numeric(format(time, "%Y"))
        month <- as.numeric(format(time, "%m"))
        day <- as.numeric(format(time, "%d"))
        hour <- as.numeric(format(time, "%H"))
        min <- as.numeric(format(time, "%M"))
        sec <- as.numeric(format(time, "%S"))
      }
      else if(nchar(tmp)==19)  ## YYYYMMDD_HHMMSS.wav format
      { print(tmp)
        year <- substr(tmp, start = 1, stop = 4)
        month <- substr(tmp, start = 5, stop = 6)
        day <- substr(tmp, start = 7, stop = 8)
        hour <- substr(tmp, start = 10, stop = 11)
        min <- substr(tmp, start = 12, stop = 13)
        sec <- substr(tmp, start = 14, stop = 15)
        time <- strptime(paste(year,month,day,hour,min,sec), "%Y%m%d%H%M%S")
      }
      res <- rbind(res, data.frame(year, month, day, hour, min, sec, time))
    }
  }
  return(res)    
  options(stringsAsFactors = TRUE)
}
dir <- "F:/WavFiles"
print(audiomoth(dir(dir, pattern="[wav]$|[WAV]$")))

which will in my case result to

> source("C:/Users/zimme/Documents/R/Scripts/do_lts_2.R")
[1] "20111208_203052.wav"
[1] "20111208_203122.wav"
  year month day hour min sec                time
1 2011    12  08   20  30  52 2011-12-08 20:30:52
2 2011    12  08   20  31  22 2011-12-08 20:31:22

Obviously directory path to data needs to be adapted

added function
Source Link
WMXZ
  • 7.6k
  • 1
  • 10
  • 35

Edit: In case of NaN NaN response, you could check what went wrong with decoding the time stamp. I did it by extacted the decoding code from seewave into local script and added some print statement in the function

audiomoth <- function(x,                  # a character vector, not a Wave object
                      tz = ""             # a character vector defining a time zone specification, see as.POSIXct(), argument 'tz'
)
{
  ## INPUT
  if (!is.character(x)) stop("'x' should be of mode character.")
  
  ## PREPARE RESULTS
  options(stringsAsFactors = FALSE)
  res <- data.frame(year = numeric(0), month = numeric(0), day = numeric(0),
                    hour = numeric(0), min = numeric(0), sec = numeric(0),
                    time = numeric(0))
  N <- length(x)  ## number of file names    
  ## LOOP
  for (i in 1:N)
  { tmp <- x[i]
    n <- nchar(tmp)
    ## check if .WAV or .wav file
    extension <- substr(tmp, start = n-3, stop = n)
    if(extension != ".wav" & extension != ".WAV"){warning(paste("File '", tmp, "' is not a '.wav' file", sep=""))}
    else{
      if(nchar(tmp)==12)   ## hexadecimal format
      {
        hex <- unlist(strsplit(tmp, extension))
        num <- strtoi(hex, base = 16)
        time <- as.POSIXct(num, tz=tz, origin = "1970-01-01")  ## POSIXct as in songmeter()
        year <- as.numeric(format(time, "%Y"))
        month <- as.numeric(format(time, "%m"))
        day <- as.numeric(format(time, "%d"))
        hour <- as.numeric(format(time, "%H"))
        min <- as.numeric(format(time, "%M"))
        sec <- as.numeric(format(time, "%S"))
      }
      else if(nchar(tmp)==19)  ## YYYYMMDD_HHMMSS.wav format
      { print(tmp)
        year <- substr(tmp, start = 1, stop = 4)
        month <- substr(tmp, start = 5, stop = 6)
        day <- substr(tmp, start = 7, stop = 8)
        hour <- substr(tmp, start = 10, stop = 11)
        min <- substr(tmp, start = 12, stop = 13)
        sec <- substr(tmp, start = 14, stop = 15)
        time <- strptime(paste(year,month,day,hour,min,sec), "%Y%m%d%H%M%S")
      }
      res <- rbind(res, data.frame(year, month, day, hour, min, sec, time))
    }
  }
  return(res)    
  options(stringsAsFactors = TRUE)
}

You see what lts passes to the decoding routine. Maybe your 'debugging' printout gives an indication. It could be that some (sub) directory remaining, etc.

Edit: In case of NaN NaN response, you could check what went wrong with decoding the time stamp. I did it by extacted the decoding code from seewave into local script and added some print statement in the function

audiomoth <- function(x,                  # a character vector, not a Wave object
                      tz = ""             # a character vector defining a time zone specification, see as.POSIXct(), argument 'tz'
)
{
  ## INPUT
  if (!is.character(x)) stop("'x' should be of mode character.")
  
  ## PREPARE RESULTS
  options(stringsAsFactors = FALSE)
  res <- data.frame(year = numeric(0), month = numeric(0), day = numeric(0),
                    hour = numeric(0), min = numeric(0), sec = numeric(0),
                    time = numeric(0))
  N <- length(x)  ## number of file names    
  ## LOOP
  for (i in 1:N)
  { tmp <- x[i]
    n <- nchar(tmp)
    ## check if .WAV or .wav file
    extension <- substr(tmp, start = n-3, stop = n)
    if(extension != ".wav" & extension != ".WAV"){warning(paste("File '", tmp, "' is not a '.wav' file", sep=""))}
    else{
      if(nchar(tmp)==12)   ## hexadecimal format
      {
        hex <- unlist(strsplit(tmp, extension))
        num <- strtoi(hex, base = 16)
        time <- as.POSIXct(num, tz=tz, origin = "1970-01-01")  ## POSIXct as in songmeter()
        year <- as.numeric(format(time, "%Y"))
        month <- as.numeric(format(time, "%m"))
        day <- as.numeric(format(time, "%d"))
        hour <- as.numeric(format(time, "%H"))
        min <- as.numeric(format(time, "%M"))
        sec <- as.numeric(format(time, "%S"))
      }
      else if(nchar(tmp)==19)  ## YYYYMMDD_HHMMSS.wav format
      { print(tmp)
        year <- substr(tmp, start = 1, stop = 4)
        month <- substr(tmp, start = 5, stop = 6)
        day <- substr(tmp, start = 7, stop = 8)
        hour <- substr(tmp, start = 10, stop = 11)
        min <- substr(tmp, start = 12, stop = 13)
        sec <- substr(tmp, start = 14, stop = 15)
        time <- strptime(paste(year,month,day,hour,min,sec), "%Y%m%d%H%M%S")
      }
      res <- rbind(res, data.frame(year, month, day, hour, min, sec, time))
    }
  }
  return(res)    
  options(stringsAsFactors = TRUE)
}

You see what lts passes to the decoding routine. Maybe your 'debugging' printout gives an indication. It could be that some (sub) directory remaining, etc.

Source Link
WMXZ
  • 7.6k
  • 1
  • 10
  • 35
Loading