Python for Game Audio: My Toolkit

Python for Game Audio: My Toolkit

In this post, I'll share the Python toolkit I use to create various tools for game audio implementation.

DISCLAIMER: I'm not a professional programmer in any possible way. All these things I'll be talking about are not Python's best practices or universally-good pieces of advice. These are simply the tools that I, being a Technical Audio Designer, personally found useful for my job.

So, Python for Game Audio?

I believe that Python is one of the best programming languages a Technical Audio Designer can learn. Not only because it is easy to learn, has a big community, is well-supported, and has a lot of libraries, but also because it easily interfaces with the industry-standard tools, such as:

I'm using Python to automate a broad range of tasks, and I can't imagine doing my job without it.

Development Tools

Here I'll be talking about the tools I use to write Python code.

Python 3

Well, to program with Python, you'll need Python.

There are two main versions of Python: Python 2 and Python 3. I strongly recommend using Python 3. Here's why.

If you're on Windows, I'll suggest getting Python 3 from its official website. If you're on macOS, it's better to use a package manager such as Homebrew since it'll allow you to manage multiple versions of Python easily. On Windows, you may also use Chocolatey package manager, though I haven't tried it for Python yet.

PyCharm Community

PyCharm Community by JetBrains is an IDE (integrated development environment, which is a fancy way of saying "text editor that can do a lot of stuff"). It's free; it's fast; it's awesome.

There are two versions of PyCharm: Community and Professional. I use the Community and don't ever feel the need to go with the Professional.

There are some folks (usually the ones with extensive programming background) who prefer "simpler" (not really, in my opinion) and more configurable editors such as Atom, Sublime Text, Visual Studio Code, or even Vim. However, I believe that PyCharm is simply the best tool to do the job quickly and comfortably, without digging into all the settings too much or learning many weird keyboard shortcuts.

Real Programmers. Source: XKCD, https://xkcd.com/378/

TabNine

TabNine is an autocomplete plugin for PyCharm. It uses Ai and deep learning to predict what I would write next and suggest that to me automagically.

While there is a perfectly good autocomplete already built into PyCharm, TabNine takes it to another level by providing the autofill for the whole lines of code, strings, and other code parts that I would usually be writing by hand. It's hard to overestimate how much time it saves. Here is an example:

A screenshot of the TabNine autocompletion results in PyCharm.

Let me explain what's going on in this picture. I wrote a single line, first_thing = "String number one". When I started to write seco, not only TabNine suggested continuing it as a second_thing, it also guessed that the second_thing's value would be "String number two", and I never mentioned the word "two" anywhere in my code. It understands the underlying semantics of my code, which makes it super-awesome. It also learns from my code (doing it privately on my computer), getting better the more I use it.

Python Libraries

In this part, I'll be talking about Python libraries I use and love the most. Most of them don't work directly with audio, but I still find them incredibly helpful. I'll keep the library's descriptions short so that this post won't end up too big.

Pathlib

Pathlib is the only library in this list that's a part of The Python Standard Library (which means you already have it, if you have Python).

It's a handy library for everything related to paths, files, folders, and filesystem. There are some "older" ways of working with paths in Python, such as os.path, though I find Pathlib much more convenient to use in almost all cases.

Tip: you can concatenate paths with / operator (combined_path = first_path / second_path), which I find pretty cool.

Loguru

Loguru is an easy-to-use logging library and a better alternative to using print() to debug the code and show valuable info on the screen. Here is how it looks:

A screenshot of the Loguru output in PyCharm.

NumPy

NumPy (Numerical Python) is an enormously powerful library for working with n-dimensional arrays. Why is it relevant? Because when I'm working with audio, the chances are that the audio data I'm working with is represented as an n-dimensional array. While Python supports a lot of kinds of arrays out-of-the-box, NumPy does it much faster, easier, and has a whole lot of additional tools. Also, there are some pretty fast math functions in Numpy that are not only working with Numpy arrays but also with the usual float numbers.

Whenever I need to do some audio analysis or offline DSP processing in Python, I'll probably end up using NumPy or SciPy, either directly or through other higher-level libraries that use them under the hood.

SciPy

SciPy (Scientific Python) is a library with digital filters and other DSP-related things in its scipy.signal part.

Matplotlib

Matplotlib is my go-to library for drawing plots and visualizing data, useful for audio and many other things. The closest alternative is Plotly, but I find it a bit harder to setup.

SoundFile

SoundFile is a library based on libsndfile (a quite popular C library) and NumPy, which makes reading audio files in Python easy.

Reapy

Reapy is a ReaScript Python API wrapper. It makes it possible to write scripts for the REAPER in Python, using my own Python 3 interpreter, not the one that should be run from the REAPER itself. In other words, I can use a lot more different libraries and write REAPER scripts much more comfortably.

Puremagic

Puremagic is a pure Python library for reading file's magic numbers. It allows me to check file types more reliably than merely by the file name extensions.

Requests

Requests is one of the most popular Python libraries ever! And I use it for everything related to HTTP requests and getting something from the internet.

Pandas

Pandas is a data manipulation library that I use for data that's somewhat similar to our usual Excel spreadsheets. When I have something that looks like a table with rows, columns, and different data types - I usually go with Pandas.

Librosa

Librosa is an audio analysis library. It's a bit higher-level than SciPy, thus a bit easier to use.

Click

Click is a beautiful library for building CLIs (command-line interfaces). It's easy to use and allows me to create CLI programs without bothering much with the built-in argparse.

P4Python

P4Python is a Perforce API library that helps me build workflows that use the Perforce version control system.

Colorama

Colorama is a library that allows me to use colors in my program's output.

Pyfiglet

Pyfiglet is an ASCII art library. I use it to draw beautiful welcome messages for my CLI programs. Like this:

HHHHHHHHH     HHHHHHHHH                                            !!! 
H:::::::H     H:::::::H                                           !!:!!
H:::::::H     H:::::::H                                           !:::!
HH::::::H     H::::::HH                                           !:::!
  H:::::H     H:::::H      eeeeeeeeeeee  yyyyyyy           yyyyyyy!:::!
  H:::::H     H:::::H    ee::::::::::::ee y:::::y         y:::::y !:::!
  H::::::HHHHH::::::H   e::::::eeeee:::::eey:::::y       y:::::y  !:::!
  H:::::::::::::::::H  e::::::e     e:::::e y:::::y     y:::::y   !:::!
  H:::::::::::::::::H  e:::::::eeeee::::::e  y:::::y   y:::::y    !:::!
  H::::::HHHHH::::::H  e:::::::::::::::::e    y:::::y y:::::y     !:::!
  H:::::H     H:::::H  e::::::eeeeeeeeeee      y:::::y:::::y      !!:!!
  H:::::H     H:::::H  e:::::::e                y:::::::::y        !!! 
HH::::::H     H::::::HHe::::::::e                y:::::::y             
H:::::::H     H:::::::H e::::::::eeeeeeee         y:::::y          !!! 
H:::::::H     H:::::::H  ee:::::::::::::e        y:::::y          !!:!!
HHHHHHHHH     HHHHHHHHH    eeeeeeeeeeeeee       y:::::y            !!! 
                                               y:::::y                 
                                              y:::::y                  
                                             y:::::y                   
                                            y:::::y                    
                                           yyyyyyy                     
Hey!

Really important advice

Always use type hints in Python.

Show Comments