Support for custom output bitrate, e.g. to target //gs in 2.8MHz mode.

For some reason playback speed is only about 1.6x (probably due to
slowing down accesses to the I/O page to 1MHz, so as not to mess up
hardware timings), but happily this comes within 3% of being 44100/2.
This commit is contained in:
kris 2019-07-14 22:05:20 +01:00
parent 35f999bc8a
commit 451523bdef
3 changed files with 34 additions and 9 deletions

View File

@ -8,21 +8,38 @@ import numpy as np
class Audio:
"""
Decodes audio stream from input file and resamples.
Notes on audio bitrate:
At 73 cycles/tick, true audio playback sample rate is
roughly 1024*1024/73 = 14364 Hz (ignoring ACK slow path).
Typical audio encoding is 44100Hz which is close to 14700*3
Downscaling by 3x gives better results than trying to resample
to a non-divisor. So we cheat a bit and play back the video a
tiny bit (<2%) faster.
For //gs playback at 2.8MHz, the effective speed increase is only about
1.6x. This is probably because accessing the I/O page is done at 1MHz
to not mess up hardware timings.
This is close (2.1%) to 22500Hz which is again a simple divisor of the
base frequency (1/2).
"""
def __init__(
self, filename: str, normalization: float = None):
self,
filename: str,
bitrate: int = 14700,
normalization: float = None):
self.filename = filename # type: str
# TODO: take into account that the available range is slightly offset
# as fraction of total cycle count?
self._tick_range = [4, 66]
# At 73 cycles/tick, true audio playback sample rate is
# roughly 1024*1024/73 = 14364 Hz (ignoring ACK slow path).
# Typical audio encoding is 44100Hz which is close to 14700*3
# Downscaling by 3x gives better results than trying to resample
# to a non-divisor. So we cheat a bit and play back the video a tiny
# bit (<2%) faster.
self.sample_rate = 14700. # type: float
self.sample_rate = float(bitrate) # type: float
self.normalization = (
normalization or self._normalization()) # type: float

View File

@ -20,6 +20,11 @@ parser.add_argument(
'--audio_normalization', type=float, default=None,
help='Override auto-detected multiplier for audio normalization.'
)
parser.add_argument(
'--audio_bitrate', type=int, default=14700,
help='Select output audio bitrate (Hz), controls video speed (Default: '
'14700; try 22500 for //gs 2.8MHz mode)'
)
parser.add_argument(
'--every_n_video_frames', type=int, default=2,
help='Allows skipping frames of input video to lower effective output '
@ -42,6 +47,7 @@ def main(args):
m = movie.Movie(
filename,
every_n_video_frames=args.every_n_video_frames,
audio_bitrate=args.audio_bitrate,
audio_normalization=args.audio_normalization,
max_bytes_out=1024. * 1024 * args.max_output_mb,
video_mode=video_mode.VideoMode[args.video_mode],

View File

@ -15,6 +15,7 @@ class Movie:
def __init__(
self, filename: str,
every_n_video_frames: int = 1,
audio_bitrate: int = 14700,
audio_normalization: float = None,
max_bytes_out: int = None,
video_mode: VideoMode = VideoMode.HGR,
@ -27,7 +28,8 @@ class Movie:
self.palette = palette # type: Palette
self.audio = audio.Audio(
filename, normalization=audio_normalization) # type: audio.Audio
filename, bitrate=audio_bitrate,
normalization=audio_normalization) # type: audio.Audio
self.frame_grabber = frame_grabber.FileFrameGrabber(
filename, mode=video_mode, palette=self.palette)