private IntPtr WriteDataPort(IntPtr self, IntPtr args) { var argTuple = new PyTuple(new PyObject(args, false)); var data = new PyArray(argTuple.Get(0)); var portName = argTuple.Get(1).GetString(); var dataPort = OutputPorts.First(port => port.Name == portName) as NodeSystemLib2.FormatData1D.OutputPortData1D; var result = data.ToArrayDouble(); dataPort.Buffer.Write(result, 0, result.Length); return(new PyLong(1, false).Handle); }
private IntPtr SetSamplerate(IntPtr self, IntPtr args) { var argTuple = new PyTuple(new PyObject(args, false)); var portName = argTuple.Get(0).GetString(); var samplerate = argTuple.Get(1).GetDouble(); var dataPort = OutputPorts.First(port => port.Name == portName) as NodeSystemLib2.FormatData1D.OutputPortData1D; if (dataPort != null) { dataPort.Samplerate = (int)samplerate; return(new PyLong(1, false).Handle); } return(new PyLong(0, false).Handle); }
private IntPtr ReadDataPort(IntPtr self, IntPtr args) { var argTuple = new PyTuple(new PyObject(args, false)); var portName = argTuple.Get(0).GetString(); var dataPort = InputPorts.First(port => port.Name == portName) as NodeSystemLib2.FormatData1D.InputPortData1D; var buffer = dataPort.Read(); return(new PyArray(buffer.Data.Take(buffer.Available).ToArray(), false).Object.Handle); }
private IntPtr GetSamplerate(IntPtr self, IntPtr args) { var argTuple = new PyTuple(new PyObject(args, false)); var portName = argTuple.Get(0).GetString(); var dataPort = InputPorts.First(port => port.Name == portName) as NodeSystemLib2.FormatData1D.InputPortData1D; double samplerate = 0; if (dataPort != null) { samplerate = dataPort.Samplerate; } return(new PyFloat(samplerate, false).Handle); }
private void GetPortDefs() { PyObject callResult = null; try { callResult = _ctx.Call("init", new PyObject[0]); DisplayPythonError(null); } catch (PyException e) { DisplayPythonError(e); Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: While executing init function: {e.Message}")); return; } catch (EntryPointNotFoundException e) { Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: While executing init function: {e.Message}")); return; } var existingInputs = InputPorts.ToList(); var existingOutputs = OutputPorts.ToList(); using (var portDefs = new PyDict(callResult)) { using (var portInDefs = new PyList(portDefs["in"])) { foreach (var item in portInDefs) { var tuple = new PyTuple(item); var portName = new PyString(tuple.Get(0).Handle, false).Value; var portType = new PyString(tuple.Get(1).Handle, false).Value; var existingPort = existingInputs.FirstOrDefault(p => p.Name == portName); try { switch (portType) { case "data": if (existingPort != null) { if (!existingPort.DataType.Equals(PortDataTypes.TypeIdSignal1D)) { RemovePort(existingPort); new NodeSystemLib2.FormatData1D.InputPortData1D(this, portName); } existingInputs.Remove(existingPort); } else { new NodeSystemLib2.FormatData1D.InputPortData1D(this, portName); } break; case "value": if (existingPort != null) { if (!existingPort.DataType.Equals(PortDataTypes.TypeIdValueDouble)) { RemovePort(existingPort); new NodeSystemLib2.FormatValue.InputPortValueDouble(this, portName); } existingInputs.Remove(existingPort); } else { new NodeSystemLib2.FormatValue.InputPortValueDouble(this, portName); } break; default: Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: unknown port format '{portType}' for input port named {portName}")); break; } } catch (Exception e) { Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: error while processing input port: {portName}: {e.Message}")); } } } foreach (var port in existingInputs) { RemovePort(port); } using (var portOutDefs = new PyList(portDefs["out"])) { foreach (var item in portOutDefs) { var tuple = new PyTuple(item); var portName = new PyString(tuple.Get(0).Handle, false).Value; var portType = new PyString(tuple.Get(1).Handle, false).Value; var existingPort = existingOutputs.FirstOrDefault(p => p.Name == portName); try { switch (portType) { case "data": if (existingPort != null) { if (!existingPort.DataType.Equals(PortDataTypes.TypeIdSignal1D)) { RemovePort(existingPort); new NodeSystemLib2.FormatData1D.OutputPortData1D(this, portName); } existingOutputs.Remove(existingPort); } else { new NodeSystemLib2.FormatData1D.OutputPortData1D(this, portName); } break; case "value": if (existingPort != null) { if (!existingPort.DataType.Equals(PortDataTypes.TypeIdValueDouble)) { RemovePort(existingPort); new NodeSystemLib2.FormatValue.OutputPortValueDouble(this, portName); } existingOutputs.Remove(existingPort); } else { new NodeSystemLib2.FormatValue.OutputPortValueDouble(this, portName); } break; default: Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: unknown port format '{portType}' for output port named {portName}")); break; } } catch (Exception e) { Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: error while processing output port: {portName}: {e.Message}")); } } } } }