Skip to content

Commit 51a13c0

Browse files
committed
Implement audio.moving_average_filter()
1 parent 5fb21c9 commit 51a13c0

2 files changed

Lines changed: 29 additions & 54 deletions

File tree

skoolkit/audio.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@
2727
INTERRUPT_DELAY = 'InterruptDelay'
2828
SAMPLE_RATE = 'SampleRate'
2929

30+
def moving_average_filter(delays, options, volume=1):
31+
sample_delay = options[CLOCK_SPEED] / options[SAMPLE_RATE]
32+
s0, s1 = 0, sample_delay
33+
t, t1 = 0, ceil(s1)
34+
bit = bits = 0
35+
samples = []
36+
for d in delays:
37+
while True:
38+
if t + d < t1:
39+
if bit:
40+
bits += d
41+
t += d
42+
break
43+
i = t1 - t
44+
if bit:
45+
bits += i
46+
d -= i
47+
samples.append(volume * bits / (t1 - s0))
48+
s0 = t = t1
49+
s1 += sample_delay
50+
t1 = ceil(s1)
51+
bits = 0
52+
bit = 1 - bit
53+
return samples
54+
3055
def write_wav(audio_file, samples, sample_rate, channels=1):
3156
bits_per_sample = 16
3257
bytes_per_sample = (bits_per_sample // 8) * channels
@@ -102,7 +127,7 @@ def write_audio(self, audio_file, delays, contention=False, interrupts=False, of
102127
if contention or interrupts:
103128
self._add_contention(delays, contention, interrupts, offset, options)
104129
if ma_filter:
105-
samples = self._moving_average_filter(delays, options)
130+
samples = moving_average_filter(delays, options)
106131
else:
107132
samples = self._delays_to_samples(delays, options)
108133
write_wav(audio_file, samples, options[SAMPLE_RATE])
@@ -164,31 +189,6 @@ def _add_contention(self, delays, contention, interrupts, cycle, options):
164189
d_offset += f_duration - cycle
165190
cycle = 0
166191

167-
def _moving_average_filter(self, delays, options):
168-
sample_delay = options[CLOCK_SPEED] / options[SAMPLE_RATE]
169-
s0, s1 = 0, sample_delay
170-
t, t1 = 0, ceil(s1)
171-
bit = bits = 0
172-
samples = []
173-
for d in delays:
174-
while True:
175-
if t + d < t1:
176-
if bit:
177-
bits += d
178-
t += d
179-
break
180-
i = t1 - t
181-
if bit:
182-
bits += i
183-
d -= i
184-
samples.append(bits / (t1 - s0))
185-
s0 = t = t1
186-
s1 += sample_delay
187-
t1 = ceil(s1)
188-
bits = 0
189-
bit = 1 - bit
190-
return samples
191-
192192
def _delays_to_samples(self, delays, options):
193193
sample_delay = options[CLOCK_SPEED] / options[SAMPLE_RATE]
194194
samples = []

skoolkit/ay.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
from math import ceil
1818

19-
from skoolkit.audio import CLOCK_SPEED, FRAME_DURATION, SAMPLE_RATE, write_wav
19+
from skoolkit.audio import (CLOCK_SPEED, FRAME_DURATION, SAMPLE_RATE,
20+
moving_average_filter, write_wav)
2021
from skoolkit.simutils import CLOCK_SPEEDS, FRAME_DURATIONS
2122

2223
AY_CLOCK_RATE = 1773400
@@ -224,7 +225,7 @@ def write_audio(self, audio_file, audio_log, options):
224225
ay_samples = ay.render(ay_log, frame_duration, ay_res, ay_volume)
225226
if options.beeper and beeper_log:
226227
delays = [t1 - t0 for t0, t1 in zip(beeper_log, beeper_log[1:])]
227-
beeper_samples = self._render_beeper(delays, volume)
228+
beeper_samples = moving_average_filter(delays, self.options, volume)
228229
samples = self._combine(ay_samples, beeper_samples)
229230
else:
230231
samples = ay_samples
@@ -240,32 +241,6 @@ def _parse_log(self, audio_log):
240241
beeper_log.append(record[0])
241242
return ay_log, beeper_log
242243

243-
def _render_beeper(self, delays, volume):
244-
# Apply a moving average filter
245-
sample_delay = self.options[CLOCK_SPEED] / self.options[SAMPLE_RATE]
246-
s0, s1 = 0, sample_delay
247-
t, t1 = 0, ceil(s1)
248-
bit = bits = 0
249-
samples = []
250-
for d in delays:
251-
while True:
252-
if t + d < t1:
253-
if bit:
254-
bits += d
255-
t += d
256-
break
257-
i = t1 - t
258-
if bit:
259-
bits += i
260-
d -= i
261-
samples.append(volume * bits / (t1 - s0))
262-
s0 = t = t1
263-
s1 += sample_delay
264-
t1 = ceil(s1)
265-
bits = 0
266-
bit = 1 - bit
267-
return samples
268-
269244
def _combine(self, ay_samples, beeper_samples):
270245
samples = []
271246
ay_len = len(ay_samples) // 2

0 commit comments

Comments
 (0)