Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add article for splitting channels in R #93

Open
atruskie opened this issue Feb 4, 2025 · 1 comment
Open

Add article for splitting channels in R #93

atruskie opened this issue Feb 4, 2025 · 1 comment
Assignees
Labels
documentation Improvements or additions to documentation duplicate This issue or pull request already exists

Comments

@atruskie
Copy link
Contributor

atruskie commented Feb 4, 2025

I have my recordings made on a stereo recorder but with the left and the right channels reflecting different experimental set ups

Request for help from website contact form

@atruskie atruskie added documentation Improvements or additions to documentation duplicate This issue or pull request already exists labels Feb 4, 2025
@atruskie
Copy link
Contributor Author

atruskie commented Feb 4, 2025

e.g.

# Function to split stereo WAV into two mono WAVs using ffmpeg
split_stereo_wav <- function(file_path) {
  # Ensure ffmpeg is installed
  if (system2("ffmpeg", "-version", stdout = TRUE, stderr = TRUE)[1] == "") {
    stop("ffmpeg not found. Please install ffmpeg and ensure it is in your system PATH.")
  }
  
  # Extract file name without extension and directory
  file_name <- tools::file_path_sans_ext(basename(file_path))
  file_dir <- dirname(file_path)
  
  # Construct output file paths
  left_channel_path <- file.path(file_dir, paste0(file_name, "_L.wav"))
  right_channel_path <- file.path(file_dir, paste0(file_name, "_R.wav"))
  
  # Construct ffmpeg command
  ffmpeg_cmd <- sprintf(
    "-i \"%s\" -map_channel 0.0.0 \"%s\" -map_channel 0.0.1 \"%s\"",
    file_path, left_channel_path, right_channel_path
  )
  
  # Run ffmpeg command
  system2("ffmpeg", args = ffmpeg_cmd, stdout = TRUE, stderr = TRUE)
  
  # Return output file paths
  return(list(left_channel = left_channel_path, right_channel = right_channel_path))
}

# Example usage
# split_stereo_wav("path/to/your/stereo.wav")

It would be great if we could find a native R way to do this as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants