Class: CW::Coreaudio

Inherits:
Object
  • Object
show all
Includes:
ToneHelpers
Defined in:
lib/cw/coreaudio.rb

Constant Summary

Constants included from ToneHelpers

ToneHelpers::TWO_PI

Instance Method Summary collapse

Methods included from ToneHelpers

#convert_words, #generate_space, #last_element?, #space_sample?

Constructor Details

#initializeCoreaudio

Returns a new instance of Coreaudio.



9
10
11
12
# File 'lib/cw/coreaudio.rb', line 9

def initialize
  dev = CoreAudio.default_output_device
  @buf = dev.output_buffer(8192)
end

Instance Method Details

#generate_silence(number_of_samples) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/cw/coreaudio.rb', line 36

def generate_silence(number_of_samples)
  ary = []
  number_of_samples.round.times do |sample_number|
    ary << 0.0
  end
  @buf << ary
end

#generate_tone(number_of_samples) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cw/coreaudio.rb', line 22

def generate_tone(number_of_samples)
  @sample_rate = 44100
  @frequency = 1000
  @w = (@frequency * TWO_PI) / @sample_rate
  ary = []
  number_of_samples.round.times do |sample_number|
    amplitude = ramp_filter(number_of_samples, sample_number)
    sine_radians = @w * sample_number
    temp = (amplitude * Math.sin(sine_radians) * 0x7FFF).round
    ary << temp
  end
  @buf << ary
end

#ramp_filter(size, count) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/cw/coreaudio.rb', line 14

def ramp_filter(size, count)
  @max_amplitude = 1
  ramp = 0.03
  ramp_point =  @max_amplitude / ramp
  ampl = (count < ramp_point) ? (ramp * count) : @max_amplitude
  (count > (size - ramp_point)) ? (ramp * (size - count)) : ampl
end

#startObject



44
45
46
# File 'lib/cw/coreaudio.rb', line 44

def start
  @buf.start
end

#stopObject



48
49
50
# File 'lib/cw/coreaudio.rb', line 48

def stop
  @buf.stop
end