Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applescript to set rate #128

Open
wants to merge 2 commits into
base: improvements-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Quality.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
1293436B28131591002E19A8 /* CurrentUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1293436A28131591002E19A8 /* CurrentUser.swift */; };
12AFF5C12811AD40001CC6ED /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AFF5C02811AD40001CC6ED /* AppDelegate.swift */; };
12F1AA572868639A006C1AD8 /* DeviceMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12F1AA562868639A006C1AD8 /* DeviceMenuItem.swift */; };
A7402CE52B672F920023878E /* ScriptableApplicationCommandSetRate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7402CE42B672F920023878E /* ScriptableApplicationCommandSetRate.swift */; };
BF0C90C82B20BFD6002F99C9 /* LosslessSwitcher.sdef in Resources */ = {isa = PBXBuildFile; fileRef = BF0C90C72B20BFD6002F99C9 /* LosslessSwitcher.sdef */; };
BF0C90CA2B20C163002F99C9 /* ScriptableApplicationCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF0C90C92B20C163002F99C9 /* ScriptableApplicationCommand.swift */; };
BF7E0D09296336DA009FFEEC /* AudioStreamBasicDescription+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF7E0D08296336DA009FFEEC /* AudioStreamBasicDescription+Equatable.swift */; };
Expand All @@ -49,6 +50,7 @@
1293436A28131591002E19A8 /* CurrentUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentUser.swift; sourceTree = "<group>"; };
12AFF5C02811AD40001CC6ED /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
12F1AA562868639A006C1AD8 /* DeviceMenuItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceMenuItem.swift; sourceTree = "<group>"; };
A7402CE42B672F920023878E /* ScriptableApplicationCommandSetRate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScriptableApplicationCommandSetRate.swift; sourceTree = "<group>"; };
BF0C90C72B20BFD6002F99C9 /* LosslessSwitcher.sdef */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = LosslessSwitcher.sdef; sourceTree = "<group>"; };
BF0C90C92B20C163002F99C9 /* ScriptableApplicationCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptableApplicationCommand.swift; sourceTree = "<group>"; };
BF7E0D08296336DA009FFEEC /* AudioStreamBasicDescription+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AudioStreamBasicDescription+Equatable.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -97,6 +99,7 @@
1272AA96280DBB4900FD72BA /* Quality */ = {
isa = PBXGroup;
children = (
A7402CE42B672F920023878E /* ScriptableApplicationCommandSetRate.swift */,
1254A79D2814024300241107 /* Info.plist */,
12AFF5C02811AD40001CC6ED /* AppDelegate.swift */,
1272AA97280DBB4900FD72BA /* QualityApp.swift */,
Expand Down Expand Up @@ -222,6 +225,7 @@
1272AA9A280DBB4900FD72BA /* ContentView.swift in Sources */,
1293436B28131591002E19A8 /* CurrentUser.swift in Sources */,
1272AA98280DBB4900FD72BA /* QualityApp.swift in Sources */,
A7402CE52B672F920023878E /* ScriptableApplicationCommandSetRate.swift in Sources */,
127C972D281FCF000087313B /* AppVersion.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
9 changes: 9 additions & 0 deletions Quality/LosslessSwitcher.sdef
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@
<cocoa class="LosslessSwitcher.ScriptableApplicationCommand"/>
<result type="integer" description="sample rate"/>
</command>

<command name="setsamplerate" code="setlrate" description="set the current device sample rate" access="w" >
<cocoa type="text" key="samplerate" class="LosslessSwitcher.ScriptableApplicationCommandSetRate"/>
<parameter name="rate" code="setr" type="text" description="desired rate">
<cocoa key="newrate"/>
</parameter>
<result type="integer" description="Value or negative error code"/>
</command>

</suite>
</dictionary>
50 changes: 50 additions & 0 deletions Quality/ScriptableApplicationCommandSetRate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// ScriptableApplicationCommand.swift
// LosslessSwitcher
//
// Created by Vincent Neo on 6/12/23.
//

import Cocoa

class ScriptableApplicationCommandSetRate: NSScriptCommand {

override func performDefaultImplementation() -> Any? {
guard let delegate = AppDelegate.instance else {
return -1000
}

// print( "in" )
let newrate = self.evaluatedArguments!["newrate"] as! String

let newratenum = Float64(newrate)

print( "newrate " + newrate )

if ( newrate == "" || newratenum != nil ) {

let od = delegate.outputDevices!
if let outdev = od.selectedOutputDevice ?? od.defaultOutputDevice {

print("setOutputDeviceRate -- \(newratenum)" )

let retval = outdev.setNominalSampleRate( Float64(newratenum!) )

if retval != true {
return -2000
}

od.updateSampleRate( newratenum! )
return newratenum

} else {
return -3000
}

} else {
return -4000
}

}

}
59 changes: 59 additions & 0 deletions hqplayer_AMHD_log_2_hqp5.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
#
HQPHOSTDEFAULT="localhost"
HQPHOST="${1:-$HQPHOSTDEFAULT}"
#
defsamlerateapp="LosslessSwitcher"
samlerateapp="${2:-$defsamlerateapp}"
#
AMHDlog="/Users/$USER/Library/Application Support/Amazon Music/Logs/AmazonMusic.log"
GREPfor="Audio Attributes updated"
#
tailcmd="tail -f \"$AMHDlog\" | grep 'Audio Attributes updated'"
#
echo "$0 starts for $HQPHOST -- $tailcmd"
#
#
tail -f "$AMHDlog" | grep --line-buffered "$GREPfor" |
while IFS= read -r LINE0
#while read LINE0
do
newrate=""
#
if echo "$LINE0" | grep -q -E "Audio Quality: HD44"; then
newrate="44100"
elif echo "$LINE0" | grep -q -E "Audio Quality: UHD44"; then
newrate="44100"
elif echo "$LINE0" | grep -q -E "Audio Quality: HD48"; then
newrate="48000"
elif echo "$LINE0" | grep -q -E "Audio Quality: UHD48"; then
newrate="48000"
elif echo "$LINE0" | grep -q -E "Audio Quality: UHD88"; then
newrate="88000"
elif echo "$LINE0" | grep -q -E "Audio Quality: UHD96"; then
newrate="96000"
elif echo "$LINE0" | grep -q -E "Audio Quality: UHD192"; then
newrate="192000"
else
newrate=""
fi
#
if [[ $newrate != "" ]]
then
echo "changing $HQPHOST to $newrate"
/Applications/hqp5-control.app/Contents/MacOS/hqp5-control $HQPHOST --set-transport-rate audio:default/$newrate/2

echo "changing $samlerateapp to $newrate"
osascript -e "tell application \"$samlerateapp\" to setsamplerate rate $newrate"ls


echo "changed to $newrate"

fi
#
#done < <( $tailcmd )
done
#
echo "$0 done"
#