Exemple #1
0
func main() {
	dev := flag.String("d", "/dev/input/by-id/usb-0810_Twin_USB_Joystick-event-joystick", "The GH controller event device")
	flag.Int64Var(&baseNote, "b", 48, "The base midi note with no button pressed")
	flag.Int64Var(&vel, "v", 100, "Midi note velocity")
	flag.Int64Var(&midiChan, "c", 1, "Midi channel")
	flag.Parse()
	portmidi.Initialize()
	out, err := portmidi.NewOutputStream(portmidi.GetDefaultOutputDeviceId(), 32, 0)
	if err != nil {
		fmt.Println(err)
		return
	}

	c := make(chan *evdev.InputEvent)
	e := make(chan error)
	go ReadGuitar(c, e, *dev)

	for {
		select {
		case but := <-c:
			switch but.Code {
			case butGreen, butRed, butYellow, butBlue, butOrange:
				SetNote(butMap[but.Code], but.Value)
				if playing != -1 {
					SwapNote(out, baseNote+butState+octave, vel)
				}
			case butStrumbar:
				if but.Value == 255 || but.Value == 0 {
					NoteOn(out, baseNote+butState+octave, vel)
				} else if !hold {
					NoteOff(out, playing, vel)
				}
			case butSelect:
				if but.Value != 0 {
					hold = !hold
					if !hold {
						NoteOff(out, playing, vel)
					}
				}
			case butTilt:
				if but.Value == 1 {
					octave += 12
				} else {
					octave -= 12
				}
				if playing != -1 {
					SwapNote(out, baseNote+butState+octave, vel)
				}
			case butStart:
				AllNotesOff(out)
			}
		case err := <-e:
			fmt.Println(err)
			close(c)
			close(e)
			return
		default:
		}
	}
}
Exemple #2
0
func ExampleStream_WriteSysEx() {
	out, err := portmidi.NewOutputStream(portmidi.DefaultOutputDeviceID(), 1024, 0)
	if err != nil {
		log.Fatal(err)
	}

	if err = out.WriteSysEx(portmidi.Time(), "F0 0A 0A 1B 00 7F 30 F7"); err != nil {
		log.Fatal(err)
	}
}
Exemple #3
0
func ExampleStream_WriteSysExBytes() {
	out, err := portmidi.NewOutputStream(portmidi.DefaultOutputDeviceID(), 1024, 0)
	if err != nil {
		log.Fatal(err)
	}

	if err = out.WriteSysExBytes(portmidi.Time(), []byte{0xF0, 0x0A, 0x0A, 0x1B, 0x00, 0x7F, 0x30, 0xF7}); err != nil {
		log.Fatal(err)
	}
}
Exemple #4
0
func run() error {
	s, err := portmidi.NewOutputStream(portmidi.DeviceId(*deviceN), 1024, 0)
	if err != nil {
		return err
	}
	defer s.Close()
	ls := &lockedStream{s: s}

	quit := make(chan bool)

	spd := 1000 * time.Millisecond

	n1 := make(chan Note)
	g1 := generator{
		minN: 48, maxN: 72,
		minD:    spd,
		maxD:    4 * spd,
		stepD:   spd / 8,
		scale:   dorian,
		ringLen: 16,
		noteP:   4,
		timeP:   2,
		switchP: 16,
	}
	go g1.run(n1, quit)
	go play(ls, 2, n1, quit)

	n2 := make(chan Note)
	g2 := generator{
		minN: 36, maxN: 60,
		minD:    4 * spd,
		maxD:    16 * spd,
		stepD:   spd,
		scale:   dorian,
		ringLen: 4,
		noteP:   2,
		timeP:   2,
		switchP: 4,
	}
	go g2.run(n2, quit)
	go play(ls, 3, n2, quit)

	b := make([]byte, 1)
	os.Stdin.Read(b)
	close(quit)

	time.Sleep(time.Second)
	return nil
}
Exemple #5
0
// Open opens a connection Launchpad and initializes an input and output
// stream to the currently connected device. If there are no
// devices are connected, it returns an error.
func Open() (*Launchpad, error) {
	input, output, err := discover()
	if err != nil {
		return nil, err
	}

	var inStream, outStream *portmidi.Stream
	if inStream, err = portmidi.NewInputStream(input, 1024); err != nil {
		return nil, err
	}
	if outStream, err = portmidi.NewOutputStream(output, 1024, 0); err != nil {
		return nil, err
	}
	return &Launchpad{inputStream: inStream, outputStream: outStream}, nil
}
Exemple #6
0
func midiOpen(argv []*ell.Object) (*ell.Object, error) {
	//	defaultInput := "USB Oxygen 8 v2"
	//	defaultOutput := "IAC Driver Bus 1"
	latency := int64(10)
	if !midiOpened {
		err := portmidi.Initialize()
		if err != nil {
			return nil, err
		}
		midiOpened = true
		midiInDevice = ell.StringValue(argv[0])
		midiOutDevice = ell.StringValue(argv[1])
		midiBufsize = ell.Int64Value(argv[2])

		outdev, outname := findMidiOutputDevice(midiOutDevice)
		out, err := portmidi.NewOutputStream(outdev, midiBufsize, latency)
		if err != nil {
			return nil, err
		}
		midiOut = out
		midiOutDevice = outname
		if midiInDevice != "" {
			indev := findMidiInputDevice(midiInDevice)
			if indev >= 0 {
				in, err := portmidi.NewInputStream(indev, midiBufsize)
				if err != nil {
					return nil, err
				}
				midiIn = in
			}
		}
		midiBaseTime = ell.Now()

	}
	result := ell.MakeStruct(4)
	if midiInDevice != "" {
		ell.Put(result, inputKey, ell.String(midiInDevice))
	}
	if midiOutDevice != "" {
		ell.Put(result, outputKey, ell.String(midiOutDevice))
	}
	ell.Put(result, bufsizeKey, ell.Number(float64(midiBufsize)))
	return result, nil
}
Exemple #7
0
func initPortMidi() *portmidi.Stream {
	portmidi.Initialize()

	deviceCount := portmidi.CountDevices()
	fmt.Println("Number of MIDI devices: ", deviceCount)

	dev := portmidi.DeviceId(3 - 1)

	for i := 0; i < deviceCount; i++ {
		id := portmidi.DeviceId(i)
		fmt.Println("Index ", i, "Id", id, " Device ", *portmidi.GetDeviceInfo(id))
	}

	out, err := portmidi.NewOutputStream(dev, 0, 0)
	if err == nil {
		fmt.Println("used device info: ", *portmidi.GetDeviceInfo(dev))
	} else {
		fmt.Println("Error: ", err)
		os.Exit(2)
	}
	return out
}
Exemple #8
0
func main() {

	var (
		ccValue  int64
		ccNumber int64
		deviceId portmidi.DeviceId
		err      error
	)

	ccNumber, ccValue, err = parseArgs(os.Args)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	portmidi.Initialize()
	defer portmidi.Terminate()

	deviceId, err = getDeviceId()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	out, err := portmidi.NewOutputStream(deviceId, 1024, 0)
	if err != nil {
		fmt.Println("Error opening MicroBrute output port:", err)
		os.Exit(1)
	}

	err = out.WriteShort(ccMessage, ccNumber, ccValue)
	if err != nil {
		fmt.Println("Error sending message to MicroBrute:", err)
		os.Exit(1)
	}

}
Exemple #9
0
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)
		}
	}
}