Skip to content

Commit 32bac12

Browse files
authored
Merge pull request #127 from beetbox/baseclass
Add common base class for all audio file types
2 parents e97fd83 + 0255cfd commit 32bac12

File tree

8 files changed

+30
-5
lines changed

8 files changed

+30
-5
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Version History
7474
requires Python 3.6+.
7575
Increase default block size in FFmpegAudioFile to get slightly faster file reading.
7676
Cache backends for faster lookup (thanks to @bmcfee).
77+
Audio file classes now inherit from a common base ``AudioFile`` class.
7778

7879
2.1.9
7980
Work correctly with GStreamer 1.18 and later (thanks to @ssssam).

audioread/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from . import ffdec
1818
from .exceptions import DecodeError, NoBackendError
1919
from .version import version as __version__ # noqa
20+
from .base import AudioFile # noqa
2021

2122

2223
def _gst_available():

audioread/base.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This file is part of audioread.
2+
# Copyright 2021, Adrian Sampson.
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining
5+
# a copy of this software and associated documentation files (the
6+
# "Software"), to deal in the Software without restriction, including
7+
# without limitation the rights to use, copy, modify, merge, publish,
8+
# distribute, sublicense, and/or sell copies of the Software, and to
9+
# permit persons to whom the Software is furnished to do so, subject to
10+
# the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be
13+
# included in all copies or substantial portions of the Software.
14+
15+
16+
class AudioFile:
17+
"""The base class for all audio file types.
18+
"""

audioread/ffdec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from io import DEFAULT_BUFFER_SIZE
2626

2727
from .exceptions import DecodeError
28+
from .base import AudioFile
2829

2930
COMMANDS = ('ffmpeg', 'avconv')
3031

@@ -118,7 +119,7 @@ def available():
118119
windows_error_mode_lock = threading.Lock()
119120

120121

121-
class FFmpegAudioFile:
122+
class FFmpegAudioFile(AudioFile):
122123
"""An audio file decoded by the ffmpeg command-line utility."""
123124
def __init__(self, filename, block_size=DEFAULT_BUFFER_SIZE):
124125
# On Windows, we need to disable the subprocess's crash dialog

audioread/gstdec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
from urllib.parse import quote
5858

5959
from .exceptions import DecodeError
60+
from .base import AudioFile
6061

6162
QUEUE_SIZE = 10
6263
BUFFER_SIZE = 10
@@ -141,7 +142,7 @@ def run(self):
141142

142143
# The decoder.
143144

144-
class GstAudioFile:
145+
class GstAudioFile(AudioFile):
145146
"""Reads raw audio data from any audio file that Gstreamer
146147
knows how to decode.
147148

audioread/macca.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import sys
2121

2222
from .exceptions import DecodeError
23+
from .base import AudioFile
2324

2425

2526
# CoreFoundation and CoreAudio libraries along with their function
@@ -184,7 +185,7 @@ class AudioBufferList(ctypes.Structure):
184185

185186
# Main functionality.
186187

187-
class ExtAudioFile:
188+
class ExtAudioFile(AudioFile):
188189
"""A CoreAudio "extended audio file". Reads information and raw PCM
189190
audio data from any file that CoreAudio knows how to decode.
190191

audioread/maddec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
import mad
1717

1818
from . import DecodeError
19+
from .base import AudioFile
1920

2021

2122
class UnsupportedError(DecodeError):
2223
"""The file is not readable by MAD."""
2324

2425

25-
class MadAudioFile:
26+
class MadAudioFile(AudioFile):
2627
"""MPEG audio file decoder using the MAD library."""
2728
def __init__(self, filename):
2829
self.fp = open(filename, 'rb')

audioread/rawread.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import wave
2121

2222
from .exceptions import DecodeError
23+
from .base import AudioFile
2324

2425
# Produce two-byte (16-bit) output samples.
2526
TARGET_WIDTH = 2
@@ -50,7 +51,7 @@ def byteswap(s):
5051
return b''.join(parts)
5152

5253

53-
class RawAudioFile:
54+
class RawAudioFile(AudioFile):
5455
"""An AIFF, WAV, or Au file that can be read by the Python standard
5556
library modules ``wave``, ``aifc``, and ``sunau``.
5657
"""

0 commit comments

Comments
 (0)