1# Copyright 2006 Joe Wreschnig
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7
8import os
9import wave
10
11from senf import fsn2text
12
13from ._audio import AudioFile, translate_errors
14
15
16extensions = [".wav"]
17
18
19class WAVEFile(AudioFile):
20    format = "WAVE"
21    mimes = ["audio/wav", "audio/x-wav", "audio/wave"]
22
23    def __init__(self, filename):
24        with translate_errors():
25            with open(filename, "rb") as h:
26                f = wave.open(h)
27                self["~#length"] = float(f.getnframes()) / f.getframerate()
28                self["~#channels"] = f.getnchannels()
29                self["~#samplerate"] = f.getframerate()
30                self["~#bitdepth"] = f.getsampwidth() * 8
31        self.sanitize(filename)
32
33    def sanitize(self, filename):
34        super(WAVEFile, self).sanitize(filename)
35        self["title"] = fsn2text(os.path.splitext(
36            os.path.basename(self["~filename"]))[0])
37
38    def write(self):
39        pass
40
41    def can_change(self, k=None):
42        if k is None:
43            return ["artist"]
44        else:
45            return k == "artist"
46
47loader = WAVEFile
48types = [WAVEFile]
49