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.
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
-
#duration ⇒ Float
Calculates the total duration of the track.
-
#fade_in!(duration_sec) ⇒ Boolean
Applies a linear fade-in to the beginning of the audio track.
-
#fade_out!(duration_sec) ⇒ Boolean
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.
-
#initialize(file_name = 'default.wav', target_channels = 0, target_sample_rate = 0) ⇒ AudioTrack
constructor
Initializes a new AudioTrack and decodes the given file.
-
#normalize!(target_db = -10.0)) ⇒ Boolean
Normalizes the audio track to a specific peak decibel level.
-
#pad!(head_sec = 0.0, tail_sec = 0.0) ⇒ Boolean
Pads the audio track with digital silence (0.0) at the beginning and/or end.
-
#pad_to_duration!(target_duration_sec) ⇒ Boolean
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.
-
#resample!(target_rate = 0) ⇒ Boolean
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! ⇒ Boolean
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) ⇒ Boolean
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 = 'default.wav', target_channels = 0, target_sample_rate = 0) ⇒ AudioTrack
Initializes a new AudioTrack and decodes the given file.
30 31 |
# File 'stubs/ruby_dsp/audio_track.rb', line 30 def initialize(file_name = 'default.wav', target_channels = 0, target_sample_rate = 0) end |
Instance Attribute Details
#channels ⇒ Integer (readonly)
Returns the number of audio channels.
10 11 12 |
# File 'stubs/ruby_dsp/audio_track.rb', line 10 def channels @channels end |
#file_name ⇒ String (readonly)
Returns the path to the loaded audio file.
7 8 9 |
# File 'stubs/ruby_dsp/audio_track.rb', line 7 def file_name @file_name end |
#is_mono? ⇒ Boolean (readonly)
Returns true if the track has exactly 1 channel.
16 17 18 |
# File 'stubs/ruby_dsp/audio_track.rb', line 16
def is_mono?
@is_mono?
end
|
#sample_count ⇒ Integer (readonly)
Returns number of samples in samples.
22 23 24 |
# File 'stubs/ruby_dsp/audio_track.rb', line 22 def sample_count @sample_count end |
#sample_rate ⇒ Integer (readonly)
Returns the sample rate of the track in Hz.
13 14 15 |
# File 'stubs/ruby_dsp/audio_track.rb', line 13 def sample_rate @sample_rate end |
#samples ⇒ Array<Float> (readonly)
Returns vector of samples from the audio file.
19 20 21 |
# File 'stubs/ruby_dsp/audio_track.rb', line 19 def samples @samples end |
Instance Method Details
#duration ⇒ Float
Calculates the total duration of the track.
60 61 |
# File 'stubs/ruby_dsp/audio_track.rb', line 60 def duration end |
#fade_in!(duration_sec) ⇒ Boolean
Applies a linear fade-in to the beginning of the audio track.
142 143 |
# File 'stubs/ruby_dsp/audio_track.rb', line 142 def fade_in!(duration_sec) end |
#fade_out!(duration_sec) ⇒ Boolean
Applies a linear fade-out to the end of the audio track.
148 149 |
# File 'stubs/ruby_dsp/audio_track.rb', line 148 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.
95 96 |
# File 'stubs/ruby_dsp/audio_track.rb', line 95 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.
109 110 |
# File 'stubs/ruby_dsp/audio_track.rb', line 109 def framed_zcr(frame_length = 2048, hop_length = 512) end |
#normalize!(target_db = -10.0)) ⇒ Boolean
Normalizes the audio track to a specific peak decibel level.
136 137 |
# File 'stubs/ruby_dsp/audio_track.rb', line 136 def normalize!(target_db = -10.0) end |
#pad!(head_sec = 0.0, tail_sec = 0.0) ⇒ Boolean
Pads the audio track with digital silence (0.0) at the beginning and/or end.
155 156 |
# File 'stubs/ruby_dsp/audio_track.rb', line 155 def pad!(head_sec = 0.0, tail_sec = 0.0) end |
#pad_to_duration!(target_duration_sec) ⇒ Boolean
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.
162 163 |
# File 'stubs/ruby_dsp/audio_track.rb', line 162 def pad_to_duration!(target_duration_sec) end |
#peak_amp ⇒ Float
Finds the maximum absolute amplitude across all channels.
66 67 |
# File 'stubs/ruby_dsp/audio_track.rb', line 66 def peak_amp end |
#resample!(target_rate = 0) ⇒ Boolean
Destructively resamples the track to the target rate using linear resampling.
81 82 |
# File 'stubs/ruby_dsp/audio_track.rb', line 81 def resample!(target_rate = 0) end |
#rms ⇒ Array<Float>
Calculates the Root Mean Square (RMS) for the entire track, per channel.
87 88 |
# File 'stubs/ruby_dsp/audio_track.rb', line 87 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.
54 55 |
# File 'stubs/ruby_dsp/audio_track.rb', line 54 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.
121 122 |
# File 'stubs/ruby_dsp/audio_track.rb', line 121 def silence_bounds(threshold_db = -60.0, frame_length = 2048, hop_length = 512) end |
#to_mono! ⇒ Boolean
Destructively converts the track to mono by averaging the channels.
73 74 |
# File 'stubs/ruby_dsp/audio_track.rb', line 73 def to_mono! end |
#to_s ⇒ String
Returns a formatted summary of the track.
166 167 |
# File 'stubs/ruby_dsp/audio_track.rb', line 166 def to_s end |
#trim_silence!(threshold_db = -60.0,, frame_length = 2048, hop_length = 512) ⇒ Boolean
Destructively trims leading and trailing silence from the track's internal sample array.
130 131 |
# File 'stubs/ruby_dsp/audio_track.rb', line 130 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.
101 102 |
# File 'stubs/ruby_dsp/audio_track.rb', line 101 def zcr end |