Skip to content

Commit

Permalink
graph encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
maca committed Jan 24, 2020
1 parent 8963cc9 commit e882867
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
.rspec_status
.byebug_history
TAGS
/.gtm/
15 changes: 15 additions & 0 deletions lib/scruby/ugen/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def input_values
inputs.values.flatten
end

# TODO: no specs
def rate_index
E_RATES.index(rate)
end

# TODO: no specs
def output_specs #:nodoc:
[ E_RATES.index(rate) ]
end

# TODO: no specs
def channels_count
1
end

protected

def rate=(rate)
Expand Down
13 changes: 9 additions & 4 deletions lib/scruby/ugen/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,31 @@ def encode
[ encode_constants,
encode_controls,
encode_control_names,
encode_nodes
encode_nodes,
encode_variants
].join
end

private

def encode_controls
[ 0 ].pack("N")
encode_int32(0)
end

def encode_control_names
[ 0 ].pack("N")
encode_int32(0)
end

def encode_variants
encode_int16(0)
end

def encode_constants
encode_floats_array(constants.map(&:value))
end

def encode_nodes
[ nodes.size ].pack("N") + nodes.map(&:encode).join
encode_int32(nodes.size) + nodes.map(&:encode).join
end
end
end
Expand Down
18 changes: 3 additions & 15 deletions lib/scruby/ugen/graph/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ def initialize(value, graph)
def encode
[
encode_string(value.name),
encode_int8(rate_index),
encode_int8(value.rate_index),
encode_int32(inputs.count),
encode_int32(channels_count),
encode_int32(value.channels_count),
encode_int16(special_index),
collect_input_specs.flatten.pack("N*"),
output_specs.map { |i| encode_int8(i) }.join
value.output_specs.map { |i| encode_int8(i) }.join
].join
end

Expand All @@ -56,18 +56,6 @@ def special_index
0
end

def channels_count
1
end

def rate_index
E_RATES.index(rate)
end

def output_specs #:nodoc:
[ E_RATES.index(rate) ]
end

def collect_input_specs #:nodoc:
inputs.map(&:input_specs)
end
Expand Down
11 changes: 9 additions & 2 deletions lib/scruby/ugen/out.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ class Out < Ugen::Base
rates :control, :audio
inputs bus: nil, channels_array: nil

# @channels = []
# def output_specs; []; end
# TODO: no specs
def output_specs
[]
end

# TODO: no specs
def channels_count
0
end

class << self
def ar(bus, *inputs)
Expand Down
22 changes: 7 additions & 15 deletions spec/ugen/graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,21 @@
let(:ugen) { Ugen::Out.ar(1, sin_osc) }

let(:expected) do
%w(
00 00 00 03 43
dc 00 00 00 00 00 00 3f 80 00 00 00 00 00 00 00
00 00 00 00 00 00 02 06 53 69 6e 4f 73 63 02 00
00 00 02 00 00 00 01 00 00 ff ff ff ff 00 00 00
00 ff ff ff ff 00 00 00 01 02 03 4f 75 74 02 00
00 00 02 00 00 00 00 00 00 ff ff ff ff 00 00 00
02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
)
[ 0, 0, 0, 3, 67, -36, 0, 0, 0, 0, 0, 0, 63, -128, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 83, 105, 110, 79, 115, 99,
2, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0,
-1, -1, -1, -1, 0, 0, 0, 1, 2, 3, 79, 117, 116, 2, 0, 0, 0,
2, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 2, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0 ].pack("C*")
end

subject(:graph) { described_class.new(ugen) }

let(:encoded) do
graph.encode.unpack("C*")
.map { |num| num.to_s(16).rjust(2, "0") }
end

it { expect(graph.constants.map(&:value)).to eq [ 440, 0, 1 ] }
it { expect(graph.nodes.map(&:value)).to eq [ sin_osc, ugen ] }

it "should encode graph" do
expect(encoded).to eq(expected)
expect(graph.encode).to eq(expected)
end
end
end
Expand Down

0 comments on commit e882867

Please sign in to comment.