0c2eba60a818142637f326ea8e885618c325deb0
1 # -*- coding: ISO-8859-1 -*-
3 # standard library imports
4 from types
import StringType
5 from struct
import unpack
8 from DataTypeConverters
import readBew
, readVar
, varLen
11 class RawInstreamFile
:
15 It parses and reads data from an input file. It takes care of big
16 endianess, and keeps track of the cursor position. The midi parser
17 only reads from this object. Never directly from the file.
21 def __init__(self
, infile
=''):
23 If 'file' is a string we assume it is a path and read from
25 If it is a file descriptor we read from the file, but we don't
27 Midi files are usually pretty small, so it should be safe to
28 copy them into memory.
31 if isinstance(infile
, StringType
):
32 infile
= open(infile
, 'rb')
33 self
.data
= infile
.read()
37 self
.data
= infile
.read()
40 # start at beginning ;-)
44 # setting up data manually
46 def setData(self
, data
=''):
47 "Sets the data from a string."
52 def setCursor(self
, position
=0):
53 "Sets the absolute position if the cursor"
54 self
.cursor
= position
58 "Returns the value of the cursor"
62 def moveCursor(self
, relative_position
=0):
63 "Moves the cursor to a new relative position"
64 self
.cursor
+= relative_position
66 # native data reading functions
68 def nextSlice(self
, length
, move_cursor
=1):
69 "Reads the next text slice from the raw data, with length"
71 slc
= self
.data
[c
:c
+length
]
73 self
.moveCursor(length
)
77 def readBew(self
, n_bytes
=1, move_cursor
=1):
79 Reads n bytes of date from the current cursor position.
80 Moves cursor if move_cursor is true
82 return readBew(self
.nextSlice(n_bytes
, move_cursor
))
87 Reads a variable length value from the current cursor position.
88 Moves cursor if move_cursor is true
90 MAX_VARLEN
= 4 # Max value varlen can be
91 var
= readVar(self
.nextSlice(MAX_VARLEN
, 0))
92 # only move cursor the actual bytes in varlen
93 self
.moveCursor(varLen(var
))
98 if __name__
== '__main__':
100 test_file
= 'test/midifiles/minimal.mid'
101 fis
= RawInstreamFile(test_file
)
102 print fis
.nextSlice(len(fis
.data
))
104 test_file
= 'test/midifiles/cubase-minimal.mid'
105 cubase_minimal
= open(test_file
, 'rb')
106 fis2
= RawInstreamFile(cubase_minimal
)
107 print fis2
.nextSlice(len(fis2
.data
))
108 cubase_minimal
.close()