123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #!/usr/bin/python
- from simplemidi.midiparser import *
- from simplemidi.midiplayer import *
- import os
- """
- md=MIDIHeaderChunk()
- md.length=6
- md.format=MIDIHeaderChunk.FORMAT_MULTIPLE_CHANNEL
- md.tracks=5
- md.division=960
- mt=MIDITrackChunk()
- mt.addNote(0.5, 60)
- mt.addNote(0.5, 63)
- mt.addNote(1, 63)
- mt.addNote(0.25, 63)
- f=open("test", "wb")
- print(f)
- md.write(f)
- mt.write(f)
- f.close()
- """
- from simplemidi.alsaconnector import AlsaConnector, AlsaPort
- from random import randint
- os.sched_setscheduler(os.getpid(), os.SCHED_FIFO, os.sched_param(1))
- class Compose:
- _DEFAULT_PARAM={
- 'keys' : [57, 59,60,62,64,65,67],
- "times": [ [0.5,4], [1,4], [1.5, 1]]
- }
- def __init__(self, param=_DEFAULT_PARAM):
- self.track = MIDITrackChunk()
- keys=param["keys"] if "keys" in param else Compose._DEFAULT_PARAM["keys"]
- self.keys=[]
- for i in keys: self.keys.append((i, randint(0,10)))
- self.times=param["times"] if "times" in param else Compose._DEFAULT_PARAM["times"]
- temps=64
- while temps>0:
- x=self.randTime(temps)
- k=self.randKey()
- self.track.addNote(x, k)
- temps-=x
- print("Mesure: "+str(8-temps))
- def randTime(self, max):
- x=0
- for i in self.times: x+=i[1] if len(i)>1 and i[1]<=max else 1
- dice=randint(0,x-1)
- for i in self.times:
- if i[1]<=max:
- dice-=i[1]
- if dice<=0: return i[0]
- return self.times[0][0]
- def randKey(self):
- x = 0
- for i in self.keys: x += i[1] if len(i) > 1 else 1
- dice = randint(0, x - 1)
- for i in self.keys:
- dice -= i[1]
- if dice <= 0: return i[0]
- return self.keys[0][0]
- def play(self):
- self.file=MidiFile()
- self.file.tracks=[self.track.events]
- mp=MidiPlayer(self.file)
- #mp.setBpmRatio(2)
- AlsaConnector.connect((133,0), (130,0))
- #input("...")
- mp.port.send(Stop())
- mp.port.send(Continue())
- mp.port.send(Start())
- while True:
- mp.play()
- print("===========")
- c=Compose()
- c.play()
|