Class: Sound::Data
- Inherits:
-
Object
- Object
- Sound::Data
- Defined in:
- lib/sound/data.rb
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#pcm_data ⇒ Object
readonly
Returns the value of attribute pcm_data.
Instance Method Summary collapse
- #generate_sine_wave(freq, duration, volume) ⇒ Object (also: #sine_wave)
-
#initialize(format = Format.new) ⇒ Data
constructor
A new instance of Data.
Constructor Details
Instance Attribute Details
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
5 6 7 |
# File 'lib/sound/data.rb', line 5 def duration @duration end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
5 6 7 |
# File 'lib/sound/data.rb', line 5 def format @format end |
#pcm_data ⇒ Object (readonly)
Returns the value of attribute pcm_data.
5 6 7 |
# File 'lib/sound/data.rb', line 5 def pcm_data @pcm_data end |
Instance Method Details
#generate_sine_wave(freq, duration, volume) ⇒ Object Also known as: sine_wave
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sound/data.rb', line 10 def generate_sine_wave(freq, duration, volume) @pcm_data = [] @duration = duration ramp = 200.0 samples = (@format.sample_rate/2*duration/1000.0).floor samples.times do |sample| angle = (2.0*Math::PI*freq) * sample/samples * duration/1000 factor = Math.sin(angle) x = 32768.0*factor*volume if sample < ramp x *= sample/ramp end if samples - sample < ramp x *= (samples - sample)/ramp end @pcm_data << x.floor end self end |