Skip to content

Commit 485281e

Browse files
author
Jiri Svoboda
committed
Allow specifying non-default audio target
Needed when there is more than one audio device. It would be nice complementary functionality if we could actually configure the default audio target.
1 parent c65590a commit 485281e

File tree

2 files changed

+73
-13
lines changed

2 files changed

+73
-13
lines changed

uspace/app/modplay/modplay.c

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <io/console.h>
3939
#include <stdio.h>
4040
#include <stdlib.h>
41+
#include <str.h>
4142
#include <str_error.h>
4243
#include <trackmod.h>
4344

@@ -66,6 +67,13 @@ static void modplay_event(cons_event_t *event)
6667
}
6768
}
6869

70+
static void print_syntax(void)
71+
{
72+
printf("syntax: modplay [<options>] <filename.mod>\n");
73+
printf("options:\n");
74+
printf("\t-t <target>\tOutput to specified audio target.\n");
75+
}
76+
6977
int main(int argc, char *argv[])
7078
{
7179
trackmod_module_t *mod;
@@ -77,18 +85,43 @@ int main(int argc, char *argv[])
7785
pcm_format_t format;
7886
void *buffer;
7987
size_t buffer_size;
88+
const char *target = HOUND_DEFAULT_TARGET;
8089
errno_t rc;
8190

82-
if (argc != 2) {
83-
printf("syntax: modplay <filename.mod>\n");
91+
++argv;
92+
--argc;
93+
94+
while (argc > 0 && (*argv)[0] == '-') {
95+
if (str_cmp(*argv, "-t") == 0) {
96+
++argv;
97+
--argc;
98+
99+
if (argc < 1) {
100+
printf("Option '-t' requires an argument.\n");
101+
print_syntax();
102+
return 1;
103+
}
104+
105+
target = *argv++;
106+
--argc;
107+
continue;
108+
}
109+
110+
printf("Invalid option '%s'\n", *argv);
111+
print_syntax();
112+
return 1;
113+
}
114+
115+
if (argc != 1) {
116+
print_syntax();
84117
return 1;
85118
}
86119

87120
con = console_init(stdin, stdout);
88121

89-
rc = trackmod_module_load(argv[1], &mod);
122+
rc = trackmod_module_load(argv[0], &mod);
90123
if (rc != EOK) {
91-
printf("Error loading %s.\n", argv[1]);
124+
printf("Error loading %s.\n", argv[0]);
92125
return 1;
93126
}
94127

@@ -113,9 +146,20 @@ int main(int argc, char *argv[])
113146
return 1;
114147
}
115148

116-
rc = hound_context_connect_target(hound, HOUND_DEFAULT_TARGET);
149+
rc = hound_context_connect_target(hound, target);
117150
if (rc != EOK) {
118-
printf("Error connecting default audio target: %s.\n", str_error(rc));
151+
printf("Error connecting audio target '%s': %s.\n",
152+
target, str_error(rc));
153+
154+
char **names = NULL;
155+
size_t count = 0;
156+
rc = hound_context_get_available_targets(hound, &names, &count);
157+
if (rc == EOK) {
158+
printf("Available targets:\n");
159+
for (size_t i = 0; i < count; i++)
160+
printf(" - %s\n", names[i]);
161+
}
162+
119163
return 1;
120164
}
121165

uspace/app/wavplay/main.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ static errno_t hplay_ctx(hound_context_t *ctx, const char *filename)
118118
* @param filename File to play.
119119
* @return Error code
120120
*/
121-
static errno_t hplay(const char *filename)
121+
static errno_t hplay(const char *filename, const char *target)
122122
{
123123
printf("Hound playback: %s\n", filename);
124124
FILE *source = fopen(filename, "rb");
@@ -157,10 +157,20 @@ static errno_t hplay(const char *filename)
157157
return ENOMEM;
158158
}
159159

160-
ret = hound_context_connect_target(hound, HOUND_DEFAULT_TARGET);
160+
ret = hound_context_connect_target(hound, target);
161161
if (ret != EOK) {
162-
printf("Failed to connect to default target: %s\n",
162+
printf("Failed to connect to target '%s': %s\n", target,
163163
str_error(ret));
164+
165+
char **names = NULL;
166+
size_t count = 0;
167+
ret = hound_context_get_available_targets(hound, &names, &count);
168+
if (ret == EOK) {
169+
printf("Available targets:\n");
170+
for (size_t i = 0; i < count; i++)
171+
printf(" - %s\n", names[i]);
172+
}
173+
164174
hound_context_destroy(hound);
165175
fclose(source);
166176
return ret;
@@ -214,6 +224,7 @@ static const struct option opts[] = {
214224
{ "device", required_argument, 0, 'd' },
215225
{ "parallel", no_argument, 0, 'p' },
216226
{ "record", no_argument, 0, 'r' },
227+
{ "target", required_argument, 0, 't' },
217228
{ "help", no_argument, 0, 'h' },
218229
{ 0, 0, 0, 0 }
219230
};
@@ -229,23 +240,25 @@ static void print_help(const char *name)
229240
printf("\t -h, --help\t Print this help.\n");
230241
printf("\t -r, --record\t Start recording instead of playback. "
231242
"(Not implemented)\n");
232-
printf("\t -d, --device\t Use specified device instead of the sound "
233-
"service. Use location path or a special device `default'\n");
243+
printf("\t -d, --device\t Direct output to specified device instead of "
244+
"the sound service. Use location path or a special device `default'\n");
245+
printf("\t -t, --target\t Output to the specified audio target.\n");
234246
printf("\t -p, --parallel\t Play given files in parallel instead of "
235247
"sequentially (does not work with -d).\n");
236248
}
237249

238250
int main(int argc, char *argv[])
239251
{
240252
const char *device = "default";
253+
const char *target = HOUND_DEFAULT_TARGET;
241254
int idx = 0;
242255
bool direct = false, record = false, parallel = false;
243256
optind = 0;
244257
int ret = 0;
245258

246259
/* Parse command line options */
247260
while (ret != -1) {
248-
ret = getopt_long(argc, argv, "d:prh", opts, &idx);
261+
ret = getopt_long(argc, argv, "d:prt:h", opts, &idx);
249262
switch (ret) {
250263
case 'd':
251264
direct = true;
@@ -257,6 +270,9 @@ int main(int argc, char *argv[])
257270
case 'p':
258271
parallel = true;
259272
break;
273+
case 't':
274+
target = optarg;
275+
break;
260276
case 'h':
261277
print_help(*argv);
262278
return 0;
@@ -333,7 +349,7 @@ int main(int argc, char *argv[])
333349
atomic_fetch_add(&playcount, 1);
334350
fibril_add_ready(fid);
335351
} else {
336-
hplay(file);
352+
hplay(file, target);
337353
}
338354
}
339355
}

0 commit comments

Comments
 (0)