如何直接使用PipeWire设置音频设备配置文件和路由?

如何直接使用PipeWire设置音频设备配置文件和路由?

据我所知,您可以使用 为音频设备选择配置文件pactl set-card-profile CARD PROFILE。但它pactl是一个 PulseAudio 实用程序,这意味着它添加了不必要的层,当然它需要您在某种程度上安装 PulseAudio。

我想知道使用 PipeWire 设置音频设备配置文件的本机方法是什么。

我发现了一个名为 PipeWire 的实用程序pw-cli,它似乎可能是我所需要的,但我不清楚如何正确使用它。 pw-cli [选项] [命令] -h, --help 显示此帮助 --version 显示版本 -d, --daemon 作为守护进程启动 (默认 false) -r, --remote 远程守护进程名称

Available commands:
    help                    Show this help
    load-module             Load a module. <module-name> [<module-arguments>]
    unload-module           Unload a module. <module-var>
    connect                 Connect to a remote. [<remote-name>]
    disconnect              Disconnect from a remote. [<remote-var>]
    list-remotes            List connected remotes.
    switch-remote           Switch between current remotes. [<remote-var>]
    list-objects            List objects or current remote. [<interface>]
    info                    Get info about an object. <object-id>|all
    create-device           Create a device from a factory. <factory-name> [<properties>]
    create-node             Create a node from a factory. <factory-name> [<properties>]
    destroy                 Destroy a global object. <object-id>
    create-link             Create a link between nodes. <node-id> <port-id> <node-id> <port-id> [<properties>]
    export-node             Export a local node to the current remote. <node-id> [remote-var]
    enum-params             Enumerate params of an object <object-id> <param-id>
    set-param               Set param of an object <object-id> <param-id> <param-json>
    permissions             Set permissions for a client <client-id> <object> <permission>
    get-permissions         Get permissions of a client <client-id>
    send-command            Send a command <object-id>
    dump                    Dump objects in ways that are cleaner for humans to understand [short|deep|resolve|notype] [-sdrt] [all|Core|Module|Device|Node|Port|Factory|Client|Link|Session|Endpoint|EndpointStream|<id>]
    quit                    Quit

我可以调用enum-params并且Route没有Profile任何错误,但输出非常神秘且难以解析。但这两个命令都提供了预期的输出:

pw-cli enum-params 45 Route
pw-cli enum-params 45 Profile

对于检索数据pw-dump似乎更有意义,因为它的输出格式是 JSON。我编写了一堆辅助函数 ( get_audio_devices,,,,,,,,, )来获取我需要的所有信息get_object_by_id,以便将所需的数据传递get_device_by_name给.然后我又编写了两个函数(和),它们环绕并使其使用起来更加合理。get_active_profilesget_active_routesget_all_profilesget_all_routesget_nodes_for_device_by_idpw-cli set-paramset_profileset_routepw-cli

这些是我的职能:

#!/bin/bash

# Function: get_audio_devices
# Description: Get all audio devices as a JSON array of objects
function get_audio_devices() {
    pw-dump | jq -r '.[] | select(.type == "PipeWire:Interface:Device" and .info.props."media.class" == "Audio/Device")'
}

# Function: get_object_by_id
# Description: Get a object by object ID as a JSON object
# Parameters:
#   - $1: The object ID of the device, node, module, factory, port, link or whatever
function get_object_by_id() {
    local object_id="$1"
    pw-dump | jq -r ".[] | select(.id == $object_id)"
}

# Function: get_device_by_name
# Description: Get a device by name as a JSON object
# Parameters:
#   - $1: The name of the device
function get_device_by_name() {
    local device_name="$1"
    pw-dump | jq -r ".[] | select(.info.props.\"device.name\" == \"$device_name\")"
}

# Function: get_active_profiles
# Description: Get active profiles for a device by object ID as a JSON array of objects
# Parameters:
#   - $1: The object ID of the device
function get_active_profiles() {
    local device_object_id="$1"
    pw-dump | jq -r ".[] | select(.id == $device_object_id) | .info.params.Profile"
}

# Function: get_active_routes
# Description: Get active routes for all nodes of the active profile(s) of a device by object ID as a JSON array of objects
# Parameters:
#   - $1: The object ID of the device
function get_active_routes() {
    local device_object_id="$1"
    pw-dump | jq -r ".[] | select(.id == $device_object_id) | .info.params.Route"
}

# Function: get_all_profiles
# Description: Get all profiles for a device by object ID as a JSON array of objects
# Parameters:
#   - $1: The object ID of the device
function get_all_profiles() {
    local device_object_id="$1"
    pw-dump | jq -r ".[] | select(.id == $device_object_id) | .info.params.EnumProfile"
}

# Function: get_all_routes
# Description: Get all routes for all nodes of a device by object ID as a JSON array of objects
# Parameters:
#   - $1: The object ID of the device
function get_all_routes() {
    local device_object_id="$1"
    pw-dump | jq -r ".[] | select(.id == $device_object_id) | .info.params.EnumRoute"
}

# Function: get_nodes_for_device_by_id
# Description: Get all nodes for a device by object ID as a JSON array of objects
# Parameters:
#   - $1: The object ID of the device
function get_nodes_for_device_by_id() {
    local device_object_id="$1"
    pw-dump | jq -r --argjson dev_id "$device_object_id" '[.[] | select(.type == "PipeWire:Interface:Node" and .info.props."device.id" == $dev_id)]'
    #pw-dump | jq -r '[.[] | select(.type == "PipeWire:Interface:Node" and .info.props."device.id" == "'"$device_object_id"'")]'
}

# Function: set_route
# Description: Set route for a given node by object ID
# Parameters:
#   - $1: The object ID of the node
#   - $2: The index of the new route
function set_route() {
    local node_object_id="$1"
    local route_index="$2"

    local node=$(get_object_by_id $node_object_id)
    local device_object_id=$(echo "$node" | jq -r '.info.props["device.id"]')

    # Get available route "templates" for the device's active profile(s)
    routes=$(get_all_routes $device_object_id)

    # Get the first route template (edit: using the fourth because the first 3 are for input rather than output)
    first_route_template=$(echo "$routes" | jq '.[3]')

    # Create a Routes entry for the given node based on the given template and save as new JSON object
    route_to_set=$(echo "$first_route_template")

    # Set the "route.hw-mute" property to false beacuase I have no clu how to find out the right value
    route_to_set=$(echo "$route_to_set" | jq '.info += ["route.hw-mute", "true"]')

    # Set the "route.hw-volume" property to false beacuase I have no clu how to find out the right value
    route_to_set=$(echo "$route_to_set" | jq '.info += ["route.hw-volume", "true"]')

    # Calculate the length of the "info" array and set the first element accordingly
    info_length=$(echo "$route_to_set" | jq '.info | length')
    route_to_set=$(echo "$route_to_set" | jq ".info[0] = ($info_length - 1) / 2")

    # Get the index of the node of which we want to change the route
    node_index=$(echo "$node" | jq -r '.info.props["card.profile.device"]')

    # Set device property
    route_to_set=$(echo "$route_to_set" | jq ". + { \"device\": $node_index }")

    # Gather values for the properties of the "props" section 
    mute=$(echo "$node" | jq -r '.info.params.Props[0].mute')
    channel_volumes=$(echo "$node" | jq -r '.info.params.Props[0].channelVolumes')
    volume_base=$(echo "$node" | jq -r '.info.params.Props[0].volume')
    volume_step=0.000015 # No clue how to get the correct value
    channel_map=$(echo "$node" | jq -r '.info.params.Props[0].channelMap')
    soft_volumes=$(echo "$node" | jq -r '.info.params.Props[0].softVolumes')
    latency_offset=$(echo "$node" | jq -r '.info.params.Props[1].latencyOffsetNsec')

    # Set the properties in the "props" section
    route_to_set=$(echo "$route_to_set" | jq "
        .props += {
            \"mute\": $mute,
            \"channelVolumes\": $channel_volumes,
            \"volumeBase\": $volume_base,
            \"volumeStep\": $volume_step,
            \"channelMap\": $channel_map,
            \"softVolumes\": $soft_volumes,
            \"latencyOffsetNsec\": $latency_offset
        }"
    )

    # Get active profile
    profiles=$(get_active_profiles $device_object_id)
    first_active_profile=$(echo "$profiles" | jq '.[0]')

    # Get profile index
    first_active_profile_index=$(echo "$first_active_profile" | jq -r '.index')
    route_to_set=$(echo "$route_to_set" | jq ". + { \"profile\": $first_active_profile_index }")

    # Add the "save" property to the "route_to_set" object
    route_to_set=$(echo "$route_to_set" | jq '. + { "save": false }')

    # Get active routes
    old_active_routes="$(get_active_routes $device_object_id)"

    # Get route index
    route_index=$(echo "$old_active_routes" | jq -r "map(.device == $node_index) | index(true)")

    # Create a new routes array where the route we want to set replaces the old one
    updated_active_routes=$(echo "$old_active_routes" | jq ".[$route_index] = $route_to_set")

    # Check diff between old and new routes array
    #file1=/tmp/updated_active_routes && file2=/tmp/old_active_routes && echo "$updated_active_routes" > "$file1" && echo "$old_active_routes" > "$file2" && meld "$file1" "$file2" ; rm "$file1" "$file2"
    
    # Set the updated routes
    pw-cli set-param $device_object_id Route "$updated_active_routes"
}

# Function: set_profile
# Description: Set route for a given device by object ID
# Parameters:
#   - $1: The object ID of the device
#   - $2: The index of the new profile
function set_profile() {
    local device_object_id="$1"
    local profile_index="$2"

    # Get available profile "templates" for device with object ID 78
    profiles="$(get_all_profiles $device_object_id)"

    # Get desired profile template
    profile_template=$(echo "$profiles" | jq ".[] | select(.index == $PROFILE_INDEX)")

    # Add the "save" property and save as new JSON object
    profile_to_set=$(echo "$profile_template" | jq '. + { "save": false }')

    # Set the new profile(s)
    pw-cli set-param $device_object_id Profile "[ $profile_to_set ]"
}

# Example usage:
# get_audio_devices
# get_object_by_id 78
# get_device_by_name alsa_card.pci-0000_00_1f.3
# get_active_profiles 78
# get_active_routes 78
# get_all_profiles 78
# get_all_routes 78
# get_nodes_for_device_by_id 78
# set_profile 78 1
# set_route 45 0

但其中涉及一些猜测工作。我一直在尝试使用 set_profile 和 set_routes 但没有成功。
我尝试更改个人资料:

#####################################################
# Attempt to change the profile                     #
#####################################################

# My audio device
DEVICE_NAME="alsa_card.pci-0000_00_1f.3"

# Index of the profile I want to use
PROFILE_INDEX=1

# Get device id
device_object_id=$(get_device_by_name "$DEVICE_NAME" | jq -r '.id')

# Set the desired profile
set_profile $device_object_id $PROFILE_INDEX
# Doesn't work and results in:
#    Array: child.size 2, child.type Spa:String
#    String "{"
#    String ""
#    String ""
#    String ""
#    String ""
#    String ""
#    String ""
#    String ""
#####################################################

我尝试改变路线:

#####################################################
# Attempt to change the route                       #
#####################################################

# My audio device
DEVICE_NAME="alsa_card.pci-0000_00_1f.3"

# Get device id
device_object_id=$(get_device_by_name "$DEVICE_NAME" | jq -r '.id')

# Get all nodes for the device
nodes=$(get_nodes_for_device_by_id $device_object_id)

# Error if there are no nodes
if [ "$nodes" == "[]" ]; then
    echo "There are no nodes for the active profile(s) of device with name '$DEVICE_NAME' (object ID: $device_object_id)" >&2
    exit 1
fi

# Get first sink node of current profile
first_sink_node_object_id=$(echo "$nodes" | jq '.[] | select(.info.props."media.class" == "Audio/Sink")'| jq -r '.id')

# Get first route output route (it's not guaranteed that this route is available for that specific node and profile)
first_output_route_index=$(get_all_routes $device_object_id | jq '.[] | select(.direction == "Output") | .index' | head -n 1)

# Set the route
set_route $first_sink_node_object_id $first_output_route_index
# Doesn't work and results in:
#    Array: child.size 2, child.type Spa:String
#    String "{"
#    String ""
#    String ""
#    String ""
#    String ""
#    String ""
#    String ""
#    String ""
#####################################################

现在让我详细说明我一直在努力做的事情。的签名set-params如下:

pw-cli set-param <object-id> <param-id> <param-json>

分别是object-id设备 ID和。param-idRouteProfile

我的第一个问题是,您不能只传递要设置的路由/配置文件的 id 或索引,而是必须传递整个 JSON 对象,并且它们的外观并不明显。

似乎每个设备都有一组可能的配置文件和路由。它们可以分别作为对象数组在.info.params.EnumProfile和中找到.info.params.EnumRoute。据我所知,这些对象是模板。活动的配置文件和路线分别存储在.info.params.Profile和中.info.params.Route。乍一看,它们与模板相同,但它们具有附加属性。中的每个对象.info.params.Profile都有一个附加属性“save”,它是一个可以是 true 或 false 的布尔值。对于.info.params.Route数组来说,更糟糕的是,它有附加属性 device、profile 和 save,并且在其名为 的属性之一中还有附加项,info并且有一个新属性“props”,它是一个具有属性mutechannelVolumesvolumeBasevolumeStepchannelMap和的softVolumes对象latencyOffsetNsec。我不知道从哪里可以获得这些属性的正确值。似乎其中一些属性可以从关联的(接收器/源)节点(例如channelMapsoftVolumes)检索,但对于某些属性,我只是猜测应该设置什么值。

另一个问题是我不明白为什么.info.params.Profile是数组。这表明可以有多个活动配置文件,但是我如何知道设备是否支持该等?

任何帮助,将不胜感激。

答案1

问题是“使用 PipeWire 设置音频设备配置文件的本机方法是什么?”

pw-cli看起来太投入适合我的口味(恕我直言,听音频不应该是一种脑疲劳)。还有另一种更容易处理的工具 - 它称为wpctl(wireplumber control)。显然它是 的一部分,但在我的书中 b/c包含在for (Debian)中wireplumber,它符合“本地”资格。wireplumberapt installpipewire

我的系统非常简单:一个 Raspberry Pi 和一个蓝牙扬声器。我可以轻松地从 CLI 设置音量:

wpctl set-volume -l 1.5 @DEFAULT_AUDIO_SINK@ 5%+

没有手册wpctl,只有一个-h帮助选项,但 Arch 的人们已经整理了一个非常好的参考

相关内容