Skip to content

Instantly share code, notes, and snippets.

@farooqkz
Created March 23, 2022 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farooqkz/b84fa5eb8b4807141372487ec4333303 to your computer and use it in GitHub Desktop.
Save farooqkz/b84fa5eb8b4807141372487ec4333303 to your computer and use it in GitHub Desktop.
The script remove generates inverted waves from a wave file assuming the wave file is mono channel 8000Hz and 16 bit and little endian
# By Farooq Karimi Zadeh under CC0.
import wave
import struct
from dataclasses import dataclass
from typing import List
@dataclass
class SoundClip16b:
data: List["int"]
def __len__(self):
return len(self.data)
def inverted(self):
inv = list()
for d in self.data:
inv.append(~d)
return SoundClip16b(data=inv)
def to_bytes(self):
databyte = b""
for d in self.data:
databyte += struct.pack("<h", d)
return databyte
@staticmethod
def from_bytes(bytedata):
return SoundClip16b(
data=[x[0] for x in struct.iter_unpack("<h", bytedata)]
)
w_noise = wave.open("noise.wav")
w_antinoise = wave.open("antinoise.wav", "w")
clip = SoundClip16b.from_bytes(w_noise.readframes(-1))
inverted_clip = clip.inverted()
w_antinoise.setnchannels(1)
w_antinoise.setsampwidth(2)
w_antinoise.setframerate(8000)
w_antinoise.writeframes(inverted_clip.to_bytes())
w_antinoise.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment