xref: /haiku/src/bin/setvolume.cpp (revision 52c4471a3024d2eb81fe88e2c3982b9f8daa5e56)
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) {
75 			int32 muted = 0;
76 			bigtime_t lastChange = 0;
77 			size_t size = sizeof(int32);
78 			mute->GetValue(&muted, &size, &lastChange);
79 			muted = 1 - muted;
80 			mute->SetValue(&muted, sizeof(int32), system_time());
81 		} else {
82 			if (strcmp(argv[1], "-i") == 0 || strcmp(argv[1], "-d") == 0) {
83 				bigtime_t when;
84 				size_t size = sizeof(volume);
85 				gain->GetValue(&volume, &size, &when);
86 				if (strcmp(argv[1], "-i") == 0)
87 					volume += 3;
88 				else
89 					volume -= 3;
90 			} else {
91 				char *end;
92 				volume = strtod(argv[1], &end);
93 				if (end == argv[1]) {
94 					fprintf(stderr, "usage: %s [<volume> | -i | -d | -m ]\n", sProgramName);
95 					exit(1);
96 				}
97 			}
98 
99 			// make sure our parameter is in range
100 			if (volume > gain->MaxValue())
101 				volume = gain->MaxValue();
102 			else if (volume < gain->MinValue())
103 				volume = gain->MinValue();
104 
105 			gain->SetValue(&volume, sizeof(volume), system_time());
106 		}
107 	}
108 
109 	bigtime_t when;
110 	size_t size = sizeof(volume);
111 	gain->GetValue(&volume, &size, &when);
112 
113 	printf("Current volume: %g (min = %g, max = %g)\n", volume, gain->MinValue(), gain->MaxValue());
114 
115 	delete web;
116 	return 0;
117 }
118 
119