xref: /haiku/src/bin/setvolume.cpp (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
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 	BParameter *mute = NULL;
52 
53 	BParameter *parameter;
54 	for (int32 index = 0; (parameter = web->ParameterAt(index)) != NULL; index++) {
55 		// assume the mute preceeding master gain control
56 		if (!strcmp(parameter->Kind(), B_MUTE))
57 			mute = parameter;
58 
59 		if (!strcmp(parameter->Kind(), B_MASTER_GAIN)) {
60 			gain = dynamic_cast<BContinuousParameter *>(parameter);
61 			break;
62 		}
63 	}
64 
65 	if (gain == NULL) {
66 		fprintf(stderr, "%s: could not found master gain!\n", sProgramName);
67 		delete web;
68 		return 1;
69 	}
70 
71 	float volume = 0.0;
72 
73 	if (argc > 1) {
74 		if (strcmp(argv[1], "-m") == 0 || strcmp(argv[1], "--mute") == 0) {
75 			int32 muted = 1;
76 			mute->SetValue(&muted, sizeof(int32), system_time());
77 			printf("Muted\n");
78 			return 0;
79 		} else if (strcmp(argv[1], "-u") == 0 || strcmp(argv[1], "--unmute") == 0) {
80 			int32 muted = 0;
81 			mute->SetValue(&muted, sizeof(int32), system_time());
82 			printf("Unmuted\n");
83 			return 0;
84 		} else if (strcmp(argv[1], "-t") == 0 || strcmp(argv[1], "--togglemute") == 0) {
85 			int32 muted = 0;
86 			bigtime_t lastChange = 0;
87 			size_t size = sizeof(int32);
88 			mute->GetValue(&muted, &size, &lastChange);
89 			muted = 1 - muted;
90 			mute->SetValue(&muted, sizeof(int32), system_time());
91 			printf("%s\n", muted ? "Muted" : "Unmuted");
92 			return 0;
93 		} else {
94 			if (strcmp(argv[1], "-i") == 0 || strcmp(argv[1], "-d") == 0
95 				|| strcmp(argv[1], "--increase") == 0 || strcmp(argv[1], "--decrease") == 0) {
96 				bigtime_t when;
97 				size_t size = sizeof(volume);
98 				gain->GetValue(&volume, &size, &when);
99 				size_t step = 3;
100 				if (argc > 2)
101 					step = atoi(argv[2]);
102 				if (strcmp(argv[1], "-i") == 0 || strcmp(argv[1], "--increase") == 0)
103 					volume += step;
104 				else
105 					volume -= step;
106 			} else {
107 				char *end;
108 				volume = strtod(argv[1], &end);
109 				if (end == argv[1]) {
110 					fprintf(stderr,
111 						"Usage: %s <volume> | [options]\n"
112 						"Sets the system volume to the specified value in dB.\n"
113 						"Alternatively there are these options:\n"
114 						"  -m  --mute\n"
115 						"  -u  --unmute\n"
116 						"  -t  --togglemute\ttoggles muting\n"
117 						"  -i  --increase x\tincreases volume by x dB\n"
118 						"  -d  --decrease x\tdecreases volume by x dB\n"
119 						"\t\t\tx defaults to 3 if not supplied\n" ,
120 						sProgramName);
121 					exit(1);
122 				}
123 			}
124 
125 			// make sure our parameter is in range
126 			if (volume > gain->MaxValue())
127 				volume = gain->MaxValue();
128 			else if (volume < gain->MinValue())
129 				volume = gain->MinValue();
130 
131 			gain->SetValue(&volume, sizeof(volume), system_time());
132 		}
133 	}
134 	bigtime_t when;
135 	size_t size = sizeof(volume);
136 	gain->GetValue(&volume, &size, &when);
137 
138 	printf("Current volume: %g (min = %g, max = %g)\n", volume, gain->MinValue(), gain->MaxValue());
139 
140 	delete web;
141 	return 0;
142 }
143 
144