github.com/256dpi/max-go@v0.7.0/example/main.go (about)

     1  package main
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/256dpi/max-go"
     7  )
     8  
     9  type instance struct {
    10  	sigIn1   *max.Inlet
    11  	sigIn2   *max.Inlet
    12  	anyIn    *max.Inlet
    13  	floatIn  *max.Inlet
    14  	intIn    *max.Inlet
    15  	sigOut1  *max.Outlet
    16  	sigOut2  *max.Outlet
    17  	anyOut   *max.Outlet
    18  	floatOut *max.Outlet
    19  	bangOut  *max.Outlet
    20  	tick     *time.Ticker
    21  	bench    bool
    22  }
    23  
    24  func (i *instance) Init(obj *max.Object, args []max.Atom) bool {
    25  	// check if doomed
    26  	if len(args) > 0 && args[0] == "fail" {
    27  		return false
    28  	}
    29  
    30  	// check if benchmark
    31  	if len(args) > 0 && args[0] == "bench" {
    32  		i.bench = true
    33  	}
    34  
    35  	// check bench
    36  	if !i.bench {
    37  		max.Pretty("init", args)
    38  	}
    39  
    40  	// declare inlets
    41  	i.sigIn1 = obj.Inlet(max.Signal, "signal 1", false)
    42  	i.sigIn2 = obj.Inlet(max.Signal, "signal 2", false)
    43  	i.anyIn = obj.Inlet(max.Any, "any", true)
    44  	i.floatIn = obj.Inlet(max.Float, "float", false)
    45  	i.intIn = obj.Inlet(max.Int, "int", false)
    46  
    47  	// declare outlets
    48  	i.sigOut1 = obj.Outlet(max.Signal, "signal 1")
    49  	i.sigOut2 = obj.Outlet(max.Signal, "signal 2")
    50  	i.anyOut = obj.Outlet(max.Any, "any")
    51  	i.floatOut = obj.Outlet(max.Float, "float")
    52  	i.bangOut = obj.Outlet(max.Bang, "bang")
    53  
    54  	// bang second outlet from a timer
    55  	if !i.bench {
    56  		// create timer
    57  		i.tick = time.NewTicker(1 * time.Second)
    58  
    59  		// send a bang for every tick
    60  		go func() {
    61  			var j int
    62  			for range i.tick.C {
    63  				max.Pretty("tick", max.IsMainThread())
    64  
    65  				// bang immediately or defer
    66  				if j++; j%2 == 0 {
    67  					i.bangOut.Bang()
    68  				} else {
    69  					max.Defer(func() {
    70  						max.Pretty("defer", max.IsMainThread())
    71  						i.bangOut.Bang()
    72  					})
    73  				}
    74  			}
    75  		}()
    76  	}
    77  
    78  	return true
    79  }
    80  
    81  func (i *instance) Handle(inlet int, msg string, data []max.Atom) {
    82  	// check bench
    83  	if !i.bench {
    84  		max.Pretty("handle", inlet, msg, data)
    85  	}
    86  
    87  	// echo, double or triple
    88  	switch inlet {
    89  	case 0, 1:
    90  		// signals
    91  	case 2:
    92  		i.anyOut.Any(msg, data)
    93  	case 3:
    94  		i.floatOut.Float(data[0].(float64) * 2)
    95  	case 4:
    96  		i.floatOut.Float(float64(data[0].(int64) * 3))
    97  	}
    98  }
    99  
   100  func (i *instance) Process(input, output [][]float64) {
   101  	// log
   102  	max.Pretty("process", len(input), len(output))
   103  
   104  	// scale output
   105  	for i := range input {
   106  		for j := range input[i] {
   107  			output[i][j] = input[i][j] / 2.0
   108  		}
   109  	}
   110  }
   111  
   112  func (i *instance) Loaded() {
   113  	// check bench
   114  	if !i.bench {
   115  		max.Pretty("loaded")
   116  	}
   117  }
   118  
   119  func (i *instance) DoubleClicked() {
   120  	// check bench
   121  	if !i.bench {
   122  		max.Pretty("double clicked")
   123  	}
   124  }
   125  
   126  func (i *instance) Free() {
   127  	// check bench
   128  	if !i.bench {
   129  		max.Pretty("free")
   130  
   131  		// stop ticker
   132  		i.tick.Stop()
   133  	}
   134  }
   135  
   136  func main() {
   137  	// initialize Max class
   138  	max.Register("maxgo", &instance{})
   139  }