From 813e2eabf3aaa16672f016b08a735840f19e2e2a Mon Sep 17 00:00:00 2001 From: zhufengning Date: Sun, 18 Dec 2022 01:45:00 +0800 Subject: [PATCH] Apply Wave.shift() to output wav file Adjust `zs` when `ts` doesn't start from zero --- code/thinkdsp.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/code/thinkdsp.py b/code/thinkdsp.py index 158f6430..5475d8d8 100644 --- a/code/thinkdsp.py +++ b/code/thinkdsp.py @@ -74,6 +74,13 @@ def write(self, wave): wave: Wave """ zs = wave.quantize(self.bound, self.dtype) + + if wave.ts[0] > 0: + front = np.zeros([int(wave.ts[0] * self.framerate)], dtype=self.dtype) + zs = np.append(front, zs) + elif wave.ts[0] < 0: + zs = zs[-int(wave.ts[0]) * self.framerate:] + self.fp.writeframes(zs.tostring()) def close(self, duration=0):