Skip to content

Instantly share code, notes, and snippets.

Last active July 31, 2023 22:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dhondta/8b3c7d95b056cae3505df853a098fc4f to your computer and use it in GitHub Desktop.
Tinyscript tool for assembling audio files and altering frequencies and speed factor

Audio Assembler

This Tinyscript-based tool, based on PyDub, allows to append audio files, filter the result as voice frequency and alter the playback speed.

This can be installed using:

$ pip install tinyscript
$ tsm install audio-assembler
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from pydub import AudioSegment
from pydub.playback import play
from tinyscript import *
__author__ = "Alexandre D'Hondt"
__version__ = "3.0"
__copyright__ = "A. D'Hondt"
__license__ = "agpl-3.0"
__doc__ = """
This simple tool, based on PyDub, allows to append audio files, filter the
result as voice frequency and alter the playback speed.
__examples__ = ["audio_dir -s 10 --voice"]
class Song(object):
def __init__(self, *song_files):
self.__song = None
def append(self, *song_files):
""" This appends song files to the current song. """
songs = [] if self.__song is None else [self.__song]
for f in song_files:
from_method = "from_{}".format(Path(f).suffix[1:])
filename = Path(f).filename
m = getattr(AudioSegment, from_method, "from_file")
logger.debug("Trying to append '{}'...".format(filename))
songs.append(m(str(f)))"Appended: {}".format(filename))
logger.debug("An error occured while trying to append {}"
" (AudioSegment.{})".format(filename, from_method))
logger.warning("Skipped: {}".format(filename))
if len(songs) > 0:
self.__song = sum(songs)
def export(self, filename):
""" This exports the current song to a target file. """
if self.__song is None:
fmt = Path(filename).suffix[1:]
self.__song.export(filename, format=fmt)"Created: {}".format(filename))
except Exception as e:
logger.error("Error while exporting the song")
def filter(self, low=None, high=None):
""" This filters low and/or high frequencies. """
if self.__song is None:
if low is not None:
logger.debug("Filtering low frequency...")
self.__song = self.__song.low_pass_filter(low)
if high is not None:
logger.debug("Filtering high frequency...")
self.__song = self.__song.high_pass_filter(high)
def play(self):
""" This plays the song. """
def speedup(self, factor):
""" This speeds up the song. """
if self.__song is None or factor == 1.0:
logger.debug("Changing playback speed...")
self.__song = self.__song.speedup(playback_speed=factor)
if __name__ == '__main__':
parser.add_argument("items", nargs='+',
help="folder with audio files or files themselves")
parser.add_argument("-o", dest="output", default="audio.wav",
help="output file")
parser.add_argument("-q", dest="quiet", action="store_true",
help="do not play the created audio")
parser.add_argument("-s", dest="speedup", type=float, default=1.0,
help="playback speed factor")
parser.add_argument("--voice", action="store_true",
help="filter voice frequency band")
# running the main stuff
s = Song()
for i in args.items:
p = Path(i)
if p.is_dir():
for f in p.walk():
if args.voice:
s.filter(300, 3000)
if not args.quiet:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment