xref: /haiku/src/tests/kits/media/playsound/playsound.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * playsound - command line sound file player for Haiku
3  * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 
20 #include <FileGameSound.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <getopt.h>
27 #include <string.h>
28 
29 volatile bool interrupted = false;
30 
31 static void
32 keyb_int(int)
33 {
34 	interrupted = true;
35 }
36 
37 
38 static void
39 usage()
40 {
41 	fprintf(stderr, "playsound - command line sound file player for Haiku\n");
42 	fprintf(stderr, "Usage:\n");
43 	fprintf(stderr, "  playsound [--loop] [--gain <value>] [--pan <value>] <filename>\n");
44 	fprintf(stderr, "  gain value in percent, can be 0 (silence) to 100 (normal) or higher\n");
45 	fprintf(stderr, "  pan value in percent, can be -100 (left) to 100 (right)\n");
46 }
47 
48 
49 int
50 main(int argc, char *argv[])
51 {
52 	const char *file;
53 	bool loop = false;
54 	int gain = 100;
55 	int pan = 0;
56 
57 	while (1) {
58 		int c;
59 		int option_index = 0;
60 		static struct option long_options[] =
61 		{
62 			{"loop", no_argument, 0, 'l'},
63 			{"gain", required_argument, 0, 'g'},
64 			{"pan", required_argument, 0, 'p'},
65 			{0, 0, 0, 0}
66 		};
67 
68 		c = getopt_long (argc, argv, "lgp:", long_options, &option_index);
69 		if (c == -1)
70 			break;
71 
72 		switch (c) {
73 			case 'l':
74 				loop = true;
75 		 		break;
76 
77 			case 'g':
78 				gain = atoi(optarg);
79 				break;
80 
81 			case 'p':
82 				pan = atoi(optarg);
83 				break;
84 
85 			default:
86 				usage();
87 				return 1;
88 		}
89 	}
90 
91 	if (optind < argc) {
92 		file = argv[optind];
93 	} else {
94 		usage();
95 		return 1;
96 	}
97 
98 	// test if file exists
99 	int fd = open(file, O_RDONLY);
100 	if (fd < 0) {
101 		fprintf(stderr, "Can't open file %s\n", file);
102 		return 1;
103 	}
104 	close(fd);
105 
106 	signal(SIGINT, keyb_int);
107 
108 	BFileGameSound snd(file, loop);
109 	status_t err;
110 
111 	err = snd.InitCheck();
112 	if (err < B_OK) {
113 		fprintf(stderr, "Init failed, error 0x%08lx (%s)\n", err, strerror(err));
114 		return 1;
115 	}
116 
117 	err = snd.SetGain(gain / 100.0);
118 	if (err < B_OK) {
119 		fprintf(stderr, "Setting gain failed, error 0x%08lx (%s)\n", err, strerror(err));
120 		return 1;
121 	}
122 
123 	err = snd.SetPan(pan / 100.0);
124 	if (err < B_OK) {
125 		fprintf(stderr, "Setting pan failed, error 0x%08lx (%s)\n", err, strerror(err));
126 		return 1;
127 	}
128 
129 	err = snd.Preload();
130 	if (err < B_OK) {
131 		fprintf(stderr, "Preload failed, error 0x%08lx (%s)\n", err, strerror(err));
132 		return 1;
133 	}
134 
135 	err = snd.StartPlaying();
136 	if (err < B_OK) {
137 		fprintf(stderr, "Start playing failed, error 0x%08lx (%s)\n", err, strerror(err));
138 		return 1;
139 	}
140 
141 	while (snd.IsPlaying() && !interrupted)
142 		snooze(50000);
143 
144 	err = snd.StopPlaying();
145 	if (err < B_OK) {
146 		fprintf(stderr, "Stop playing failed, error 0x%08lx (%s)\n", err, strerror(err));
147 		return 1;
148 	}
149 
150 	return 0;
151 }
152