graph.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import sys
  2. sys.path.append("../..")
  3. from sdf import *
  4. AUDIO_INTERRUPT_LENGTH = 160
  5. MFCCFEATURESSIZE=10
  6. NBOUTMFCC=50
  7. OVERLAPMFCC=25
  8. floatType=CType(F32)
  9. ### Define new types of Nodes
  10. class Node(GenericNode):
  11. def __init__(self,name,theType,inLength,outLength):
  12. GenericNode.__init__(self,name)
  13. self.addInput("i",theType,inLength)
  14. self.addOutput("o",theType,outLength)
  15. @property
  16. def typeName(self):
  17. return "Node"
  18. class StereoSource(GenericSource):
  19. def __init__(self,name,inLength):
  20. GenericSource.__init__(self,name)
  21. self.addOutput("o",floatType,2*inLength)
  22. @property
  23. def typeName(self):
  24. return "StereoSource"
  25. # This is a fake TFLite node just for illustration
  26. class TFLite(GenericSink):
  27. def __init__(self,name):
  28. GenericSink.__init__(self,name)
  29. self.addInput("i",floatType,NBOUTMFCC*MFCCFEATURESSIZE)
  30. @property
  31. def typeName(self):
  32. return "TFLite"
  33. # This is a fake MFCC just to illustrate how it could be used in a graph.
  34. # For a real MFCC example, look at example5
  35. class MFCC(GenericNode):
  36. def __init__(self,name,inLength):
  37. GenericNode.__init__(self,name)
  38. self.addInput("i",floatType,inLength)
  39. self.addOutput("o",floatType,MFCCFEATURESSIZE)
  40. @property
  41. def typeName(self):
  42. return "MFCC"
  43. ### Define nodes
  44. half=Constant("HALF")
  45. src=StereoSource("src",AUDIO_INTERRUPT_LENGTH)
  46. toMono=Unzip("toMono",floatType, AUDIO_INTERRUPT_LENGTH)
  47. sa=Dsp("scale",floatType,AUDIO_INTERRUPT_LENGTH)
  48. sb=Dsp("scale",floatType,AUDIO_INTERRUPT_LENGTH)
  49. add=Dsp("add",floatType,AUDIO_INTERRUPT_LENGTH)
  50. audioWindow=SlidingBuffer("audioWin",floatType,640,320)
  51. mfcc=MFCC("mfcc",640)
  52. mfccWindow=SlidingBuffer("mfccWind",floatType,NBOUTMFCC*MFCCFEATURESSIZE,OVERLAPMFCC*MFCCFEATURESSIZE)
  53. tf=TFLite("TFLite")
  54. g = Graph()
  55. g.connectWithDelay(src.o, toMono.i,10)
  56. g.connect(toMono.o1,sa.ia)
  57. g.connect(toMono.o2,sb.ia)
  58. # A constant node as no output
  59. # So it is connected directly
  60. g.connect(half,sa.ib)
  61. g.connect(half,sb.ib)
  62. g.connect(sa.o,add.ia)
  63. g.connect(sb.o,add.ib)
  64. g.connect(add.o,audioWindow.i)
  65. g.connect(audioWindow.o, mfcc.i)
  66. g.connect(mfcc.o,mfccWindow.i)
  67. g.connect(mfccWindow.o,tf.i)
  68. print("Generate graphviz and code")
  69. conf=Configuration()
  70. conf.debugLimit=1
  71. conf.cOptionalArgs="int opt1,int opt2"
  72. #conf.memoryOptimization=True
  73. #print(g.nullVector())
  74. sched = g.computeSchedule(conf)
  75. #print(sched.schedule)
  76. print("Schedule length = %d" % sched.scheduleLength)
  77. print("Memory usage %d bytes" % sched.memory)
  78. #
  79. #conf.codeArray=True
  80. sched.ccode("generated",conf)
  81. with open("test.dot","w") as f:
  82. sched.graphviz(f)