Skip to content

Commit bfcc163

Browse files
committed
[#23] Give the playback player functionality its own module.
1 parent f54c44f commit bfcc163

File tree

2 files changed

+81
-68
lines changed

2 files changed

+81
-68
lines changed

zx/_emulator.py

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -33,81 +33,14 @@
3333
from ._machine import Dispatcher
3434
from ._machine import RunEvents
3535
from ._machine import Spectrum48
36+
from ._playback import PlaybackPlayer
3637
from ._rzx import RZXFile
3738
from ._tape import TapePlayer
3839
from ._time import Time
3940
from ._z80snapshot import Z80SnapshotFormat
4041
from ._zxb import ZXBasicCompilerProgram
4142

4243

43-
# TODO: Rework to a time machine interface.
44-
class PlaybackPlayer(object):
45-
def __init__(self, machine, file):
46-
self.__machine = machine
47-
48-
assert isinstance(file, RZXFile)
49-
self._recording = file
50-
51-
def find_recording_info_chunk(self):
52-
for chunk in self._recording['chunks']:
53-
if chunk['id'] == 'info':
54-
return chunk
55-
assert 0 # TODO
56-
57-
def get_chunks(self):
58-
return self._recording['chunks']
59-
60-
def get_playback_samples(self):
61-
# TODO: Have a class describing playback state.
62-
self.playback_frame_count = 0
63-
self.playback_chunk = 0
64-
self.playback_sample_values = []
65-
self.playback_sample_i = 0
66-
67-
frame_count = 0
68-
for chunk_i, chunk in enumerate(self.get_chunks()):
69-
if isinstance(chunk, MachineSnapshot):
70-
self.__machine.install_snapshot(chunk)
71-
continue
72-
73-
if chunk['id'] != 'port_samples':
74-
continue
75-
76-
self.__machine.ticks_since_int = chunk['first_tick']
77-
78-
for frame_i, frame in enumerate(chunk['frames']):
79-
num_of_fetches, samples = frame
80-
# print(num_of_fetches, samples)
81-
82-
self.__machine.fetches_limit = num_of_fetches
83-
# print(num_of_fetches, samples, flush=True)
84-
85-
# print('START_OF_FRAME', flush=True)
86-
yield 'START_OF_FRAME'
87-
88-
for sample_i, sample in enumerate(samples):
89-
# print(self.fetches_limit)
90-
# fetch = num_of_fetches - self.fetches_limit
91-
# print('Input at fetch', fetch, 'of', num_of_fetches)
92-
# TODO: print('read_port 0x%04x 0x%02x' % (addr, n),
93-
# flush=True)
94-
95-
# TODO: Have a class describing playback state.
96-
self.playback_frame_count = frame_count
97-
self.playback_chunk = chunk
98-
self.playback_sample_values = samples
99-
self.playback_sample_i = sample_i
100-
# print(frame_count, chunk_i, frame_i, sample_i, sample,
101-
# flush=True)
102-
103-
yield sample
104-
105-
# print('END_OF_FRAME', flush=True)
106-
yield 'END_OF_FRAME'
107-
108-
frame_count += 1
109-
110-
11144
# TODO: Eliminate this class. Move everything to Spectrum48.
11245
class Emulator(Spectrum48):
11346
_SPIN_V0P5_INFO = {'id': 'info',

zx/_playback.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# ZX Spectrum Emulator.
4+
# https://github.com/kosarev/zx
5+
#
6+
# Copyright (C) 2017-2021 Ivan Kosarev.
7+
8+
#
9+
# Published under the MIT license.
10+
11+
from ._data import MachineSnapshot
12+
from ._rzx import RZXFile
13+
14+
15+
# TODO: Rework to a time machine interface.
16+
class PlaybackPlayer(object):
17+
def __init__(self, machine, file):
18+
self.__machine = machine
19+
20+
assert isinstance(file, RZXFile)
21+
self._recording = file
22+
23+
def find_recording_info_chunk(self):
24+
for chunk in self._recording['chunks']:
25+
if chunk['id'] == 'info':
26+
return chunk
27+
assert 0 # TODO
28+
29+
def get_chunks(self):
30+
return self._recording['chunks']
31+
32+
def get_playback_samples(self):
33+
# TODO: Have a class describing playback state.
34+
self.playback_frame_count = 0
35+
self.playback_chunk = 0
36+
self.playback_sample_values = []
37+
self.playback_sample_i = 0
38+
39+
frame_count = 0
40+
for chunk_i, chunk in enumerate(self.get_chunks()):
41+
if isinstance(chunk, MachineSnapshot):
42+
self.__machine.install_snapshot(chunk)
43+
continue
44+
45+
if chunk['id'] != 'port_samples':
46+
continue
47+
48+
self.__machine.ticks_since_int = chunk['first_tick']
49+
50+
for frame_i, frame in enumerate(chunk['frames']):
51+
num_of_fetches, samples = frame
52+
# print(num_of_fetches, samples)
53+
54+
self.__machine.fetches_limit = num_of_fetches
55+
# print(num_of_fetches, samples, flush=True)
56+
57+
# print('START_OF_FRAME', flush=True)
58+
yield 'START_OF_FRAME'
59+
60+
for sample_i, sample in enumerate(samples):
61+
# print(self.fetches_limit)
62+
# fetch = num_of_fetches - self.fetches_limit
63+
# print('Input at fetch', fetch, 'of', num_of_fetches)
64+
# TODO: print('read_port 0x%04x 0x%02x' % (addr, n),
65+
# flush=True)
66+
67+
# TODO: Have a class describing playback state.
68+
self.playback_frame_count = frame_count
69+
self.playback_chunk = chunk
70+
self.playback_sample_values = samples
71+
self.playback_sample_i = sample_i
72+
# print(frame_count, chunk_i, frame_i, sample_i, sample,
73+
# flush=True)
74+
75+
yield sample
76+
77+
# print('END_OF_FRAME', flush=True)
78+
yield 'END_OF_FRAME'
79+
80+
frame_count += 1

0 commit comments

Comments
 (0)