YevhenBeskubskyi

audiotracks

Oct 1st, 2020
614
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Track:
  2.  
  3.     def __init__(self, start, duration, silence, channel):
  4.         self.start = start
  5.         self.duration = duration
  6.         self.silence = silence
  7.         self.channel = channel
  8.  
  9.     def __str__(self):
  10.         ans = "[" + str(self.start) + " "
  11.         ans += str(self.duration) + " "
  12.         ans += str(self.silence) + " "
  13.         ans += str(self.channel) + "]"
  14.         return ans
  15.  
  16. def get_speech_percentage(tracks):
  17.     tracks.sort(key=lambda x : x.start)
  18.     total = max([t.start+t.duration for t in tracks]) - tracks[0].start
  19.     spTime = []
  20.     for t in tracks:
  21.         if not t.silence:
  22.             spTime.append([t.start, t.start+t.duration])
  23.     if len(spTime) == 0:
  24.         return 0.0
  25.     totalSp = 0
  26.     start = spTime[0][0]
  27.     end = spTime[0][1]
  28.     for i in range(1, len(spTime)):
  29.         if spTime[i][0] < spTime[i-1][1]:
  30.             end = max(spTime[i-1][1], spTime[i][1])
  31.         else:
  32.             totalSp += end - start
  33.             start = spTime[i][0]
  34.             end = spTime[i][1]
  35.     totalSp += end - start
  36.     return totalSp / total
  37.    
  38. def test1():
  39.     t1 = Track(5000, 5000, False, 0)
  40.     t2 = Track(0, 20000, True, 1)
  41.     t3 = Track(10000, 10000, True, 2)
  42.     t4 = Track(13000, 2000, False, 3)
  43.     t5 = Track(7000, 5000, False, 0)
  44.     t6 = Track(14000, 5000, False, 0)
  45.     tracks = [t1, t2, t3, t4, t5, t6]
  46.     print(get_speech_percentage(tracks))
  47.  
  48. def test2():
  49.     track1 = Track(5000, 5000, False, 0)
  50.     track2 = Track(0, 20000, True, 1)
  51.     track3 = Track(10000, 10000, True, 2)
  52.     tracks = [track1, track2, track3]
  53.     print(get_speech_percentage(tracks))
  54.  
  55. def test3():
  56.     track1 = Track(5000, 5000, False, 0)
  57.     print(get_speech_percentage([track1]))
  58.  
  59. def test4():
  60.     track1 = Track(5000, 5000, True, 0)
  61.     print(get_speech_percentage([track1]))
  62.  
  63. def test5():
  64.     track1 = Track(0, 5000, True, 0)
  65.     track2 = Track(1000, 5000, False, 1)
  66.     tracks = [track1, track2]
  67.     print(get_speech_percentage(tracks))
  68.  
  69. def test6():
  70.     track1 = Track(0, 5000, True, 0)
  71.     track2 = Track(1000, 5000, False, 1)
  72.     track3 = Track(0, 7000, False, 2)
  73.     tracks = [track1, track2, track3]
  74.     print(get_speech_percentage(tracks))
  75.  
  76. def test7():
  77.     track1 = Track(0, 5000, True, 0)
  78.     track2 = Track(1000, 5000, False, 1)
  79.     track3 = Track(0, 7000, False, 2)
  80.     track4 = Track(10000, 2000, False, 3)
  81.     tracks = [track1, track2, track3, track4]
  82.     print(get_speech_percentage(tracks))
  83.  
  84. test1()
  85. test2()
  86. test3()
  87. test4()
  88. test5()
  89. test6()
  90. test7()
RAW Paste Data