| 1 |
# Copyright (C) 2024 Johannes Sasongko <johannes sasongko org> |
| 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 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License along |
| 14 |
# with this program; if not, write to the Free Software Foundation, Inc., |
| 15 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
|
| 28 |
import io |
| 29 |
import os |
| 30 |
import struct |
| 31 |
from typing import Tuple |
| 32 |
|
| 33 |
from xl.metadata._base import BaseFormat, NotReadable |
| 34 |
|
| 35 |
|
| 36 |
class AuFormat(BaseFormat): |
| 37 |
writable = False |
| 38 |
|
| 39 |
def load(self): |
| 40 |
try: |
| 41 |
with open(self.loc, 'rb', buffering=0) as f: |
| 42 |
file_size = os.stat(f.fileno()).st_size |
| 43 |
bitrate, length = _read_metadata(f, file_size) |
| 44 |
except IOError: |
| 45 |
raise NotReadable |
| 46 |
self.mutagen = {'__bitrate': bitrate, '__length': length} |
| 47 |
|
| 48 |
|
| 49 |
def _read_metadata(f: io.FileIO, file_size: int) -> Tuple[float, float]: |
| 50 |
""" |
| 51 |
:return: bitrate, length |
| 52 |
""" |
| 53 |
|
| 54 |
# See https://en.wikipedia.org/wiki/Au_file_format |
| 55 |
|
| 56 |
try: |
| 57 |
header = f.read(24) |
| 58 |
except IOError: |
| 59 |
return -1, -1 |
| 60 |
|
| 61 |
# > = big-endian; I = uint32 |
| 62 |
magic, data_offset, data_size, encoding, sample_rate, channels = struct.unpack( |
| 63 |
'>IIIIII', header |
| 64 |
) |
| 65 |
if magic != 0x2E736E64: # '.snd' |
| 66 |
return -1, -1 |
| 67 |
|
| 68 |
# Data size can be unknown (uint32_max) or wrong; try to guess it |
| 69 |
data_size = min(data_size, file_size - data_offset) |
| 70 |
|
| 71 |
# fmt: off |
| 72 |
ENCODING_WIDTHS = { |
| 73 |
1: 1, |
| 74 |
2: 1, 3: 2, 4: 3, 5: 4, |
| 75 |
6: 4, 7: 8, |
| 76 |
11: 1, 12: 2, 13: 3, 14: 4, |
| 77 |
18: 2, 19: 2, 20: 2, |
| 78 |
27: 1, |
| 79 |
} |
| 80 |
# fmt: on |
| 81 |
try: |
| 82 |
sample_width = ENCODING_WIDTHS[encoding] |
| 83 |
except KeyError: |
| 84 |
return -1, -1 |
| 85 |
|
| 86 |
byterate = sample_rate * sample_width * channels |
| 87 |
return byterate * 8, data_size / byterate |