Jump to content
IGNORED

Applescript to match Audio Midi and iTunes sampling frequencies and auto-restart iTunes if needed


wgscott

Recommended Posts

With a huge amount of help from other people, here is an applescript that does the following:

 

Gets the sampling frequency from the currently playing track.

 

Checks with audio midi to see if the freqency matches.

 

If it doesn't, it quits iTunes, changes the Audio MIDI Setup settings, and then re-opens iTunes to restart the same track in the same playlist.

 

Right now, you can run it manually, but ultimately I want to hook it so it does this automatically. (It doesn't re-open iTunes like it should when run from the ~/Library/iTunes/Scripts folder from within iTunes itself).

 

AFAIK, it only works with 10.6. The 10.5 Audio MIDI interface looks quite different, and I haven't had a chance to make a separate version.

 

Please feel free to tell me how to improve it.

 

Bill

 

 

 

Link to comment

Manual, from the Scripting Menu

 

 

If you save it as an application, and then put it in ~/Library/iTunes/Scripts and run it from the script interface of iTunes, it will do the right thing. I guess when it is compiled it is treated like one single command rather than a procedural script.

 

Automatic, bootstrapped with iTunes Visualizer

 

 

If you put nyquist.zsh into /Library/iTunes/etc (you need to make that directory) and the plugin into either "/Library/iTunes/iTunes Plug-ins" or the user's own "~/Library/iTunes/iTunes Plug-ins" and select "iTunesPlugin" from the visualizer menu item in iTunes, then it appears to work (at least in my hands). You also have to "Enable Access for Assistive Devices" in the Universal Access preference pane so that the GUI scripting of Audio MIDI Setup will work.

 

If you like to turn on the music, drop acid, and watch your favorite visualizer, this probably isn't for you. Sorry.

 

Link to comment

That is what the visualizer plug-in does.

 

Give it a try:

 

0. Quit iTunes

 

1. Download http://www.computeraudiophile.com/files/Library.zip to (for example) your Desktop

 

2. Copy the file ~/Desktop/Library/iTunes/iTunes Plug-ins/NyquistHook.bundle into either /Library/iTunes/iTunes Plug-ins or /Users/yourname/Library/iTunes/iTunes Plug-ins

 

3. Copy the directory ~/Desktop/Library/iTunes/etc into /Library/iTunes (not your home directory one). The /Library/iTunes/etc directory contains the actual script file. There is nothing evil in it that I put there, but you should look at it with a text editor as a precaution.

 

4. Open iTunes and set the visualizer to use "iTunesPlugIn".

 

 

 

Link to comment

I did. It opens the Audio MIDI panel at the start of tracks but then nothing happens, it doesn't change the Format menu, it doesn't close/open iTunes either. On the latest OS X / iTunes version.

 

? MBP ? M2Tech hiFace ? Heed Q-PSU/Dactilus 2 ? Heed CanAmp ? Sennheiser HD650

Link to comment

Sounds like the GUI scripting is failing.

 

Just to double-check, here is how it should work:

 

1. You open iTunes manually, select a track manually, and hit the play button manually.

 

2. The script then activates, opens Audio MIDI, and checks to see if the sample frequencies match. If they do, it will do nothing more until the next track comes along, except to "hide" the Audio MIDI window.

 

3. If there is a mismatch (say 441000 Hz in Audio MIDI and the track is 48000 Hz), it should pause iTunes, reset Audio MIDI, quit iTunes, restart iTunes, pause for 5 seconds, and then restart the current track and playlist, playing the track from the beginning.

 

Does you Audio MIDI window look like this (all buttons, toggle boxes, etc, in the same location)?

 

junkmidi.png

 

 

Link to comment

As I use the hiFace interface the device list is different obviously. So I guess there are no Applescript hooks into the Audio MIDI panel, one has to manipulate the menus/buttons directly? That's a bumpy road...

 

? MBP ? M2Tech hiFace ? Heed Q-PSU/Dactilus 2 ? Heed CanAmp ? Sennheiser HD650

Link to comment

Yeah, GUI Scripting is not a robust solution.

 

The code for doing this properly is publicly available (Play does this and is GPL). The only problem is I can't program my way out of a wet paper bag. But as soon as I figure it out, I'll implement it.

 

So the way this can be done properly is to use the iTunes plugin SDK to embed code to do the automatic switching, start and stop iTunes, and interact with CoreAudio, thus bypassing Audio MIDI entirely.

 

The way it stands currently, I hacked into the iTunes plugin SDK example code to call the shell script, which in turn has embedded the GUI scripting. It is a proof of principle, and it works on my machine.

 

If you are willing to play around, please take a snapshot of the Audio MIDI window for the hiface, and I'll see if there is a simple way to adjust the script...

 

Also, if you download a free trial version of UIBrowser, http://pfiddlesoft.com/uibrowser/ it makes it easier to figure out the path to the toggle box.

 

Here is a screen shot of how I found the path to my toggle box:

 

 

 

Link to comment

That "shouldn't" matter but maybe it does.

 

One other thing I noticed is that you have 44100,0 and I have 44100.0, which I am assuming is the difference between US/UK English and maybe other European numerology. This is a bit of a guess, but I am wondering if that comma is messing it up? I'll try to figure out how to get the Applescript to cope with it.

 

edit: here is how one can remove the ,0 from (eg) 41000,0 in Applescript:

 

 

set testVar to "44000,0"

set truncVar to text 1 thru -3 of testVar

 

 

This returns the integer 44000.

 

Meanwhile, if you would be willing to paste the contents of the actual Applescript into Apple Script Editor and run it from there, it might give better diagnostics...

 

 

EDIT:

 

Ok, here is the fix.

 

Change the line

 

set currentRate to (first word of (get value of comboBox)) as number

 

to this

 

set currentRate to (text 1 thru -3 of first word of (get value of comboBox)) as number

 

and

 

set anItem to (first word of anItem) as number

 

to

 

set anItem to (text 1 thru -3 of first word of anItem) as number

 

Here is a link to the revision:

 

http://www.computeraudiophile.com/files/Nyquist_0.0.3.zip

 

I hope it works for you.

 

 

 

Link to comment

Downloaded the rev., first the same thing happened (stopped after opening the A/M panel). That was the error message on the console:

 

2010.09.21. 11:31:17 [0x0-0x46e46e].com.apple.iTunes[6595] execution error: System Events got an error: Access for assistive devices is disabled. (-1728)

 

So I turned on 'Enable access for assistive devices' on the UA panel, seems to be mandatory. Now the Thing closed iTunes, opened the A/M panel, set the sample rate correctly, but left it open. iTunes stalled or waited on restart, unleashed some nasty error message about locked library (attached). I closed and reopened it manually and it was fine.

 

Now the best part: for the next try and since then it's working! So I personally guarantee the demigod status for you. I'm sure others who suffered in the iTunes restart purgatory will join me and praise you! :)

 

? MBP ? M2Tech hiFace ? Heed Q-PSU/Dactilus 2 ? Heed CanAmp ? Sennheiser HD650

Link to comment

- In playlist's grid view if one wants to play an album ('Play Album') this Thing sometimes honors the end of the album and stops, sometimes continues to the next album.

- Even if the track to be started has the same sample rate as the previous one, the Thing checks the Audio MIDI panel. I guess it would take some temporary place to hide the sample rate of the last track to compare with the starting one, making the A/M check unnecessary during e.g. an album playing.

- The Thing closes only the window of the Audio MIDI panel, the application remains open. So if one switches to another Space the A/M check will switch back to the Space the A/M panel opened the first time on. (This can be neutralised by assigning the A/M Setup to 'Every Space'.)

- Rarely that 'library locked' error occurs and iTunes must be restarted manually. Couldn't find a pattern yet.

 

? MBP ? M2Tech hiFace ? Heed Q-PSU/Dactilus 2 ? Heed CanAmp ? Sennheiser HD650

Link to comment

That library locked problem may be related to this console error:

 

2010.09.21. 14:40:24 [0x0-0x546546].com.apple.iTunes 1873:1881:

2010.09.21. 14:40:24 [0x0-0x4da4da].com.apple.iTunes execution error: iTunes got an error: Connection is invalid. (-609)

 

...and it seems to occur when I manually switch to another album/track.

 

? MBP ? M2Tech hiFace ? Heed Q-PSU/Dactilus 2 ? Heed CanAmp ? Sennheiser HD650

Link to comment

First, I cannot accept anything other than demi-wit status; 90% of this is the work and suggestion of others, cobbled together in an unholy alliance. (I did figure out the comma decimal thing all by myself -- what a mess.)

 

The iTunes error sounds like the one I got once, and I put in a "delay" command in the script, I think 5 seconds. Then it went away. Maybe yours needs more time (different, probably bigger library, different external drive responsiveness, etc).

 

One of the reasons I have it hook a shell script is so that it can be easily edited and modified. If you are willing to experiment and get it to work, I would be very happy to incorporate your fixes.

 

Really, this should be implemented as objC code within an iTunes visualizer plug-in, and audio midi should be bypassed with direct communication to core audio. I just simply lack the skills to do it. I mean it when I say I can't program out of a wet paper bag.

 

 

 

Link to comment

I got this a couple of times yesterday. I made a couple of very subtle changes in the script that seem to make a difference, but I have trouble reproducing the error reliably. Anyway, here is what I now have:

 

 

tell application "iTunes" to quit

delay 25 -- iTunes reopens automatically (why?), but we need to give it a chance to get going

tell application "iTunes"

launch

activate

set thePlaylist to some playlist whose persistent ID is playlistID

play (some track of thePlaylist whose persistent ID is trackID)

-- start replaying the same track from the same playlist

end tell

 

 

Putting tell application "iTunes" to quit

all one one line seems to cause this to behave the way I want it to. In other words iTunes quits, then there is a delay (I put in 25 seconds just to debug it -- five seconds should be adequate). Then I put in the launch command. I am not sure which of these ensures that iTunes doesn't open prematurely, but now it seems to wait for the library to catch up. I think what happens is that when iTunes closes, it dumps everything to an xml file, and depending on your external hard drive and library size, this can take awhile.

 

I'll put the latest changes here.

 

Link to comment

I have a friend who can do this. I am kind of working up the courage to ask. AFAIK the visualizer is the most straightforward way to do it. I found out it still works even if you pick a different visualizer, which seems a bit odd, but I guess it eliminates the objection that you can't use your favorite visualizer if you do this.

 

The stuff that is apple-scripted is very easy, even for an idiot like me. You can use something called the Applescript-Objective C bridge. But I need to get advice from someone who understands this stuff better to code the direct interactions with coreaudio that enable bypassing audio midi. It is probably easy, it is just I don't know what i am doing...

 

Can you test to see if the new version avoids the library lock problem?

 

Link to comment

Actually it got worse :) Now it gets locked for every sample rate change despite the bigger delay. Don't ask me why... Btw, I have 13000 tracks in the library, I guess it won't help for the restarts.

 

? MBP ? M2Tech hiFace ? Heed Q-PSU/Dactilus 2 ? Heed CanAmp ? Sennheiser HD650

Link to comment

I have a suggestion for you guys.

 

Instead of trying to make iTunes do what you want it to - which, as you have found, is a challenge - why don't you apply your AppleScript skills to let you use iTunes as a library manager, with some other application as the playback engine? Both iTunes and Play can be controlled through AppleScript.

 

Link to comment

Oh, f-- me! I am so glad i don't write software for a living.

 

There is a nuclear option: Write-protect the XML file, so iTunes can't write to it, and then it will just store everything in binary and close and open quickly.

 

But I should get this part right, because even a properly encoded audio midi workaround won't address this issue.

 

Link to comment

In other words, make an Applescript that quits (or at least pauses) iTunes and starts the track in Play?

 

Play 2.0 is on its way. Hopefully it will show album art. If it had decent visual cues, it would be perfect for those of us who can't read.

 

Or, here is a sick idea: Use Play itself as a means to change audio midi, if it can be apple-scripted to do so.

 

Link to comment

William, you are not supposed to nest tell blocks directed to iTunes within tell blocks directed to System Events. Only GUI commands should be within the latter.

 

I suspect the unexplained launching of iTunes after the quit command is caused by your putting commands directed to iTunes inside of "System Events" tell blocks.

 

See the attached AppleScript in which I attempted to separate the iTunes and System Events tell blocks.

 

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

"In other words, make an Applescript that quits (or at least pauses) iTunes and starts the track in Play?"

 

Yeah - why not? It's really not very hard to do.

 

The only real trick might be that there is no option within Play to not save the existing playlist, so any tracks that you add from iTunes would be appended to the end of the existing list. That might not be desirable. But, you could delete the Play playlist as part of the AppleScript before adding the new tracks so it all could be pretty much transparent.

 

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...