-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathblock.lua
665 lines (568 loc) · 19.5 KB
/
block.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
---
-- Support classes for creating blocks.
--
-- @module radio.block
local ffi = require('ffi')
local class = require('radio.core.class')
local pipe = require('radio.core.pipe')
local util = require('radio.core.util')
local debug = require('radio.core.debug')
---
-- Block input port descriptor. This contains the name and data type of a block
-- input port.
--
-- @class Input
-- @tparam string name Name
-- @tparam type|function data_type Data type, e.g. `radio.types.ComplexFloat32`, or a function predicate
-- @usage
-- local inputs = {radio.block.Input("in1", radio.types.ComplexFloat32),
-- radio.block.Input("in2", radio.types.ComplexFloat32)}
-- local outputs = {...}
-- ...
-- self:add_type_signature(inputs, outputs)
local Input = class.factory()
function Input.new(name, data_type)
local self = setmetatable({}, Input)
self.name = name
self.data_type = data_type
return self
end
function Input:__tostring()
local data_type_str = type(self.data_type) == "function" and "function" or self.data_type.type_name or self.data_type
return string.format("%s [%s]", self.name, data_type_str)
end
---
-- Block output port descriptor. This contains the name and data type of
-- a block output port.
--
-- @class Output
-- @tparam string name Name
-- @tparam type|str data_type Data type, e.g. `radio.types.ComplexFloat32`, or "copy" to copy input data type
-- @usage
-- local inputs = {...}
-- local outputs = {radio.block.Output("out", radio.types.ComplexFloat32)}
-- ...
-- self:add_type_signature(inputs, outputs)
local Output = class.factory()
function Output.new(name, data_type)
local self = setmetatable({}, Output)
self.name = name
self.data_type = data_type
return self
end
function Output:__tostring()
local data_type_str = self.data_type == "copy" and "copy" or self.data_type.type_name or self.data_type
return string.format("%s [%s]", self.name, data_type_str)
end
---
-- Input port of a block. These are created in Block's add_type_signature().
--
-- @internal
-- @class
-- @tparam Block owner Block owner
-- @tparam string name Input name
local InputPort = class.factory()
function InputPort.new(owner, name)
local self = setmetatable({}, InputPort)
self.owner = owner
self.name = name
self.data_type = nil
self.pipe = nil
return self
end
function InputPort:__tostring()
local data_type_str = self.data_type == nil and "n/a" or self.data_type.type_name or self.data_type
if self.pipe then
return string.format(".%-5s [%s] <- {%s.%s}", self.name, data_type_str, self.pipe.output.owner.name, self.pipe.output.name)
else
return string.format(".%-5s [%s] <- unconnected", self.name, data_type_str)
end
end
---
-- Close input end of associated pipe.
--
-- @internal
-- @function InputPort:close
function InputPort:close()
self.pipe:close_input()
end
---
-- Get input file descriptors of associated pipe.
--
-- @internal
-- @function InputPort:filenos
-- @treturn array Array of file descriptors
function InputPort:filenos()
return {self.pipe:fileno_input()}
end
---
-- Output port of a block. These are created in Block's add_type_signature().
--
-- @internal
-- @class
-- @tparam Block owner Block owner
-- @tparam string name Output name
local OutputPort = class.factory()
function OutputPort.new(owner, name)
local self = setmetatable({}, OutputPort)
self.owner = owner
self.name = name
self.data_type = nil
self.pipes = {}
return self
end
function OutputPort:__tostring()
local data_type_str = self.data_type == nil and "n/a" or self.data_type.type_name or self.data_type
if #self.pipes > 0 then
local input_strs = {}
for _, p in ipairs(self.pipes) do
input_strs[#input_strs + 1] = string.format("%s.%s", p.input.owner.name, p.input.name)
end
return string.format(".%-5s [%s] -> {%s}", self.name, data_type_str, table.concat(input_strs, ", "))
else
return string.format(".%-5s [%s] -> unconnected", self.name, data_type_str)
end
end
---
-- Close output end of associated pipe.
--
-- @internal
-- @function InputPort:close
function OutputPort:close()
for i=1, #self.pipes do
self.pipes[i]:close_output()
end
end
---
-- Get output file descriptors of associated pipe.
--
-- @internal
-- @function InputPort:filenos
-- @treturn array Array of file descriptors
function OutputPort:filenos()
local fds = {}
for i = 1, #self.pipes do
fds[i] = self.pipes[i]:fileno_output()
end
return fds
end
---
-- Aliased input port of a block. These alias InputPort objects, and are
-- created in CompositeBlock's add_type_signature().
--
-- @internal
-- @class
-- @tparam Block owner Block owner
-- @tparam string name Output name
local AliasedInputPort = class.factory()
function AliasedInputPort.new(owner, name)
local self = setmetatable({}, AliasedInputPort)
self.owner = owner
self.name = name
self.data_type = nil
return self
end
function AliasedInputPort:__tostring()
local data_type_str = self.data_type == nil and "n/a" or self.data_type.type_name or self.data_type
return string.format(".%-5s [%s]", self.name, data_type_str)
end
---
-- Aliased output port of a block. These alias OutputPort objects, and are
-- created in CompositeBlock's add_type_signature().
--
-- @internal
-- @class
-- @tparam Block owner Block owner
-- @tparam string name Output name
local AliasedOutputPort = class.factory()
function AliasedOutputPort.new(owner, name)
local self = setmetatable({}, AliasedOutputPort)
self.owner = owner
self.name = name
self.data_type = nil
return self
end
function AliasedOutputPort:__tostring()
local data_type_str = self.data_type == nil and "n/a" or self.data_type.type_name or self.data_type
return string.format(".%-5s [%s]", self.name, data_type_str)
end
---
-- Block base class.
--
-- @class Block
local Block = class.factory()
---
-- Add a type signature.
--
-- @function Block:add_type_signature
-- @tparam array inputs Input ports, array of `radio.block.Input` instances
-- @tparam array outputs Output ports, array of `radio.block.Output` instances
-- @tparam[opt=nil] function process_func Optional process function for this
-- type signature, defaults to
-- `process()`
-- @tparam[opt=nil] function initialize_func Optional process initialization
-- for this type signature, defaults
-- to `initialize()`
-- @raise Invalid input port descriptor error.
-- @raise Invalid output port descriptor error.
-- @raise Invalid type signature, input count mismatch error.
-- @raise Invalid type signature, input name mismatch error.
-- @raise Invalid type signature, output count mismatch error.
-- @raise Invalid type signature, output name mismatch error.
function Block:add_type_signature(inputs, outputs, process_func, initialize_func)
-- Assert inputs are Inputs
for i = 1, #inputs do
assert(class.isinstanceof(inputs[i], Input), string.format("Invalid input port descriptor (index %d).", i))
end
if not self.inputs then
-- Create inputs with a InputPort for each input
self.inputs = {}
for i = 1, #inputs do
self.inputs[i] = InputPort(self, inputs[i].name)
end
else
-- Check input count
assert(#self.inputs == #inputs, string.format("Invalid type signature, input count mismatch (got %d, expected %d).", #inputs, #self.inputs))
-- Check input names match
for i = 1, #inputs do
assert(self.inputs[i].name == inputs[i].name, string.format("Invalid type signature, input name mismatch (index %d).", i))
end
end
-- Assert outputs are Outputs
for i = 1, #outputs do
assert(class.isinstanceof(outputs[i], Output), string.format("Invalid output port descriptor (index %d).", i))
end
if not self.outputs then
-- Create outputs with a OutputPort for each output
self.outputs = {}
for i = 1, #outputs do
self.outputs[i] = OutputPort(self, outputs[i].name)
end
else
-- Check output count
assert(#self.outputs == #outputs, string.format("Invalid type signature, output count mismatch (got %d, expected %d).", #outputs, #self.outputs))
-- Check output names match
for i = 1, #outputs do
assert(self.outputs[i].name == outputs[i].name, string.format("Invalid type signature, output name mismatch (index %d).", i))
end
end
-- Add the type signature to our signatures list
self.signatures[#self.signatures+1] = {
inputs = inputs,
outputs = outputs,
initialize_func = initialize_func or self.initialize,
process_func = process_func or self.process,
}
end
---
-- Differentiate this block to a type signature.
--
-- @function Block:differentiate
-- @tparam array input_data_types Array of input data types
-- @raise No compatible type signatures found error.
function Block:differentiate(input_data_types)
-- Eliminate type signature candidates that don't allow the specified input
-- names and data types
local signature_candidates = {}
for _, signature in ipairs(self.signatures) do
signature_candidates[signature] = true
-- Compare signature input types with specified input types
for i = 1, #signature.inputs do
-- Compare input type with block's type signature input type
local predicate
if type(signature.inputs[i].data_type) == "function" then
predicate = signature.inputs[i].data_type(input_data_types[i])
else
predicate = input_data_types[i] == signature.inputs[i].data_type
end
-- If they're incompatible, remove the signature candidate
if not predicate then
signature_candidates[signature] = nil
break
end
end
end
-- If a compatible signature wasn't found
if util.table_length(signature_candidates) ~= 1 then
-- Build list of supplied data type at each input port
local input_descs = {}
for i = 1, #input_data_types do
input_descs[i] = string.format("\"%s\": [%s]", self.signatures[1].inputs[i].name, input_data_types[i].type_name or "Unknown Type")
end
assert(false, string.format("No compatible type signatures found for block %s with input data types: %s.", self.name, table.concat(input_descs, ", ")))
end
-- Differentiate to the type signature
self.signature, _ = next(signature_candidates)
self.initialize = self.signature.initialize_func
self.process = self.signature.process_func
for i = 1, #input_data_types do
self.inputs[i].data_type = input_data_types[i]
end
for i = 1, #self.signature.outputs do
if self.signature.outputs[i].data_type == "copy" then
self.outputs[i].data_type = input_data_types[i]
else
self.outputs[i].data_type = self.signature.outputs[i].data_type
end
end
end
---
-- Get the differentiated input data type.
--
-- @function Block:get_input_type
-- @tparam[opt=1] int index Index of input, starting at 1
-- @treturn array Array of data types
-- @raise Block not yet differentiated error.
function Block:get_input_type(index)
assert(self.signature, "Block not yet differentiated.")
index = index or 1
return self.inputs[index] and self.inputs[index].data_type
end
---
-- Get the differentiated output data type.
--
-- @function Block:get_output_type
-- @tparam[opt=1] int index Index of output, starting at 1
-- @treturn data_type Data type
-- @raise Block not yet differentiated error.
function Block:get_output_type(index)
assert(self.signature, "Block not yet differentiated.")
index = index or 1
return self.outputs[index] and self.outputs[index].data_type
end
---
-- Get the block rate.
--
-- @function Block:get_rate
-- @treturn number Block rate in samples per second
-- @raise Block not yet differentiated error.
function Block:get_rate()
assert(self.signature, "Block not yet differentiated.")
assert(#self.inputs > 0, "get_rate() not implemented for source " .. self.name .. ".")
-- Default to copying rate from first input
return self.inputs[1].pipe:get_rate()
end
---
-- Get a string representation with the block name and port connectivity.
--
-- @function Block:__tostring
-- @treturn string String representation
function Block:__tostring()
if self.inputs == nil or self.outputs == nil then
-- tostring() on class
return self.name
elseif self.signature == nil then
-- tostring() on undifferentiated instance
local strs = {}
strs[1] = string.format("%s (undifferentiated)", self.name)
for _, input in ipairs(self.inputs) do
strs[#strs + 1] = " " .. tostring(input)
end
for _, output in ipairs(self.outputs) do
strs[#strs + 1] = " " .. tostring(output)
end
strs[#strs + 1] = " Type Signatures Available"
for _, signature in ipairs(self.signatures) do
local input_strs = {}
for _, input in ipairs(signature.inputs) do
input_strs[#input_strs + 1] = tostring(input)
end
local output_strs = {}
for _, output in ipairs(signature.outputs) do
output_strs[#output_strs + 1] = tostring(output)
end
strs[#strs + 1] = string.format(" {%s} -> {%s}", table.concat(input_strs, ", "), table.concat(output_strs, ", "))
end
return table.concat(strs, "\n")
else
-- tostring() on differentiated instance
local strs = {}
local rate_available, rate = pcall(function () return self:get_rate() end)
if rate_available then
strs[1] = string.format("%s [%.0f Hz]", self.name, rate)
else
strs[1] = self.name
end
for _, input in ipairs(self.inputs) do
strs[#strs + 1] = " " .. tostring(input)
end
for _, output in ipairs(self.outputs) do
strs[#strs + 1] = " " .. tostring(output)
end
return table.concat(strs, "\n")
end
end
---
-- Instantiate hook, default no-op implementation.
--
-- @function Block:instantiate
function Block:instantiate()
-- No operation
end
---
-- Initialize hook, default no-op implementation.
--
-- @function Block:initialize
function Block:initialize()
-- No operation
end
---
-- Process hook, default implementation raises a not implemented error.
--
-- @function Block:process
function Block:process(...)
error("process() not implemented")
end
---
-- Cleanup hook, default no-op implementation.
--
-- @function Block:cleanup
function Block:cleanup()
-- No operation
end
---
-- Run block once. Currently used for unit testing.
--
-- @internal
-- @function Block:run_once
-- @treturn bool|nil New samples produced or nil on EOF
function Block:run_once()
-- FIXME input pipes, output pipes, and pipe mux should really only be
-- built once, but currently this method is only in unit testing.
-- Gather input pipes
local input_pipes = {}
for i=1, #self.inputs do
input_pipes[i] = self.inputs[i].pipe
end
-- Gather output pipes
local output_pipes = {}
for i=1, #self.outputs do
output_pipes[i] = {}
for j=1, #self.outputs[i].pipes do
output_pipes[i][j] = self.outputs[i].pipes[j]
end
end
-- Create pipe mux
local pipe_mux = pipe.PipeMux(input_pipes, output_pipes, self.control_socket)
-- Read inputs
local data_in, eof, shutdown = pipe_mux:read()
-- Check for upstream EOF or control socket shutdown
if eof or shutdown then
return nil
end
-- Process inputs into outputs
local data_out = {self:process(unpack(data_in))}
-- Check for block generated EOF
if #data_out ~= #self.outputs then
return nil
end
-- Write outputs
local eof, eof_pipe, shutdown = pipe_mux:write(data_out)
-- Check for downstream EOF or control socket shutdown
if shutdown then
return nil
elseif eof then
error(string.format("[%s] Downstream block %s terminated unexpectedly.\n", self.name, eof_pipe.input.owner.name))
end
-- Check if new samples were created
local new_samples = false
for i=1, #self.outputs do
new_samples = new_samples or data_out[i].length > 0
end
-- Return true or false if new samples were produced
return new_samples
end
---
-- Run block until inputs reach EOF, then call cleanup().
--
-- @internal
-- @function Block:run
function Block:run()
-- Gather input pipes
local input_pipes = {}
for i=1, #self.inputs do
input_pipes[i] = self.inputs[i].pipe
end
-- Gather output pipes
local output_pipes = {}
for i=1, #self.outputs do
output_pipes[i] = {}
for j=1, #self.outputs[i].pipes do
output_pipes[i][j] = self.outputs[i].pipes[j]
end
end
-- Create pipe mux
local pipe_mux = pipe.PipeMux(input_pipes, output_pipes, self.control_socket)
while true do
-- Read inputs
local data_in, eof, shutdown = pipe_mux:read()
-- Check for upstream EOF or control socket shutdown
if eof or shutdown then
break
end
-- Process inputs into outputs
local data_out = {self:process(unpack(data_in))}
-- Check for block generated EOF
if #data_out ~= #self.outputs then
break
end
-- Write outputs
local eof, eof_pipe, shutdown = pipe_mux:write(data_out)
-- Check for downstream EOF or control socket shutdown
if shutdown then
break
elseif eof then
io.stderr:write(string.format("[%s] Downstream block %s terminated unexpectedly.\n", self.name, eof_pipe.input.owner.name))
break
end
end
debug.printf("[%s] Block pid %d terminating...\n", self.name, ffi.C.getpid())
-- Clean up
self:cleanup()
end
---
-- Block class factory.
--
-- @function factory
-- @tparam string name Block name
-- @tparam[opt=nil] class parent_class Inherited parent class
--
-- @usage
-- local MyBlock = radio.block.factory("MyBlock")
--
-- function MyBlock:instantiate(a, b)
-- self.param = a + b
--
-- self:add_type_signature({radio.block.Input("in", radio.types.Float32)},
-- {radio.block.Output("out", radio.types.Float32)})
-- end
--
-- function MyBlock:initialize()
-- -- Differentiated data type and sample rate dependent initialization
-- end
--
-- function MyBlock:process(x)
-- return x
-- end
local function factory(name, parent_class)
local class = class.factory(parent_class or Block)
class.name = name
class.new = function (...)
local self = setmetatable({}, class)
-- Pipe inputs and outputs
self.inputs = nil
self.outputs = nil
-- Open files
self.files = {[io.stdout] = true, [io.stderr] = true}
-- Type signatures and differentiated signature
self.signatures = {}
self.signature = nil
-- Disable instance call operator
self.new = function () error("Block instance not callable") end
self:instantiate(...)
return self
end
return class
end
-- Exported module
return {Input = Input, Output = Output, InputPort = InputPort, OutputPort = OutputPort, AliasedInputPort = AliasedInputPort, AliasedOutputPort = AliasedOutputPort, Block = Block, factory = factory}