Jump to content

Applescript geekout: auto sample rate switching and afplay


Recommended Posts

Here is an Applescript that takes the tracks or album, playlist or whatever that you SELECT in iTunes, and then automatically resets Audio MIDI Setup to match the sample frequency, and then plays the track using /usr/bin/afplay.


afplay -q 1 should play the track in "full quality" as a 64-bit-enabled player.


I would be interested to know if anyone can hear a difference (for better or worse) vs. iTunes.




You can just put this thing into iTunes's script folder and use it from there. Uncomment the penultimate line if you want the Audio MIDI window to be hidden from view.


Link to comment

How do you stop afplay before a track is finished?


I assume there is no way to pause and resume.


My guess is that if you're playing a long track, the command "do shell script "afplay ..." will time out with an error before it finishes. I'm not saying that afplay itself will time out or return an error, but that AppleScript probably will time out waiting for afplay to complete.


HQPlayer (on 3.8 GHz 8-core i7 iMac 2020) > NAA (on 2012 Mac Mini i7) > RME ADI-2 v2 > Benchmark AHB-2 > Thiel 3.7

Link to comment

killall afplay


is the only way I have to stop it so far.


I also have a shell script version that would avoid a potential applescript timeout issue, should it exist. I'm 30 min into "Thick as a Brick" (43 min) so far...


What I would really like to know is if afplay sounds any better or worse.


Apple provides the source code for AFplay, so I thought it might be a good place to start to write an open-source player that can use the iTunes interface. All the Applescript-type commands can be replaced with the Cocoa bridge.


Link to comment

I couldn't find anything about it, and two blog posts certainly don't "discuss it to death."



If it is as good as iTunes (with the -q 1 flag), that would really be quite remarkable, because in essence it means it is very simple to "patch" the sample rate change irritation in iTunes.


On my equipment, and with my ears, I struggle to hear a difference between it, play, ayreware, iTunes, etc. That is why I asked.


Link to comment

I didn't link to all the posts of the day, but when afplay and afconvert first came out, it seemed like every online source of Mac information that could spell "terminal" or "script" had loads of suggestions about using it.


Sorry if you've already covered this, but if you can't hear any difference between the various playback applications (which is far more common than people hereabouts like to admit to), then why are you concerned about adjusting the sample rate? I'd just set Audio MIDI Setup to 96 KHz and be done with it.


Link to comment

I struggle to hear the difference. I find it very subtle, and I think it is safe to say that my equipment is at the low end of the spectrum for people here (definitely in terms of cost, most likely in terms of absolute performance as well. I had to start somewhere.). I can hear when there is a sample rate mismatch, but it takes the form of an occasional distortion, so I am not sure I am hearing differences that other people are. Simply put, I am too new to this to make these kinds of judgements with any degree of confidence.


Until about April 2010, I didn't know the difference between a lossy and lossless file, and was only dimly aware of alternatives to iTunes, so I hadn't seen anything about afplay the first time around. I don't know anything about it. Is it bit-perfect when played with the -q 1 flag, for example? Are their any known, obvious flaws that make it inferior?


What would be ideal is if iTunes allowed you to plug in any audio player engine you want, so I could use the iTunes interface, and plug in a player like afplay, Ayreware, or whatever.


Also, I am having trouble getting Ayrware to change the sample rate (and I definitely can hear the difference with that), so I wanted to see whether or not it was just because of my equipment. My klunky gui script does have the small merit of always working, and I like not having to shut down iTunes. The lack of ability to control afplay is its biggest limitation, but since it is open-source, I might try hacking on it at some point.


If I search afplay here, except for the very last entry, it is just a listing of my own (unanswered) questions about it.


Link to comment

Actually, and this is truly only my opinion, unless they are using real basic very low budget equipment, or equipment that is incompetent to begin with - and you are not - people who are sensitive to these details and differences can usually hear them regardless of the rig. This is largely a learned skill and may be something that is learned at a very young age. There is a bunch of research that indicates this to be true.


So, if you or anybody can't hear the difference or it just plain doesn't matter when it comes to listening to music, why worry about it?


Beyond that, perhaps part of the problem with iTunes from the engineering side is that it is so complicated and has such a complex user interface. At minimum, that adds loads of additional processing activity to the overall system. That can indeed cause noise and other artifacts that don't affect the bit perfection of the data stream as sent from one box to another. But, since this indeed a system there are second and higher order considerations (like that electrical noise) that can potentially be audible. Whether or not that is a concern for any individual is up to them. In many cases, there may be preferences for certain imperfections or trade-offs that one system may offer over another for any individual. Just as people have different preferences in music, which we call tastes, their auditory processing systems likely have different preferences for what they find to be most realistic or enjoyable.


Anyway, I apologize for going on about that. I just hate to see people having less fun or getting less satisfaction in listening to music while they are off worrying about things that may not actually be of true consequence to them.


Link to comment

I actually like tinkering with this stuff, and at some point when I am not spending money faster than I earn it, will probably tinker with hardware more too. The glass cable really did make a difference, and having cleaner mains power is an audible improvement, so I am beginning to see how things can be improved incrementally.


The sample rate switching thing is a challenge rather than an irritation. I taught myself shell scripting a few years ago, and with that a small amount of apple-scripting, and I like finding challenges this might address.


I just had an idea for using a plug-in to pause a track when it starts in iTunes, change the sample rate, play the track with afplay, then at the end of the track, advance to the next one, check/change sample rate, play in afplay, etc., which is probably as close to a drop-in replacement for the iTunes engine as is reasonably possible. iTunes is actually a good interface for the most part, but as a player is clearly sub-optimal. I was glad to see the AyreWare interface was designed to take advantage of iTunes in this way. I'll certainly buy it when it is ready.


Link to comment

OK, I figured out how to get reasonable "pause" functionality, ffwd, rewind, etc. via the iTunes interface, running this as a shell script from a visualizer plug-in. Basically, it all runs reasonably seamlessly within iTunes.


You can download everything here: Link for Library contents.


There are three components:


1. The plug-in bundle, which goes into /Library/iTunes/iTunes Plug-ins


A zipped version of this hacked visualizer SDK example bundle is here: NyquistHook.bundle.zip


2. The shell script, which goes into /Library/iTunes/etc and is called nyquist.zsh.


3. A toggle script, to turn it on and off, which goes into /Library/iTunes/Scripts and is called ToggleNyquist.scpt.


So you open iTunes, and hit the play button, and it runs the script once for each track, changing the sample rate as necessary, and then playing it with afplay.


In essence, it permits you to use iTunes in close to the usual way without the annoyance of manually or automatically closing it, resetting Audio MIDI, and reopening it, every time you want to play something with a new sample frequency. The main limitations are that pause and resume now resets it to the beginning of the tract (which is not what iTunes does) and there is no progress bar, etc. It seems to work fine with remote control of iTunes.


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...