import os from Brick import * from copy import deepcopy _showCSoundMsg = False _showDebugMsg = False class MPScore: def minusone(self, n1, n2): array = [[-1 for i in range(n2)] for j in range(n1)] return array def zeros(self, n1, n2): array = [[0 for i in range(n2)] for j in range(n1)] return array def zeros3(self, n1, n2, n3): array = [[[0 for i in range(n3)] for j in range(n2)] for k in range(n1)] return array def delete_selection(self): w = self.ngrid_h h = self.ngrid_v # self.bsel = deepcopy(self.sel) self.sel = self.zeros3(8, w, h) # score region that is already selected (after mouse_release) self.scut = self.zeros3(8, w, h) # score region (cut) that is already selected (after mouse_release) self.select_state = 0 # 0: none 1: selected def __init__(self, w, h, platform, mp): self.main = mp self.platform = platform self.top = self.minusone(w, h) self.top_buf = self.minusone(w, h) self.keyon = [0 for i in range(h)] self.map = self.zeros3(8, w, h) self.cut = self.zeros3(8, w, h) self.title = "" self.description = "" self.note = "" self.wsize = 8 self.csize = 8 self.tesize = 12 self.tsize = 8 self.work_bricks = [Brick(w, h, platform, mp) for i in range(self.wsize)] self.mymelody_bricks = [Brick(w, h, platform, mp) for i in range(self.csize)] self.myrhythm_bricks = [Brick(w, h, platform, mp) for i in range(self.csize)] self.scale_bricks = [Brick(w, h, platform, mp) for i in range(6)] self.cmelody_bricks = [Brick(w, h, platform, mp) for i in range(self.csize)] self.crhythm_bricks = [Brick(w, h, platform, mp) for i in range(self.csize)] self.trash_bricks = [Brick(w, h, platform, mp) for i in range(self.tsize)] self.temp_bricks = [Brick(w, h, platform, mp) for i in range(self.tesize)] self.work_bricks_cnt = 6 self.mymelody_bricks_cnt = 0 self.myrhythm_bricks_cnt = 0 self.scale_bricks_cnt = 6 self.cmelody_bricks_cnt = 0 self.crhythm_bricks_cnt = 0 self.trash_bricks_cnt = 0 self.temp_bricks_cnt = 0 self.srv_brick_list = [] self.local_brick_list = [] self.bricks = self.work_bricks self.sel = self.zeros3(8, w, h) # score region that is already selected (after mouse_release) self.scut = self.zeros3(8, w, h) # score region (cut) that is already selected (after mouse_release) self.select_state = 0 # 0: none 1: selected self.bsel = self.zeros3(8, w, h) # score region that is already selected (after mouse_release) (backup) # self.bscut = self.zeros3(8, w, h) # score region (cut) that is already selected (after mouse_release) (backup) self.drag = self.zeros3(8, w, h) # score region drag backup self.dcut = self.zeros3(8, w, h) # score region (cut) drag backup self.filename = "" self.is_edited = False self.save_working_brick = False self.play_continue = -1 self.time_continue = 99999999 self.is_load_profile = 0 self.ngrid_h = w self.ngrid_v = h self.scale_mode = 0 self.key_shift = 0 self.last_note = -1 self.play_state = -2 # -2: stop -1~3: playing self.grid_tdiv = 0.25 self.currentTime = 0 # to be set self.scale_list = ["Major", "Minor", "Chromatic", "Chinese_Pentatonic", "Japanese_Pentatonic", "Blue_note"] self.load_scale_bricks() self.create_local_brick_list() # def convert_username(self, name): # return name.replace(" ","").replace("-","").replace("\"","") def check_if_server_has_file(self, filename): if self.main.ftp_status == 0: return l = len(self.srv_brick_list) for i in range(l): if self.srv_brick_list[i] == filename: return srv_filename = filename try: stream = open("myscore/" + filename, 'r') msg = self.main.ftp.storlines("STOR " + srv_filename, stream) print "ftp::STOR " + srv_filename + "....." + msg stream.close() except: print "ftp error...." self.main.ftp_status = 0 def load_collected_list(self): size = len(self.local_brick_list) for i in range(size): n = self.local_brick_list[i].split("_") l = len(n) #n[l-1] -> filename n[l-3] -> type if (int)(n[l-3]) == 0: self.cmelody_bricks[self.cmelody_bricks_cnt].read_score(self.local_brick_list[i], self) self.cmelody_bricks_cnt = self.cmelody_bricks_cnt + 1 elif (int)(n[l-3]) == 2: self.crhythm_bricks[self.crhythm_bricks_cnt].read_score(self.local_brick_list[i], self) self.crhythm_bricks_cnt = self.crhythm_bricks_cnt + 1 for i in range(self.crhythm_bricks_cnt): print self.crhythm_bricks[i].filename if self.main.bricksarea.active_group == 4 or self.main.bricksarea.active_group == 5: self.main.bricksarea.draw_scores() def toInt(self, line): return (int)(line.replace("\n","").replace("\r","")) def toFloat(self, line): return (float)(line.replace("\n","").replace("\r","")) def toStr(self, line): return line.replace("\n","").replace("\r","") def load_profile(self, username): self.is_load_profile = 1 try: stream = open("profile/" + username + ".txt", 'r') except: print username + "'s user profile doesn't exist." return self.work_bricks_cnt = self.toInt(stream.readline()) # work brick for i in range(self.work_bricks_cnt): line = self.toStr(stream.readline()) if line[0] != "-" and i < self.wsize: self.work_bricks[i].read_score(line, self) if self.work_bricks_cnt > self.wsize: self.work_bricks_cnt = self.wsize if self.work_bricks_cnt < 6: self.work_bricks_cnt = 6 self.mymelody_bricks_cnt = self.toInt(stream.readline()) # mymelody brick for i in range(self.mymelody_bricks_cnt): line = self.toStr(stream.readline()) if i < self.csize: self.mymelody_bricks[i].read_score(line, self) if self.mymelody_bricks_cnt > self.csize: self.mymelody_bricks_cnt = self.csize self.myrhythm_bricks_cnt = self.toInt(stream.readline()) # myrhythm brick for i in range(self.myrhythm_bricks_cnt): line = self.toStr(stream.readline()) if i < self.csize: self.myrhythm_bricks[i].read_score(line, self) if self.myrhythm_bricks_cnt > self.csize: self.myrhythm_bricks_cnt = self.csize self.cmelody_bricks_cnt = self.toInt(stream.readline()) # mymelody brick for i in range(self.cmelody_bricks_cnt): line = self.toStr(stream.readline()) if i < self.csize: self.cmelody_bricks[i].read_score(line, self) if self.cmelody_bricks_cnt > self.csize: self.cmelody_bricks_cnt = self.csize self.crhythm_bricks_cnt = self.toInt(stream.readline()) # myrhythm brick for i in range(self.crhythm_bricks_cnt): line = self.toStr(stream.readline()) if i < self.csize: self.crhythm_bricks[i].read_score(line, self) if self.crhythm_bricks_cnt > self.csize: self.crhythm_bricks_cnt = self.csize stream.close() def save_profile(self, username): if self.is_load_profile == 0: return stream = open("musicpainter.config", 'w') stream.writelines(username + "\n" + self.main.ip) stream.close() stream = open("profile/" + username + ".txt", 'w') if self.save_working_brick == True: stream.writelines(str(self.work_bricks_cnt) + "\n") # don't upload bricks in working area for i in range(self.work_bricks_cnt): if self.work_bricks[i].is_blank == 1: stream.writelines("-\n") else: if self.work_bricks[i].filename == "": self.work_bricks[i].write_score(self) self.check_if_server_has_file(self.work_bricks[i].filename) stream.writelines(self.work_bricks[i].filename + "\n") else: stream.writelines("0\n") stream.writelines(str(self.mymelody_bricks_cnt) + "\n") for i in range(self.mymelody_bricks_cnt): if self.mymelody_bricks[i].filename == "": self.mymelody_bricks[i].write_score(self) self.check_if_server_has_file(self.mymelody_bricks[i].filename) stream.writelines(self.mymelody_bricks[i].filename + "\n") stream.writelines(str(self.myrhythm_bricks_cnt) + "\n") for i in range(self.myrhythm_bricks_cnt): if self.myrhythm_bricks[i].filename == "": self.myrhythm_bricks[i].write_score(self) self.check_if_server_has_file(self.myrhythm_bricks[i].filename) stream.writelines(self.myrhythm_bricks[i].filename + "\n") stream.writelines(str(self.cmelody_bricks_cnt) + "\n") for i in range(self.cmelody_bricks_cnt): if self.cmelody_bricks[i].filename == "": self.cmelody_bricks[i].write_score(self) stream.writelines(self.cmelody_bricks[i].filename + "\n") stream.writelines(str(self.crhythm_bricks_cnt) + "\n") for i in range(self.crhythm_bricks_cnt): if self.crhythm_bricks[i].filename == "": self.crhythm_bricks[i].write_score(self) self.check_if_server_has_file(self.crhythm_bricks[i].filename) stream.writelines(self.crhythm_bricks[i].filename + "\n") stream.close() def create_local_brick_list(self): self.local_brick_list = os.listdir("myscore") self.local_brick_list = filter(lambda name: name.endswith(".mpb") or name.endswith(".mps"), self.local_brick_list) def load_scale_bricks(self): list = os.listdir("myscore") list = filter(lambda name: name.endswith(".scale"), list) list.sort() for i in range(len(list)): filename = list[i] self.scale_bricks[i].read_score(filename,self) self.scale_bricks[i].brick_type = 1 def clear_score(self): w = self.ngrid_h h = self.ngrid_v if self.play_state != -2: self.stop_music(self.main.csound) if self.select_state != 0: self.delete_selection() self.top = self.minusone(w, h) self.map = self.zeros3(8, w, h) self.cut = self.zeros3(8, w, h) def max(self, a, b): if a > b: return a return b def copy_drag_2_sel(self, dx, dy): self.bsel = deepcopy(self.sel) # self.bscut = copy(self.scut) self.sel = self.zeros3(8, self.ngrid_h, self.ngrid_v) # score region that is already selected (after mouse_release) self.scut = self.zeros3(8, self.ngrid_h, self.ngrid_v) # score region (cut) that is already selected (after mouse_release) for k in range(8): for i in range(self.ngrid_h): nx = i + dx if not (0 <= nx and nx < self.ngrid_h): continue for j in range(self.ngrid_v): ny = j + dy if not (0 <= ny and ny < self.ngrid_v): continue if self.drag[k][i][j] != 0: self.sel[k][nx][ny] = self.drag[k][i][j] self.scut[k][nx][ny] = self.dcut[k][i][j] def de_selection(self): if self.select_state == 0: return self.bsel = deepcopy(self.sel) for k in range(8): for i in range(self.ngrid_h): for j in range(self.ngrid_v): if self.sel[k][i][j] != 0: self.map[k][i][j] = max(self.map[k][i][j], self.sel[k][i][j]) self.cut[k][i][j] = self.scut[k][i][j] # print "bsel = %d" % self.bsel[k][i][j] # self.bsel[k][i][j] = self.sel[k][i][j] self.sel[k][i][j] = 0 self.scut[k][i][j] = 0 self.select_state = 0 def map_scale2(self, v, mode, shift): major_scale = [0,2,4,5,7,9,10,12,14,16,17,19,21,22,24,26,28,29] minor_scale = [0,1,4,5,7,8,10,12,13,16,17,19,20,22,24,25,28,29] chromatic_scale = range(self.ngrid_v); pentatonic_scale = [0, 3, 5, 8, 10, 12, 15, 17, 20, 22, 24, 27, 29, 32, 34, 36, 39, 41] japanese_scale = [-7, -3, -1, 0, 4, 5, 9, 11, 12, 16, 17, 21, 23, 24, 28, 29, 33, 35, 36, 40, 41] #blue_scale = [0, 1, 4, 6, 9, 11, 12, 13, 16, 18, 21, 23, 24, 25, 28, 30, 33, 35] # blue_scale = [-6, -5, -2, 0, 2, 3, 5, 6, 7, 10, 12, 14, 15, 17, 18, 19, 22, 24] blue_scale = [0, 1, 4, 6, 9, 11, 12, 13, 16, 18, 21, 23, 24, 25, 28, 30, 33, 35] if mode == 0: return 55 + major_scale[self.ngrid_v-v-1] + shift; elif mode == 1: return 55 + minor_scale[self.ngrid_v-v-1] + shift; elif mode == 2: return 57 + chromatic_scale[self.ngrid_v-v-1] + shift; elif mode == 3: return 52 + pentatonic_scale[self.ngrid_v-v-1] + shift; elif mode == 4: return 60 + japanese_scale[self.ngrid_v-v-1] + shift; elif mode == 5: return 54 + blue_scale[self.ngrid_v-v-1] + shift - 5; # return 60 + blue_scale[self.ngrid_v-v-1] + shift - 5; def map_scale(self, v): return self.map_scale2(v, self.scale_mode, self.key_shift) def note2str(self, note): rstr = str((int)((note+36)/12)) + "." if note%12 < 10: rstr += "0" rstr += str(note%12) return rstr def knote_on(self, csound, inst, note, nid): v = 1 if inst == 8: eventStr = "i5" + str(inst) + "." + str(nid) + " 0 -1 " + str(v) + " " + str(note) elif inst == 7: eventStr = "i5" + str(inst) + "." + str(nid) + " 0 -1 " + str(v) + " " + self.note2str(note-12) else: eventStr = "i5" + str(inst) + "." + str(nid) + " 0 -1 " + str(v) + " " + self.note2str(note) self.lastNoteOnTime = self.currentTime csound.sendLinevt(eventStr) def knote_off(self, csound, inst, note, nid): if inst == 8: return if inst == 7: eventStr = "i-5" + str(inst) + "." + str(nid) + " 0 0 1 " + self.note2str(note-12) elif inst == 0: eventStr = "i-5" + str(inst) + "." + str(nid) + " 0.45 0 1 " + self.note2str(note-12) else: eventStr = "i-5" + str(inst) + "." + str(nid) + " 0 0 1 " + self.note2str(note) if _showCSoundMsg: print "CSound Event: " + eventStr csound.sendLinevt(eventStr) def note_on(self, csound, inst, note, toolbar_sound = 0, v = 1): if toolbar_sound == 0: if inst == 8: eventStr = "i5" + str(inst) + " 0 -1 " + str(v) + " " + str(note) elif inst == 7: eventStr = "i5" + str(inst) + " 0 -1 " + str(v) + " " + self.note2str(note-12) else: eventStr = "i5" + str(inst) + " 0 -1 " + str(v) + " " + self.note2str(note) self.lastNoteOnTime = self.currentTime else: if inst == 8: eventStr = "i" + str(inst) + " 0 0.8 1 0" elif inst == 7: eventStr = "i" + str(inst) + " 0 0.8 1 " + self.note2str(note-12) else: eventStr = "i" + str(inst) + " 0 0.8 1 " + self.note2str(note) if _showCSoundMsg: print "CSound Event: " + eventStr csound.sendLinevt(eventStr) def note_off(self, csound, inst, note): if inst == 8: return # if self.currentTime - self.lastNoteOnTime < 0.25: # d = 0.35 # else: d = 0 if inst == 7: eventStr = "i-5" + str(inst) + " " + str(d) + " 0 1 " + self.note2str(note-12) elif inst == 0: eventStr = "i-5" + str(inst) + " " + str(0.45) + " 0 1 " + self.note2str(note-12) else: eventStr = "i-5" + str(inst) + " " + str(d) + " 0 1 " + self.note2str(note) if _showCSoundMsg: print "CSound Event: " + eventStr csound.sendLinevt(eventStr) def drag_on_vol(self, csound, toolsel, colorsel, gy, v): if colorsel == 7: note = gy else: note = self.map_scale(gy) if self.last_note !=-1 : self.note_off(csound, colorsel+1, self.last_note) self.last_note = note self.note_on(csound, colorsel+1, note, 0, self.floor3(v)) def keyboard_change_instrument(self, csound, prev_color): for i in range(self.ngrid_v): if prev_color == 7: note = i else: note = self.map_scale(i) if self.keyon[i] == 1: self.knote_off(csound, colorsel+1, note, i) self.keyon[i] = 0 def keyboard_press(self, csound, colorsel, gy): if _showDebugMsg: print "note on: " + str(colorsel+1) + "," + str(gy) if self.keyon[gy] == 1: return if colorsel == 7: note = gy else: note = self.map_scale(gy) self.knote_on(csound, colorsel+1, note, gy) self.main.canvas.write_note_keyboard(gy) self.keyon[gy] = 1 def keyboard_release(self, csound, colorsel, gy): if _showDebugMsg: print "note off: " + str(colorsel+1) + "," + str(gy) if colorsel == 7: note = gy else: note = self.map_scale(gy) self.knote_off(csound, colorsel+1, note, gy) self.keyon[gy] = 0 def drag_on(self, csound, toolsel, colorsel, gy): if toolsel != 2: return if colorsel == 7: note = gy else: note = self.map_scale(gy) if note != self.last_note: # to be fix, note != 0 if self.last_note !=-1 : self.note_off(csound, colorsel+1, self.last_note) self.last_note = note self.note_on(csound, colorsel+1, note) def drag_off(self, csound, toolsel, colorsel): if not (toolsel == 2 or toolsel == -1 or toolsel == 0): return if self.last_note !=-1 : self.note_off(csound, colorsel+1, self.last_note) self.last_note = -1 def timeUpdate(self, time): self.currentTime = (float)(time) if self.time_continue < self.currentTime: self.continue_music() # print "time = %.3f" % (self.currentTime - self.startSesstionTime) # print "%.3f" % (self.backup_event[self.play_continue][1]) #print "time = %.3f %.3F" % (self.currentTime - self.startSessionTime, self.backup_event[self.play_continue][1]) # print "note time = %.3f" % self.backup_event[self.play_continue][1] # if self.play_continue != -1 and self.backup_event[self.play_continue][1] - self.currentTime + self.startSessionTime < 0.5: # self.continue_music() self.main.canvas.highlight_grid() def tempoChange(self, sval): odiv = self.grid_tdiv ndiv = 0.1 * pow(6.25, 1-sval) if self.play_state != -2: if _showDebugMsg: print "%.2f %.2f %.2f" % (self.currentTime - self.startSessionTime, self.score_start_time, self.score_end_time) self.main.csound.sendClearLines() g = (self.currentTime - self.startSessionTime) / odiv self.score_start_time = (self.score_start_time + self.currentTime - self.startSessionTime) / odiv * ndiv self.score_end_time = (self.score_end_time - self.currentTime + self.startSessionTime) / odiv * ndiv self.startSessionTime = self.currentTime limit = 100 cnt = 0 con = -1 if _showDebugMsg: print "%.1f %.2f %.2f" % (g, self.score_start_time, self.score_end_time) for i in range(self.ecnt): st = self.backup_events[i][1] if st == -1: continue; if st / odiv + 0.1 < g: self.backup_events[i][1] = -1 continue; en = self.backup_events[i][2] st = (st / odiv - g) * ndiv en = en / odiv * ndiv if st < 0: en = en + st st = 0 st = self.floor3(st) en = self.floor3(en) if self.backup_events[i][0] == 8: n = self.backup_events[i][4] else: n = self.note2str(self.map_scale(self.backup_events[i][4])) estr = "i" + str(self.backup_events[i][0]) + " " + str(st) + " " + str(en) + " " + str(self.backup_events[i][3]) + " " + str(n) self.backup_events[i][1] = st self.backup_events[i][2] = en #if _showGraphcisEvent: # print estr if cnt < limit: self.main.csound.sendLinevt(estr) elif con == -1: con = i cnt = cnt + 1 if cnt >= limit: self.time_continue = self.backup_events[con][1] + self.currentTime - 0.5 self.play_continue = con else: self.play_continue = -1 self.time_continue = 99999999 self.grid_tdiv = ndiv def floor3(self, v): return 1.0 * (int) ((v + 0.0005) * 1000) / 1000 def score2events(self, score, cut, div = 0): if div == 0: div = self.grid_tdiv events = list() if self.main.toolbar.from_start == 1: first_note_time = 0 else: first_note_time = -1 end_note_time = -1 for i in range(self.ngrid_h): for j in range(self.ngrid_v): for k in range(8): if score[k][i][j] != 0 and (i == 0 or score[k][i-1][j] == 0 or cut[k][i-1][j] == 1): if first_note_time == -1: first_note_time = div*i len = 1 while i+len < self.ngrid_h and score[k][i+len][j] != 0 and cut[k][i+len-1][j] == 0: len = len + 1 # if k == 7: # n = j # else: # n = self.note2str(self.map_scale(j)) st = div*i - first_note_time du = div*len if end_note_time == -1 or end_note_time < st+du: end_note_time = st+du if k == 7: # percussion du = du * 3 event = [k+1, self.floor3(st), self.floor3(du), self.floor3(score[k][i][j]), j] events.append(event) if self.main.toolbar.record == 1: if self.main.toolbar.sval >= 0.5: p = 4 elif self.main.toolbar.sval >= 0.1: p = 2 else: p = 1 if i % p != 0: continue st = div*i - first_note_time du = 3*div if end_note_time == -1 or end_note_time < st+du: end_note_time = st+du event = [8, self.floor3(st), self.floor3(du), 0.5, 1] events.append(event) return (events, first_note_time, end_note_time) def read_score(self, filename): stream = open(filename, 'r') map = self.zeros3(8, self.ngrid_h, self.ngrid_v) cut = self.zeros3(8, self.ngrid_h, self.ngrid_v) try: (sval, mode, shift, type, author, title, desc, note) = (0.25, 0, 0, 0, "", "", "", "") # default cnt = 0 flag = 0 for line in stream: if line[0] == "=": flag = 1 continue n = line.split(" ") if flag == 0: if line[0] == "T": # tempo_slider_value sval = self.toFloat(n[1]) elif line[0] == "S": # scale_mode n[1] = self.toStr(n[1]) for i in range(len(self.scale_list)): if n[1] == self.scale_list[i]: mode = i elif line[0] == "P": # pitch_shift shift = self.toInt(n[1]) elif line[0] == "A": line = self.toStr(line).replace("Author: ","") author = line elif line[0] == "B": # brick_type type = self.toInt(n[1]) elif line[0] == "C": # title title = self.toStr(line).replace("CTitle: ","") elif line[0] == "D": #description desc = self.toStr(line).replace("Description: ","") elif line[0] == "N": #note note = self.toStr(line).replace("Note: ","") else: # read score (p1,p2,p3,p4,p5) = ((int)(n[0])-1,(int)(n[1]),(int)(n[2]),(int)(n[3]),(float)(n[4])) if p2 != 0 and map[p1][p2-1][p4] != 0: cut[p1][p2-1][p4] = 1 for i in range(p3): map[p1][p2+i][p4] = p5 cnt = cnt + 1 finally: stream.close() return (map, cut, sval, mode, shift, type, author, title, desc, note) def read_journal(self, filename): (self.map, self.cut, sval, self.scale_mode, self.key_shift, type, author, self.title, self.description, self.note) = self.read_score(filename) if self.main.toolbar.change_slider_val(sval - self.main.toolbar.sval): self.tempoChange(self.main.toolbar.sval) def read_mpscore(self, filename): self.filename = filename (self.map, self.cut, sval, self.scale_mode, self.key_shift, type, author, self.title, self.description, self.note) = self.read_score("myscore/" + filename) if self.main.toolbar.change_slider_val(sval - self.main.toolbar.sval): self.tempoChange(self.main.toolbar.sval) def write_journal(self, filename): self.write_score(filename, self.map, self.cut, self.grid_tdiv, self.scale_mode, self.key_shift) def write_mpscore(self): filename = "myscore/" + self.filename self.write_score(filename, self.map, self.cut, self.grid_tdiv, self.scale_mode, self.key_shift) self.upload_mpscore() def upload_mpscore(self): if self.main.ftp_status == 0: return try: stream = open("myscore/" + self.filename, 'r') msg = self.main.ftp.storlines("STOR " + self.filename, stream) print "ftp::STOR " + self.filename + "....." + msg stream.close() except: print "ftp error...." self.main.ftp_status = 0 def write_score(self, filename, score, cut, grid_tdiv, scale_mode, pitch_shift, type = 0): stream = open(filename, 'w') stream.writelines("Author: " + self.main.username + "\n") stream.writelines("Brick_type: " + str(type) + "\n") stream.writelines("Tempo_slider_val: " + str(self.main.toolbar.sval) + "\n") stream.writelines("Scale_mode: " + self.scale_list[scale_mode] + "\n") stream.writelines("Pitch_shift: " + str(self.key_shift) + "\n") if filename.endswith("mps"): stream.writelines("CTitle: " + self.title + "\n") stream.writelines("Description: " + self.description + "\n") stream.writelines("Note: " + self.note + "\n") stream.writelines("==============================\n") for i in range(self.ngrid_h): for k in range(8): for j in range(self.ngrid_v): if score[k][i][j] != 0 and (i == 0 or score[k][i-1][j] == 0 or cut[k][i-1][j] == 1): len = 1 while i+len < self.ngrid_h and score[k][i+len][j] != 0 and cut[k][i+len-1][j] == 0: len = len + 1 stream.writelines(str(k+1) + " " + str(i) + " " + str(len) + " " + str(j) + " " + str(self.floor3(score[k][i][j])) + "\n") # instrument - time (x) - duration - pitch (y) - volume stream.close() def stop_music(self, csound): if self.play_state == -1: self.main.canvas.highlight_grid(True) self.play_state = -2 self.main.toolbar.update() csound.sendClearLines() #def sendEvents(self): #for i in range(self.ecnt): #str = "i" + str(events #csound.sendLinevt() ## while self.epcnt < self.ecnt: ## self.play_time #for i in range(len(events)): #csound.sendLinevt(events[i]) def continue_music(self): print "continue_music" csound = self.main.csound if csound.status != 1 or self.play_continue == -1: return td = self.currentTime - self.startSessionTime events = self.backup_events flag = 0 limit = 72 for j in range(self.ecnt-self.play_continue): if j == limit: flag = 1 break i = j + self.play_continue if events[i][0] == 8: estr = "i" + str(events[i][0]) + " " + str(events[i][1]-td) + " " + str(events[i][2]) + " " + str(events[i][3]) + " " + str(events[i][4]) elif events[i][0] == 7: n = self.note2str(self.map_scale(events[i][4])-12) estr = "i" + str(events[i][0]) + " " + str(events[i][1]-td) + " " + str(events[i][2]) + " " + str(events[i][3]) + " " + str(n) else: n = self.note2str(self.map_scale(events[i][4])) estr = "i" + str(events[i][0]) + " " + str(events[i][1]-td) + " " + str(events[i][2]) + " " + str(events[i][3]) + " " + str(n) csound.sendLinevt(estr) if flag == 1: self.play_continue = self.play_continue + limit self.time_continue = events[self.play_continue][1] + self.startSessionTime - 0.5 else: self.play_continue = -1 self.time_continue = 99999999 def play_music(self, csound): print "Csound.status = %d" % csound.status if csound.status != 1: return if self.play_state == -1: self.main.canvas.highlight_grid(True) self.main.canvas.last_gx_key = -1 csound.sendClearLines() self.startSessionTime = self.currentTime if self.select_state == 0: (events, st, et) = self.score2events(self.map, self.cut) else: (events, st, et) = self.score2events(self.sel, self.scut) self.backup_events = events self.ecnt = len(events) #length limit = 144 if st != -1: if self.ecnt > limit: t = limit self.play_continue = limit self.time_continue = events[limit][1] + self.currentTime - 0.5 # print "time to continue %.3f" % events[200][1] else: t = self.ecnt self.play_continue = -1 self.time_continue = 99999999 for i in range(t): if events[i][0] == 8: estr = "i" + str(events[i][0]) + " " + str(events[i][1]) + " " + str(events[i][2]) + " " + str(events[i][3]) + " " + str(events[i][4]) elif events[i][0] == 7: n = self.note2str(self.map_scale(events[i][4])-12) estr = "i" + str(events[i][0]) + " " + str(events[i][1]) + " " + str(events[i][2]) + " " + str(events[i][3]) + " " + str(n) else: n = self.note2str(self.map_scale(events[i][4])) estr = "i" + str(events[i][0]) + " " + str(events[i][1]) + " " + str(events[i][2]) + " " + str(events[i][3]) + " " + str(n) csound.sendLinevt(estr) # self.sendEvents() # self.play_time = 0 self.play_state = -1 self.score_start_time = st self.score_end_time = et else: self.play_state = -2 self.main.toolbar.update() if _showCSoundMsg: print "Playing CSound Music..."