1 # -*- coding: ISO-8859-1 -*-
3 # standard library imports
5 from types
import StringType
6 from struct
import unpack
7 from cStringIO
import StringIO
10 from DataTypeConverters
import writeBew
, writeVar
, fromBytes
12 class RawOutstreamFile
:
16 Writes a midi file to disk.
20 def __init__(self
, outfile
=''):
21 self
.buffer = StringIO()
22 self
.outfile
= outfile
25 # native data reading functions
28 def writeSlice(self
, str_slice
):
29 "Writes the next text slice to the raw data"
30 self
.buffer.write(str_slice
)
33 def writeBew(self
, value
, length
=1):
34 "Writes a value to the file as big endian word"
35 self
.writeSlice(writeBew(value
, length
))
38 def writeVarLen(self
, value
):
39 "Writes a variable length word to the file"
40 var
= self
.writeSlice(writeVar(value
))
46 if isinstance(self
.outfile
, StringType
):
47 outfile
= open(self
.outfile
, 'wb')
48 outfile
.write(self
.getvalue())
51 self
.outfile
.write(self
.getvalue())
53 sys
.stdout
.write(self
.getvalue())
56 return self
.buffer.getvalue()
59 if __name__
== '__main__':
61 out_file
= 'test/midifiles/midiout.mid'
63 rawOut
= RawOutstreamFile(out_file
)
64 rawOut
.writeSlice('MThd')
68 rawOut
.writeBew(15360, 2)