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

Source Code for Module jazzparser.data.input.tools

  1  """Shell tools relating to input data types. 
  2   
  3  """ 
  4  """ 
  5  ============================== License ======================================== 
  6   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
  7    
  8   This file is part of The Jazz Parser. 
  9    
 10   The Jazz Parser is free software: you can redistribute it and/or modify 
 11   it under the terms of the GNU General Public License as published by 
 12   the Free Software Foundation, either version 3 of the License, or 
 13   (at your option) any later version. 
 14    
 15   The Jazz Parser is distributed in the hope that it will be useful, 
 16   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 18   GNU General Public License for more details. 
 19    
 20   You should have received a copy of the GNU General Public License 
 21   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 22   
 23  ============================ End license ====================================== 
 24   
 25  """ 
 26  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 27   
 28  from jazzparser.shell.tools import Tool 
 29  from jazzparser.shell import ShellError 
30 31 -class InputTool(Tool):
32 name = "Input lister" 33 commands = ["input"] 34 usage = ("input", "show the input data object") 35 help = """\ 36 Very simple tool to display the input data. It doesn't do anything fancy to 37 output the contents of the data, but just prints out the data object's 38 str. 39 """ 40
41 - def run(self, args, state):
42 print str(state.input_data)
43
44 45 -class PlayMidiChunksTool(Tool):
46 name = "Midi chunk player" 47 commands = ["play"] 48 usage = ("play", "play the MIDI input chunk by chunk") 49 help = """\ 50 Input tool specially for segmented MIDI input. Will play the input chunk by 51 chunk. Use a keyboard interrupt (Ctrl+C) to stop playback. 52 """ 53
54 - def run(self, args, state):
55 from jazzparser.data.input import SegmentedMidiInput 56 if state.input_data is None or type(state.input_data) != SegmentedMidiInput: 57 raise ShellError, "input is not playable" 58 59 if len(args) > 0: 60 chunk = int(args[0]) 61 else: 62 chunk = None 63 64 PlayMidiChunksTool.play_segmidi(state.input_data, chunk=chunk)
65 66 @staticmethod
67 - def play_segmidi(segmidi, chunk=None):
68 from jazzparser.utils.midi import play_stream 69 # Select an individual chunk if a number is given 70 if chunk is None: 71 strms = enumerate(segmidi) 72 else: 73 strms = [(chunk, segmidi[chunk])] 74 75 try: 76 for i,strm in strms: 77 print "Playing chunk %d (%d events)" % (i, len(strm.trackpool)) 78 play_stream(strm, block=True) 79 except KeyboardInterrupt: 80 print "Stopping playback"
81
82 -class PlayBulkMidiChunksTool(Tool):
83 name = "Midi chunk player (bulk)" 84 commands = ["play"] 85 usage = ("play [<input number> [<chunk number>]]", "play the numbered MIDI input (the first by default)") 86 help = """\ 87 Input tool specially for segmented MIDI input, bulk input version. 88 89 Will play the input chunk by chunk. Use a keyboard interrupt (Ctrl+C) to stop 90 playback. 91 """ 92
93 - def run(self, args, state):
94 from jazzparser.data.input import SegmentedMidiBulkInput 95 96 if len(args) == 0: 97 inputno = 0 98 else: 99 try: 100 inputno = int(args[0]) 101 except ValueError: 102 raise ShellError, "input number must be an integer" 103 104 # Check we have appropriate input 105 if state.input_data is None or type(state.input_data) != SegmentedMidiBulkInput: 106 raise ShellError, "input is not playable" 107 108 if len(args) == 2: 109 chunk = int(args[1]) 110 else: 111 chunk = None 112 113 segmidi = state.input_data[inputno] 114 PlayMidiChunksTool.play_segmidi(segmidi, chunk=chunk)
115
116 -class PrintMidiChunksTool(Tool):
117 name = "Midi chunk printer" 118 commands = ["chunk"] 119 usage = ("chunk [<chunk>]", "print out the events in chunk <chunk>, or all chunks") 120 help = """\ 121 Displays all the MIDI events in a chunk of the input, or all the chunks. 122 """ 123
124 - def run(self, args, state):
125 input_data = state.input_data 126 if len(args): 127 chunks = [int(args[0])] 128 else: 129 chunks = range(len(input_data)) 130 131 # Print each chunk 132 for chunkno in chunks: 133 print "### Chunk %d: ###" % chunkno 134 print "\n".join([str(ev) for ev in sorted(input_data[chunkno].trackpool)])
135