xref: /haiku/src/bin/setvolume.cpp (revision 893988af824e65e49e55f517b157db8386e8002b)
1 /*
2  * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <Application.h>
8 #include <MediaRoster.h>
9 #include <ParameterWeb.h>
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 
16 extern const char *__progname;
17 static const char *sProgramName = __progname;
18 
19 
20 int
21 main(int argc, char **argv)
22 {
23 	BApplication app("application/x-vnd.haiku.setvolume");
24 
25 	BMediaRoster *roster = BMediaRoster::Roster();
26 	if (roster == NULL) {
27 		fprintf(stderr, "%s: media roster not available\n", sProgramName);
28 		return 1;
29 	}
30 
31 	media_node mixer;
32 	status_t status = roster->GetAudioMixer(&mixer);
33 	if (status != B_OK) {
34 		fprintf(stderr, "%s: cannot get audio mixer: %s\n", sProgramName, strerror(status));
35 		return 1;
36 	}
37 
38 	BParameterWeb *web;
39 	status = roster->GetParameterWebFor(mixer, &web);
40 
41 	roster->ReleaseNode(mixer);
42 		// the web is all we need :-)
43 
44 	if (status != B_OK) {
45 		fprintf(stderr, "%s: cannot get parameter web for audio mixer: %s\n",
46 			sProgramName, strerror(status));
47 		return 1;
48 	}
49 
50 	BContinuousParameter *gain = NULL;
51 
52 	BParameter *parameter;
53 	for (int32 index = 0; (parameter = web->ParameterAt(index)) != NULL; index++) {
54 		if (!strcmp(parameter->Kind(), B_MASTER_GAIN)) {
55 			gain = dynamic_cast<BContinuousParameter *>(parameter);
56 			break;
57 		}
58 	}
59 
60 	if (gain == NULL) {
61 		fprintf(stderr, "%s: could not found master gain!\n", sProgramName);
62 		delete web;
63 		return 1;
64 	}
65 
66 	float volume = 0.0;
67 
68 	if (argc > 1) {
69 		char *end;
70 		volume = strtod(argv[1], &end);
71 		if (end == argv[1]) {
72 			fprintf(stderr, "usage: %s <volume>\n", sProgramName);
73 		} else {
74 			// make sure our parameter is in range
75 			if (volume > gain->MaxValue())
76 				volume = gain->MaxValue();
77 			else if (volume < gain->MinValue())
78 				volume = gain->MinValue();
79 
80 			gain->SetValue(&volume, sizeof(volume), system_time());
81 		}
82 	}
83 
84 	bigtime_t when;
85 	size_t size = sizeof(volume);
86 	gain->GetValue(&volume, &size, &when);
87 
88 	printf("Current volume: %g (min = %g, max = %g)\n", volume, gain->MinValue(), gain->MaxValue());
89 
90 	delete web;
91 	return 0;
92 }
93 
94