Skip to content

Instantly share code, notes, and snippets.

@tetlabo
Created March 25, 2021 16:24
Show Gist options
  • Save tetlabo/e398b6c23c1ec9e95c42ebfb530ba2fa to your computer and use it in GitHub Desktop.
Save tetlabo/e398b6c23c1ec9e95c42ebfb530ba2fa to your computer and use it in GitHub Desktop.
音声データ (MP3) をRでプロットする
library(tuneR)
library(ggplot2)
voice_orig <- readMP3(file.path("voice.mp3"))
# 音声の切り出し
start <- 0.6 * voice_orig@samp.rate
end <- 1.1 * voice_orig@samp.rate
voice <- voice_orig[start:end]
# データフレームへの変換
sample <- seq(1:length(voice@left))
time <- sample/voice@samp.rate
sample.left <- as.vector(cbind(voice@left))
voice_df <- data.frame(sample, time, sample.left)
# ノイズの付与
voice_df[["noise_sample.left"]] <- sample.left + runif(n = length(sample.left), min = min(sample.left) / 2, max = max(sample.left) / 2)
# プロット
ggplot(voice_df, aes(x = time, y = sample.left)) + geom_line() + theme_void()
ggsave("voice_clear.png", width = 10, height = 4, units = "cm")
ggplot(voice_df, aes(x = time, y = noise_sample.left)) + geom_line() + theme_void()
ggsave("voice_noise.png", width = 10, height = 4, units = "cm")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment