#!/usr/bin/python
 # -*- coding: utf-8 -*-
from string import *

csd = q.getDocument("pythonLinSeg.csd") # get thes index of the csd (having also the LineEdit widget with channel name "string"
orc = q.getOrc(csd) # read the contents of the orchestra
newOrc = "" # that will be written later here

inputline = q.getChannelString("string",csd) # get the contets of channel 'string'  in document having index csd
data = inputline.split() # split into list 
durations = map(float,data[1: :2]) # extraxt every odd member, convert strings to floats
duration = sum(durations) # get the sum of durations of the segments
withCommas = join(data,",") #join the numbers again into string with commas

for line in orc.split("\n"): # split orchestra into lines, scan them for  insertLinSegHere
    if ( string.find(line,";insertLinSegHere") > 0 ) :
         line = "kOut linseg " + withCommas + ";insertLinSegHere"# form the new line
         print line
    newOrc = newOrc + line + "\n" # put the lines to new orchestra-string, replace the newlines tha had been splitted out
q.setOrc(newOrc,csd) # replace the orchestra
q.setSco("i 1 0 " + str(duration)) # play instrument 1 for 'duration' seconds
q.play(csd) # and play it