Class: ColtraneSynth::Base

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

Constant Summary collapse

SAMPLE_RATE =
44000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



11
12
13
# File 'lib/coltrane_synth/base.rb', line 11

def initialize
  @device = CoreAudio.default_output_device
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



3
4
5
# File 'lib/coltrane_synth/base.rb', line 3

def buffer
  @buffer
end

#deviceObject (readonly)

Returns the value of attribute device.



3
4
5
# File 'lib/coltrane_synth/base.rb', line 3

def device
  @device
end

Class Method Details

.play(something, duration = 1.0) ⇒ Object



7
8
9
# File 'lib/coltrane_synth/base.rb', line 7

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

Instance Method Details

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



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/coltrane_synth/base.rb', line 15

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