diff --git a/fretwork/midi/EventDispatcher.py b/fretwork/midi/EventDispatcher.py index cd15fb9..50bb881 100644 --- a/fretwork/midi/EventDispatcher.py +++ b/fretwork/midi/EventDispatcher.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- -from DataTypeConverters import readBew -from DataTypeConverters import toBytes - -from constants import * +from .DataTypeConverters import readBew +from .DataTypeConverters import toBytes +from .constants import * class EventDispatcher: diff --git a/fretwork/midi/MidiFileParser.py b/fretwork/midi/MidiFileParser.py index e546aef..f5ce334 100644 --- a/fretwork/midi/MidiFileParser.py +++ b/fretwork/midi/MidiFileParser.py @@ -1,10 +1,8 @@ # -*- coding: utf-8 -*- -# uhh I don't really like this, but there are so many constants to -# import otherwise -from constants import * +from .EventDispatcher import EventDispatcher +from .constants import * -from EventDispatcher import EventDispatcher class MidiFileParser(object): @@ -41,7 +39,7 @@ def parseMThdChunk(self): header_chunk_zise = raw_in.readBew(4) # check if it is a proper midi file - if header_chunk_type != 'MThd': + if header_chunk_type != b'MThd': raise TypeError("It is not a valid midi file!") # Header values are at fixed locations, so no reason to be clever diff --git a/fretwork/midi/MidiInFile.py b/fretwork/midi/MidiInFile.py index ce48059..c5442b4 100644 --- a/fretwork/midi/MidiInFile.py +++ b/fretwork/midi/MidiInFile.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- -from RawInstreamFile import RawInstreamFile -from MidiFileParser import MidiFileParser +from .MidiFileParser import MidiFileParser +from .RawInstreamFile import RawInstreamFile class MidiInFile(object): @@ -38,14 +38,12 @@ def __init__(self, outStream, infile): self.raw_in = RawInstreamFile(infile) self.parser = MidiFileParser(self.raw_in, outStream) - def read(self): "Start parsing the file" p = self.parser p.parseMThdChunk() p.parseMTrkChunks() - def setData(self, data=''): "Sets the data from a plain string" self.raw_in.setData(data) diff --git a/fretwork/midi/MidiInStream.py b/fretwork/midi/MidiInStream.py index f9a6376..f3f5f44 100644 --- a/fretwork/midi/MidiInStream.py +++ b/fretwork/midi/MidiInStream.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- -from MidiOutStream import MidiOutStream +from .MidiOutStream import MidiOutStream + class MidiInStream(object): diff --git a/fretwork/midi/MidiOutFile.py b/fretwork/midi/MidiOutFile.py index 51db2bb..29fc139 100644 --- a/fretwork/midi/MidiOutFile.py +++ b/fretwork/midi/MidiOutFile.py @@ -1,10 +1,11 @@ # -*- coding: utf-8 -*- -from MidiOutStream import MidiOutStream -from RawOutstreamFile import RawOutstreamFile +from .DataTypeConverters import fromBytes +from .DataTypeConverters import writeVar +from .MidiOutStream import MidiOutStream +from .RawOutstreamFile import RawOutstreamFile +from .constants import * -from constants import * -from DataTypeConverters import fromBytes, writeVar class MidiOutFile(MidiOutStream): diff --git a/fretwork/midi/MidiToText.py b/fretwork/midi/MidiToText.py index 0fa19d3..23d9441 100644 --- a/fretwork/midi/MidiToText.py +++ b/fretwork/midi/MidiToText.py @@ -1,7 +1,9 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -from MidiOutStream import MidiOutStream +from .MidiOutStream import MidiOutStream + + class MidiToText(MidiOutStream): diff --git a/fretwork/midi/RawInstreamFile.py b/fretwork/midi/RawInstreamFile.py index 260d4a3..d1aa030 100644 --- a/fretwork/midi/RawInstreamFile.py +++ b/fretwork/midi/RawInstreamFile.py @@ -3,9 +3,9 @@ import os.path -from DataTypeConverters import readBew as readBew_ -from DataTypeConverters import readVar -from DataTypeConverters import varLen +from .DataTypeConverters import readBew as readBew_ +from .DataTypeConverters import readVar +from .DataTypeConverters import varLen class RawInstreamFile(object): diff --git a/fretwork/midi/RawOutstreamFile.py b/fretwork/midi/RawOutstreamFile.py index cfd8af8..c28fc0d 100644 --- a/fretwork/midi/RawOutstreamFile.py +++ b/fretwork/midi/RawOutstreamFile.py @@ -1,12 +1,16 @@ # -*- coding: utf-8 -*- -# standard library imports import sys -from types import StringType -from cStringIO import StringIO +try: + from cStringIO import StringIO + from types import StringType +except ImportError: + import io as StringIO + StringType = str + +from .DataTypeConverters import writeVar +from .DataTypeConverters import writeBew -# custom import -from DataTypeConverters import writeBew, writeVar class RawOutstreamFile(object): diff --git a/fretwork/midi/__init__.py b/fretwork/midi/__init__.py index fd71283..362ca9b 100644 --- a/fretwork/midi/__init__.py +++ b/fretwork/midi/__init__.py @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- -from MidiOutStream import MidiOutStream -from MidiOutFile import MidiOutFile -from MidiInStream import MidiInStream -from MidiInFile import MidiInFile -from MidiToText import MidiToText +from .MidiInFile import MidiInFile +from .MidiInStream import MidiInStream +from .MidiOutFile import MidiOutFile +from .MidiOutStream import MidiOutStream +from .MidiToText import MidiToText __all__ = ['MidiOutStream', 'MidiOutFile', diff --git a/tests/midi/__init__.py b/tests/midi/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/midi/drumtest_notes.mid b/tests/midi/drumtest_notes.mid new file mode 100644 index 0000000..a7da58d Binary files /dev/null and b/tests/midi/drumtest_notes.mid differ diff --git a/tests/midi/test_datatypeconverters.py b/tests/midi/test_datatypeconverters.py new file mode 100644 index 0000000..16ded2b --- /dev/null +++ b/tests/midi/test_datatypeconverters.py @@ -0,0 +1,63 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# FoFiX +# Copyright (C) 2017 FoFiX team +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import sys +import unittest + +from fretwork.midi.DataTypeConverters import readBew +from fretwork.midi.DataTypeConverters import readVar +from fretwork.midi.DataTypeConverters import toBytes +from fretwork.midi.DataTypeConverters import varLen + + +class DataTypeConvertersTest(unittest.TestCase): + + def test_toBytes_type(self): + if sys.version_info.major == 2: + value = bytes('áâãa') + else: + value = bytes('áâãa', 'utf-8') + result = toBytes(value) + self.assertIs(type(result), tuple) + + def test_readBew_keyerror(self): + if sys.version_info.major == 2: + value = bytes('aà') + else: + value = bytes('aà', 'utf-8') + self.assertIs(readBew(value), 0) + + def test_readBew_type(self): + value = b'a' + result = readBew(value) + self.assertIs(type(result), int) + + def test_readVar(self): + if sys.version_info.major == 2: + value = bytes('áâãa') + else: + value = bytes('áâãa', 'utf-8') + self.assertEqual(readVar(value), 295821045191137) + + def test_varLen(self): + self.assertEqual(varLen(97), 1) + self.assertEqual(varLen(256), 2) + self.assertEqual(varLen(20000), 3) + self.assertEqual(varLen(2107151), 4) diff --git a/tests/midi/test_midiinfile.py b/tests/midi/test_midiinfile.py new file mode 100644 index 0000000..2472fd4 --- /dev/null +++ b/tests/midi/test_midiinfile.py @@ -0,0 +1,37 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# FoFiX +# Copyright (C) 2017 FoFiX team +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import unittest + +from fretwork.midi.MidiInFile import MidiInFile +from fretwork.midi.MidiToText import MidiToText + + +class MidiInFileTest(unittest.TestCase): + + def setUp(self): + self.test_file = "tests/midi/drumtest_notes.mid" + + def test_init(self): + MidiInFile(MidiToText(), self.test_file) + + def test_read(self): + midi_in = MidiInFile(MidiToText(), self.test_file) + midi_in.read()