|
| 1 | +package sh.ball.graph.blocks.types; |
| 2 | + |
| 3 | +import com.sun.glass.ui.Clipboard; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.stream.Stream; |
| 8 | +import javafx.scene.Node; |
| 9 | +import javafx.scene.paint.Paint; |
| 10 | +import sh.ball.audio.engine.AudioDevice; |
| 11 | +import sh.ball.graph.blocks.Block; |
| 12 | +import sh.ball.graph.blocks.BlockDesigner; |
| 13 | +import sh.ball.graph.blocks.BlockInput; |
| 14 | +import sh.ball.graph.blocks.BlockProcessor; |
| 15 | + |
| 16 | +public abstract class BasicBlock implements Block { |
| 17 | + |
| 18 | + private final BlockInput[] inputs; |
| 19 | + private final List<Node> inputNodes; |
| 20 | + private final List<Node> outputNodes; |
| 21 | + private final int totalInputs; |
| 22 | + private final int totalOutputs; |
| 23 | + private final double[] inputBuffer; |
| 24 | + private final double[] outputBuffer; |
| 25 | + private final Paint color; |
| 26 | + private final String name; |
| 27 | + private final List<Node> nodes = new ArrayList<>(); |
| 28 | + |
| 29 | + private BlockProcessor processor; |
| 30 | + private int previousSampleNumber = -1; |
| 31 | + protected AudioDevice device; |
| 32 | + |
| 33 | + public BasicBlock(BlockProcessor processor, int totalInputs, int totalOutputs, Paint color, String name) { |
| 34 | + this.processor = processor; |
| 35 | + this.totalInputs = totalInputs; |
| 36 | + this.totalOutputs = totalOutputs; |
| 37 | + this.color = color; |
| 38 | + this.name = name; |
| 39 | + this.inputNodes = BlockDesigner.inputNodes(totalInputs); |
| 40 | + this.outputNodes = BlockDesigner.outputNodes(totalOutputs); |
| 41 | + this.inputs = new BlockInput[totalInputs]; |
| 42 | + this.inputBuffer = new double[totalInputs]; |
| 43 | + this.outputBuffer = new double[totalOutputs]; |
| 44 | + } |
| 45 | + |
| 46 | + public BasicBlock(int totalInputs, int totalOutputs, Paint color, String name) { |
| 47 | + this(null, totalInputs, totalOutputs, color, name); |
| 48 | + } |
| 49 | + |
| 50 | + public void setProcessor(BlockProcessor processor) { |
| 51 | + this.processor = processor; |
| 52 | + } |
| 53 | + |
| 54 | + public void addNode(Node node) { |
| 55 | + nodes.add(node); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public double process(int sampleNumber, int index) { |
| 60 | + if (sampleNumber != previousSampleNumber) { |
| 61 | + previousSampleNumber = sampleNumber; |
| 62 | + for (int i = 0; i < totalInputs(); i++) { |
| 63 | + if (inputs[i] != null) { |
| 64 | + inputBuffer[i] = inputs[i].block().process(sampleNumber, inputs[i].index()); |
| 65 | + } else { |
| 66 | + inputBuffer[i] = 0; |
| 67 | + } |
| 68 | + } |
| 69 | + processor.process(inputBuffer, outputBuffer); |
| 70 | + } |
| 71 | + |
| 72 | + return outputBuffer[index]; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public List<BlockInput> getInputs() { |
| 77 | + return List.of(inputs); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void setInput(BlockInput input, int index) { |
| 82 | + if (index >= totalInputs()) { |
| 83 | + throw new IllegalArgumentException("Block only has " + totalInputs() + " inputs"); |
| 84 | + } |
| 85 | + if (inputs[index] != null) { |
| 86 | + throw new IllegalStateException("Block already has an input at index " + index); |
| 87 | + } |
| 88 | + inputs[index] = input; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void removeInput(int index) { |
| 93 | + if (index >= totalInputs()) { |
| 94 | + throw new IllegalArgumentException("AddBlock only has " + totalInputs() + " inputs"); |
| 95 | + } |
| 96 | + inputs[index] = null; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int totalInputs() { |
| 101 | + return totalInputs; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public int totalOutputs() { |
| 106 | + return totalOutputs; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public int currentInputs() { |
| 111 | + return (int) Stream.of(inputs).filter(Objects::nonNull).count(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public Node getNode() { |
| 116 | + Stream<Node> nodeStream = Stream.concat(Stream.concat(getInputNodes().stream(), getOutputNodes().stream()), nodes.stream()); |
| 117 | + return BlockDesigner.createNode(color, name, 70, 20, nodeStream.toList()); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public List<Node> getInputNodes() { |
| 122 | + return inputNodes; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public List<Node> getOutputNodes() { |
| 127 | + return outputNodes; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void audioDeviceChanged(AudioDevice audioDevice) { |
| 132 | + this.device = audioDevice; |
| 133 | + } |
| 134 | +} |
0 commit comments