Getting Started to Stop Faking My Way through the Electroacoustic World Using Python

Now we get to the real reason for these posts.

For those of you who opted to skip the rant that is the first post I made, welcome! For those of you who want some context before jumping into some deep Python, click here.

Almost a week ago, I was discussing with my father (no relation to “Father”, but relation to my tribulations in needing to enter the electroacoustic world. I told him how I am unable to get started because I don’t know what tools I’m working with. In an orchestra, you know you’re going to have woodwinds for all of those woodwind-y moments. But with electroacoustics? Well, you have whatever you’ve given yourself to work with.

He said to me that any programmer builds a library over their life of pieces of code that they use over and over. For example, a weather app maker will always need a piece of code to convert Celsius to Fahrenheit and back. So I need to build mine. And that process, he said, will be slow and frustrating.

So last Wednesday, I went through that process.

I’m hoping I can give pieces of advice through the telling of this story to help those of you who might be thinking of moving away from Max and need some help in the cold, desolate world of Python libraries. So here goes:

(1) First, I began by trying to use PyAudio raw

This was a mistake. PyAudio is very convenient for very basic sound processing. It will record audio. It will play audio. It will even playback your microphone live. But any manipulation on the data it handles will most likely be to the bits themselves. Trust me, this is like messing with the registry on your computer. You can do it, but you’ve really got to know what you’re doing in order to not break the thing you’re playing with.

(2) I cleaned up my Python environment

I had installed Python on my computer many months before this day. So when I went to install some new libraries, I saw that there were two install locations, and I had no idea what was going on. So I uninstalled everything. Which is not inherently a bad idea! But, here are the mistakes I made in reinstalling Python:

(a) I installed Python 3.7

As of today, August 27th, 2018, this isn’t a good idea. Python 3.7 has some glitches with PIP (the auto-libary-installer) that make it impossible to use. If you want to use Python libraries (you do), you want PIP.

(b) So, I installed Python 3.68 to the root of my C Drive

Which was not smart. Be sure to make a folder for your Python.

(c) And then, I installed the 64-bit Python

It is so dumb that this was a mistake. But this is Windows, and I’m about to explain why this was a mistake.

So, in the end (after many installs and uninstalls), I had a 32-bit Python 3.68 installed in its own folder (“Python36-32”), and that folder was in the root of the C Drive (C:/). When I installed, I told it to install PIP and add Python to my environment variables. I was ready to go again.

(As an aside, I already had my text editor, Sublime installed. If you have no experience with Python and don’t know what to use to write and execute your code, pick your code editor here. I won’t go into much more detail, but I believe that if you use Sublime, and you did everything above correctly, then all you have to do is save your file as .py, hit Ctrl+B on your file, and everything should run smoothly.)

(3) I played around with pysndfx

Short for Python Sound Effects. These guys have built a sleek library that does all the basic effects an electroacoustic musician could ask to start with. However, their live sound manipulation is still in beta. But, if this strikes anyone’s fancy, I invite you to take a look at it (check them out on their GitHub page).

(4) Finally, I discovered pyo

Let’s be clear, at this moment, I was only 1/3 of the way through my day. I will tell you right now, pyo is what I stuck with (spoiler alert). And while it is an INSANELY powerful Python library, it has its quirks. And they took me another third of my day to figure out. So here’s that process…

(5) Installing and setting up pyo

Real quick thing about pyo: this is a product of AJAX Sound Studio that essentially takes the inner guts of PyAudio and injects it with steroids to allow for the live electroacoustic manipulation I was looking for, all wrapped up in a neat package. This is basically what I was trying to build with PyAudio, but done in C (a much faster language) and already completed (so I don’t have to do it). Check it out on their site:

So here’s what I went through to get it installed:

(a) This installs through a .exe

I know, I know. I just made a lot of noise about making sure PIP works for your Python. And at this moment, that doesn’t matter. But… still make sure your PIP works for your Python.

Anyway, you download the .exe and you run it simple.

(b) You have to make sure you’re working with 32-bit Python

For whatever reason, the default folder this saves to is always a 32-bit Python folder. And it doesn’t automatically aim for the right destination. So make sure you’re installing to your Python 36-32 folder then go to > Lib > site-packages and give it a folder in there. This is you doing PIP’s job for it. (This step is why I said installing 64-bit Python was a mistake before. 32-bit libraries should work in 64-bit worlds but… what can you do when they don’t? So you use 32-bit worlds)

(c) Do the damn introductory files

After fighting through a difficult setup and install process, the first thing I always want to do is hop into is playing with the software and getting results. But I learned so much faster by going through their example files. Here is what you will learn: (1) pyo sets up a “server” for your sound to run through. This server’s settings is what controls the settings of your sound and is the reason why pyo runs so quickly. (2) If this server isn’t interacting with your sound driver correctly, pyo won’t work at all. Here are two solutions I came up with:

(a) Specify the driver with winhost:

Ex. Server(winhost=”asio”).boot()

I never really figured out how the winhost names correlated with my driver, but if you go onto pyo’s audio setup documentation, you’ll see there’s not too many of them, so you can guess and check. “[“mme”, “directsound”, “asio”, “wasapi”, “wdm-ks”]” wasapi is the default. asio is the one that worked for me.

(b) If you read that documentation, you’ll notice there’s some stuff lower down about specifying sampling rate.

I prefer this, because my computer changes drivers depending on whether I’m playing through speakers or headphones. And this is WAY TOO MUCH risk for me (imagine, I’m testing through speakers as I build my program. Then I get to the performance and have to plug into speakers, and can’t figure out which driver to route to… literal nightmare). This is what I found worked for me:

Ex. Server(sr = 48000)

How do you know what your sampling rate is? Right click your lower-right sound icon > Open sound settings > Device properties > “Advanced” tab. You’ll see in the dropdown box the default format your driver uses. Mine says “24 bit, 48000 Hz (Studio Quality)”. So if I tell pyo to use that sample rate, no matter the output device, it knows what sample rate to use and, therefore (I think), which driver to use. As a result, my sounds play no matter where I want it to come out of.

(d) Once you get here, you can start playing

And playing was the last third of my day. The introductory files were still a huge help, but I had a lot of fun playing with different methods and developing the sound I wanted for a piece (all with the help of pyo’s decent documentation)!

And this is exactly what my dad was talking about. You need to build a world so you can play in it. Now, you might have noticed, I said I did this in a day. Which means the world I built to play in is PRETTY SIMPLE. But it makes me excited to do electroacoustics and gives me a path to open up my creativity into this world. I’m excited to show you what I make in it, and be sure to show me what you make too!

– Dewey


… And because I’m a nice guy, here’s the code I used that gives me simple playback from the microphone.

2 thoughts on “Getting Started to Stop Faking My Way through the Electroacoustic World Using Python”

Leave a Reply

Your email address will not be published. Required fields are marked *