| 1 |
from errno import EAGAIN |
| 2 |
|
| 3 |
import cython |
| 4 |
from cython.cimports.av.filter.graph import Graph |
| 5 |
|
| 6 |
from av.error import FFmpegError |
| 7 |
|
| 8 |
|
| 9 |
@cython.final |
| 10 |
@cython.cclass |
| 11 |
class AudioResampler: |
| 12 |
"""AudioResampler(format=None, layout=None, rate=None) |
| 13 |
|
| 14 |
:param AudioFormat format: The target format, or string that parses to one |
| 15 |
(e.g. ``"s16"``). |
| 16 |
:param AudioLayout layout: The target layout, or an int/string that parses |
| 17 |
to one (e.g. ``"stereo"``). |
| 18 |
:param int rate: The target sample rate. |
| 19 |
""" |
| 20 |
|
| 21 |
def __cinit__(self, format=None, layout=None, rate=None, frame_size=None): |
| 22 |
if format is not None: |
| 23 |
self.format = ( |
| 24 |
format if isinstance(format, AudioFormat) else AudioFormat(format) |
| 25 |
) |
| 26 |
|
| 27 |
if layout is not None: |
| 28 |
self.layout = AudioLayout(layout) |
| 29 |
|
| 30 |
self.rate = int(rate) if rate else 0 |
| 31 |
self.frame_size = int(frame_size) if frame_size else 0 |
| 32 |
self.graph = None |
| 33 |
|
| 34 |
@cython.ccall |
| 35 |
def resample(self, frame: AudioFrame | None) -> list: |
| 36 |
"""resample(frame) |
| 37 |
|
| 38 |
Convert the ``sample_rate``, ``channel_layout`` and/or ``format`` of |
| 39 |
a :class:`~.AudioFrame`. |
| 40 |
|
| 41 |
:param AudioFrame frame: The frame to convert or `None` to flush. |
| 42 |
:returns: A list of :class:`AudioFrame` in new parameters. If the nothing is to be done return the same frame |
| 43 |
as a single element list. |
| 44 |
|
| 45 |
""" |
| 46 |
# We don't have any input, so don't bother even setting up. |
| 47 |
if not self.graph and frame is None: |
| 48 |
return [] |
| 49 |
|
| 50 |
# Shortcut for passthrough. |
| 51 |
if self.is_passthrough: |
| 52 |
return [frame] |
| 53 |
|
| 54 |
# Take source settings from the first frame. |
| 55 |
if not self.graph: |
| 56 |
self.template = frame |
| 57 |
|
| 58 |
# Set some default descriptors. |
| 59 |
self.format = self.format or frame.format |
| 60 |
self.layout = self.layout or frame.layout |
| 61 |
self.rate = self.rate or frame.sample_rate |
| 62 |
|
| 63 |
# Check if we can passthrough or if there is actually work to do. |
| 64 |
if ( |
| 65 |
frame.format.sample_fmt == self.format.sample_fmt |
| 66 |
and frame.layout == self.layout |
| 67 |
and frame.sample_rate == self.rate |
| 68 |
and self.frame_size == 0 |
| 69 |
): |
| 70 |
self.is_passthrough = True |
| 71 |
return [frame] |
| 72 |
|
| 73 |
# handle resampling with aformat filter |
| 74 |
# (similar to configure_output_audio_filter from ffmpeg) |
| 75 |
self.graph = Graph() |
| 76 |
extra_args = {} |
| 77 |
if frame.time_base is not None: |
| 78 |
extra_args["time_base"] = f"{frame.time_base}" |
| 79 |
|
| 80 |
abuffer = self.graph.add( |
| 81 |
"abuffer", |
| 82 |
sample_rate=f"{frame.sample_rate}", |
| 83 |
sample_fmt=AudioFormat(frame.format).name, |
| 84 |
channel_layout=frame.layout.name, |
| 85 |
**extra_args, |
| 86 |
) |
| 87 |
aformat = self.graph.add( |
| 88 |
"aformat", |
| 89 |
sample_rates=f"{self.rate}", |
| 90 |
sample_fmts=self.format.name, |
| 91 |
channel_layouts=self.layout.name, |
| 92 |
) |
| 93 |
abuffersink = self.graph.add("abuffersink") |
| 94 |
abuffer.link_to(aformat) |
| 95 |
aformat.link_to(abuffersink) |
| 96 |
self.graph.configure() |
| 97 |
|
| 98 |
if self.frame_size > 0: |
| 99 |
self.graph.set_audio_frame_size(self.frame_size) |
| 100 |
|
| 101 |
if frame is not None: |
| 102 |
if ( |
| 103 |
frame.format.sample_fmt != self.template.format.sample_fmt |
| 104 |
or frame.layout != self.template.layout |
| 105 |
or frame.sample_rate != self.template.rate |
| 106 |
): |
| 107 |
raise ValueError("Frame does not match AudioResampler setup.") |
| 108 |
|
| 109 |
self.graph.push(frame) |
| 110 |
|
| 111 |
output: list = [] |
| 112 |
while True: |
| 113 |
try: |
| 114 |
output.append(self.graph.pull()) |
| 115 |
except EOFError: |
| 116 |
break |
| 117 |
except FFmpegError as e: |
| 118 |
if e.errno != EAGAIN: |
| 119 |
raise |
| 120 |
break |
| 121 |
|
| 122 |
return output |