示例#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:
		}
	}
}
示例#2
0
func main() {
	flag.Parse()

	portaudio.Initialize()
	defer portaudio.Terminate()

	portmidi.Initialize()
	defer portmidi.Terminate()

	if *doDemo {
		if err := demo(); err != nil {
			log.Println(err)
		}
		return
	}

	http.Handle("/", http.FileServer(http.Dir("static")))
	http.HandleFunc("/socket", socket.Handler)

	l, err := net.Listen("tcp", *listenAddr)
	if err != nil {
		log.Fatal(err)
	}
	go http.Serve(l, nil)

	u := fmt.Sprintf("http://%v/", *listenAddr)
	if !*doBrowser || !openBrowser(u) {
		fmt.Printf("Open your web browser to %v\n\n", u)
	}

	fmt.Println("Press enter to quit...")
	os.Stdin.Read([]byte{0})
}
示例#3
0
文件: main.go 项目: rakyll/sigourney
func main() {
	flag.Parse()

	portaudio.Initialize()
	defer portaudio.Terminate()

	portmidi.Initialize()
	defer portmidi.Terminate()

	if *doDemo {
		if err := demo(); err != nil {
			log.Println(err)
		}
		return
	}

	http.Handle("/", http.FileServer(http.Dir("static")))
	http.HandleFunc("/socket", socketHandler)
	go func() {
		if err := http.ListenAndServe(*listenAddr, nil); err != nil {
			log.Println(err)
		}
	}()

	os.Stdout.Write([]byte("Press enter to stop...\n"))
	os.Stdin.Read([]byte{0})
}
示例#4
0
func newDevices() (devs devices) {
	portmidi.Initialize()
	devs = devices{
		info: map[portmidi.DeviceId]*portmidi.DeviceInfo{},
	}

	for id := 0; id < portmidi.CountDevices(); id++ {
		devID := deviceID(id)
		devs.info[devID] = portmidi.GetDeviceInfo(devID)
	}
	return
}
示例#5
0
文件: gen.go 项目: mdwhatcott/gen
func main() {
	flag.Parse()
	rand.Seed(time.Now().Unix())

	if err := portmidi.Initialize(); err != nil {
		log.Fatal(err)
	}
	defer portmidi.Terminate()
	if *deviceN == -1 {
		listDevices()
		return
	}
	if err := run(); err != nil {
		portmidi.Terminate()
		log.Fatal(err)
	}
}
示例#6
0
文件: midi.go 项目: boynton/midi-ell
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
}
示例#7
0
文件: demo.go 项目: mantyr/go-sound
// 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)
		}
	*/
}
示例#8
0
func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, usage)
		flag.PrintDefaults()
		os.Exit(2)
	}
	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
	}
	p, err := drum.DecodeFile(flag.Arg(0))
	if err != nil {
		log.Fatalf("cannot decode splice file: %v", err)
	}
	if *showInfo {
		fmt.Print(p)
		return
	}
	patches, err := readPatches(p, *sampleDir)
	if err != nil {
		log.Fatalf("cannot read sample patches: %v", err)
	}
	drumMod, err := drummachine.New(p, patches)
	if err != nil {
		log.Fatalf("cannot make new drum machine: %v", err)
	}

	portaudio.Initialize()
	defer portaudio.Terminate()

	portmidi.Initialize()
	defer portmidi.Terminate()

	e := audio.NewEngine()
	e.Input("in", drumMod)
	if err := e.Start(); err != nil {
		panic(err)
	}
	select {}
}
示例#9
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
}
示例#10
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)
	}

}
示例#11
0
func main() {
	var synthID int32
	const synthName = "sineTone"

	// Set up SuperCollider client.
	client, err := sc.NewClient("udp", "127.0.0.1:57121", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	_, err = client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	err = client.DumpOSC(int32(1))
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		freq := p.Add("freq", 440)
		gain := p.Add("gain", 0.5)
		bus := sc.C(0)
		env := sc.EnvGen{
			Env:        sc.EnvPerc{},
			Done:       sc.FreeEnclosing,
			LevelScale: gain,
		}.Rate(sc.KR)
		sig := sc.SinOsc{Freq: freq}.Rate(sc.AR).Mul(env)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	err = client.SendDef(def)
	if err != nil {
		log.Fatal(err)
	}

	// initialize midi
	portmidi.Initialize()
	// this code can be uncommented to discover the
	// device ID's portmidi comes up with
	// deviceCount := portmidi.CountDevices()
	// enc := json.NewEncoder(os.Stdout)
	// for i := 0; i < deviceCount; i++ {
	// 	info := portmidi.GetDeviceInfo(portmidi.DeviceId(i))
	// 	log.Printf("device %d - ", i)
	// 	err = enc.Encode(info)
	// 	if err != nil {
	// 		log.Fatal(err)
	// 	}
	// }

	// setup midi input stream and listen for midi events
	in, err := portmidi.NewInputStream(3, 1024)
	if err != nil {
		log.Fatal(err)
	}
	ch := in.Listen()
	for event := range ch {
		if event.Status == 144 {
			// MIDI note
			log.Printf("Note %-3d Velocity %-3d\n", event.Data1, event.Data2)
			if event.Data2 > 0 {
				// Note On
				synthID = client.NextSynthID()
				ctls := map[string]float32{
					"freq": sc.Midicps(float32(event.Data1)),
					"gain": float32(event.Data2 / 127.0),
				}
				_, err = client.Synth(synthName, synthID, sc.AddToTail, sc.DefaultGroupID, ctls)
			}
		}
	}
	portmidi.Terminate()
}
示例#12
0
文件: main.go 项目: rakyll/launchpad
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)
		}
	}
}