A useful script

The best way to remember to do something when you’re going to run some important program is to put in the program itself. Or at the very least, put it in a script that you use to invoke the program.
I have a few things I need to remember for the performance I’m preparing for. One has to do with a projector. I’m using a stylus to draw cloud shapes on my screen. And one way I can do this is to mirror my screen to a projector so the audience can see my GUI. However, doing this usually changes the geometry of my laptop screen, so that instead of extending all the way to the edges, there are empty black bars on either side of the used portion of my display. That’s fine, except the stylus doesn’t know and doesn’t adjust. So to reach the far right edge of the drawn portion of the screen, I need to touch the far right edge of the drawn portion, which puts over a centimetre between the stylus tip and the arrow pointer. Suboptimal!
Ideally, I’d like to have any change in screen geometry trigger a script that changes the settings for for the stylus (and I have ideas about how that may or may not work, using upstart, xrandr and xsetwacom), but in the absence of that, I just want to launch a manual calibration program. If I launch the settings panel, there’s a button on that that launches one. So the top part of my script checks if the calibration is different than normal and launches settings if it is.
The next things I need to remember are audio related. I need to kill pulseaudio. If my soundcard (a Fast Track Ultra) is attached, I need to change the amplitude settings internally so it doesn’t send the input straight to the output. Then I need to start jack using it. Or if it’s not attached, I need to start jack using a default device. Then, because it’s useful, I should start up Jack Control, so I can do some routing, should I need it. (Note: in 12.04 if you start qjackctl after starting jackd, it doesn’t work properly. This is fixed by 13.04.) Finally, I should see if SuperCollider is already running and if not, I should start it.
That’s a bit too much to remember for a performance, so I wrote a script. The one thing I need to remember with this script is that if I want to kill jack, it won’t die from Jack Control, so I’ll need to do a kill -9 from the prompt. hopefully, this will not be an issue on stage.
This is my script:

#!/bin/bash


# first check the screen

LINE=`xrandr -q | grep Screen`
WIDTH=`echo ${LINE} | awk '{ print $8 }'`
HEIGHT=`echo ${LINE} | awk '{ print $10 }' | awk -F"," '{ print $1 }'`

if  [[ ${WIDTH} != 1366 || ${HEIGHT} != 768 ]]
  then
 gnome-control-center wacom
  else
 echo normal resolution
fi


# now setup the audio

pulseaudio --kill

# is the ultra attached?
if aplay -l | grep -qi ultra
  then
 echo ultra
 
 #adjust amplitude
 i=0
 j=0
 for i in $(seq 8); do
         for j in $(seq 8); do
                 if [ "$i" != "$j" ]; then
                         amixer -c Ultra set "DIn$i - Out$j" 0% > /dev/null
                 else
                         amixer -c Ultra set "DIn$i - Out$j" 100% > /dev/null
                 fi
                 amixer -c Ultra set "AIn$i - Out$j" 0% > /dev/null
         done
 done

 #for i in $(seq 4); do 
 # amixer -c Ultra set "Effects return $i" 0% > /dev/null 
 #done 

 #start jack
 jackd -d alsa -d hw:Ultra &
  else
 #start jack with default hardware
 jackd -d alsa -d hw:0 &
fi

sleep 2

# jack control
qjackctl &

sleep 1

# is supercollider running?
if ps aux | grep -vi grep | grep -q scide
  then
 echo already running
  else
 scide test.scd &
fi

Recording Audio and Video from SuperCollider on Ubuntu Studio

Recently, I needed to record my screen and audio while SuperCollidering on Ubuntu Studio. (This will work on other operating systems also also with some tweaks.) This is the code I included:

s.waitForBoot({

("ffmpeg -f jack -ac 2 -i ffmpeg -f x11grab -r 30 -s $(xwininfo -root | grep 'geometry' | awk '{print $2;}') -i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0" +
    "/home/celesteh/Documents/" ++ Date.getDate.bootSeconds ++ ".mkv"
        ).runInTerminal;

....

})

As far as I am able to determine, that allows you to record stereo audio from jack and the screen content of your primary screen. Obviously the syntax is slightly dense.

I was unable to figure out how to tell it to automatically get the jack output I wanted, so in order to run this, I first started JACK Control, used that to start jack, and then evaluated my SC code, which included the above line. That opened a terminal window.

Then I went back to JACK Control and clicked the Connect button to open a window with a list of connections. I took the supercollider output connections and dragged them to the ffmpeg input connections, which enabled the sound. This step is very important, as otherwise the recording will be silent. The dragging is illustrated in the accompanying image by the thick, red line.

Then AFTER making the audio connection, I started making sound with SuperCollider, which was then recorded. When I finished, I went to the terminal window opened by supercollider and typed control-c to stop the recording. Then, I quit the SC server and JACK.

After a few moments, the ffmpeg process finished quitting. I could then watch the video and hear sound via VLC (on my system, VLC does not play audio if jack is running, so check that if your film is silent). I used OpenShot to cut off the beginning part, which included a recording of me doing the jack connections.

The recommended way of recording desktop output on ubuntu studio is recordmydesktop, but there are many advantages to doing it with ffmpeg instead. There is a bug in recordmydesktop which means it won’t accept jack input, so you have to record the video and audio separately and splice them together. (If you chose to do this, note you need to quit jack before recordmydesktop will generate your output file). Also, I found that recordmydesktop took up a lot of processing power and the recording was too glitchy to actually use. The command line for it is a LOT easier, though, so pick your preference.

You need not include the command to start ffmpeg in your SuperCollider code. Instead of using the runInTerminal message, you can just run it in a terminal. As above, make sure you start JACK before you start this code and don’t forget to make the connections between it and ffmpeg. I prefer to put it in the code because then I don’t forget to run it, but this is a matter entirely for personal preference.

Cross-platform

Almost all of the programs linked above are cross-platform, so this is very likely to work on mac or windows with only a few changes. Mac users have two ways to proceed. One is to use jack with QuickTime. You can use jack to route audio on your system so the SuperCollider output goes to the quicktime input. Or, if you’re in windows or just want to use ffmpeg, you will need to change the command line so that it gets the screen geometry in a different way and so that it captures from a different device. Check the man pages for ffmpeg and for avconv, which comes with it. Users on the channel on freenode can also help. Leave a comment if you’ve figured it out.

Audio software on 10.5 / Intel

Audacity

I’ve been starting to try to record things on my new mac, despite feeling cruddy with a bad cold. I first started out with trying to use the beta version of Audacity. It acted much more like an alpha version. After the 4th crash in which all my data was lost, I took a look back at the non-beta version of Audacity. This seems to be stable and work well. However, it just doesn’t have enough features to use it for composing. It’s great for recording a vocal-only podcast or running FX on pre-existing audio, but it’s not going to cut it for my needs now. So I turned to Ardour.

Ardour

Ardour does just about everything I need. It’s a competitor to Protools and Digital Performer. And it’s free!! W00t. In the past, I only ever used this on my macmini because I had a disk space shortage on my old laptop. I was less worried about the program itself and more concerned about having space for y projects. Audio files can take up a lot of space.
It has some system requirements. If you’re on 10.4, you will need to install x-windows, if you don’t already have it, which means you need to go dig out your system disk. If you’re on 10.5, you will have other issues. It requires a helper application called Jack.

Jack

Jack is awesome. If you’re on intel, once you install it, you will need to open /Applications/Utilities/Audio MIDI Setup. Under the Audio menu, open the Aggregate Device Editor.
Aggregate Device Editor

At the top part, you can create aggregate devices and give them names. In the bottom part, you can see the actual audio devices on your computer, with check boxes next to them. Check the ones that you want to use.
Then, you need to configure jack. Fire up JackPilot. You need to tell the preferences to use your aggregate device.

JackPilot Preferences

Patching the X version

10.4 is now ready to go, but 10.5 has some changes in how X windows is handled. You have two options on running Ardour. One is to run a patch. The development team has already figured out how to solve this problem, but they’re short staffed or something and haven’t updated the version or download. Ergo, you need to apply some small changes to the program by typing a few things at the prompt. This is easy enough, but if you don’t like prompts, skip to the alternate solution.
The terminal application is located at /Applications/Utilities/Terminal. Open it up. Between you and me, the terminal is awesome. It gives you all kinds of power over your computer. In a future post, I’ll link to a cool manual, but I can’t find it right now, alas. Anyway. Here’s what you do:

  1. Select the following text and copy it (by going to Copy under the Edit Menu or typing apple-C)
    Index: script
    ===================================================================
    --- script      (revision 2354)
    +++ script      (working copy)
    @@ -32,17 +32,22 @@
         sed 's/xterm/# xterm/' /usr/X11R6/lib/X11/xinit/xinitrc >> ~/.xinitrc
     fi
     
    -mkdir -p $TMP
    -cp -f "$CWD/bin/getdisplay.sh" $TMP
    -rm -f $TMP/display
    -open-x11 $TMP/getdisplay.sh || 
    -open -a XDarwin $TMP/getdisplay.sh || 
    -echo ":0" > $TMP/display
    +if uname -r | grep -sq '^9' ; then
    +    # leopard will auto-start X11 for us
    +    :
    +else 
    +    mkdir -p $TMP
    +    cp -f "$CWD/bin/getdisplay.sh" $TMP
    +    rm -f $TMP/display
    +    open-x11 $TMP/getdisplay.sh || 
    +       open -a XDarwin $TMP/getdisplay.sh || 
    +       echo ":0" > $TMP/display
     
    -while [ "$?" == "0" -a ! -f $TMP/display ]; do sleep 1; done
    -export "DISPLAY=`cat $TMP/display`"
    +    while [ "$?" == "0" -a ! -f $TMP/display ]; do sleep 1; done
    +    export "DISPLAY=`cat $TMP/display`"
     
    -ps -wx -ocommand | grep -e '[X]11' > /dev/null || exit 11
    +    ps -wx -ocommand | grep -e '[X]11' > /dev/null || exit 11
    +fi
     
     cd ~/
     shift
    
    
  2. Ok, now be relaxed. If you don’t want to know more about what that code is doing, you don’t have to. Go to your terminal and in the window there, type:
    cd; cat > ardpatch
  3. Now, still in your terminal window, paste in all the code from the clip board, by selecting paste in the edit menu or typing apple-v
  4. Still in the terminal window, type ctrl-d. What you did just then was change to your home directory (with “cd”) and then put the code into a file called “ardpatch” (with the cat > ardpatch). And then closed that file by typing ctrl-d.
  5. Ok, now you need to know the directory where you put Ardour. If you put it in /Applications, then you’re going to type:
    cd /Applications/Ardour2.app/Contents/Resources/

    but if you put it in a folder in /Applications called Audio, then what you’ll need to type is:

    cd /Applications/Audio/Ardour2.app/Contents/Resources/

    cd is changing directory and you need it to change to a hidden directory inside Ardour, so the first part is the location where you stuck the program.

  6. Then type:
    patch -p0 < ~/ardpatch

    It will tell you strange things and possibly give you an error. Ignore all that. Instead, start jack with JackPilot and then click on the Ardour2 icon to start the program. It should start up, but for me, this took several minutes, I think just because it was the first time.

You only have to apply the patch once, so you're good to go from now on. Or you can try a riskier but easier route.

Native Version

Beta software is always fun, isn't it? You can try running the native version instead. It's beta. It could crash terribly. I haven't tested it much, so I can't recommend it or warn you away or do anythng else aside from tell you it's semi-secret location.
Well, it's more an open secret. I got it from the IRC channel on freenode. If you need help, that's a good place to go, by the way. (Is IRC undergoing a renaissance or is it just me?)
The native version is at http://ardour.org/files/releases/Ardour2.2-Intel-2920.zip
It's probably a secret for a reason. I'll give it a try this afternoon and let y'all know what I think.
The native version still requires Jack. You will still need to do all the Jack configuration listed near the top.

Conclusion

The nice thing about configurations is that you usually only need to do them once. Given the amount of awesomeness crammed into Ardour, it's totally worth the bother.

Yay, jack works!

In the interest of writing things down, in case if I have future problems . . .. Ok, so Ardour, which is a very nice piece of software, requires a library called Jack. when I heard this, several months ago, my first thought was get to get jack from fink, but the version wasn’t new enough, so I removed it. Then I tried various other versions, none of which worked properly.

So, I modified . . . . something to get a working version. It was a few versions sort of stuck together like frankenjack (and it died right before Halloween . . . spooky!)
Yeah, so unable to remember what I did, I turned to IRC for help. IRC is very cool, I don’t know I didn’t use it before this last summer. You can find all sorts of people doing sort of the same thing as you and then help each other solve problems. It’s awesome! IRC.Freenode.net has some really cool channels, including and . (It also has , but that is really inactive.) Anyway, I went for help in and learned about something called a paste site. It’s bad form to paste a lot of crap into a channel and if you do so and then say you think you might be a hacker, people will laugh at you for being a silly n00b. Um, not that I totally did that and am all embarassed right this second. um.
anyway
Somebody there told me to blow away every jack-related file and install JackOSX. which I did, or tried to do, and it still blew up. Sie suggested I do a system-wide find command looking for wrong versions of jackd. That turned up nothing, but when I broadened my search, I found that I had removed fink’s version of jack, but not fink’s version of jack-shlibs. So when I tried to run jack, it was pointing at the wrong libraries.
On the one hand, that was kind of a dumb oversight, but on the other hand I’m kind of amazed that I was able to work around this before. I wish I could remember what I did.
Because being a hacker isn’t about knowing about paste sites (that’s just nettiquite). Being a hacker means being able to get impossible things to work. It’s a terrible thing to do, because you can never figure it out later, especially if somebody else did it. Anyway, I’m kind of amazed at myself six months ago.
And, if you’re thinking of running Ardour, which is a nice piece of software, you won’t have problems unless you try to do something impossible. (But if you want to run it on one machine and display it on another via X-windows, you still have to modify some of the contents, alas)
The route from my computer to my speakers on my mixing board is still working, which makes me think I’m all wrong about that too, but there’s some intermittent drop out, so I’m going to be confident in my previous assessment of the problem.

Mercury is in retrograde

As an Aquarius, of course, I don’t believe in astrology. But sometimes, when things start going wrong in rapid succession, I ask my California friends and learn that Mercury is, indeed, in retrograde. I have no idea what this means as per Mercury’s position relative to the Earth (and certainly have no idea how this could possibly have any measurable effect on the Earth), but in astrology terms, it means things are going to break. It’s a bad time to start new projects. One friend, Polly, who actually worked giving readings, explained that this is actually a good thing in that it helps people weed things out and focus, etc. It’s important to lead a balanced life. So my last week or so has been very balancing.

So I mentioned in my last post that I was moving from CVS to svn (don’t worry, this won’t get too technical, but anyway, the next paragraph will be non-geeky). I was all set to start work on new projects for a new year and so I created the organizational scheme that I would use on one computer and when I tried to get it onto the other one . . . no dice. So I decided enough was enough and switched systems. Which was fraught with all kinds of peril. In brief, it didn’t like my data and wouldn’t work with it. Developers started to recognize my login name and ask if I could submit bug reports. This is not a good thing. I had to add every file to svn, one at a time, trying to figure out which one was the culprit. I never did find out, but this process was not fast. At least it’s all in now, so my data is more or less safe.
While this was going on, I finally realized that none of my classmates were going to volunteer to watch my dog. My supervisor would have offered, but his wife just gave birth on Monday, so I told him no. There’s a pet store near my house, that I’ve talked about before. the owner was the guy who told me that my dog would be stolen from in front of the supermarket. Anyway, he has a chip reader and I wanted to see if Xena’s chip could be read with a standard, English reader and I wanted to get advice about kennels, so I went back to the shop with Xena.
The smell wasn’t very nice when I first went, but this time, it was like the air was soupy with it. The shop is full of birds and rodents and other small animals and a cat and there was a pungent mix of the smells of their food, their bedding, their fur and their feces. They also do dog grooming there, so there was a smell of wet dog and the hairs brown around my the hair dryers wafted through the shop (if only I was exaggerating). There were cases of vegetables on the ground, which I guess were for the rabbits, and clearly some forgotten ones, behind the overly stuffed shelves, had been left to rot. I imagined that a portal had opened to some foul layer of hell and stinking demons entered and left through this shop, whose smell would mask their comings and goings.
The shop guy didn’t have his chip reader with him. As for kennels, there were no good ones in Birmingham. I would get Xena back half starved, with open wounds, if I got her back at all. One of his customers got the wrong dog back. I would have to go at least 50 km out of town to find a decent kennel. I looked dumbfounded and he went on, saying that it was midterm break and all the kennels were already completely booked anyway. And I should give him a ring and make an appointment to get Xena’s chip read.
Right. I like to get all my distress out of the way in one go, so I went in the barber shop a few doors down and asked for a haircut. When I tried this in The Hague, I was refused service. In other places, it’s lead to huge arguments about appropriate haircuts. Etc etc. Well, England wins for least stress place to get a hair cut. They asked me to clarify, but no arguments at all. It just got cut. Alas, not quite as well as my Amsterdam barber, but quite a bit cheaper. The barber was indignant about the kennel situation. “We have animal welfare laws in this country!” she said. “No kennel would treat a dog like that! They would get shut down, maybe go to jail.” Her assistant wrote down some phone numbers from the yellow pages (“Do you have yellow pages in California?”) and later I called them and the first one had space and I booked it. Yay.
So when I got back, I finished up my svn conversion and all seemed well and it was time to write the music that I had due. I approached a high profile blogger about trading a commission for a plug and a banner ad and he said ok, and then my backup thing went totally wrong and I had to email him to ask for an extra week. Yikes. So I went to work on that. There’s a sports theme, so I recorded some football sounds from the TV and then layered them in a sort of interesting manners and then went to record some processed white noise. I thought it would be nice to have it start with very strongly resonant filterings and settle out into plain noise, which could be faded down to sound like crowd noise. This is still my plan, but the nice thing about hardware synths is that you never really know how it’s going to come out. This is why it’s fun.
My synth got kind of battered in the move (always tighten your bolts before shipping!), and this was my first time turning it on and, thank gods, it worked. So I set up my patch and tried to record it and got unfiltered noise. After a lot of head scratching, it became clear that the noise source was my mixer! Arg.
I took it apart and all the solder joints looked ok. I don’t know what I thought I could fix. I put it back together and reattached it and got the sound I was expecting. Yay. So I re-attached it to the computer and got nothing but noise again. Oh. Must be a software problem.
I went to download the manual for the Ardour, which is the mixing software that I use. (It’s free, and quite nice) and noticed both that the manual section I was looking for is not yet written (alas) and there’s a new version out with many improvements for mac users. (Huzzah.) The new version, though, didn’t like my version of Jack, which is an audio library that alot of free software uses. So I got the new version of jack and installed it and it wouldn’t run at all. And it completely blew away my old version. And then, slowly, I remembered that it had taken me over a week to figure out how to custom compile it last time so it would run on my machine. And I had never gotten around to doing the write up of what I changed to get it to work. oh crap.
That’s ok. I have other software. so I fired up Audacity, (which is also free software and very nice) and all it recorded was noise. ( . . . ) I tested my mixer again and it wasn’t working.
So a flaky mixer caused me to blow away my working software.
There’s no way I can finish this project without a mixer. So I have to buy a new one. In the mean time, I’ve missed the second deadline and created a mountain of work. This is not good.
What killed the mixer? It’s not surprising that it’s given up the ghost. It’s spent a lot of time bouncing around in a backpack and I plugged the american transformer into a step-down converter, which meant it was getting 50 Hz when it expected 60. Still, it functioned for 2 years in this manner. I’ve been having odd electrical problems recently. I go to sleep and leave things on (yeah, my carbon footprint, I know) and when I wake up, they’re inexplicably off. Still plugged in and switched on, but not getting electricity. Oh my god, the first time this happened was with my laptop. The battery had drained utterly and the power brick would not spit out juice. I thought the computer had died. But, unplugging and replugging fixes it every time so far. It’s hit my other computer, my speakers, my synths, a power bar (the light-up switch was not lit up). This is really weird. In other countries I’ve lived in, a plug in the wall means either that power is flowing or a fuse or breaker has gone. It doesn’t stop and wait to be replugged.
I think I need a UPS, which is expensive enough in the US. It’s going to cost a month’s rent here. They’re very useful in a studio, though. They suppress surges, they keep voltage steady in dips and brown outs, and, best of all, they tale a lot of noise out of the power and therefore make your gear actually sound better.
And I need to buy a new mixing board, which would be much, much cheaper in New York, where I’ll be on November 1, but stupid power differences and Britian’s odd obsession with grounded, fuse-containing plugs (hm, or not so odd after all), means that I should buy one in this country. Which is probably the most expensive place in all of Europe to buy electronics gear.
So, in summary, I have to spend a fortune housing my dog, replacing my mixer and getting a UPS. If I get my dog back, she’ll be wounded and half-dead. I totally blew it in a really high profile commission. My recording/mixing machine’s software is offline for the foreseeable future. My existing hardware is in danger. I have a meeting with my supervisor on wednesday and I’ve accomplished exactly nothing. Less than nothing, because before I had the potential of accomplishing something. But, to balance things, I don’t have to fight for haircuts. So it’s not all bad. Oh, and I’m moderating the livejournal feminist group again, which could go either way.
Still no social life, but my sanity is staying much more stable that I would have expected. So that’s good too. Really, things can only get better. Unless they don’t.

Jacked

Yeah, so I can get Ardour to display on a remote machine, but forget about making sounds thus displayed. Run it in VNC and it’s fine. Run with ssh X tunneling, and there are problems. What kind of problems, you ask?

allocate_mach_clientport: can't find mach server port
Can't allocate mach port

For the longest time, I thought that error was originating within Ardour. So I downloaded the source. Man, you need a ton of libraries to compile it, several of which are either in Fink’s unstable tree or not present within fink at all. Ardour developers use Darwin Ports, I guess. Anyway, the fink version of jack absolutely does not work for me. So I was giving up on the project, when I looking at console logs revealed that the problem seemed to be coming from Jack.

Maybe if I could just discover the name of the jack sever, there exists an undocumented command line argument to pass to Ardour to tell it which server to use! Fortunately, there exists a utility for just such discovery: jack_lsp. So after learning of it’s existence, I typed in the command and got:

allocate_mach_clientport: can't find mach server port
Can't allocate mach port
jack_client_open() failed, status = 0x  

aha! So I downloaded the JackOSX code and it was a terrible mess. So I downloaded the Jackit code, but it doesn’t play well with core audio. There’s some goofy thing on mactel computers such that you have to create an aggregate device or else in and outs are treated differently. There’s probably a command line argument to fix this, but I like the nice JackOSC GUI, so I went back to their messy, messy code and started banging away at the core audio driver, since that seemed like a possible culprit.
Several hours later, after learning about some macros in C that I’d never heard of and borrowing some code form JackIt and otherwise swearing, I got a new library for coreaudio to build and link. Hooray. That was way too much effort. And I fired up the JackOSC GUI and everything worked. Yay!
And then I typed jack_lsp and got the same error again. grrrr. The code for jack_lsp is not in the Jack OSX source repository, as far as I can tell. It is included with JackIt, but it’s clearly not the problem. The problem is some Jack library that they ship out as a binary. Perhaps using my special sekrit powers, I can build the JackIT kit to use my core audio library, since it’s got a bunch of jackit code in there now anyway. Or maybe I can give up and just use VNC.
Anyway, this is why I haven’t written any music the last few days, nor implemented a very, very fast pitch tracker that just uses the samples around zero crossings.
In other news, the weird mole on my back was not cancer. Wonder why it itched so much.