No internet connection
  1. Home
  2. How to

Change sample rate of audio device

By Vladimir @poterukha
    2020-12-20 16:13:13.855Z

    Hi everyone!
    Is there is a way to change a sample rate of the particular audio device in the background (with a help of terminal command or SF Audio Engine)?
    The initial question came from @Andrey_Kireev as a part of a more complex workflow. I tried to write the script automating mouse clicks in the Audio Midi Setup app, but it is not a stable solution. Picking the UI items from the main window SF gets only raws, not the names of the audio devices. And the raw number changes when the number of devices changes.

    • 8 replies
    1. I'm not sure that this is possible from the command line.

      Would it be possible to create two different aggregate devices with different sample rates and then just switch between those?

      1. Depending on the physical device, it may also be possible to change this by talking directly to the driver of the device. We'd need some more background info on that to help.

        1. I could use this code though to change the sample rate of an audio device in the Audio MIDI Setup app:

          
          function setAudioDeviceSampleRate({ deviceName, sampleRate }) {
              var app = sf.ui.app('com.apple.audio.AudioMIDISetup');
              var rows = app.mainWindow.splitGroups.first.scrollAreas.first.children.whoseRole.is("AXOutline").whoseDescription.is('Audio Device List').first.children.whoseRole.is("AXRow");
          
              var devices = rows.allItems.map(row => ({
                  title: row.children.whoseRole.is("AXCell").first.children.whoseRole.is("AXStaticText").first.value.value,
                  row: row,
              }));
          
              var device = devices.filter(d => d.title === deviceName)[0];
              device.row.mouseClickElement();
          
              var formatBtn = app.mainWindow.splitGroups.first.tabGroups.first.popupButtons.whoseDescription.is('Format').first;
              formatBtn.popupMenuSelect({
                  menuPath: [sampleRate]
              });
          
          }
          
          setAudioDeviceSampleRate({
              deviceName: 'MacBook Air Speakers',
              sampleRate: '48.000 Hz',
          });
          
          1. Vladimir @poterukha
              2020-12-20 18:28:51.870Z

              Thanks, I will try this tomorrow!
              The full picture behind this question is about to solve the problem with Dante Virtual Sound Card. The whole studio locked to 48 kHz and it is not easy to open a session at different sample rate. The current workaround @Andrey_Kireev found out is to create a virtual device in Loopback connecting it to Dante output. Next, at Audio Midi Setup he switches Loopback SR to 96 kHz, then launches Pro Tools with Loopback as Playback Engine. Next step is to create a temp session, close it. After all, you can work in any session at 96k! I see it as a perfect example of the routine SF can help.

              1. In reply tochrscheuer:
                Vladimir @poterukha
                  2020-12-22 14:43:02.606Z

                  The script works under macOS 10.15.7 but crashes on 10.14.6.
                  Also, I added a few lines to open Audio Midi setup:

                      sf.file.open({
                          path: "/System/Applications/Utilities/Audio MIDI Setup.app",
                          applicationPath: "/System/Library/CoreServices/Finder.app",
                      });
                  
                      var app = sf.ui.app('com.apple.audio.AudioMIDISetup');
                  
                      app.appActivate();
                      //app.appWaitForActive();
                      app.children.whoseRole.is("AXWindow").whoseTitle.is('Audio Devices').first.
                          elementWaitFor();
                  
                  

                  Maybe the name of the service and UI layout is different in Mojave if Audio Midi Setup is closed it crashes here app.appActivate(); with Could not find running app with bundle id: '' (SR Select: Line 10)
                  If I open AuMIDI in advance it crashes here device.row.mouseClickElement(); with TypeError: Cannot read property 'row' of undefined (SR Select line 24)

                  1. Yea, this indicates the layout is different on Mojave.

            • D
              In reply topoterukha:
              Drew Jurecka @Drew_Jurecka
                2023-10-05 14:46:04.178Z

                Just following up on this - I'm trying to build a script based on the code you provided @chrscheuer. The script works (as long as Audio MIDI setup is already running.... I'll fix that later) as far as selecting the audio device and opening the popup menu, but then it seems uable to actually select the correct item in the drop down.
                Here's the code.

                function setAudioDeviceSampleRate({ deviceName, sampleRate }) {
                    var app = sf.ui.app('com.apple.audio.AudioMIDISetup');
                    var rows = app.mainWindow.splitGroups.first.scrollAreas.first.children.whoseRole.is("AXOutline").whoseDescription.is('Audio Device List').first.children.whoseRole.is("AXRow");
                
                    var devices = rows.allItems.map(row => ({
                        title: row.children.whoseRole.is("AXCell").first.children.whoseRole.is("AXStaticText").first.value.value,
                        row: row,
                    }));
                
                    var device = devices.filter(d => d.title === deviceName)[0];
                    device.row.mouseClickElement();
                
                    var formatBtn = app.mainWindow.splitGroups.first.tabGroups.first.popupButtons.whoseDescription.is('Format').first;
                    formatBtn.popupMenuSelect({
                        menuPath: [sampleRate]
                    });
                }
                
                setAudioDeviceSampleRate({
                    deviceName: 'Pro Tools | HDX',
                    sampleRate: '48.000 Hz',
                });
                
                1. DDrew Jurecka @Drew_Jurecka
                    2023-10-05 14:48:27.326Z

                    ok skip that. lol. I needed to replace the period in the sample rate string with a comma and now it works. Please disregard!

                    Followup though - how can I return the value of the currently selected ample rate from that dropdown menu? (I want to first check what the sample rate of 'Pro Tools | HDX' is before changing it.)
                    THanks!