czyoung commited on
Commit
e1cef83
·
verified ·
1 Parent(s): 92e7a87

Update sonogram_utility.py

Browse files
Files changed (1) hide show
  1. sonogram_utility.py +10 -8
sonogram_utility.py CHANGED
@@ -123,22 +123,24 @@ def splitIntoTimeSegments(testFile,maxDurationInSeconds=60):
123
  def audioNormalize(waveform,sampleRate,stepSizeInSeconds = 2,dbThreshold = -50,dbTarget = -5):
124
  print("In audioNormalize")
125
  copyWaveform = waveform.clone().detach()
126
- copyWaveform_db = waveform.clone().detach()
127
- print("Copies made")
128
  transform = torchaudio.transforms.AmplitudeToDB(stype="amplitude", top_db=80)
129
- copyWaveform_db = transform(copyWaveform_db)
130
- print("DB levels calculated")
131
  currStart = 0
132
  currEnd = int(min(currStart + stepSizeInSeconds * sampleRate, len(copyWaveform_db[0])-1))
133
  done = False
134
  while(not done):
135
- if torch.max(copyWaveform_db[0][currStart:currEnd]).item() > dbThreshold:
136
- gain = torch.min(dbTarget - copyWaveform_db[0][currStart:currEnd])
 
 
 
 
 
137
  adjustGain = torchaudio.transforms.Vol(gain,'db')
138
  copyWaveform[0][currStart:currEnd] = adjustGain(copyWaveform[0][currStart:currEnd])
139
  if len(copyWaveform_db) > 1:
140
- if torch.max(copyWaveform_db[1][currStart:currEnd]).item() > dbThreshold:
141
- gain = torch.min(dbTarget - copyWaveform_db[1][currStart:currEnd])
142
  adjustGain = torchaudio.transforms.Vol(gain,'db')
143
  copyWaveform[1][currStart:currEnd] = adjustGain(copyWaveform[1][currStart:currEnd])
144
  currStart += int(stepSizeInSeconds * sampleRate)
 
123
  def audioNormalize(waveform,sampleRate,stepSizeInSeconds = 2,dbThreshold = -50,dbTarget = -5):
124
  print("In audioNormalize")
125
  copyWaveform = waveform.clone().detach()
126
+ print("Waveform copy made")
 
127
  transform = torchaudio.transforms.AmplitudeToDB(stype="amplitude", top_db=80)
 
 
128
  currStart = 0
129
  currEnd = int(min(currStart + stepSizeInSeconds * sampleRate, len(copyWaveform_db[0])-1))
130
  done = False
131
  while(not done):
132
+ copyWaveform_db = waveform[:,currStart:currEnd].clone().detach()
133
+ copyWaveform_db = transform(copyWaveform_db)
134
+ if currStart == 0:
135
+ print("First DB level calculated")
136
+
137
+ if torch.max(copyWaveform_db[0]).item() > dbThreshold:
138
+ gain = torch.min(dbTarget - copyWaveform_db[0])
139
  adjustGain = torchaudio.transforms.Vol(gain,'db')
140
  copyWaveform[0][currStart:currEnd] = adjustGain(copyWaveform[0][currStart:currEnd])
141
  if len(copyWaveform_db) > 1:
142
+ if torch.max(copyWaveform_db[1]).item() > dbThreshold:
143
+ gain = torch.min(dbTarget - copyWaveform_db[1])
144
  adjustGain = torchaudio.transforms.Vol(gain,'db')
145
  copyWaveform[1][currStart:currEnd] = adjustGain(copyWaveform[1][currStart:currEnd])
146
  currStart += int(stepSizeInSeconds * sampleRate)