|
27 | 27 | INTERRUPT_DELAY = 'InterruptDelay' |
28 | 28 | SAMPLE_RATE = 'SampleRate' |
29 | 29 |
|
| 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 | + |
30 | 55 | def write_wav(audio_file, samples, sample_rate, channels=1): |
31 | 56 | bits_per_sample = 16 |
32 | 57 | bytes_per_sample = (bits_per_sample // 8) * channels |
@@ -102,7 +127,7 @@ def write_audio(self, audio_file, delays, contention=False, interrupts=False, of |
102 | 127 | if contention or interrupts: |
103 | 128 | self._add_contention(delays, contention, interrupts, offset, options) |
104 | 129 | if ma_filter: |
105 | | - samples = self._moving_average_filter(delays, options) |
| 130 | + samples = moving_average_filter(delays, options) |
106 | 131 | else: |
107 | 132 | samples = self._delays_to_samples(delays, options) |
108 | 133 | write_wav(audio_file, samples, options[SAMPLE_RATE]) |
@@ -164,31 +189,6 @@ def _add_contention(self, delays, contention, interrupts, cycle, options): |
164 | 189 | d_offset += f_duration - cycle |
165 | 190 | cycle = 0 |
166 | 191 |
|
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 | | - |
192 | 192 | def _delays_to_samples(self, delays, options): |
193 | 193 | sample_delay = options[CLOCK_SPEED] / options[SAMPLE_RATE] |
194 | 194 | samples = [] |
|
0 commit comments