1 """Utilities for chord input processing
2
3 Various general utilities for processing input to the parser. These are
4 for processing string chord input and date back to the days when all the
5 parser's input was in that form.
6
7 DbInput: this class has moved to L{jazzparser.data.input}, where it fits into
8 a more general framework of input processing.
9
10 """
11 """
12 ============================== License ========================================
13 Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding
14
15 This file is part of The Jazz Parser.
16
17 The Jazz Parser is free software: you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation, either version 3 of the License, or
20 (at your option) any later version.
21
22 The Jazz Parser is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with The Jazz Parser. If not, see <http://www.gnu.org/licenses/>.
29
30 ============================ End license ======================================
31
32 """
33 __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"
34
36 """
37 Computes the durations for each chord token in the string.
38 Chords on their own get a duration of 1. Comma-separated sequences
39 of chords get a duration of 1, split evenly between the members
40 of the sequence.
41
42 """
43 from jazzparser.data import Fraction
44
45 string = string.replace(",", " , ")
46 string = string.replace(":", " : ")
47 string = string.replace("|", "")
48 tokens = string.split()
49
50 in_sequence = False
51 units = []
52 for token in tokens:
53 if token == "," or token == ":":
54
55 in_sequence = True
56 elif in_sequence:
57
58 units[-1].append(token)
59 in_sequence = False
60 else:
61
62 units.append([token])
63
64
65 durations = []
66 for unit in units:
67
68
69 denominator = len(unit)
70 members = [Fraction(4,denominator)] * denominator
71 durations.extend(members)
72
73 return durations
74
84