// discovers the currently connected Launchpad device // as a MIDI device. func discover() (input portmidi.DeviceID, output portmidi.DeviceID, err error) { in := -1 out := -1 for i := 0; i < portmidi.CountDevices(); i++ { info := portmidi.Info(portmidi.DeviceID(i)) if strings.Contains(info.Name, "Launchpad") { if info.IsInputAvailable { in = i } if info.IsOutputAvailable { out = i } } } if in == -1 || out == -1 { err = errors.New("launchpad: no launchpad is connected") } else { input = portmidi.DeviceID(in) output = portmidi.DeviceID(out) } return }
func main() { flag.Parse() var err error if err = portmidi.Initialize(); err != nil { log.Fatal("error while initializing portmidi", err) } if out, err = portmidi.NewOutputStream(portmidi.DeviceID(*flagOutput), 1024, 0); err != nil { log.Fatal("error while initializing connection to midi out stream") } if pad, err = launchpad.Open(); err != nil { log.Fatal("error while initializing connection to launchpad", err) } grid = newGrid() instruments = []int{ 70, // maracas 38, // snare 1 40, // snare 2 51, // ride cymbal 42, // closed hi-hat 58, // vibra slap 46, // open hi-hat 81} // open triangle // load an initial drum pattern // hi-hats grid[0][4] = true grid[2][4] = true grid[4][4] = true grid[6][4] = true // snares grid[5][3] = true grid[5][2] = true grid[5][1] = true // bells grid[6][7] = true grid[7][7] = true grid[5][6] = true // clear pad.Reset() // listen button toggles ch := pad.Listen() go func() { for { hit := <-ch log.Println("drum toggled at", hit) if hit.Y == -1 || hit.X > 7 { // a controller button is hit continue } grid[hit.X][hit.Y] = !grid[hit.X][hit.Y] if !grid[hit.X][hit.Y] { // turn off immediately pad.Light(hit.X, hit.Y, 0, 0) } } }() for { for x := 0; x < NumberofHGrids; x++ { tick(x) } } }