Class: ColtraneSynth::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/coltrane/synth/base.rb

Constant Summary collapse

SAMPLE_RATE =
44_000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



13
14
15
# File 'lib/coltrane/synth/base.rb', line 13

def initialize
  @device = CoreAudio.default_output_device
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



5
6
7
# File 'lib/coltrane/synth/base.rb', line 5

def buffer
  @buffer
end

#deviceObject (readonly)

Returns the value of attribute device.



5
6
7
# File 'lib/coltrane/synth/base.rb', line 5

def device
  @device
end

Class Method Details

.play(something, duration = 1.0) ⇒ Object



9
10
11
# File 'lib/coltrane/synth/base.rb', line 9

def self.play(something, duration = 1.0)
  new.play(*extract_frequencies(something), duration: duration)
end

Instance Method Details

#play(*freqs, duration: 1.0) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/coltrane/synth/base.rb', line 17

def play(*freqs, duration: 1.0)
  @device = CoreAudio.default_output_device
  buffer = @device.output_loop(SAMPLE_RATE)

  SAMPLE_RATE.times do |i|
    sound = freqs.reduce(0) { |sum, freq| Math.sin(phase(freq) * i) + sum }
    buffer[i] = (sound * (0x6AFF / freqs.size)).round
  end

  buffer.start
  sleep duration
  buffer.stop
end