Mercurial > hg > syncopation-dataset
comparison Syncopation models/music_objects.py @ 9:c2843ef4de2c
changing filenames to Python conventions
author | csong |
---|---|
date | Fri, 03 Apr 2015 11:41:01 +0100 |
parents | |
children | a3ed7d2b57d8 bc3b9022ebc4 |
comparison
equal
deleted
inserted
replaced
8:2c5df6a4a22f | 9:c2843ef4de2c |
---|---|
1 | |
2 from BasicFuncs import ceiling | |
3 | |
4 import ParameterSetter | |
5 import RhythmParser | |
6 | |
7 class Note(): | |
8 def __init__(self, argstring): | |
9 intlist = map(int,argstring.split(',')) | |
10 self.startTime = intlist[0] | |
11 self.duration = intlist[1] | |
12 self.velocity = intlist[2] | |
13 | |
14 # toString() | |
15 | |
16 # NoteSequence is a list of Note | |
17 class NoteSequence(list): | |
18 def __init__(self, noteSequenceString=None): | |
19 if noteSequenceString!=None: | |
20 self.string_to_note_sequence(noteSequenceString) | |
21 | |
22 def string_to_note_sequence(self, noteSequenceString): | |
23 noteSequenceString = RhythmParser.discardSpaces(noteSequenceString) | |
24 # try: | |
25 # Turning "(1,2,3),(4,5,6),(7,8,9)" into ["1,2,3","4,5,6,","7,8,9"] | |
26 listStrings = noteSequenceString[1:-1].split("),(") | |
27 for localString in listStrings: | |
28 self.append(Note(localString)) | |
29 | |
30 # toString() | |
31 | |
32 | |
33 print NoteSequence("(1,2,3),(4,5,6),(7,8,9)") | |
34 # class VelocitySequence(list): | |
35 # def __init__(self, noteSequenceString=None): | |
36 # if noteSequenceString!=None: | |
37 # self.string_to_note_sequence(noteSequenceString) | |
38 | |
39 # def string_to_note_sequence(string): | |
40 | |
41 | |
42 class BarList(list): | |
43 def append(self,bar): | |
44 if(len(self)>0): | |
45 bar.set_previous_bar(self[-1]) | |
46 self[-1].set_next_bar(bar) | |
47 super(BarList, self).append(bar) | |
48 | |
49 | |
50 | |
51 class Bar: | |
52 | |
53 def __init__(self, rhythmSequence, timeSignature, ticksPerQuarter=None, qpmTempo=None, nextBar=None, prevBar=None): | |
54 if isinstance(rhythmSequence, NoteSequence): | |
55 self.noteSequence = rhythmSequence | |
56 self.velocitySequence = None | |
57 elif isinstance(rhythmSequence, VelocitySequence): | |
58 self.velocitySequence = rhythmSequence | |
59 self.noteSequence = None | |
60 | |
61 self.tpq = ticksPerQuarter | |
62 self.qpm = qpmTempo | |
63 self.timeSignature = timeSignature | |
64 self.nextBar = nextBar | |
65 self.prevBar = prevBar | |
66 | |
67 def get_note_sequence(self): | |
68 #if self.noteSequence==None: | |
69 # self.noteSequence = velocitySequenceToNotes(self.velocitySequence) | |
70 return self.noteSequence | |
71 | |
72 def get_velocity_sequence(self): | |
73 if self.velocitySequence==None: | |
74 self.velocitySequence = noteSequenceToVelocities(self.velocitySequence) | |
75 return self.velocitySequence | |
76 | |
77 def get_binary_sequence(self): | |
78 return ceiling(self.get_velocity_sequence()) | |
79 | |
80 def get_next_bar(self): | |
81 return self.nextBar | |
82 | |
83 def get_previous_bar(self): | |
84 return self.prevBar | |
85 | |
86 def set_next_bar(self, bar): | |
87 self.nextBar = bar | |
88 | |
89 def set_previous_bar(self, bar): | |
90 self.prevBar = bar | |
91 | |
92 def get_subdivision_sequence(self): | |
93 return ParameterSetter.get_subdivision_seq(self.timeSignature) | |
94 | |
95 def get_beat_level(self): | |
96 return ParameterSetter.get_beat_level(self.timeSignature) | |
97 | |
98 def get_time_signature(self): | |
99 return self.timeSignature | |
100 | |
101 def get_t_span(self): | |
102 # return the length of a bar in time units | |
103 return None # NEED TO IMPLEMENT | |
104 |