Skip to content

Instantly share code, notes, and snippets.

@chaoslogick
Created December 25, 2022 05:14
Show Gist options
  • Save chaoslogick/15882a81f166389b40c9619c42c4a62d to your computer and use it in GitHub Desktop.
Save chaoslogick/15882a81f166389b40c9619c42c4a62d to your computer and use it in GitHub Desktop.
RB: Sample chunker
require 'ruby-audio'
# Open the original audio file
song = RubyAudio::Sound.open("/Users/username/Desktop/filename.wav")
chunk_length_ms = 1000 # chunk length in milliseconds
chunk_length_samples = chunk_length_ms * song.info.samplerate / 1000 # convert chunk length to number of samples
# Make chunks of one second
chunks = []
song.each_buffer(chunk_length_samples) do |buffer|
chunks << buffer
end
# Export all of the individual chunks as wav files
chunks.each_with_index do |chunk, i|
chunk_name = "/Users/username/Desktop/chunks/filename#{i}.wav"
RubyAudio::Sound.new(chunk, song.info).save(chunk_name)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment