HackyHour Würzburg

Code - Tools - Science - Help - Social

View on GitHub

HackyHour Würzburg 40

Topic Suggestions

Add your :+1: to the end of a line you are interested in

Ideas for another time

Participants

Code

import numpy as np
from scipy.io import wavfile
from pydub import AudioSegment
from pydub.playback import play

wav=wavfile.read("../sayit/text.wav")

begin = np.array([np.abs(wav[1][i*800:(i+1)*800]).sum() for i in range(60)])

# https://stackoverflow.com/a/24892274/4969760
def zero_runs(a, min_len=3):
    # Create an array that is 1 where a is 0, and pad each end with an extra 0.
    iszero = np.concatenate(([0], np.equal(a, 0).view(np.int8), [0]))
    absdiff = np.abs(np.diff(iszero))
    # Runs start and end where absdiff is 1.
    ranges = np.where(absdiff == 1)[0].reshape(-1,2)
    # Filter stretches smaller than min_len
    ranges = np.array(list(filter(lambda x: x[1]-x[0]>=min_len, ranges)))
    return ranges

np.split(wav[1][:160000], zero_runs(wav[1][:16000],min_len=100).flatten()

def play_fragment(data):
	audio_segment = AudioSegment(data.tobytes(),frame_rate=wav[0],sample_width=wav[1].dtype.itemsize,channels=1)
	play(audio_segment)

play_fragment(wav[1][:160000])

first10 = list(filter(lambda x: np.abs(x).sum()>0, np.split(wav[1][:160000], zero_runs(wav[1][:160000],min_len=100).flatten())))

first1000 = list(filter(lambda x: np.abs(x).sum()>0, np.split(wav[1][:16000000], zero_runs(wav[1][:16000000],min_len=100).flatten())))

# out of memory
# allWords = list(filter(lambda x: np.abs(x).sum()>0, np.split(wav[1], zero_runs(wav[1],min_len=100).flatten())))

# find chimera
longest = np.argmax([x.shape[0] for x in first1000])
play_fragment(first1000[longest])

# check shortest
shortest = np.argmax([x.shape[0] for x in first1000])
play_fragment(first1000[shortest])

# save for inspection in audacity
wavfile.write("chimera.wav",wav[0],first1000[longest])