Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # combine all individual graph sounds into one waveform (v2)
- # This version will get rid of short glitch sounds.
- import soundfile as sf
- import numpy as np
- idle_duration = 0.1
- graph_duration = 5.0
- min_duration = 1.0
- data, samplerate = sf.read('graphs.wav')
- graph_samples = int(graph_duration*samplerate)
- # process each data point
- last_time = 0
- triggered = False
- start_points = []
- end_points = []
- for i, samp in enumerate(data):
- cur_time = i / samplerate
- if np.abs(samp[0]) > 0.001 or np.abs(samp[1]) > 0.001: # sound detected
- if not triggered: # this is a new chunk
- start_points.append(i)
- triggered = True
- last_time = cur_time
- elif triggered and cur_time - last_time > idle_duration: # silence detected
- end_points.append(i)
- triggered = False
- # in case the last end point is not detected, add one.
- if len(start_points) != len(end_points):
- end_points.append(len(data[0]) - 1)
- # new combined graph sound
- arr = np.zeros((graph_samples, 2))
- graphs = 0
- for i, point in enumerate(start_points):
- if end_points[i]/samplerate - point/samplerate < min_duration:
- continue
- arr += data[point:point+graph_samples]
- graphs += 1
- print(f'{graphs} graphs detected')
- arr /= graphs # normalize
- sf.write('graphs_combined.wav', arr, samplerate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement