Class: RubyDSP::AudioTrack
- Inherits:
-
Object
- Object
- RubyDSP::AudioTrack
- Defined in:
- lib/ruby_dsp.rb,
stubs/ruby_dsp/audio_track.rb
Overview
A high-performance audio track processor backed by miniaudio.
This class heavily utilizes a fluent API pattern. Most processing methods end
in ! to indicate that they destructively mutate the audio data in C++ memory
for maximum performance with zero Garbage Collection overhead. These methods
return self to allow for clean method chaining.
Instance Attribute Summary collapse
-
#channels ⇒ Integer
readonly
The number of audio channels.
-
#file_name ⇒ String
readonly
The path to the loaded audio file.
-
#is_mono? ⇒ Boolean
readonly
True if the track has exactly 1 channel.
-
#sample_count ⇒ Integer
readonly
Number of samples in
samples. -
#sample_rate ⇒ Integer
readonly
The sample rate of the track in Hz.
-
#samples ⇒ Array<Float>
readonly
Vector of samples from the audio file.
Instance Method Summary collapse
-
#add_wave!(wave_type, frequency, duration_sec, start_sec = -1.0,, amplitude = 1.0) ⇒ AudioTrack
Generates and mixes a mathematical waveform into the track.
-
#band_pass!(cutoff_freq) ⇒ AudioTrack
Applies a 2nd-order band-pass filter to the track.
-
#clip! ⇒ AudioTrack
Destructively clamps all audio samples to the standard [-1.0, 1.0] range.
-
#dup ⇒ AudioTrack
(also: #clone)
Creates a deep copy of the audio track in memory.
-
#duration ⇒ Float
Calculates the total duration of the track.
-
#fade_in!(duration_sec) ⇒ AudioTrack
Applies a linear fade-in to the beginning of the audio track.
-
#fade_out!(duration_sec) ⇒ AudioTrack
Applies a linear fade-out to the end of the audio track.
-
#framed_rms(frame_length = 2048, hop_length = 512) ⇒ Array<Array<Float>>
Calculates the framed Root Mean Square (RMS) over time.
-
#framed_zcr(frame_length = 2048, hop_length = 512) ⇒ Array<Array<Float>>
Calculates the framed Zero Crossing Rate (ZCR) over time.
-
#high_pass!(cutoff_freq) ⇒ AudioTrack
Applies a 2nd-order high-pass filter to the track.
-
#high_shelf!(cutoff_freq, gain_db, q = 0.707) ⇒ AudioTrack
Applies a 2nd-order high-shelf filter.
-
#initialize(file_name = '', target_channels = 0, target_sample_rate = 0, start_sec = 0.0, duration_sec = 0.0) ⇒ AudioTrack
constructor
Initializes a new AudioTrack.
-
#low_pass!(cutoff_freq) ⇒ AudioTrack
Applies a high-order low-pass filter (Butterworth) to the track.
-
#low_shelf!(cutoff_freq, gain_db, q = 0.707) ⇒ AudioTrack
Applies a 2nd-order low-shelf filter.
-
#normalize!(target_db = -10.0)) ⇒ AudioTrack
Normalizes the audio track to a specific peak decibel level.
-
#notch!(center_freq, q = 0.707) ⇒ AudioTrack
Applies a 2nd-order notch filter to surgically remove a specific frequency.
-
#pad!(head_sec = 0.0, tail_sec = 0.0) ⇒ AudioTrack
Pads the audio track with digital silence (0.0) at the beginning and/or end.
-
#pad_to_duration!(target_duration_sec) ⇒ AudioTrack
Pads the audio track with digital silence so that it reaches an exact target duration.
-
#peak_amp ⇒ Float
Finds the maximum absolute amplitude across all channels.
-
#peak_eq!(center_freq, gain_db, q = 0.707) ⇒ AudioTrack
Applies a 2nd-order peaking EQ filter to boost or cut a specific frequency band.
-
#resample!(target_rate = 0) ⇒ AudioTrack
Destructively resamples the track to the target rate using linear resampling.
-
#rms ⇒ Array<Float>
Calculates the Root Mean Square (RMS) for the entire track, per channel.
-
#save_track(out_file, format = :auto) ⇒ Boolean
Saves the audio track to disk.
-
#silence_bounds(threshold_db = -60.0,, frame_length = 2048, hop_length = 512) ⇒ Array<Integer>
Finds the start and end sample indices of non-silent audio.
-
#to_mono! ⇒ AudioTrack
Destructively converts the track to mono by averaging the channels.
-
#to_s ⇒ String
A formatted summary of the track.
-
#trim_silence!(threshold_db = -60.0,, frame_length = 2048, hop_length = 512) ⇒ AudioTrack
Destructively trims leading and trailing silence from the track's internal sample array.
-
#zcr ⇒ Array<Float>
Calculates the Zero Crossing Rate (ZCR) for the entire track, per channel.
Constructor Details
#initialize(file_name = '', target_channels = 0, target_sample_rate = 0, start_sec = 0.0, duration_sec = 0.0) ⇒ AudioTrack
Initializes a new AudioTrack.
Decodes the given file using miniaudio. If file_name is an empty string "",
it initializes an empty, blank audio canvas for synthesis and sequencing.
49 50 |
# File 'stubs/ruby_dsp/audio_track.rb', line 49 def initialize(file_name = '', target_channels = 0, target_sample_rate = 0, start_sec = 0.0, duration_sec = 0.0) end |
Instance Attribute Details
#channels ⇒ Integer (readonly)
Returns the number of audio channels.
15 16 17 |
# File 'stubs/ruby_dsp/audio_track.rb', line 15 def channels @channels end |
#file_name ⇒ String (readonly)
Returns the path to the loaded audio file.
12 13 14 |
# File 'stubs/ruby_dsp/audio_track.rb', line 12 def file_name @file_name end |
#is_mono? ⇒ Boolean (readonly)
Returns true if the track has exactly 1 channel.
21 22 23 |
# File 'stubs/ruby_dsp/audio_track.rb', line 21
def is_mono?
@is_mono?
end
|
#sample_count ⇒ Integer (readonly)
Returns number of samples in samples.
27 28 29 |
# File 'stubs/ruby_dsp/audio_track.rb', line 27 def sample_count @sample_count end |
#sample_rate ⇒ Integer (readonly)
Returns the sample rate of the track in Hz.
18 19 20 |
# File 'stubs/ruby_dsp/audio_track.rb', line 18 def sample_rate @sample_rate end |
#samples ⇒ Array<Float> (readonly)
Returns vector of samples from the audio file.
24 25 26 |
# File 'stubs/ruby_dsp/audio_track.rb', line 24 def samples @samples end |
Instance Method Details
#add_wave!(wave_type, frequency, duration_sec, start_sec = -1.0,, amplitude = 1.0) ⇒ AudioTrack
Generates and mixes a mathematical waveform into the track.
Dynamically resizes the track if the wave extends past the current duration. If a wave overlaps with existing audio data, it is mixed (added) together, allowing for polyphony.
243 244 |
# File 'stubs/ruby_dsp/audio_track.rb', line 243 def add_wave!(wave_type, frequency, duration_sec, start_sec = -1.0, amplitude = 1.0) end |
#band_pass!(cutoff_freq) ⇒ AudioTrack
Applies a 2nd-order band-pass filter to the track.
Preserves frequencies around the cutoff, heavily attenuating both higher and lower frequencies.
287 288 |
# File 'stubs/ruby_dsp/audio_track.rb', line 287 def band_pass!(cutoff_freq) end |
#clip! ⇒ AudioTrack
Destructively clamps all audio samples to the standard [-1.0, 1.0] range.
This prevents harsh digital clipping when exporting polyphonic or boosted audio.
251 252 |
# File 'stubs/ruby_dsp/audio_track.rb', line 251 def clip! end |
#dup ⇒ AudioTrack Also known as: clone
Creates a deep copy of the audio track in memory.
Since most processing methods mutate the track in-place for performance,
use dup when you want to branch off a new version of the audio while
preserving the original state.
62 63 |
# File 'stubs/ruby_dsp/audio_track.rb', line 62 def dup end |
#duration ⇒ Float
Calculates the total duration of the track.
93 94 |
# File 'stubs/ruby_dsp/audio_track.rb', line 93 def duration end |
#fade_in!(duration_sec) ⇒ AudioTrack
Applies a linear fade-in to the beginning of the audio track.
192 193 |
# File 'stubs/ruby_dsp/audio_track.rb', line 192 def fade_in!(duration_sec) end |
#fade_out!(duration_sec) ⇒ AudioTrack
Applies a linear fade-out to the end of the audio track.
202 203 |
# File 'stubs/ruby_dsp/audio_track.rb', line 202 def fade_out!(duration_sec) end |
#framed_rms(frame_length = 2048, hop_length = 512) ⇒ Array<Array<Float>>
Calculates the framed Root Mean Square (RMS) over time.
134 135 |
# File 'stubs/ruby_dsp/audio_track.rb', line 134 def framed_rms(frame_length = 2048, hop_length = 512) end |
#framed_zcr(frame_length = 2048, hop_length = 512) ⇒ Array<Array<Float>>
Calculates the framed Zero Crossing Rate (ZCR) over time.
148 149 |
# File 'stubs/ruby_dsp/audio_track.rb', line 148 def framed_zcr(frame_length = 2048, hop_length = 512) end |
#high_pass!(cutoff_freq) ⇒ AudioTrack
Applies a 2nd-order high-pass filter to the track.
Attenuates frequencies below the cutoff, letting higher frequencies pass through.
275 276 |
# File 'stubs/ruby_dsp/audio_track.rb', line 275 def high_pass!(cutoff_freq) end |
#high_shelf!(cutoff_freq, gain_db, q = 0.707) ⇒ AudioTrack
Applies a 2nd-order high-shelf filter.
Boosts or cuts frequencies above the cutoff without affecting lower frequencies.
338 339 |
# File 'stubs/ruby_dsp/audio_track.rb', line 338 def high_shelf!(cutoff_freq, gain_db, q = 0.707) end |
#low_pass!(cutoff_freq) ⇒ AudioTrack
Applies a high-order low-pass filter (Butterworth) to the track.
Attenuates frequencies above the cutoff, letting lower frequencies pass through.
263 264 |
# File 'stubs/ruby_dsp/audio_track.rb', line 263 def low_pass!(cutoff_freq) end |
#low_shelf!(cutoff_freq, gain_db, q = 0.707) ⇒ AudioTrack
Applies a 2nd-order low-shelf filter.
Boosts or cuts frequencies below the cutoff without affecting higher frequencies.
324 325 |
# File 'stubs/ruby_dsp/audio_track.rb', line 324 def low_shelf!(cutoff_freq, gain_db, q = 0.707) end |
#normalize!(target_db = -10.0)) ⇒ AudioTrack
Normalizes the audio track to a specific peak decibel level.
182 183 |
# File 'stubs/ruby_dsp/audio_track.rb', line 182 def normalize!(target_db = -10.0) end |
#notch!(center_freq, q = 0.707) ⇒ AudioTrack
Applies a 2nd-order notch filter to surgically remove a specific frequency.
298 299 |
# File 'stubs/ruby_dsp/audio_track.rb', line 298 def notch!(center_freq, q = 0.707) end |
#pad!(head_sec = 0.0, tail_sec = 0.0) ⇒ AudioTrack
Pads the audio track with digital silence (0.0) at the beginning and/or end.
213 214 |
# File 'stubs/ruby_dsp/audio_track.rb', line 213 def pad!(head_sec = 0.0, tail_sec = 0.0) end |
#pad_to_duration!(target_duration_sec) ⇒ AudioTrack
Pads the audio track with digital silence so that it reaches an exact target duration. The padding is distributed evenly to both the head and the tail, effectively centering the audio.
224 225 |
# File 'stubs/ruby_dsp/audio_track.rb', line 224 def pad_to_duration!(target_duration_sec) end |
#peak_amp ⇒ Float
Finds the maximum absolute amplitude across all channels.
99 100 |
# File 'stubs/ruby_dsp/audio_track.rb', line 99 def peak_amp end |
#peak_eq!(center_freq, gain_db, q = 0.707) ⇒ AudioTrack
Applies a 2nd-order peaking EQ filter to boost or cut a specific frequency band.
310 311 |
# File 'stubs/ruby_dsp/audio_track.rb', line 310 def peak_eq!(center_freq, gain_db, q = 0.707) end |
#resample!(target_rate = 0) ⇒ AudioTrack
Destructively resamples the track to the target rate using linear resampling.
120 121 |
# File 'stubs/ruby_dsp/audio_track.rb', line 120 def resample!(target_rate = 0) end |
#rms ⇒ Array<Float>
Calculates the Root Mean Square (RMS) for the entire track, per channel.
126 127 |
# File 'stubs/ruby_dsp/audio_track.rb', line 126 def rms end |
#save_track(out_file, format = :auto) ⇒ Boolean
Saves the audio track to disk.
The format can be inferred from the out_file extension, or explicitly forced
via the format argument. If no extension or format is provided, it defaults
to saving as a WAV file and will append the .wav extension automatically.
Note: Currently, only the WAV format (:wav) is supported for encoding.
87 88 |
# File 'stubs/ruby_dsp/audio_track.rb', line 87 def save_track(out_file, format = :auto) end |
#silence_bounds(threshold_db = -60.0,, frame_length = 2048, hop_length = 512) ⇒ Array<Integer>
Finds the start and end sample indices of non-silent audio.
This scans the track's framed RMS energy and compares it against the global peak. Any frame that falls below the top_db threshold relative to the peak is considered silent.
160 161 |
# File 'stubs/ruby_dsp/audio_track.rb', line 160 def silence_bounds(threshold_db = -60.0, frame_length = 2048, hop_length = 512) end |
#to_mono! ⇒ AudioTrack
Destructively converts the track to mono by averaging the channels.
109 110 |
# File 'stubs/ruby_dsp/audio_track.rb', line 109 def to_mono! end |
#to_s ⇒ String
Returns a formatted summary of the track.
342 343 |
# File 'stubs/ruby_dsp/audio_track.rb', line 342 def to_s end |
#trim_silence!(threshold_db = -60.0,, frame_length = 2048, hop_length = 512) ⇒ AudioTrack
Destructively trims leading and trailing silence from the track's internal sample array.
172 173 |
# File 'stubs/ruby_dsp/audio_track.rb', line 172 def trim_silence!(threshold_db = -60.0, frame_length = 2048, hop_length = 512) end |
#zcr ⇒ Array<Float>
Calculates the Zero Crossing Rate (ZCR) for the entire track, per channel.
140 141 |
# File 'stubs/ruby_dsp/audio_track.rb', line 140 def zcr end |