1# -*- coding: utf-8 -*-
2#
3# Picard, the next-generation MusicBrainz tagger
4#
5# Copyright (C) 2018 Philipp Wolfer
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License
9# as published by the Free Software Foundation; either version 2
10# of the License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21
22from mutagen.smf import SMF
23
24from picard import log
25from picard.file import File
26from picard.metadata import Metadata
27from picard.util import encode_filename
28
29
30class MIDIFile(File):
31    EXTENSIONS = [".mid", ".kar"]
32    NAME = "Standard MIDI File"
33    _File = SMF
34
35    def _load(self, filename):
36        log.debug("Loading file %r", filename)
37        metadata = Metadata()
38        file = self._File(encode_filename(filename))
39        self._info(metadata, file)
40        return metadata
41
42    def _save(self, filename, metadata):
43        log.debug("Saving file %r", filename)
44
45    def _info(self, metadata, file):
46        super()._info(metadata, file)
47        # mutagen.File.filename can be either a bytes or str object
48        filename = file.filename
49        if isinstance(filename, bytes):
50            filename = filename.decode()
51        if filename.lower().endswith(".kar"):
52            metadata['~format'] = "Standard MIDI File (Karaoke File)"
53
54    @classmethod
55    def supports_tag(cls, name):
56        return False
57
58    def can_analyze(self):
59        return False
60