Пример #1
0
func findMidiInputDevice(name string) portmidi.DeviceId {
	devcount := portmidi.CountDevices()
	for i := 0; i < devcount; i++ {
		id := portmidi.DeviceId(i)
		info := portmidi.GetDeviceInfo(id)
		if info.IsInputAvailable {
			if info.Name == name {
				return id
			}
		}
	}
	return portmidi.DeviceId(-1)
}
Пример #2
0
func listDevices() {
	n := portmidi.CountDevices()
	for i := 0; i < n; i++ {
		info := portmidi.GetDeviceInfo(portmidi.DeviceId(i))
		fmt.Printf("Device %d: %v\n", i, info)
	}
}
Пример #3
0
func initMidi() {
	s, err := portmidi.NewInputStream(portmidi.DeviceId(*midiDevice), 1024)
	if err != nil {
		log.Println(err)
		return
	}
	go midiLoop(s)
}
Пример #4
0
func getDeviceId() (portmidi.DeviceId, error) {
	total := portmidi.CountDevices()

	for id := 0; id < total; id++ {
		id := portmidi.DeviceId(id)
		info := portmidi.GetDeviceInfo(id)
		if info.Name == "MicroBrute" && info.IsOutputAvailable {
			return id, nil
		}
	}

	return -1, fmt.Errorf("MicroBrute not found")
}
Пример #5
0
// discovers the currently connected LaunchControl 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.GetDeviceInfo(portmidi.DeviceId(i))
		if strings.Contains(info.Name, "Launch Control XL") {
			if info.IsInputAvailable {
				in = i
			}
			if info.IsOutputAvailable {
				out = i
			}
		}
	}
	if in == -1 || out == -1 {
		err = errors.New("launchcontrol: no launch control is connected")
	} else {
		input = portmidi.DeviceId(in)
		output = portmidi.DeviceId(out)
	}
	return
}
Пример #6
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
}
Пример #7
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
}
Пример #8
0
func findMidiOutputDevice(name string) (portmidi.DeviceId, string) {
	devcount := portmidi.CountDevices()
	for i := 0; i < devcount; i++ {
		id := portmidi.DeviceId(i)
		info := portmidi.GetDeviceInfo(id)
		if info.IsOutputAvailable {
			if info.Name == name {
				return id, info.Name
			}
		}
	}
	id := portmidi.GetDefaultOutputDeviceId()
	info := portmidi.GetDeviceInfo(id)
	return id, info.Name
}
Пример #9
0
func initMidi() {
	device := portmidi.DeviceId(*midiDevice)
	if device == -1 {
		device = portmidi.GetDefaultInputDeviceId()
	}
	s, err := portmidi.NewInputStream(device, 1024)
	if err != nil {
		log.Println(err)
		return
	}
	if s == nil {
		log.Println("could not initialize MIDI input device")
		return
	}
	go midiLoop(s)
}
Пример #10
0
// renderMidi reads a midi device, converts it to a live sound, and renders the waveform to screen.
func renderMidi() {
	fmt.Println("Loading portmidi...")
	pm.Initialize()

	midi := s.NewMidiInput(pm.DeviceId(3)) // ++See below
	fmt.Printf("Rendering midi...\n")

	// Render the generated sine wave to screen:
	output.Render(midi, 2000, 500)
	// ...or, play it live:
	// output.Play(midi)

	/*
		++ Note: to find your device id, use a version of this:
		for i := 1; i <= pm.CountDevices(); i++ {
			fmt.Printf("Reading device: %d\n", i)
			di := pm.GetDeviceInfo(pm.DeviceId(i))
			fmt.Printf("Info = %v\n", di)
		}
	*/
}
Пример #11
0
func deviceID(idint int) portmidi.DeviceId {
	return portmidi.DeviceId(idint)
}
Пример #12
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.New(); 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)
		}
	}
}