import numpy
import Axon

class MonoMixer(Axon.AdaptiveCommsComponent.AdaptiveCommsComponent):
    channels = 8
    bufferSize = 1024

    def __init__(self, **argd):
        super(MonoMixer, self).__init__(**argd)
        for i in range(self.channels):
            self.addInbox("in%i" % i)

    def main(self):
        while 1:
            output = numpy.zeros(self.bufferSize)
            for i in range(self.channels):
                if self.dataReady("in%i" % i):
                    output += self.recv("in%i" % i)
            output /= self.channels
            self.send(output, "outbox")
            if not self.anyReady():
                self.pause()
            yield 1


import numpy
import Axon
import time
from Axon.SchedulingComponent import SchedulingAdaptiveCommsComponent

class MonoMixer(SchedulingAdaptiveCommsComponent):
    channels = 8
    bufferSize = 1024
    sampleRate = 44100

    def __init__(self, **argd):
        super(MonoMixer, self).__init__(**argd)
        for i in range(self.channels):
            self.addInbox("in%i" % i)
        self.period = float(self.bufferSize)/self.sampleRate
        self.lastSendTime = time.time()
        self.scheduleAbs("Send", self.lastSendTime + self.period)


    def main(self):
        while 1:
            if self.dataReady("event"):
                output = numpy.zeros(self.bufferSize)
                self.recv("event")
                for i in range(self.channels):
                    if self.dataReady("in%i" % i):
                        data = self.recv("in%i" % i)
                        if data != None:
                            output += data
                output /= self.channels
                self.send(output, "outbox")
                self.lastSendTime += self.period
                self.scheduleAbs("Send", self.lastSendTime + self.period)
            else:
                self.pause()

Change the mixer to be a scheduled component, and stop it from sending unnecessary messages when it has only received data from a few of it's inputs.
Change the mixer to be a scheduled component, and stop it from sending unnecessary messages when it has only received data from a few of it's inputs.

  import numpy
  import Axon
+ import time
+ from Axon.SchedulingComponent import SchedulingAdaptiveCommsComponent
  
- class MonoMixer(Axon.AdaptiveCommsComponent.AdaptiveCommsComponent):
+ class MonoMixer(SchedulingAdaptiveCommsComponent):
      channels = 8
      bufferSize = 1024
+     sampleRate = 44100
  
      def __init__(self, **argd):
          super(MonoMixer, self).__init__(**argd)
          for i in range(self.channels):
              self.addInbox("in%i" % i)
+         self.period = float(self.bufferSize)/self.sampleRate
+         self.lastSendTime = time.time()
+         self.scheduleAbs("Send", self.lastSendTime + self.period)
+ 
  
      def main(self):
          while 1:
+             if self.dataReady("event"):
-             output = numpy.zeros(self.bufferSize)
+                 output = numpy.zeros(self.bufferSize)
+                 self.recv("event")
-             for i in range(self.channels):
+                 for i in range(self.channels):
-                 if self.dataReady("in%i" % i):
+                     if self.dataReady("in%i" % i):
-                     output += self.recv("in%i" % i)
+                         data = self.recv("in%i" % i)
+                         if data != None:
+                             output += data
-             output /= self.channels
+                 output /= self.channels
-             self.send(output, "outbox")
+                 self.send(output, "outbox")
-             if not self.anyReady():
+                 self.lastSendTime += self.period
+                 self.scheduleAbs("Send", self.lastSendTime + self.period)
+             else:
                  self.pause()
-             yield 1
  
- 
Change the mixer to be a scheduled component, and stop it from sending unnecessary messages when it has only received data from a few of it's inputs.
## Code Before:
import numpy
import Axon

class MonoMixer(Axon.AdaptiveCommsComponent.AdaptiveCommsComponent):
    channels = 8
    bufferSize = 1024

    def __init__(self, **argd):
        super(MonoMixer, self).__init__(**argd)
        for i in range(self.channels):
            self.addInbox("in%i" % i)

    def main(self):
        while 1:
            output = numpy.zeros(self.bufferSize)
            for i in range(self.channels):
                if self.dataReady("in%i" % i):
                    output += self.recv("in%i" % i)
            output /= self.channels
            self.send(output, "outbox")
            if not self.anyReady():
                self.pause()
            yield 1


## Instruction:
Change the mixer to be a scheduled component, and stop it from sending unnecessary messages when it has only received data from a few of it's inputs.
## Code After:
import numpy
import Axon
import time
from Axon.SchedulingComponent import SchedulingAdaptiveCommsComponent

class MonoMixer(SchedulingAdaptiveCommsComponent):
    channels = 8
    bufferSize = 1024
    sampleRate = 44100

    def __init__(self, **argd):
        super(MonoMixer, self).__init__(**argd)
        for i in range(self.channels):
            self.addInbox("in%i" % i)
        self.period = float(self.bufferSize)/self.sampleRate
        self.lastSendTime = time.time()
        self.scheduleAbs("Send", self.lastSendTime + self.period)


    def main(self):
        while 1:
            if self.dataReady("event"):
                output = numpy.zeros(self.bufferSize)
                self.recv("event")
                for i in range(self.channels):
                    if self.dataReady("in%i" % i):
                        data = self.recv("in%i" % i)
                        if data != None:
                            output += data
                output /= self.channels
                self.send(output, "outbox")
                self.lastSendTime += self.period
                self.scheduleAbs("Send", self.lastSendTime + self.period)
            else:
                self.pause()
