예제 #1
0
def filter_lowpass(wav: Wave_read, cutoff: int):
    signal = wav.data
    signal = np.fromstring(signal, "Int16")

    index = -1
    frames = []
    for frame in signal:
        index += 1
        if abs(frame) < cutoff:
            frames.append(10)
            pass
        else:
            frames.append(frame)

    wav.close()

    filtered: wave.Wave_write = wave.open(join(const.AUDIO_DIR, 'temp.wav'),
                                          'w')
    filtered.setframerate(wav.getframerate())
    filtered.setsampwidth(wav.getsampwidth())
    filtered.setnchannels(wav.getnchannels())
    for frame in frames:
        data = struct.pack('<h', frame)
        filtered.writeframesraw(data)
    filtered.close()
    return wave.open(join(const.AUDIO_DIR, 'temp.wav'), 'r')
예제 #2
0
    def encode_chunk(self, thread_id: str, file: Wave_read,
                     total_samples_to_read: int, output: BytesIO) -> None:
        options = STARTUPINFO()
        options.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        options.wShowWindow = subprocess.SW_HIDE
        process = Popen(self.command,
                        stdin=PIPE,
                        stdout=PIPE,
                        stderr=PIPE,
                        startupinfo=options)

        read_data_thread = Thread(
            target=lambda: output.write(process.stdout.read()))
        read_data_thread.daemon = True
        read_data_thread.start()

        samples_to_read, samples_left = self.update_samples_to_read(
            total_samples_to_read, 1024)
        last_progress = 0
        while samples_left > 0:
            process.stdin.write(file.readframes(samples_to_read))

            progress = int((total_samples_to_read - samples_left) * 100 /
                           total_samples_to_read)
            if progress != last_progress:
                self.listener.encode_update(thread_id, progress)
                last_progress = progress

            samples_to_read, samples_left = self.update_samples_to_read(
                samples_left, 1024)

        self.listener.encode_update(thread_id, 100)
        process.stdin.close()
        read_data_thread.join()
        process.stdout.close()
        process.stderr.close()
        file.close()