Skip to content

Commit

Permalink
[#23] Give the playback player functionality its own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
kosarev committed Mar 27, 2021
1 parent f54c44f commit bfcc163
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 68 deletions.
69 changes: 1 addition & 68 deletions zx/_emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,81 +33,14 @@
from ._machine import Dispatcher
from ._machine import RunEvents
from ._machine import Spectrum48
from ._playback import PlaybackPlayer
from ._rzx import RZXFile
from ._tape import TapePlayer
from ._time import Time
from ._z80snapshot import Z80SnapshotFormat
from ._zxb import ZXBasicCompilerProgram


# TODO: Rework to a time machine interface.
class PlaybackPlayer(object):
def __init__(self, machine, file):
self.__machine = machine

assert isinstance(file, RZXFile)
self._recording = file

def find_recording_info_chunk(self):
for chunk in self._recording['chunks']:
if chunk['id'] == 'info':
return chunk
assert 0 # TODO

def get_chunks(self):
return self._recording['chunks']

def get_playback_samples(self):
# TODO: Have a class describing playback state.
self.playback_frame_count = 0
self.playback_chunk = 0
self.playback_sample_values = []
self.playback_sample_i = 0

frame_count = 0
for chunk_i, chunk in enumerate(self.get_chunks()):
if isinstance(chunk, MachineSnapshot):
self.__machine.install_snapshot(chunk)
continue

if chunk['id'] != 'port_samples':
continue

self.__machine.ticks_since_int = chunk['first_tick']

for frame_i, frame in enumerate(chunk['frames']):
num_of_fetches, samples = frame
# print(num_of_fetches, samples)

self.__machine.fetches_limit = num_of_fetches
# print(num_of_fetches, samples, flush=True)

# print('START_OF_FRAME', flush=True)
yield 'START_OF_FRAME'

for sample_i, sample in enumerate(samples):
# print(self.fetches_limit)
# fetch = num_of_fetches - self.fetches_limit
# print('Input at fetch', fetch, 'of', num_of_fetches)
# TODO: print('read_port 0x%04x 0x%02x' % (addr, n),
# flush=True)

# TODO: Have a class describing playback state.
self.playback_frame_count = frame_count
self.playback_chunk = chunk
self.playback_sample_values = samples
self.playback_sample_i = sample_i
# print(frame_count, chunk_i, frame_i, sample_i, sample,
# flush=True)

yield sample

# print('END_OF_FRAME', flush=True)
yield 'END_OF_FRAME'

frame_count += 1


# TODO: Eliminate this class. Move everything to Spectrum48.
class Emulator(Spectrum48):
_SPIN_V0P5_INFO = {'id': 'info',
Expand Down
80 changes: 80 additions & 0 deletions zx/_playback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-

# ZX Spectrum Emulator.
# https://github.com/kosarev/zx
#
# Copyright (C) 2017-2021 Ivan Kosarev.
# [email protected]
#
# Published under the MIT license.

from ._data import MachineSnapshot
from ._rzx import RZXFile


# TODO: Rework to a time machine interface.
class PlaybackPlayer(object):
def __init__(self, machine, file):
self.__machine = machine

assert isinstance(file, RZXFile)
self._recording = file

def find_recording_info_chunk(self):
for chunk in self._recording['chunks']:
if chunk['id'] == 'info':
return chunk
assert 0 # TODO

def get_chunks(self):
return self._recording['chunks']

def get_playback_samples(self):
# TODO: Have a class describing playback state.
self.playback_frame_count = 0
self.playback_chunk = 0
self.playback_sample_values = []
self.playback_sample_i = 0

frame_count = 0
for chunk_i, chunk in enumerate(self.get_chunks()):
if isinstance(chunk, MachineSnapshot):
self.__machine.install_snapshot(chunk)
continue

if chunk['id'] != 'port_samples':
continue

self.__machine.ticks_since_int = chunk['first_tick']

for frame_i, frame in enumerate(chunk['frames']):
num_of_fetches, samples = frame
# print(num_of_fetches, samples)

self.__machine.fetches_limit = num_of_fetches
# print(num_of_fetches, samples, flush=True)

# print('START_OF_FRAME', flush=True)
yield 'START_OF_FRAME'

for sample_i, sample in enumerate(samples):
# print(self.fetches_limit)
# fetch = num_of_fetches - self.fetches_limit
# print('Input at fetch', fetch, 'of', num_of_fetches)
# TODO: print('read_port 0x%04x 0x%02x' % (addr, n),
# flush=True)

# TODO: Have a class describing playback state.
self.playback_frame_count = frame_count
self.playback_chunk = chunk
self.playback_sample_values = samples
self.playback_sample_i = sample_i
# print(frame_count, chunk_i, frame_i, sample_i, sample,
# flush=True)

yield sample

# print('END_OF_FRAME', flush=True)
yield 'END_OF_FRAME'

frame_count += 1

0 comments on commit bfcc163

Please sign in to comment.