Lian Arzbecker

Postdoctoral researcher


Curriculum vitae


lian (at) arzbecker (dot) com


Motor Speech Disorders Lab

Communicative Disorders and Sciences, University at Buffalo



R scripts


Customizable scripts for simple tasks


Reading and writing the sampling rate of a wave file
1. Location of file
2. Target sampling rate
library(tuneR)

file_location <- "«C:\your\location\here.wav»"      # 01 USER INPUT
target_sampling_rate <- 48000                       # 02 USER INPUT

# Read audio file (may take a moment to run)
audio <- readWave(file_location)

# Print original sampling rate
sampling_rate <- [email protected]
print(paste("Original sampling rate:",
            sampling_rate, "Hz"))

# Check if resampling is needed
if (sampling_rate == target_sampling_rate) {
  print(paste("The audio is already at the target sampling rate.",
              "No resampling performed."))}

# Downsample to target sampling rate
else {
  audio_resampled <-
    downsample(audio,
               samp.rate = target_sampling_rate)
  
  # Create filename for resampled audio
  file_location_resampled <- sub
  "\\.wav$", "_resampled.wav", file_location)
  
  # Save resampled audio
  writeWave(audio_resampled, file_location_resampled)
  
  # Confirm resampled audio save location
  print(paste("Saved resampled file to:",
              file_location_resampled))}

Tools
Translate to