ruby_dsp 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/ext/ruby_dsp/ruby_dsp.cpp +78 -10
- data/ext/ruby_dsp/vendor/pocketfft_hdronly.h +3760 -0
- data/lib/ruby_dsp/version.rb +1 -1
- data/stubs/ruby_dsp/audio_track.rb +86 -1
- metadata +2 -1
data/lib/ruby_dsp/version.rb
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
module RubyDSP
|
|
4
4
|
# A high-performance audio track processor backed by miniaudio.
|
|
5
|
+
#
|
|
6
|
+
# This class heavily utilizes a fluent API pattern. Most processing methods end
|
|
7
|
+
# in `!` to indicate that they destructively mutate the audio data in C++ memory
|
|
8
|
+
# for maximum performance with zero Garbage Collection overhead. These methods
|
|
9
|
+
# return `self` to allow for clean method chaining.
|
|
5
10
|
class AudioTrack
|
|
6
11
|
# @return [String] the path to the loaded audio file
|
|
7
12
|
attr_reader :file_name
|
|
@@ -26,12 +31,37 @@ module RubyDSP
|
|
|
26
31
|
# Decodes the given file using miniaudio. If `file_name` is an empty string `""`,
|
|
27
32
|
# it initializes an empty, blank audio canvas for synthesis and sequencing.
|
|
28
33
|
#
|
|
34
|
+
# @example Load an entire audio file
|
|
35
|
+
# track = RubyDSP::AudioTrack.new("vocals.wav")
|
|
36
|
+
#
|
|
37
|
+
# @example Load a 10-second snippet starting at the 1-minute mark
|
|
38
|
+
# snippet = RubyDSP::AudioTrack.new("podcast.mp3", 0, 0, 60.0, 10.0)
|
|
39
|
+
#
|
|
40
|
+
# @example Initialize a blank mono canvas at 48kHz
|
|
41
|
+
# canvas = RubyDSP::AudioTrack.new("", 1, 48000)
|
|
42
|
+
#
|
|
29
43
|
# @param file_name [String] Path to the audio file, or `""` for a blank track.
|
|
30
44
|
# @param target_channels [Integer] Optional. Force a specific number of channels (Defaults to 1 for blank tracks).
|
|
31
45
|
# @param target_sample_rate [Integer] Optional. Force a specific sample rate (Defaults to 44100 for blank tracks).
|
|
46
|
+
# @param start_sec [Float] Optional. The timestamp in seconds to start reading from. Defaults to 0.0.
|
|
47
|
+
# @param duration_sec [Float] Optional. The length of audio to read in seconds. Defaults to 0.0 (reads to the end).
|
|
32
48
|
# @raise [RuntimeError] if a file is provided but cannot be processed or read.
|
|
33
|
-
def initialize(file_name = '', target_channels = 0, target_sample_rate = 0)
|
|
49
|
+
def initialize(file_name = '', target_channels = 0, target_sample_rate = 0, start_sec = 0.0, duration_sec = 0.0)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Creates a deep copy of the audio track in memory.
|
|
53
|
+
#
|
|
54
|
+
# Since most processing methods mutate the track in-place for performance,
|
|
55
|
+
# use `dup` when you want to branch off a new version of the audio while
|
|
56
|
+
# preserving the original state.
|
|
57
|
+
#
|
|
58
|
+
# @example Non-destructive chaining
|
|
59
|
+
# clean_vocals = track.dup.high_pass!(100).normalize!
|
|
60
|
+
#
|
|
61
|
+
# @return [AudioTrack] A new, independent instance of the track.
|
|
62
|
+
def dup
|
|
34
63
|
end
|
|
64
|
+
alias clone dup
|
|
35
65
|
|
|
36
66
|
# Saves the audio track to disk.
|
|
37
67
|
#
|
|
@@ -71,6 +101,9 @@ module RubyDSP
|
|
|
71
101
|
|
|
72
102
|
# Destructively converts the track to mono by averaging the channels.
|
|
73
103
|
#
|
|
104
|
+
# @example
|
|
105
|
+
# track.to_mono!
|
|
106
|
+
#
|
|
74
107
|
# @return [AudioTrack] self for method chaining.
|
|
75
108
|
# @raise [RuntimeError] if channel count is invalid.
|
|
76
109
|
def to_mono!
|
|
@@ -78,6 +111,9 @@ module RubyDSP
|
|
|
78
111
|
|
|
79
112
|
# Destructively resamples the track to the target rate using linear resampling.
|
|
80
113
|
#
|
|
114
|
+
# @example Resample to standard CD quality
|
|
115
|
+
# track.resample!(44100)
|
|
116
|
+
#
|
|
81
117
|
# @param target_rate [Integer] The new sample rate in Hz.
|
|
82
118
|
# @return [AudioTrack] self for method chaining.
|
|
83
119
|
# @raise [RuntimeError] if the resampler fails to initialize or process.
|
|
@@ -126,6 +162,9 @@ module RubyDSP
|
|
|
126
162
|
|
|
127
163
|
# Destructively trims leading and trailing silence from the track's internal sample array.
|
|
128
164
|
#
|
|
165
|
+
# @example Trim anything quieter than -50dB from the start and end
|
|
166
|
+
# track.trim_silence!(-50.0)
|
|
167
|
+
#
|
|
129
168
|
# @param threshold_db [Float] The threshold in decibels below the peak RMS to consider as silence. Default is -60.0.
|
|
130
169
|
# @param frame_length [Integer] The number of samples per frame. Default is 2048.
|
|
131
170
|
# @param hop_length [Integer] The number of samples to advance each frame. Default is 512.
|
|
@@ -134,24 +173,40 @@ module RubyDSP
|
|
|
134
173
|
end
|
|
135
174
|
|
|
136
175
|
# Normalizes the audio track to a specific peak decibel level.
|
|
176
|
+
#
|
|
177
|
+
# @example Normalize peak to -1.0 dBFS
|
|
178
|
+
# track.normalize!(-1.0)
|
|
179
|
+
#
|
|
137
180
|
# @param target_db [Float] The target peak amplitude in dBFS. Defaults to -10.0.
|
|
138
181
|
# @return [AudioTrack] self for method chaining.
|
|
139
182
|
def normalize!(target_db = -10.0)
|
|
140
183
|
end
|
|
141
184
|
|
|
142
185
|
# Applies a linear fade-in to the beginning of the audio track.
|
|
186
|
+
#
|
|
187
|
+
# @example Fade in over 1.5 seconds
|
|
188
|
+
# track.fade_in!(1.5)
|
|
189
|
+
#
|
|
143
190
|
# @param duration_sec [Float] The length of the fade-in in seconds.
|
|
144
191
|
# @return [AudioTrack] self for method chaining.
|
|
145
192
|
def fade_in!(duration_sec)
|
|
146
193
|
end
|
|
147
194
|
|
|
148
195
|
# Applies a linear fade-out to the end of the audio track.
|
|
196
|
+
#
|
|
197
|
+
# @example Fade out over the last 2 seconds
|
|
198
|
+
# track.fade_out!(2.0)
|
|
199
|
+
#
|
|
149
200
|
# @param duration_sec [Float] The length of the fade-out in seconds.
|
|
150
201
|
# @return [AudioTrack] self for method chaining.
|
|
151
202
|
def fade_out!(duration_sec)
|
|
152
203
|
end
|
|
153
204
|
|
|
154
205
|
# Pads the audio track with digital silence (0.0) at the beginning and/or end.
|
|
206
|
+
#
|
|
207
|
+
# @example Add 1 second of silence to the start, and 0.5 to the end
|
|
208
|
+
# track.pad!(1.0, 0.5)
|
|
209
|
+
#
|
|
155
210
|
# @param head_sec [Float] Seconds of silence to add to the beginning. Defaults to 0.0.
|
|
156
211
|
# @param tail_sec [Float] Seconds of silence to add to the end. Defaults to 0.0.
|
|
157
212
|
# @return [AudioTrack] self for method chaining.
|
|
@@ -160,6 +215,10 @@ module RubyDSP
|
|
|
160
215
|
|
|
161
216
|
# Pads the audio track with digital silence so that it reaches an exact target duration.
|
|
162
217
|
# The padding is distributed evenly to both the head and the tail, effectively centering the audio.
|
|
218
|
+
#
|
|
219
|
+
# @example Center a 3-second sound effect into exactly a 5-second window
|
|
220
|
+
# track.pad_to_duration!(5.0)
|
|
221
|
+
#
|
|
163
222
|
# @param target_duration_sec [Float] The desired total length of the track in seconds.
|
|
164
223
|
# @return [AudioTrack] self for method chaining.
|
|
165
224
|
def pad_to_duration!(target_duration_sec)
|
|
@@ -170,6 +229,11 @@ module RubyDSP
|
|
|
170
229
|
# Dynamically resizes the track if the wave extends past the current duration.
|
|
171
230
|
# If a wave overlaps with existing audio data, it is mixed (added) together, allowing for polyphony.
|
|
172
231
|
#
|
|
232
|
+
# @example Create a polyphonic C Major chord
|
|
233
|
+
# track.add_wave!("sine", 261.63, 2.0) # C4
|
|
234
|
+
# .add_wave!("sine", 329.63, 2.0, 0.0) # E4 (starts at 0.0)
|
|
235
|
+
# .add_wave!("sine", 392.00, 2.0, 0.0) # G4 (starts at 0.0)
|
|
236
|
+
#
|
|
173
237
|
# @param wave_type [String] The shape of the waveform (`"sine"`, `"square"`, `"sawtooth"`, `"noise"`).
|
|
174
238
|
# @param frequency [Float] The frequency of the wave in Hz (e.g., 440.0).
|
|
175
239
|
# @param duration_sec [Float] The length of the generated wave in seconds.
|
|
@@ -191,6 +255,9 @@ module RubyDSP
|
|
|
191
255
|
#
|
|
192
256
|
# Attenuates frequencies above the cutoff, letting lower frequencies pass through.
|
|
193
257
|
#
|
|
258
|
+
# @example Cut out harsh high frequencies above 5kHz
|
|
259
|
+
# track.low_pass!(5000)
|
|
260
|
+
#
|
|
194
261
|
# @param cutoff_freq [Integer, Float] The threshold frequency in Hz.
|
|
195
262
|
# @return [AudioTrack] self for method chaining.
|
|
196
263
|
def low_pass!(cutoff_freq)
|
|
@@ -200,6 +267,9 @@ module RubyDSP
|
|
|
200
267
|
#
|
|
201
268
|
# Attenuates frequencies below the cutoff, letting higher frequencies pass through.
|
|
202
269
|
#
|
|
270
|
+
# @example Cut out low-end rumble/wind noise below 80Hz
|
|
271
|
+
# track.high_pass!(80)
|
|
272
|
+
#
|
|
203
273
|
# @param cutoff_freq [Integer, Float] The threshold frequency in Hz.
|
|
204
274
|
# @return [AudioTrack] self for method chaining.
|
|
205
275
|
def high_pass!(cutoff_freq)
|
|
@@ -209,6 +279,9 @@ module RubyDSP
|
|
|
209
279
|
#
|
|
210
280
|
# Preserves frequencies around the cutoff, heavily attenuating both higher and lower frequencies.
|
|
211
281
|
#
|
|
282
|
+
# @example Create a "telephone" effect by isolating 1000Hz
|
|
283
|
+
# track.band_pass!(1000)
|
|
284
|
+
#
|
|
212
285
|
# @param cutoff_freq [Integer, Float] The center frequency in Hz.
|
|
213
286
|
# @return [AudioTrack] self for method chaining.
|
|
214
287
|
def band_pass!(cutoff_freq)
|
|
@@ -216,6 +289,9 @@ module RubyDSP
|
|
|
216
289
|
|
|
217
290
|
# Applies a 2nd-order notch filter to surgically remove a specific frequency.
|
|
218
291
|
#
|
|
292
|
+
# @example Eliminate 60Hz electrical hum
|
|
293
|
+
# track.notch!(60)
|
|
294
|
+
#
|
|
219
295
|
# @param center_freq [Integer, Float] The exact frequency to eliminate in Hz.
|
|
220
296
|
# @param q [Float] The resonance/width of the notch. Defaults to 0.707 (Butterworth).
|
|
221
297
|
# @return [AudioTrack] self for method chaining.
|
|
@@ -224,6 +300,9 @@ module RubyDSP
|
|
|
224
300
|
|
|
225
301
|
# Applies a 2nd-order peaking EQ filter to boost or cut a specific frequency band.
|
|
226
302
|
#
|
|
303
|
+
# @example Boost 3kHz by 2.5dB for vocal clarity
|
|
304
|
+
# track.peak_eq!(3000, 2.5)
|
|
305
|
+
#
|
|
227
306
|
# @param center_freq [Integer, Float] The target frequency in Hz.
|
|
228
307
|
# @param gain_db [Float] The amount to boost (positive) or cut (negative) in decibels.
|
|
229
308
|
# @param q [Float] The resonance/width of the bell curve. Defaults to 0.707.
|
|
@@ -235,6 +314,9 @@ module RubyDSP
|
|
|
235
314
|
#
|
|
236
315
|
# Boosts or cuts frequencies below the cutoff without affecting higher frequencies.
|
|
237
316
|
#
|
|
317
|
+
# @example Add a 3dB bass boost below 150Hz
|
|
318
|
+
# track.low_shelf!(150, 3.0)
|
|
319
|
+
#
|
|
238
320
|
# @param cutoff_freq [Integer, Float] The threshold frequency in Hz.
|
|
239
321
|
# @param gain_db [Float] The amount to boost (positive) or cut (negative) in decibels.
|
|
240
322
|
# @param q [Float] The resonance/slope of the shelf. Defaults to 0.707.
|
|
@@ -246,6 +328,9 @@ module RubyDSP
|
|
|
246
328
|
#
|
|
247
329
|
# Boosts or cuts frequencies above the cutoff without affecting lower frequencies.
|
|
248
330
|
#
|
|
331
|
+
# @example Reduce harsh treble above 8kHz by 4dB
|
|
332
|
+
# track.high_shelf!(8000, -4.0)
|
|
333
|
+
#
|
|
249
334
|
# @param cutoff_freq [Integer, Float] The threshold frequency in Hz.
|
|
250
335
|
# @param gain_db [Float] The amount to boost (positive) or cut (negative) in decibels.
|
|
251
336
|
# @param q [Float] The resonance/slope of the shelf. Defaults to 0.707.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ruby_dsp
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Radek C.
|
|
@@ -204,6 +204,7 @@ files:
|
|
|
204
204
|
- ext/ruby_dsp/extconf.rb
|
|
205
205
|
- ext/ruby_dsp/ruby_dsp.cpp
|
|
206
206
|
- ext/ruby_dsp/vendor/miniaudio.h
|
|
207
|
+
- ext/ruby_dsp/vendor/pocketfft_hdronly.h
|
|
207
208
|
- lib/ruby_dsp.rb
|
|
208
209
|
- lib/ruby_dsp/version.rb
|
|
209
210
|
- ruby_dsp.gemspec
|