Advertisement
yclee126

Combine sounds into one (v2)

Jan 25th, 2022
600
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # combine all individual graph sounds into one waveform (v2)
  2. # This version will get rid of short glitch sounds.
  3.  
  4. import soundfile as sf
  5. import numpy as np
  6.  
  7. idle_duration = 0.1
  8. graph_duration = 5.0
  9. min_duration = 1.0
  10.  
  11. data, samplerate = sf.read('graphs.wav')
  12. graph_samples = int(graph_duration*samplerate)
  13.  
  14. # process each data point
  15. last_time = 0
  16. triggered = False
  17. start_points = []
  18. end_points = []
  19. for i, samp in enumerate(data):
  20.     cur_time = i / samplerate
  21.  
  22.     if np.abs(samp[0]) > 0.001 or np.abs(samp[1]) > 0.001: # sound detected
  23.         if not triggered: # this is a new chunk
  24.             start_points.append(i)
  25.             triggered = True
  26.         last_time = cur_time
  27.            
  28.     elif triggered and cur_time - last_time > idle_duration: # silence detected
  29.         end_points.append(i)
  30.         triggered = False
  31.  
  32. # in case the last end point is not detected, add one.
  33. if len(start_points) != len(end_points):
  34.     end_points.append(len(data[0]) - 1)
  35.  
  36. # new combined graph sound
  37. arr = np.zeros((graph_samples, 2))
  38.  
  39. graphs = 0
  40. for i, point in enumerate(start_points):
  41.     if end_points[i]/samplerate - point/samplerate < min_duration:
  42.         continue
  43.     arr += data[point:point+graph_samples]
  44.     graphs += 1
  45. print(f'{graphs} graphs detected')
  46. arr /= graphs # normalize
  47.  
  48. sf.write('graphs_combined.wav', arr, samplerate)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement