Class: SPCore::SignalGenerator
- Inherits:
-
Object
- Object
- SPCore::SignalGenerator
- Includes:
- Hashmake::HashMakeable
- Defined in:
- lib/spcore/generation/signal_generator.rb
Overview
Provides methods for generating a Signal that contains test waveforms or noise.
Constant Summary collapse
- ARG_SPECS =
used to process hashed args in #initialize.
{ :size => arg_spec(:reqd => true, :type => Fixnum, :validator => ->(a){ a > 0 }), :sample_rate => arg_spec(:reqd => true, :type => Fixnum, :validator => ->(a){ a > 0 }) }
Instance Attribute Summary collapse
-
#sample_rate ⇒ Object
readonly
Returns the value of attribute sample_rate.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#initialize(args) ⇒ SignalGenerator
constructor
A new instance of SignalGenerator.
-
#make_noise(amplitude = 1.0) ⇒ Object
Generate a Signal object with noise data.
-
#make_signal(freqs, extra_osc_args = {}) ⇒ Object
Generate a Signal object with waveform data at the given frequencies.
Constructor Details
#initialize(args) ⇒ SignalGenerator
A new instance of SignalGenerator.
16 17 18 |
# File 'lib/spcore/generation/signal_generator.rb', line 16 def initialize args hash_make args, ARG_SPECS end |
Instance Attribute Details
#sample_rate ⇒ Object (readonly)
Returns the value of attribute sample_rate.
12 13 14 |
# File 'lib/spcore/generation/signal_generator.rb', line 12 def sample_rate @sample_rate end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
12 13 14 |
# File 'lib/spcore/generation/signal_generator.rb', line 12 def size @size end |
Instance Method Details
#make_noise(amplitude = 1.0) ⇒ Object
Generate a Signal object with noise data.
21 22 23 24 25 26 27 28 |
# File 'lib/spcore/generation/signal_generator.rb', line 21 def make_noise amplitude = 1.0 output = Array.new(@size) output.each_index do |i| output[i] = rand * amplitude end return Signal.new(:sample_rate => @sample_rate, :data => output) end |
#make_signal(freqs, extra_osc_args = {}) ⇒ Object
Generate a Signal object with waveform data at the given frequencies.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/spcore/generation/signal_generator.rb', line 31 def make_signal freqs, extra_osc_args = {} args = { :sample_rate => @sample_rate }.merge! extra_osc_args oscs = [] freqs.each do |freq| oscs.push Oscillator.new args.merge(:frequency => freq) end output = Array.new(@size, 0.0) @size.times do |n| oscs.each do |osc| output[n] += osc.sample end end return Signal.new(:sample_rate => @sample_rate, :data => output) end |