| [b67968c] | 1 | /*
|
|---|
| [c5c81f2] | 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| [b67968c] | 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup mixerctl
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file Mixer control for audio devices
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| [9d1488d] | 35 | #include <assert.h>
|
|---|
| [b67968c] | 36 | #include <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| [deff6b4] | 38 | #include <str.h>
|
|---|
| [b67968c] | 39 | #include <devman.h>
|
|---|
| 40 | #include <audio_mixer_iface.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 |
|
|---|
| [ce047249] | 43 | #define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/control"
|
|---|
| [b67968c] | 44 |
|
|---|
| [9d1488d] | 45 | static void print_levels(async_exch_t *exch)
|
|---|
| [b67968c] | 46 | {
|
|---|
| 47 | const char* name = NULL;
|
|---|
| 48 | unsigned count = 0;
|
|---|
| [9d1488d] | 49 | int ret = audio_mixer_get_info(exch, &name, &count);
|
|---|
| [b67968c] | 50 | if (ret != EOK) {
|
|---|
| 51 | printf("Failed to get mixer info: %s.\n", str_error(ret));
|
|---|
| [9d1488d] | 52 | return;
|
|---|
| [b67968c] | 53 | }
|
|---|
| [9d1488d] | 54 | printf("MIXER %s:\n", name);
|
|---|
| [b67968c] | 55 |
|
|---|
| 56 | for (unsigned i = 0; i < count; ++i) {
|
|---|
| 57 | const char *name = NULL;
|
|---|
| 58 | unsigned channels = 0;
|
|---|
| 59 | const int ret =
|
|---|
| 60 | audio_mixer_get_item_info(exch, i, &name, &channels);
|
|---|
| 61 | if (ret != EOK) {
|
|---|
| 62 | printf("Failed to get item %u info: %s.\n",
|
|---|
| 63 | i, str_error(ret));
|
|---|
| 64 | continue;
|
|---|
| 65 | }
|
|---|
| 66 | for (unsigned j = 0; j < channels; ++j) {
|
|---|
| 67 | const char *chan = NULL;
|
|---|
| 68 | int ret = audio_mixer_get_channel_info(
|
|---|
| 69 | exch, i, j, &chan, NULL);
|
|---|
| 70 | if (ret != EOK) {
|
|---|
| 71 | printf(
|
|---|
| 72 | "Failed to get channel %u-%u info: %s.\n",
|
|---|
| 73 | i, j, str_error(ret));
|
|---|
| 74 | }
|
|---|
| 75 | unsigned level = 0, max = 0;
|
|---|
| 76 | ret = audio_mixer_channel_volume_get(
|
|---|
| 77 | exch, i, j, &level, &max);
|
|---|
| 78 | if (ret != EOK) {
|
|---|
| 79 | printf("Failed to get channel %u-%u volume:"
|
|---|
| 80 | " %s.\n", i, j, str_error(ret));
|
|---|
| 81 | }
|
|---|
| 82 | bool mute = false;
|
|---|
| 83 | ret = audio_mixer_channel_mute_get(
|
|---|
| 84 | exch, i, j, &mute);
|
|---|
| 85 | if (ret != EOK) {
|
|---|
| 86 | printf("Failed to get channel %u-%u mute"
|
|---|
| 87 | " status: %s.\n", i, j, str_error(ret));
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| [9d1488d] | 90 | printf("\tChannel(%u/%u) %s %s volume: %u/%u%s.\n",
|
|---|
| [b67968c] | 91 | i, j, name, chan, level, max, mute ? " (M)":"");
|
|---|
| [9d1488d] | 92 | free(chan);
|
|---|
| [b67968c] | 93 | }
|
|---|
| [9d1488d] | 94 | free(name);
|
|---|
| 95 |
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| [deff6b4] | 98 | /*----------------------------------------------------------------------------*/
|
|---|
| 99 | static unsigned get_number(const char* str)
|
|---|
| 100 | {
|
|---|
| 101 | uint16_t num;
|
|---|
| 102 | str_uint16_t(str, NULL, 10, false, &num);
|
|---|
| 103 | return num;
|
|---|
| 104 | }
|
|---|
| 105 | /*----------------------------------------------------------------------------*/
|
|---|
| 106 | static void set_volume(async_exch_t *exch, int argc, char *argv[])
|
|---|
| 107 | {
|
|---|
| 108 | assert(exch);
|
|---|
| 109 | if (argc != 5 && argc != 6) {
|
|---|
| 110 | printf("%s [device] setvolume item channel value\n", argv[0]);
|
|---|
| 111 | }
|
|---|
| 112 | unsigned params = argc == 6 ? 3 : 2;
|
|---|
| 113 | const unsigned item = get_number(argv[params++]);
|
|---|
| 114 | const unsigned channel = get_number(argv[params++]);
|
|---|
| 115 | const unsigned value = get_number(argv[params]);
|
|---|
| 116 | int ret = audio_mixer_channel_volume_set(exch, item, channel, value);
|
|---|
| 117 | if (ret != EOK) {
|
|---|
| 118 | printf("Failed to set mixer volume: %s.\n", str_error(ret));
|
|---|
| 119 | return;
|
|---|
| 120 | }
|
|---|
| 121 | printf("Channel %u-%u volume set to %u.\n", item, channel, value);
|
|---|
| 122 | }
|
|---|
| 123 | /*----------------------------------------------------------------------------*/
|
|---|
| 124 | static void get_volume(async_exch_t *exch, int argc, char *argv[])
|
|---|
| 125 | {
|
|---|
| 126 | assert(exch);
|
|---|
| 127 | if (argc != 4 && argc != 5) {
|
|---|
| 128 | printf("%s [device] getvolume item channel\n", argv[0]);
|
|---|
| 129 | }
|
|---|
| 130 | unsigned params = argc == 5 ? 3 : 2;
|
|---|
| 131 | const unsigned item = get_number(argv[params++]);
|
|---|
| 132 | const unsigned channel = get_number(argv[params++]);
|
|---|
| 133 | unsigned value = 0, max = 0;
|
|---|
| [9d1488d] | 134 |
|
|---|
| [deff6b4] | 135 | int ret = audio_mixer_channel_volume_get(
|
|---|
| 136 | exch, item, channel, &value, &max);
|
|---|
| 137 | if (ret != EOK) {
|
|---|
| 138 | printf("Failed to get mixer volume: %s.\n", str_error(ret));
|
|---|
| 139 | return;
|
|---|
| 140 | }
|
|---|
| 141 | printf("Channel %u-%u volume: %u/%u.\n", item, channel, value, max);
|
|---|
| 142 | }
|
|---|
| 143 | /*----------------------------------------------------------------------------*/
|
|---|
| [9d1488d] | 144 | int main(int argc, char *argv[])
|
|---|
| 145 | {
|
|---|
| 146 | const char *device = DEFAULT_DEVICE;
|
|---|
| [deff6b4] | 147 | void (*command)(async_exch_t *, int, char*[]) = NULL;
|
|---|
| 148 |
|
|---|
| 149 | if (argc >= 2 && str_cmp(argv[1], "setvolume") == 0) {
|
|---|
| 150 | command = set_volume;
|
|---|
| 151 | if (argc == 6)
|
|---|
| 152 | device = argv[1];
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | if (argc >= 2 && str_cmp(argv[1], "getvolume") == 0) {
|
|---|
| 156 | command = get_volume;
|
|---|
| 157 | if (argc == 5)
|
|---|
| 158 | device = argv[1];
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | if ((argc == 2 && command == NULL))
|
|---|
| [9d1488d] | 162 | device = argv[1];
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 | devman_handle_t mixer_handle;
|
|---|
| 166 | int ret = devman_fun_get_handle(device, &mixer_handle, 0);
|
|---|
| 167 | if (ret != EOK) {
|
|---|
| 168 | printf("Failed to get device(%s) handle: %s.\n",
|
|---|
| [deff6b4] | 169 | device, str_error(ret));
|
|---|
| [9d1488d] | 170 | return 1;
|
|---|
| 171 | }
|
|---|
| [b67968c] | 172 |
|
|---|
| [9d1488d] | 173 | async_sess_t *session = devman_device_connect(
|
|---|
| 174 | EXCHANGE_ATOMIC, mixer_handle, IPC_FLAG_BLOCKING);
|
|---|
| 175 | if (!session) {
|
|---|
| 176 | printf("Failed to connect to device.\n");
|
|---|
| 177 | return 1;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | async_exch_t *exch = async_exchange_begin(session);
|
|---|
| 181 | if (!exch) {
|
|---|
| 182 | printf("Failed to start session exchange.\n");
|
|---|
| 183 | async_hangup(session);
|
|---|
| 184 | return 1;
|
|---|
| [b67968c] | 185 | }
|
|---|
| 186 |
|
|---|
| [deff6b4] | 187 | if (command) {
|
|---|
| 188 | command(exch, argc, argv);
|
|---|
| 189 | } else {
|
|---|
| 190 | print_levels(exch);
|
|---|
| 191 | }
|
|---|
| [b67968c] | 192 |
|
|---|
| 193 | async_exchange_end(exch);
|
|---|
| 194 | async_hangup(session);
|
|---|
| 195 | return 0;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /** @}
|
|---|
| 199 | */
|
|---|