fix min max and add end time from csv

This commit is contained in:
Philipp Kramer 2022-05-13 10:09:14 +02:00
parent 2d6ec40ba8
commit 077392f6a7
1 changed files with 53 additions and 31 deletions

View File

@ -17,12 +17,12 @@ import numpy as np #pip install numpy
3: t=150s bis t=5*60s daten laufen pro zeile als 1/8 bei tempo 50 =0,6s/beat. schnelles durchalufen wie gehabt. mit zufälligen pausen
'''
midiChannel=3 #<- Change This Manually. TODO: Monday
scenetimes=[0,20,60,70,120,150,4*60+52] #definition of times
scenetimes=[0,20,60,70,120,150,500] #definition of times . Last value will be overwritten by last time of csv file
#scenetimes=[0,2,4,5,6,10,300] #definition of times FASTER FOR TESTING
scenes =[0,1, 0, 2, 0, 3, 0] #definition of scenes
scenes =[0,1, 0, 2, 0, 3, -1] #definition of scenes. scene -1 will end the program
currentScene=-1 #keeps track of current scene
@ -35,25 +35,10 @@ valueScene=['','','','']
valueSceneOff=[0,0,0,0]
timemultiplierScene=[1,1,1,1]
#Scene 0
#Scene 1
topicScene[1]="/"+str(midiChannel)+"/oscplayer/strophe"
valueScene[1]=1
stropheTriggered=False #keep track if it is already triggered once
#Scene 2
topicScene[2]="/"+str(midiChannel)+"/oscplayer/satz/?"
valueScene[2]=1
stropheTriggered=False
valueCommulative=0
#Scene 3
topicScene[3]="/"+str(midiChannel)+"/oscplayer/value/?"
valueScene[3]=1
timemultiplierScene[3]=60.0 / 50 / 2
midiChannel=0 #Can be changed through args TODO: Monday
header=None
rows=None
@ -72,15 +57,42 @@ meanvalues=[]
def init():
parser = argparse.ArgumentParser()
parser.add_argument("--ip", default="127.0.0.1", help="The ip of the OSC server")
parser.add_argument("--port", type=int, default=7005, help="The port the OSC server is listening on")
parser.add_argument("--channel", type=int, default=1, help="The midi channel to use for osc topic")
args = parser.parse_args()
global client
client = udp_client.SimpleUDPClient(args.ip, args.port)
global midiChannel
midiChannel=args.channel
print("Using midi Channel "+str(midiChannel))
# Initialize parameters for scenes
#Scene 0
#Scene 1
topicScene[1]="/"+str(midiChannel)+"/oscplayer/strophe"
valueScene[1]=1
stropheTriggered=False #keep track if it is already triggered once
#Scene 2
topicScene[2]="/"+str(midiChannel)+"/oscplayer/satz/?"
valueScene[2]=1
valueCommulative=0
#Scene 3
topicScene[3]="/"+str(midiChannel)+"/oscplayer/value/?"
valueScene[3]=1
timemultiplierScene[3]=60.0 / 50 / 2
#########
csvlist=None
with open('heart_cluster_max.csv', newline='') as csvfile:
@ -95,7 +107,7 @@ def init():
global header
header=csvlist[0]
global rows
rows=csvlist[1:]
rows=csvlist[1:] #discard first row/header
global rowid
rowid=0 #waiting for this row to send
@ -108,8 +120,13 @@ def init():
global meanvalues
minvalues=[float(min(x)) for x in filterEmpty(rows)]
maxvalues=[float(max(x)) for x in filterEmpty(rows)]
minvalues=[min([float(a) for a in x]) for x in filterEmpty(rows)]
maxvalues=[max([float(a) for a in x]) for x in filterEmpty(rows)]
scenetimes[-1]=maxvalues[0] #take maximum value of first column (time) as endtime
print("Scenetimes:" + str(scenetimes))
#print(np.mean(np.array((filterEmpty(rows))),axis=1)) #TODO: mittelwert ausrechnen funktioniert noch nicht
#exit()
@ -175,7 +192,7 @@ def update(ctime):
if (i==0): #were only using the first column of the csv file
if currentScene==1:
if not stropheTriggered:
_threshold=(minvalues[i]+(maxvalues[i]-minvalues[i])*0.2)
_threshold=(minvalues[i+1]+(maxvalues[i+1]-minvalues[i+1])*0.2)
print("waiting for "+str(value)+"<"+str(_threshold))
if value<_threshold or getSceneFromTime(timeFromStart+1)!=currentScene: #trigger "randomly" or when scene is about to change
print("OSC: "+topicScene[currentScene]+": "+str(valueScene[currentScene]))
@ -184,18 +201,18 @@ def update(ctime):
stropheTriggered=True
if currentScene==2:
print("waiting. diff="+str(abs(maxvalues[i]-value))+" commulative="+str(valueCommulative))
valueCommulative+=abs(maxvalues[i]-value)
print("waiting. diff="+str(abs(maxvalues[i+1]-value))+" commulative="+str(valueCommulative))
valueCommulative+=abs(maxvalues[i+1]-value)
if valueCommulative>100:
valueCommulative=0 #reset
sendvalue=int(mapRange(value,minvalues[i],maxvalues[i],1,4+1))
sendvalue=int(mapRangeConstrain(value,minvalues[i+1],maxvalues[i+1],1,4))
print("OSC: "+topicScene[currentScene].replace('?',str(sendvalue))+": "+str(valueScene[currentScene]))
client.send_message(topicScene[currentScene].replace('?',str(sendvalue)), valueScene[currentScene])
client.send_message(topicScene[currentScene].replace('?',str(sendvalue)), valueSceneOff[currentScene])
if currentScene==3:
sendvalue=int(mapRange(value,minvalues[i],maxvalues[i],1,40+1))
sendvalue=int(mapRangeConstrain(value,minvalues[i+1],maxvalues[i+1],1,40))
print("OSC: "+str(rowid)+"/"+str(len(rows))+" t="+str(round(timeFromStart,2))+" "+topicScene[currentScene].replace('?',str(sendvalue))+": "+str(valueScene[currentScene]))
client.send_message(topicScene[currentScene].replace('?',str(sendvalue)), valueScene[currentScene])
client.send_message(topicScene[currentScene].replace('?',str(sendvalue)), valueSceneOff[currentScene])
@ -206,12 +223,12 @@ def update(ctime):
rowid+=1
return True
return currentScene>=0
def filterEmpty(rows):
m=[np.array(rows)[:,i+1] for i in range(np.shape(np.array(rows))[1]-1)]
m=[np.array(rows)[:,i] for i in range(np.shape(np.array(rows))[1])]
n=[[x if len(x)>0 else None for x in y] for y in m]
n=[list(filter(None, x)) for x in n]
return n
@ -219,6 +236,11 @@ def filterEmpty(rows):
def mapRange(value, inMin, inMax, outMin, outMax):
return outMin + (((value - inMin) / (inMax - inMin)) * (outMax - outMin))
def mapRangeConstrain(v,minIn,maxIn,minOut,maxOut): #include minOut and maxOut and constrain
return int(max(minOut,min(maxOut,mapRange(v,minIn,maxIn,minOut,maxOut+1))))
if __name__ == "__main__":
init()