Skip to content

Commit df6530e

Browse files
committedJan 13, 2025
remove
1 parent 764ecf8 commit df6530e

File tree

1 file changed

+0
-121
lines changed

1 file changed

+0
-121
lines changed
 

‎open_dubbing/pydub_audio_segment.py

-121
Original file line numberDiff line numberDiff line change
@@ -1275,125 +1275,4 @@ def append(self, seg, crossfade=100):
12751275
output.close()
12761276
return obj
12771277

1278-
def fade(self, to_gain=0, from_gain=0, start=None, end=None,
1279-
duration=None):
1280-
"""
1281-
Fade the volume of this audio segment.
1282-
1283-
to_gain (float):
1284-
resulting volume_change in db
1285-
1286-
start (int):
1287-
default = beginning of the segment
1288-
when in this segment to start fading in milliseconds
1289-
1290-
end (int):
1291-
default = end of the segment
1292-
when in this segment to start fading in milliseconds
1293-
1294-
duration (int):
1295-
default = until the end of the audio segment
1296-
the duration of the fade
1297-
"""
1298-
if None not in [duration, end, start]:
1299-
raise TypeError('Only two of the three arguments, "start", '
1300-
'"end", and "duration" may be specified')
1301-
1302-
# no fade == the same audio
1303-
if to_gain == 0 and from_gain == 0:
1304-
return self
1305-
1306-
start = min(len(self), start) if start is not None else None
1307-
end = min(len(self), end) if end is not None else None
1308-
1309-
if start is not None and start < 0:
1310-
start += len(self)
1311-
if end is not None and end < 0:
1312-
end += len(self)
1313-
1314-
if duration is not None and duration < 0:
1315-
raise InvalidDuration("duration must be a positive integer")
1316-
1317-
if duration:
1318-
if start is not None:
1319-
end = start + duration
1320-
elif end is not None:
1321-
start = end - duration
1322-
else:
1323-
duration = end - start
1324-
1325-
from_power = db_to_float(from_gain)
1326-
1327-
output = []
1328-
1329-
# original data - up until the crossfade portion, as is
1330-
before_fade = self[:start]._data
1331-
if from_gain != 0:
1332-
before_fade = audioop.mul(before_fade,
1333-
self.sample_width,
1334-
from_power)
1335-
output.append(before_fade)
1336-
1337-
gain_delta = db_to_float(to_gain) - from_power
1338-
1339-
# fades longer than 100ms can use coarse fading (one gain step per ms),
1340-
# shorter fades will have audible clicks so they use precise fading
1341-
# (one gain step per sample)
1342-
if duration > 100:
1343-
scale_step = gain_delta / duration
1344-
1345-
for i in range(duration):
1346-
volume_change = from_power + (scale_step * i)
1347-
chunk = self[start + i]
1348-
chunk = audioop.mul(chunk._data,
1349-
self.sample_width,
1350-
volume_change)
1351-
1352-
output.append(chunk)
1353-
else:
1354-
start_frame = self.frame_count(ms=start)
1355-
end_frame = self.frame_count(ms=end)
1356-
fade_frames = end_frame - start_frame
1357-
scale_step = gain_delta / fade_frames
1358-
1359-
for i in range(int(fade_frames)):
1360-
volume_change = from_power + (scale_step * i)
1361-
sample = self.get_frame(int(start_frame + i))
1362-
sample = audioop.mul(sample, self.sample_width, volume_change)
1363-
1364-
output.append(sample)
1365-
1366-
# original data after the crossfade portion, at the new volume
1367-
after_fade = self[end:]._data
1368-
if to_gain != 0:
1369-
after_fade = audioop.mul(after_fade,
1370-
self.sample_width,
1371-
db_to_float(to_gain))
1372-
output.append(after_fade)
1373-
1374-
return self._spawn(data=output)
1375-
1376-
def fade_out(self, duration):
1377-
return self.fade(to_gain=-120, duration=duration, end=float('inf'))
1378-
1379-
def fade_in(self, duration):
1380-
return self.fade(from_gain=-120, duration=duration, start=0)
1381-
1382-
def reverse(self):
1383-
return self._spawn(
1384-
data=audioop.reverse(self._data, self.sample_width)
1385-
)
1386-
1387-
def _repr_html_(self):
1388-
src = """
1389-
<audio controls>
1390-
<source src="data:audio/mpeg;base64,{base64}" type="audio/mpeg"/>
1391-
Your browser does not support the audio element.
1392-
</audio>
1393-
"""
1394-
fh = self.export()
1395-
data = base64.b64encode(fh.read()).decode('ascii')
1396-
return src.format(base64=data)
1397-
1398-
13991278
from . import pydub_effects

0 commit comments

Comments
 (0)