что делает этот код подробно: #if 0 gcc -g3 -Wall pa_volume.c -lpulse exit #endif /*…
- User
что делает этот код подробно:
#if 0 gcc -g3 -Wall pa_volume.c -lpulse exit #endif /* pa_volume sets the client volume in PA's client database Copyright (C) 2017 - 2020 The Board of Trustees of the University of Illinois
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Authors: Roland Haas*/ #include <assert.h> #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <getopt.h> #include <unistd.h> #include <pulse/pulseaudio.h> #include <pulse/ext-stream-restore.h>
static const char *client; static char *server = NULL; static double volume = -1.; static bool toggle_mute = false; static bool set_mute = false; static bool set_nomute = false; static const char *device = NULL; static int show_device = 0;
static int client_found = 0;
// version string to match man page #define VERSION "0.1.3"
// PA_CLAMP_VOLUME fails for me since PA_CLAMP_UNLIKELY is not defined #define CLAMP_VOLUME(v)
((v) < PA_VOLUME_MUTED ? PA_VOLUME_MUTED :
(v) > PA_VOLUME_MAX ? PA_VOLUME_MAX : (v))// exit codes used by us #define PAVO_EXIT_SUCCESS 0 #define PAVO_EXIT_CLIENT_NO_FOUND 1 #define PAVO_EXIT_FAILURE 2
// the actual worker that checks if we have found the client we are looking for // then updates its volume static void read_callback(pa_context *context, const pa_ext_stream_restore_info *info, int eol, void *userdata) { assert(info || eol);
if(info) { if(strstr(info->name, "sink-input-by-application-name:") || strstr(info->name, "sink-input-by-media-role:")) { const int take_action = (volume >= 0. || toggle_mute || set_mute || set_nomute) && (client && strcmp(strchr(info->name, ':')+1, client) == 0); const int show_volume = !(volume >= 0. || toggle_mute || set_mute || set_nomute) && ( !client || strcmp(strchr(info->name, ':')+1, client) == 0); if(client && strcmp(strchr(info->name, ':')+1, client) == 0) client_found = 1;
if(take_action) { // we found the client we are looking for, now make a new info struct // replacing just the volume pa_ext_stream_restore_info new_info = *info; // it seems that as if the volume was never set then the database // contains invalid entries so I have to make a (hopefully sane) // default. Ideally I would like to query PulseAudio for "default" // values. if(new_info.channel_map.channels == 0) { if(!pa_channel_map_init_stereo(&new_info.channel_map)) { fprintf(stderr, "pa_channel_map_init_stereo() faiiled\n"); pa_mainloop_quit((pa_mainloop*)userdata, PAVO_EXIT_FAILURE); } } if(new_info.volume.channels == 0) { new_info.volume.channels = 2; } if(volume >= 0.) { pa_volume_t channel_volume = CLAMP_VOLUME((pa_volume_t)(volume*PA_VOLUME_NORM)); pa_cvolume_set(&new_info.volume, new_info.volume.channels, channel_volume); } else if(toggle_mute) { new_info.mute = !new_info.mute; } else if(set_mute) { new_info.mute = 1; } else if(set_nomute) { new_info.mute = 0; } else { assert(0 && "Internal error: unexpected action"); } if(device) { new_info.device = device; } // use REPLACE rather than SET to keep the other client's information // intact pa_operation *write_op = pa_ext_stream_restore_write( context, PA_UPDATE_REPLACE, &new_info, 1, 1, NULL, NULL); if(write_op) { pa_operation_unref(write_op); } else { fprintf(stderr, "pa_ext_stream_restore_write() failed: %s\n", pa_strerror(pa_context_errno(context))); pa_mainloop_quit((pa_mainloop*)userdata, PAVO_EXIT_FAILURE); } } if(show_volume) { char buf[PA_VOLUME_SNPRINT_MAX]; if(info->channel_map.channels != 0) { pa_volume_snprint(buf, sizeof(buf), pa_cvolume_avg(&info->volume)); } else { pa_volume_snprint(buf, sizeof(buf), PA_VOLUME_NORM); } if(show_device) { printf("client: %s %s [%s]%s\n", strchr(info->name, ':')+1, buf, info->device ? info->device : "default", info->mute ? " (muted)" : ""); } else { printf("client: %s %s%s\n", strchr(info->name, ':')+1, buf, info->mute ? " (muted)" : ""); } } } - Assistant
- User
а на баш можно?
- Assistant
Да, можно написать сценарий на Bash для управления громкостью PulseAudio, используя утилиту командной строки , которая входит в состав PulseAudio. Вот пример, который может выполнить основные функции, такие как установка уровня громкости, mute/unmute, и отображение текущей громкости для заданного клиента:
- User
а на yad
- Assistant
- User
clear
- Assistant
Конечно! Если вы хотите узнать, как использовать
yadдля создания простого графического скрипта, который может управлять громкостью PulseAudio с помощью командной строки, дайте знать, и я помогу вам с подробностями. Просто уточните, какие функции вы хотели бы реализовать, и я помогу создать соответствующий сценарий.