osctools/oscplayer/oscplayer.py

280 lines
8.9 KiB
Python
Raw Normal View History

2022-05-05 15:00:31 +00:00
import argparse
import random
2022-05-10 15:09:22 +00:00
from sqlite3 import TimeFromTicks
2022-05-05 15:00:31 +00:00
import time
2022-05-12 13:46:43 +00:00
from pythonosc import udp_client #pip install python-osc
2022-05-05 15:00:31 +00:00
import csv
2022-05-12 13:46:43 +00:00
import numpy as np #pip install numpy
2022-05-05 15:00:31 +00:00
2022-05-10 15:09:22 +00:00
'''
0: t=0s bis t=20s keine ausgabe per osc
1: t=20s bis t=60s einmal die strophe triggern (also nachricht /3/oscplayer/strophe 1) "zufällig"(=basierend auf heartrate daten) zwischen t=20 und t=60
0: t=60s bis t=70s passiert nichts
2: t=70s bis t=120s nachricht /3/oscplayer/satz/<1-4> 1 . "zufällig" auf heartrate daten basierend triggern (muss nicht im beat sein), nicht zu häufig. ca alle 5-10s einmal
0: t=120s bis t=150s nichts, vorheriges hört auf
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
'''
2022-05-13 08:09:14 +00:00
2022-05-17 07:54:09 +00:00
scenetimes=[0,10,60,70,130,150,155,210,220,290,500] #definition of times . Last value will be overwritten by last time of csv file
2022-05-12 13:46:43 +00:00
#scenetimes=[0,2,4,5,6,10,300] #definition of times FASTER FOR TESTING
2022-05-17 07:54:09 +00:00
scenes =[0,1, 0, 2, 0 ,3 , 0, 3 , 0, 3, -1] #definition of scenes. scene -1 will end the program
2022-05-10 15:09:22 +00:00
currentScene=-1 #keeps track of current scene
timemultiplier=1.0 #current time multiplier for speed changes
topicScene=['','','','']
2022-05-16 06:57:08 +00:00
timemultiplierScene=[1,1,1,1] #only initializiation
2022-05-13 08:09:14 +00:00
stropheTriggered=False
2022-05-10 15:09:22 +00:00
valueCommulative=0
2022-05-13 08:09:14 +00:00
midiChannel=0 #Can be changed through args TODO: Monday
2022-05-05 15:00:31 +00:00
header=None
rows=None
rowid=0
time_start=0
client=None
lastvalue=[]
minvalues=[]
maxvalues=[]
meanvalues=[]
2022-05-10 15:09:22 +00:00
2022-05-05 15:00:31 +00:00
def init():
2022-05-13 08:09:14 +00:00
2022-05-05 15:00:31 +00:00
parser = argparse.ArgumentParser()
parser.add_argument("--ip", default="127.0.0.1", help="The ip of the OSC server")
2022-05-12 13:46:43 +00:00
parser.add_argument("--port", type=int, default=7005, help="The port the OSC server is listening on")
2022-05-17 07:54:09 +00:00
parser.add_argument("--channel", type=int, default=None, help="The midi channel to use for osc topic. Default=Automatic")
2022-05-16 06:57:08 +00:00
parser.add_argument("--file", type=str, default='heart_cluster_max.csv', help="The csv file used for data input")
2022-05-05 15:00:31 +00:00
args = parser.parse_args()
global client
client = udp_client.SimpleUDPClient(args.ip, args.port)
2022-05-13 08:09:14 +00:00
global midiChannel
midiChannel=args.channel
print("Using midi Channel "+str(midiChannel))
2022-05-16 06:57:08 +00:00
csvfilename=args.file
print("Using csv file "+str(csvfilename))
2022-05-13 08:09:14 +00:00
#########
2022-05-05 15:00:31 +00:00
csvlist=None
2022-05-16 06:57:08 +00:00
with open(csvfilename, newline='') as csvfile:
2022-05-05 15:00:31 +00:00
csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
csvlist = list(csvreader)
'''for row in csvreader:
if header is None:
header=row
else:
rows.vstack(row)
'''
global header
header=csvlist[0]
global rows
2022-05-13 08:09:14 +00:00
rows=csvlist[1:] #discard first row/header
2022-05-17 07:54:09 +00:00
rows = [x for x in rows if float(x[1])>40] #filter out rows with value <40
2022-05-05 15:00:31 +00:00
global rowid
rowid=0 #waiting for this row to send
global lastvalue
lastvalue=np.zeros(len(rows)-1)
2022-05-17 07:54:09 +00:00
2022-05-05 15:00:31 +00:00
global minvalues
global maxvalues
global meanvalues
2022-05-13 08:09:14 +00:00
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)]
2022-05-16 07:11:15 +00:00
meanvalues=[np.mean([float(a) for a in x]) for x in filterEmpty(rows)]
2022-05-17 07:54:09 +00:00
if midiChannel is None:
midiChannel=int(mapRangeConstrain(float(rows[0][1]),minvalues[1],maxvalues[1],1,16))
print("Using midi Channel "+str(midiChannel))
2022-05-13 08:09:14 +00:00
scenetimes[-1]=maxvalues[0] #take maximum value of first column (time) as endtime
2022-05-17 07:54:09 +00:00
scenetimes[-2]=maxvalues[0]-20
2022-05-13 08:09:14 +00:00
print("Scenetimes:" + str(scenetimes))
2022-05-16 07:11:15 +00:00
2022-05-17 07:54:09 +00:00
2022-05-16 07:11:15 +00:00
print("minvalues="+str(minvalues))
print("maxvalues="+str(maxvalues))
print("meanvalues="+str(meanvalues))
2022-05-13 08:09:14 +00:00
2022-05-17 07:54:09 +00:00
# Initialize parameters for scenes
#Scene 0
#Scene 1
topicScene[1]="/"+str(midiChannel)+"/oscplayer/strophe"
stropheTriggered=False #keep track if it is already triggered once
#Scene 2
topicScene[2]="/"+str(midiChannel)+"/oscplayer/satz/?"
valueCommulative=0
#Scene 3
topicScene[3]="/"+str(midiChannel)+"/oscplayer/value/?"
timemultiplierScene[3]=60.0 / 50 / 2
2022-05-05 15:00:31 +00:00
#print(np.mean(np.array((filterEmpty(rows))),axis=1)) #TODO: mittelwert ausrechnen funktioniert noch nicht
#exit()
#meanvalues=[float(np.mean(x)) for x in filterEmpty(rows)]
global time_start
time_start = time.time()
2022-05-10 15:09:22 +00:00
def getSceneFromTime(t):
i=0
while i<len(scenetimes) and t>=scenetimes[i]:
i+=1
return scenes[i-1]
2022-05-05 15:00:31 +00:00
def update(ctime):
global minvalues
global maxvalues
global meanvalues
global rowid
global client
global lastvalue
2022-05-10 15:09:22 +00:00
global timemultiplier
global currentScene
global stropheTriggered
global valueCommulative
timeFromStart=ctime-time_start
2022-05-16 06:57:08 +00:00
if currentScene!=getSceneFromTime(timeFromStart): #When scene changed
2022-05-10 15:09:22 +00:00
currentScene=getSceneFromTime(timeFromStart)
timemultiplier=timemultiplierScene[currentScene] #update speed
_rowidBefore=rowid
rowid=0
while timeFromStart>=(float(rows[rowid%len(rows)][0])+float(rows[len(rows)-1][0])*int(rowid/len(rows)))*timemultiplier: #fast forward rowid
rowid+=1
'''if rowid>=len(rows): #ended?
rowid=0'''
rowid-=1
if _rowidBefore!=rowid:
print("Updated Row Id from "+str(_rowidBefore)+" to "+str(rowid)+" of "+str(len(rows)))
print("t="+str(timeFromStart)+"s: Changed Scene to "+str(currentScene)+". timemultiplier="+str(timemultiplier))
2022-05-16 06:57:08 +00:00
# Below here things that should happen once on scene change to "currentScene"
if currentScene==3: #on scene change to 3
sendOSCTrigger("/sprachsamples/beatrepeatonoff")
if currentScene==-1: #ended
sendOSCTrigger("/stopcommand")
sendOSCTrigger("/sprachsamples/beatrepeatonoff")
2022-05-10 15:09:22 +00:00
'''if rowid>=len(rows): #ended?
rowid=0 #when finished csv start from beginning
#return False
'''
if timeFromStart>=(float(rows[rowid%len(rows)][0])+float(rows[len(rows)-1][0])*int(rowid/len(rows)))*timemultiplier:
2022-05-05 15:00:31 +00:00
#print(rows[rowid])
2022-05-10 15:09:22 +00:00
crow=rows[rowid%len(rows)]
2022-05-05 15:00:31 +00:00
for i in range(len(header)-1):
if len(crow[i+1])>0: #value not empty
value=float(crow[i+1])
2022-05-10 15:09:22 +00:00
if (i==0): #were only using the first column of the csv file
2022-05-16 06:57:08 +00:00
#Below here things that should happen when in "currentScene" every time new data from the csv file is read
2022-05-10 15:09:22 +00:00
if currentScene==1:
if not stropheTriggered:
2022-05-13 08:09:14 +00:00
_threshold=(minvalues[i+1]+(maxvalues[i+1]-minvalues[i+1])*0.2)
2022-05-10 15:09:22 +00:00
print("waiting for "+str(value)+"<"+str(_threshold))
if value<_threshold or getSceneFromTime(timeFromStart+1)!=currentScene: #trigger "randomly" or when scene is about to change
2022-05-16 07:11:15 +00:00
sendOSCTrigger(topicScene[currentScene])
2022-05-10 15:09:22 +00:00
stropheTriggered=True
if currentScene==2:
2022-05-13 08:09:14 +00:00
print("waiting. diff="+str(abs(maxvalues[i+1]-value))+" commulative="+str(valueCommulative))
2022-05-17 07:54:09 +00:00
#valueCommulative+=abs(maxvalues[i+1]-value)
valueCommulative+=mapRangeConstrain(value,minvalues[i+1],maxvalues[i+1],0,100)
2022-05-10 15:09:22 +00:00
2022-05-17 07:54:09 +00:00
if valueCommulative>500: #higher value triggers less
valueCommulative-=500 #reset
2022-05-13 08:09:14 +00:00
sendvalue=int(mapRangeConstrain(value,minvalues[i+1],maxvalues[i+1],1,4))
2022-05-16 07:11:15 +00:00
sendOSCTrigger(topicScene[currentScene].replace('?',str(sendvalue)))
2022-05-10 15:09:22 +00:00
if currentScene==3:
2022-05-13 08:09:14 +00:00
sendvalue=int(mapRangeConstrain(value,minvalues[i+1],maxvalues[i+1],1,40))
2022-05-16 07:11:15 +00:00
sendOSCTrigger(topicScene[currentScene].replace('?',str(sendvalue)))
print("OSC: "+str(rowid)+"/"+str(len(rows))+" t="+str(round(timeFromStart,2)))
2022-05-10 15:09:22 +00:00
2022-05-05 15:00:31 +00:00
lastvalue[i]=value #save last value
rowid+=1
2022-05-10 15:09:22 +00:00
2022-05-13 08:09:14 +00:00
return currentScene>=0
2022-05-05 15:00:31 +00:00
def filterEmpty(rows):
2022-05-13 08:09:14 +00:00
m=[np.array(rows)[:,i] for i in range(np.shape(np.array(rows))[1])]
2022-05-05 15:00:31 +00:00
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
2022-05-17 07:54:09 +00:00
2022-05-05 15:00:31 +00:00
def mapRange(value, inMin, inMax, outMin, outMax):
return outMin + (((value - inMin) / (inMax - inMin)) * (outMax - outMin))
2022-05-13 08:09:14 +00:00
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))))
2022-05-16 06:57:08 +00:00
def sendOSCTrigger(mesg):
print("OSC: "+str(mesg)+" : 1 and 0")
client.send_message(str(mesg), 1)
client.send_message(str(mesg), 0)
2022-05-13 08:09:14 +00:00
2022-05-05 15:00:31 +00:00
if __name__ == "__main__":
init()
2022-05-10 15:09:22 +00:00
2022-05-05 15:00:31 +00:00
res=True
while res:
2022-05-17 07:54:09 +00:00
res=update(time.time())