Package jazzparser :: Package utils :: Module input
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.utils.input

 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   
35 -def assign_durations(string):
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 # Make sure the commas are separate tokens 45 string = string.replace(",", " , ") 46 string = string.replace(":", " : ") 47 string = string.replace("|", "") 48 tokens = string.split() 49 # Group tokens into comma-separated sequences 50 in_sequence = False 51 units = [] 52 for token in tokens: 53 if token == "," or token == ":": 54 # The next token is in the same sequence as the last 55 in_sequence = True 56 elif in_sequence: 57 # This token should be added to the previous token's sequence 58 units[-1].append(token) 59 in_sequence = False 60 else: 61 # Start of a new sequence 62 units.append([token]) 63 64 # Now build the list of durations 65 durations = [] 66 for unit in units: 67 # Each sequence has a total length of 4 (assume 4 beats in bar) 68 # Split this between its members. 69 denominator = len(unit) 70 members = [Fraction(4,denominator)] * denominator 71 durations.extend(members) 72 73 return durations
74
75 -def strip_input(string):
76 """ 77 Pre-processes the input string, removing any allowed meaningless 78 characters, and returns the resulting string. 79 """ 80 from jazzparser import settings 81 for ignored in settings.IGNORED_INPUT_STRINGS + [":",","]: 82 string = string.replace(ignored," ") 83 return string
84