Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

 

 

import pypm 

import array 

import time 

 

class TriggerFinger(object): 

    pads = range(1,17) 

    faders = range(17, 21) 

    dials = range(21, 29) 

 

 

_initialized = False 

 

def init(): 

    global _initialized 

    if not _initialized: 

        pypm.Initialize() 

        _initialized = True 

 

 

def printDevices(): 

    init() 

    for deviceId in xrange(pypm.CountDevices()): 

        interf, name, inp, outp, opened = pypm.GetDeviceInfo(deviceId) 

 

        if inp: 

            inOutType = '(input)' 

        elif outp: 

            inOutType = '(output)' 

        else: 

            inOutType = '(unknown)' 

 

        status = '(opened)' if opened == 1 else '(unopened)' 

 

        print deviceId, name, inOutType, status 

 

 

def findInputDevice(deviceName): 

    init() 

    for deviceId in xrange(pypm.CountDevices()): 

        interf, name, inp, outp, opened = pypm.GetDeviceInfo(deviceId) 

        if inp and (deviceName in name): 

                return deviceId 

 

def findOutputDevice(deviceName): 

    init() 

    for deviceId in xrange(pypm.CountDevices()): 

        interf, name, inp, outp, opened = pypm.GetDeviceInfo(deviceId) 

        if outp and (deviceName in name): 

                return deviceId 

 

 

def findTriggerFinger(): 

    return findInputDevice('USB Trigger Finger MIDI') 

 

def findKorgNanoKontrol2(): 

    deviceId = findInputDevice('nanoKONTROL2 SLIDER/KNOB') 

    if deviceId is None: 

        deviceId = findInputDevice('nanoKONTROL2 MIDI 1') 

    return deviceId 

 

class MidiReader(object): 

 

    def __init__(self, deviceId=None): 

        init() 

 

        if deviceId is None: 

            deviceId = findTriggerFinger() 

 

        assert deviceId is not None 

 

        self.stream = pypm.Input(deviceId) 

 

    def _convertMessage(self, streamMessage): 

        ''' 

        Given a stream message [[b1, b2, b3, b4], timestamp] 

        Return [timestamp, b1, b2, b3, b4] 

        ''' 

        midiData, timestamp = streamMessage 

        b1, b2, b3, b4 = midiData 

        return [timestamp, b1, b2, b3, b4] 

 

    def getNextMessage(self): 

        ''' 

        Returns the next message in the buffer. 

        Does not block.  Returns None if buffer is empty. 

        ''' 

        if self.stream.Poll(): 

            message = self.stream.Read(1)[0] 

            return self._convertMessage(message) 

 

    def getMessages(self): 

        ''' 

        Returns a list of all messages currently in the buffer, up to 1024 messages. 

        Does not block.  Returns empty list if buffer is empty. 

        ''' 

        if self.stream.Poll(): 

            messages = self.stream.Read(1024) 

            return [self._convertMessage(message) for message in messages] 

        return []