#' Plot spectrogram
#'
#' Function for plotting spectrograms called by [praatpicture]. Instead of using
#' this function directly, just use
#' `praatpicture('my_sound_file', frames='spectrogram')`.
#'
#' @param sig Numeric vector corresponding to a sound signal.
#' @param sr Integer giving the sampling rate of the signal.
#' @param t Numeric vector giving times corresponding to the signal.
#' @param start Start time (in seconds) of desired plotted area.
#' @param end End time (in seconds) of desired plotted area.
#' @param tfrom0 Logical; should time on the x-axis run from 0 or from the
#' original time? Default is `TRUE`.
#' @param scale String giving the frequency scale to use for plotting
#' spectrograms. Default is `hz`. Alternatives are `erb` (for the equivalent
#' rectangular bandwidth scale), `mel` (for the Mel scale, using 1000 Hz
#' as the corner frequency, following Fant 1968), or `logarithmic` (for
#' log Hz).
#' @param freqRange Vector of two integers giving the frequency range to be
#' used for plotting spectrograms. Default is `NULL`, in which case suitable
#' ranges are chosen based on the frequency scale. For `hz`, the default is
#' `c(0,5000)`; for `erb`, the default is `c(0.8,42)`; for `mel`, the default is
#' `c(30,4400)`. The latter two correspond roughly to the human auditory
#' frequency response.
#' @param windowLength Window length in seconds for generating spectrograms.
#' Default is `0.005`.
#' @param dynamicRange Dynamic range in dB for generating spectrograms. The
#' maximum intensity minus `dynamicRange` will all be printed in white. Default
#' is `50`.
#' @param timeStep How many time steps should be calculated for spectrograms?
#' Default is `1000`. Note that this takes a while to plot, so for fiddling with
#' plotting parameters it is a good idea to choose a smaller value.
#' @param windowShape String giving the name of the window shape to be applied
#' to the signal when generating spectrograms. Default is `Gaussian`; other
#' options are `square`, `Hamming`, `Bartlett`, `Hanning`, or `Blackman`.
#' Note that the Gaussian window function provided by the `phonTools` package
#' and used in `praatpicture()` does not have the same properties as the
#' Gaussian window function used for spectral estimation in Praat; plotting
#' a simple sine wave with high dynamic range will produce sidelobes in
#' `praatpicture()` but not in Praat. It's recommended to use Blackman windows
#' instead if you have this problem.
#' @param colors Vector of strings giving the names of colors to be used
#' for plotting the spectrogram; default is `c('white', 'black')`. The first
#' value is used for plotting the lowest visible amplitude, and the last for
#' plotting the highest visible amplitude. Vectors with more than two color
#' names can be used for plotting values in between in different colors.
#' @param pitch_plotOnSpec Boolean; should pitch be plotted on top of
#' spectrogram? Default is `FALSE`.
#' @param pt Pitch object loaded using [rPraat::pt.read] or similar object.
#' @param pitch_plotType String giving the type of pitch plot to produce; default
#' is `draw` (a line plot), the only other option is `speckle` (a point plot).
#' Alternatively a vector `c('draw','speckle')` can be passed, in which case
#' both are used.
#' @param pitch_scale String giving the frequency scale to use when producing
#' pitch plots. Default is `hz`; other options are `logarithmic` (also in Hz),
#' `semitones`, `erb`, and `mel`.
#' @param pitch_freqRange Vector of two integers giving the frequency range to be
#' used for producing pitch plots. Default is `NULL`, in which case the pitch
#' range is automatically reset to `c(-12,30)` for the `semitones` scale,
#' `c(0,10)` for the `erb` scale, and `c(50,500)` for the Hz-based scales,
#' following Praat defaults.
#' @param pitch_axisLabel String giving the name of the label to print along the
#' y-axis when printing a pitch track. Default is `NULL`, in which case the
#' axis label will depend on the scale.
#' @param pitch_color String or vector of strings giving the name of the color
#' to be used for plotting pitch. Default is `'black'`. If a vector of two
#' strings is passed, the second color will be used for background highlighting.
#' @param pitch_highlight Named list giving parameters for differential
#' highlighting of pitch based on the time domain. This list
#' should contain information about which parts of the plot to highlight, either
#' done with the `start` and `end` arguments which must be numbers or numeric
#' vectors, or using the `tier` and `label` arguments to highlight based on
#' information in a plotted TextGrid. Further contains the optional arguments
#' `color` (string or vector of strings, see `pitch_color`),
#' `drawSize` or `speckleSize` (both numeric).
#' @param formant_plotOnSpec Boolean; should formants be plotted on top of
#' spectrogram? Default is `FALSE`.
#' @param fm Formant object loaded using [rPraat::formant.read] or similar
#' object.
#' @param formant_plotType String giving the type of formant plot to produce;
#' default is `speckle` (a point plot), the only other option is `draw` (a line
#' plot). Alternatively a vector `c('draw','speckle')` can be passed, in which
#' case both are used.
#' @param formant_dynamicRange Dynamic range in dB for producing formant plots.
#' When a formant plot of `formant_plotType='speckle'` is drawn, no formants are
#' shown in frames with intensity level `formant_dynamicRange` below the maximum
#' intensity. Default is `30`. If set to `0`, all formants are shown.
#' @param formant_color String or vector of strings giving the name(s) of
#' colors to be used for plotting formants. If one color is provided, all
#' formants will be plotted in this color. If multiple colors are provided,
#' different formants will be shown in different colors. Default is `'black'`.
#' If the length of this vector twice the number of formants plotted, the first
#' half of strings will be used for the formants' primary colors and the second
#' half will be used for background highlighting. If the length of this vector
#' is one more than the number of formants plotted, the last string will
#' be used for background highlighting.
#' @param formant_highlight Named list giving parameters for differential
#' highlighting of formants based on the time domain. This list
#' should contain information about which parts of the plot to highlight, either
#' done with the `start` and `end` arguments which must be numbers or numeric
#' vectors, or using the `tier` and `label` arguments to highlight based on
#' information in a plotted TextGrid. Further contains the optional arguments
#' `color` (string or vector of strings, see `formant_color`),
#' `drawSize` or `speckleSize` (both numeric).
#' @param intensity_plotOnSpec Boolean; should intensity be plotted on top of
#' spectrogram? Default is `FALSE`.
#' @param it Intensity object loaded using [rPraat::it.read] or similar object.
#' @param intensity_range Vector of two integers giving the intensity range to be
#' used for producing intensity plots. Default is `NULL`, in which case the
#' range is simply the minimum and maximum levels in the curve.
#' @param intensity_axisLabel String giving the name of the label to print along
#' the y-axis when plotting intensity. Default is `Intensity (dB)`.
#' @param intensity_color String or vector of strings giving the name of the
#' color to be used for plotting intensity. Default is `'black'`. If a vector of
#' two strings is passed, the second color will be used for background
#' highlighting.
#' @param intensity_highlight Named list giving parameters for differential
#' highlighting of the intensity contour based on the time domain. This list
#' should contain information about which parts of the plot to highlight, either
#' done with the `start` and `end` arguments which must be numbers or numeric
#' vectors, or using the `tier` and `label` arguments to highlight based on
#' information in a plotted TextGrid. Further contains the optional arguments
#' `color` (string or vector of strings, see `intensity_color`) and
#' `drawSize` (integer).
#' @param tgbool Logical; should dotted lines be plotted corresponding to
#' locations in a TextGrid? Default is `FALSE`.
#' @param lines Numeric vector giving locations in seconds of locations from
#' a TextGrid to be plotted with dotted lines. Default is `NULL`.
#' @param focusTierColor String or vector of strings giving the color(s) to
#' use for plotting focus tier lines. If multiple tiers are focused, a vector
#' of the same length can be passed, and the nth tier will be plotted in the
#' nth color. Default is `'black'`.
#' @param focusTierLineType String or vector of strings giving the line
#' type(s) for plotting focus tier lines. If multiple tiers are focused, a
#' vector of the same length can be passed, and the nth tier will be plotted in
#' the nth line type. Default is `'dotted'`.
#' @param ind Integer indexing waveform relative to other plot components.
#' Default is `NULL`.
#' @param min_max_only Logical; should only minimum and maximum values be given
#' on the y-axis? Default is `TRUE`. Can also be a logical vector if some but
#' not all plot components should have minimum and maximum values on the y-axis.
#' Ignored for TextGrid component.
#' @param highlight Named list giving parameters for differential
#' highlighting of the spectrogram based on the time domain. This list
#' should contain information about which parts of the plot to highlight, either
#' done with the `start` and `end` arguments which must be numbers or numeric
#' vectors, or using the `tier` and `label` arguments to highlight based on
#' information in a plotted TextGrid. Further contains the argument
#' `colors` (vector of strings, see `colors`). Can be nested lists to highlight
#' different parts of a figure in different ways.
#' @param axisLabel String giving the name of the label to print along the
#' y-axis when plotting a spectrogram. Default is `NULL`, in which case the
#' axis label will depend on the scale.
#' @param drawSize Number indicating the line width of plot components where
#' the `_plotType` is `'draw'` (i.e., pitch, formants, or intensity rendered as
#' line plots). Default is `1`. Controls the `lwd` argument of
#' [graphics::lines].
#' @param speckleSize Number indicating the point size of plot components where
#' the `_plotType` is `'speckle'` (i.e. pitch or formants rendered as point
#' plots). Default is `1`. Controls the `cex` arguments of [graphics::points].
#' @param suppressAxes Should all axes and the box around plots be suppressed?
#' Default is `FALSE`. Generally not recommended.
#'
#' @return No return values, called internally by [praatpicture] and sibling
#' functions.
#' @export
#'
#' @examples
#' # Don't use directly
#' datapath <- system.file('extdata', package='praatpicture')
#' soundFile <- paste0(datapath, '/1.wav')
#' praatpicture(soundFile, frames='spectrogram')
specplot <- function(sig, sr, t, start, end, tfrom0=TRUE, scale='hz',
                     freqRange=NULL, windowLength=0.005, dynamicRange=60,
                     timeStep=1000, windowShape='Gaussian',
                     colors=c('white', 'black'),
                     pitch_plotOnSpec=FALSE, pt=NULL,
                     pitch_plotType='draw', pitch_scale='hz',
                     pitch_freqRange=NULL, pitch_axisLabel=NULL,
                     pitch_color='black', pitch_highlight=NULL,
                     formant_plotOnSpec=FALSE, fm=NULL,
                     formant_plotType='speckle', formant_dynamicRange=30,
                     formant_color='black', formant_highlight=NULL,
                     intensity_plotOnSpec=FALSE, it=NULL, intensity_range=NULL,
                     intensity_axisLabel='Intensity (dB)',
                     intensity_color='black', intensity_highlight=NULL,
                     tgbool=FALSE, lines=NULL, focusTierColor='black',
                     focusTierLineType='dotted', ind=NULL,
                     min_max_only=TRUE, highlight=NULL,
                     axisLabel=NULL, drawSize=1, speckleSize=1,
                     suppressAxes=FALSE) {

  if (scale == 'logarithmic') {
    logsc <- 'y'
  } else {
    logsc <- ''
  }

  wl <- windowLength*1000
  ts <- -timeStep

  legal_ws <- c('square', 'Hamming', 'Bartlett', 'Hanning', 'Gaussian',
                'Blackman')
  if (!windowShape %in% legal_ws) {
    stop('Possible window shapes are square, Hamming, Bartlett, Hanning, ',
         'Gaussian, or Blackman.')
  }

  if (tfrom0) {
    org_start <- start
    start <- 0
  }

  if (windowShape == 'square') ws <- 'rectangular'
  if (windowShape == 'Hamming') ws <- 'hamming'
  if (windowShape == 'Bartlett') ws <- 'bartlett'
  if (windowShape == 'Hanning') ws <- 'hann'
  if (windowShape == 'Gaussian') ws <- 'gaussian'
  if (windowShape == 'Blackman') ws <- 'blackman'

  if (!min_max_only[ind]) {
    if (ind == 1) {
      yax <- 's'
    } else {
      ytix <- grDevices::axisTicks(freqRange, log=F)
      ytix <- ytix[-length(ytix)]
      yax <- 'n'
    }
  } else {
    yax <- 'n'
    ytix <- freqRange
  }

  spec <- phonTools::spectrogram(sig, sr, colors=F, show=F, timestep=ts,
                                 windowlength=wl, window=ws,
                                 dynamicrange=dynamicRange,
                                 windowparameter=0.4)

  if (scale %in% c('hz', 'log')) {
    hzFreqRange <- freqRange
  } else if (scale == 'erb') {
    hzFreqRange <- soundgen::otherToHz(freqRange, scale = 'ERB')
    if (formant_plotOnSpec) fm$frequencyArray <-
        soundgen::HzToOther(fm$frequencyArray, scale = 'ERB')
  } else {
    hzFreqRange <- 1000 * (2^(freqRange / 1000)-1)
    if (formant_plotOnSpec) fm$frequencyArray <-
        1000 / log(2) * log(1 + fm$frequencyArray / 1000)
  }

  freqdom <- as.numeric(unlist(dimnames(spec$spectrogram)[2]))
  within_freqran <- which(freqdom < hzFreqRange[2] & freqdom > hzFreqRange[1])
  spec$spectrogram <- spec$spectrogram[,within_freqran]

  time_s <- as.numeric(rownames(spec$spectrogram))/1000
  if (!tfrom0) time_s <- time_s + start
  rownames(spec$spectrogram) <- time_s

  if (grDevices::dev.capabilities()$rasterImage == 'yes' &
      scale == 'hz') {
    useRaster <- TRUE
  } else {
    useRaster <- FALSE
  }

  plot(NULL, xaxt='n', xlim=c(start, end+start),
       ylim=freqRange, yaxs='i', yaxt=yax, log = logsc)
  if (!suppressAxes) {
    if (!min_max_only[ind] & ind != 1) graphics::axis(2, at=ytix)
    if (min_max_only[ind]) graphics::axis(2, at=ytix, padj=c(0,1), las=2,
                                          tick=F)
    graphics::mtext(axisLabel, side=2, line=3.5, cex=0.8)
  }

  fillCol <- grDevices::colorRampPalette(colors)
  fillRan <- c(-dynamicRange, 0)
  nlevels <- abs(-dynamicRange - 0) * 1.2
  levels <- pretty(fillRan, nlevels)
  fillCol <- fillCol(length(levels) - 1)
  subdr <- which(spec$spectrogram < -dynamicRange)
  spec$spectrogram[subdr] <- -dynamicRange

  specDims <- dim(spec$spectrogram)
  xVals <- seq(min(time_s), max(time_s), length.out=specDims[1])
  yVals <- seq(hzFreqRange[1], hzFreqRange[2], length.out=specDims[2])

  if (scale == 'mel') yVals <- emuR::mel(yVals)
  if (scale == 'erb') yVals <- soundgen::HzToOther(yVals, scale = 'ERB')

  graphics::image(x=xVals, y=yVals, z=spec$spectrogram,
                  col=fillCol, useRaster=useRaster, add=TRUE)

  if (!is.null(highlight)) {
    if (any(sapply(highlight, is.list))) {
      highlightLoopDepth <- sum(sapply(highlight, is.list))
    } else {
      highlightLoopDepth <- 1
    }
    for (i in 1:highlightLoopDepth) {
      if (any(sapply(highlight, is.list))) {
        thisHL <- highlight[[i]]
      } else {
        thisHL <- highlight
      }
      hlFillCol <- grDevices::colorRampPalette(thisHL$colors)
      hlFillCol <- hlFillCol(length(levels) - 1)
      for (int in 1:length(thisHL$start)) {
        times <- which(xVals > thisHL$start[int] & xVals < thisHL$end[int])
        highlight_t <- xVals[times]
        highlight_spec <- spec$spectrogram[times,]
        graphics::image(x=highlight_t, y=yVals, z=highlight_spec,
                        col=hlFillCol, useRaster=useRaster, add=TRUE)
      }
    }
  }

  create_fmtHighlighting <- function(highlight, fm, nf, dynamicRange, color,
                                     drawSize, speckleSize) {
    hl_t <- 0
    if (nf > 1) {
      hl_f <- array(rep(NA, nf), dim = c(nf,1))
    } else {
      hl_f <- NA
    }
    hl_i <- c()
    for (int in 1:length(highlight$start)) {
      times <- which(fm$t > highlight$start[int] & fm$t < highlight$end[int])
      hl_t <- c(hl_t, fm$t[times], max(fm$t[times] + 0.0001))
      if (nf > 1) {
        hl_f <- cbind(hl_f, fm$frequencyArray[,times], rep(NA, nf))
      } else {
        hl_f <- c(hl_f, fm$frequencyArray[times], NA)
      }
      hl_i <- c(hl_i, db[times], 0)
    }
    if (any(highlight$start < start)) {
      highlight$start[which(highlight$start < start)] <- start
    }
    if (any(highlight$end > end)) {
      highlight$end[which(highlight$end > end)] <- end
    }
    if (dynamicRange != 0) {
      hsubdr <- which(hl_i < max(db)-dynamicRange)
      if (length(hsubdr) == 0) hsubdr <- 1
    } else {
      hsubdr <- 1
    }

    if (!'color' %in% names(highlight)) highlight$color <- color
    if (!'drawSize' %in% names(highlight)) highlight$drawSize <- drawSize
    if (!'speckleSize' %in% names(highlight)) highlight$speckleSize <-
      speckleSize
    if (length(highlight$color) == 1) highlight$color <-
        rep(highlight$color, nf)
    if (length(highlight$color) == 2 & nf > 2) highlight$color <- c(
      rep(highlight$color[1], nf),
        rep(highlight$color[2], nf))
    if (length(highlight$color) == nf+1) highlight$color <- c(
      highlight$color, rep(highlight$color[nf+1], nf-1))
    return(list(params = highlight, hl_t = hl_t, hl_f = hl_f, subdr = hsubdr))
  }

  if (formant_plotOnSpec) {
    nf <- fm$maxnFormants
    if (length(formant_color) == 1) formant_color <- rep(formant_color, nf)
    if (length(formant_color) == 2 & nf > 2) formant_color <- c(
      rep(formant_color[1], nf), rep(formant_color[2], nf))
    if (length(formant_color) == nf+1) formant_color <- c(
      formant_color, rep(formant_color[nf+1], nf-1))
    if (tfrom0) fm$t <- fm$t - org_start
    if (fm$conv2db) {
      db <- gsignal::pow2db(fm$intensityVector)
    } else {
      db <- fm$intensityVector
    }
    if (formant_dynamicRange != 0) {
      subdr <- which(db < max(db)-formant_dynamicRange)
      if (length(subdr) == 0) subdr <- 1
    } else {
      subdr <- 1
    }

    if ('draw' %in% formant_plotType) {
      if (nf > 1) {
        if (length(formant_color) == nf*2) {
          graphics::lines(fm$t, fm$frequencyArray[1,], col=formant_color[nf+1],
                          lwd=drawSize+2)
        }
        graphics::lines(fm$t, fm$frequencyArray[1,], col=formant_color[1],
                        lwd=drawSize)
        for (i in 2:nf) {
          if (length(formant_color) == nf*2) {
            graphics::lines(fm$t, fm$frequencyArray[i,], lwd=drawSize+2,
                            col=formant_color[nf+i])
          }
          graphics::lines(fm$t, fm$frequencyArray[i,], col=formant_color[i],
                          lwd=drawSize)
        }
      } else {
        if (length(formant_color) == 2) {
          graphics::lines(fm$t, fm$frequencyArray, col=formant_color[2],
                          lwd=drawSize+2)
        }
        graphics::lines(fm$t, fm$frequencyArray, col=formant_color[1],
                        lwd=drawSize)
      }

      if (!is.null(formant_highlight)) {
        if (any(sapply(formant_highlight, is.list))) {
          highlightLoopDepth <- sum(sapply(formant_highlight, is.list))
        } else {
          highlightLoopDepth <- 1
        }
        for (i in 1:highlightLoopDepth) {
          if (any(sapply(formant_highlight, is.list))) {
            thisHL <- create_fmtHighlighting(formant_highlight[[i]], fm, nf,
                                          formant_dynamicRange, formant_color,
                                          drawSize, speckleSize)
          } else {
            thisHL <- create_fmtHighlighting(formant_highlight, fm, nf,
                                          formant_dynamicRange, formant_color,
                                          drawSize, speckleSize)
          }
          if (nf > 1) {
            if (length(thisHL$params$color) == nf*2) {
              graphics::lines(thisHL$hl_t, thisHL$hl_f[1,],
                              col=thisHL$params$color[nf+1],
                              lwd=thisHL$params$drawSize+2)
            }
            graphics::lines(thisHL$hl_t, thisHL$hl_f[1,],
                            col=thisHL$params$color[1],
                            lwd=thisHL$params$drawSize)
            for (j in 2:nf) {
              if (length(thisHL$params$color) == nf*2) {
                graphics::lines(thisHL$hl_t, thisHL$hl_f[j,],
                                lwd=thisHL$params$drawSize+2,
                                col=thisHL$params$color[nf+j])
              }
              graphics::lines(thisHL$hl_t, thisHL$hl_f[j,],
                              col=thisHL$params$color[j],
                              lwd=thisHL$params$drawSize)
            }
          } else {
            if (length(thisHL$params$color) == 2) {
              graphics::lines(thisHL$hl_t, thisHL$hl_f,
                              col=thisHL$params$color[2],
                              lwd=thisHL$params$drawSize+2)
            }
            graphics::lines(thisHL$hl_t, thisHL$hl_f,
                            col=thisHL$params$color[1],
                            lwd=thisHL$params$drawSize)
          }
        }
      }
    }
    if ('speckle' %in% formant_plotType) {
      if (nf > 1) {
        if (length(formant_color) == nf*2) {
          graphics::points(fm$t[-subdr], fm$frequencyArray[1,-subdr], pch=20,
                           cex=speckleSize, lwd=3,
                           col=formant_color[nf+1])
        }
        graphics::points(fm$t[-subdr], fm$frequencyArray[1,-subdr], pch=20,
                         col=formant_color[1], cex=speckleSize)
        for (i in 2:nf) {
          if (length(formant_color) == nf*2) {
            graphics::points(fm$t[-subdr], fm$frequencyArray[i,-subdr], pch=20,
                             lwd=3, col=formant_color[nf+i], cex=speckleSize)
          }
          graphics::points(fm$t[-subdr], fm$frequencyArray[i,-subdr], pch=20,
                           col=formant_color[i], cex=speckleSize)
        }
      } else {
        if (length(formant_color) == 2) {
          graphics::points(fm$t[-subdr], fm$frequencyArray[-subdr], pch=20,
                           cex=speckleSize, lwd=3,
                           col=formant_color[2])
        }
        graphics::points(fm$t[-subdr], fm$frequencyArray[-subdr], pch=20,
                         col=formant_color[1], cex=speckleSize)
      }

      if (!is.null(formant_highlight)) {
        if (any(sapply(formant_highlight, is.list))) {
          highlightLoopDepth <- sum(sapply(formant_highlight, is.list))
        } else {
          highlightLoopDepth <- 1
        }
        for (i in 1:highlightLoopDepth) {
          if (any(sapply(formant_highlight, is.list))) {
            thisHL <- create_fmtHighlighting(formant_highlight[[i]], fm, nf,
                                          formant_dynamicRange, formant_color,
                                          drawSize, speckleSize)
          } else {
            thisHL <- create_fmtHighlighting(formant_highlight, fm, nf,
                                          formant_dynamicRange, formant_color,
                                          drawSize, speckleSize)
          }
          if (nf > 1) {
            if (length(thisHL$params$color) == nf*2) {
              graphics::points(thisHL$hl_t[-thisHL$subdr],
                               thisHL$hl_f[1,-thisHL$subdr],
                               col=thisHL$params$color[nf+1], pch=20,
                               lwd=3, cex=thisHL$params$speckleSize)
            }
            graphics::points(thisHL$hl_t[-thisHL$subdr],
                             thisHL$hl_f[1,-thisHL$subdr],
                             col=thisHL$params$color[1], pch=20,
                             cex=thisHL$params$speckleSize)
            for (j in 2:nf) {
              if (length(thisHL$params$color) == nf*2) {
                graphics::points(thisHL$hl_t[-thisHL$subdr],
                                 thisHL$hl_f[j,-thisHL$subdr],
                                 col=thisHL$params$color[nf+j], pch=20,
                                 lwd=3, cex=thisHL$params$speckleSize)
              }
              graphics::points(thisHL$hl_t[-thisHL$subdr],
                               thisHL$hl_f[j,-thisHL$subdr],
                               col=thisHL$params$color[j], pch=20,
                               cex=thisHL$params$speckleSize)
            }
          } else {
            if (length(thisHL$params$color) == 2) {
              graphics::points(thisHL$hl_t[-thisHL$subdr],
                               thisHL$hl_f[-thisHL$subdr],
                               col=thisHL$params$color[2], pch=20,
                               lwd=3, cex=thisHL$params$speckleSize)
            }
            graphics::points(thisHL$hl_t[-thisHL$subdr],
                             thisHL$hl_f[-thisHL$subdr],
                             col=thisHL$params$color[1], pch=20,
                             cex=thisHL$params$speckleSize)
          }
        }
      }

    }
  }

  if (pitch_plotOnSpec) {
    pitch_overlay(pt, freqRange[1], freqRange[2], start, org_start, tfrom0,
                  pitch_freqRange, pitch_plotType, pitch_scale, pitch_color,
                  ind, drawSize, speckleSize, pitch_axisLabel, min_max_only,
                  pitch_highlight, intensity_plotOnSpec, suppressAxes)
  }

  if (intensity_plotOnSpec) {
    intensity_overlay(it, freqRange[1], freqRange[2], start, org_start,
                      tfrom0, intensity_range, intensity_color, ind, drawSize,
                      intensity_axisLabel, min_max_only, intensity_highlight,
                      pitch_plotOnSpec, suppressAxes)
  }

  if (tgbool) {
    for (i in 1:length(lines)) {
      graphics::abline(v=lines[[i]], col=focusTierColor[i],
                       lty=focusTierLineType[i])
    }
  }

}
