Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / bin / playsound / playsound.cpp
blobb6aa49590dc24c9a15933d79358f9f885cedca00
1 /*
2 * playsound - command line sound file player for Haiku
3 * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de>
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.
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.
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.
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>
29 volatile bool interrupted = false;
31 static void
32 keyb_int(int)
34 interrupted = true;
38 static void
39 usage()
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");
49 int
50 main(int argc, char *argv[])
52 const char *file;
53 bool loop = false;
54 int gain = 100;
55 int pan = 0;
57 while (1) {
58 int c;
59 int option_index = 0;
60 static struct option long_options[] =
62 {"loop", no_argument, 0, 'l'},
63 {"gain", required_argument, 0, 'g'},
64 {"pan", required_argument, 0, 'p'},
65 {0, 0, 0, 0}
66 };
68 c = getopt_long (argc, argv, "lgp:", long_options, &option_index);
69 if (c == -1)
70 break;
72 switch (c) {
73 case 'l':
74 loop = true;
75 break;
77 case 'g':
78 gain = atoi(optarg);
79 break;
81 case 'p':
82 pan = atoi(optarg);
83 break;
85 default:
86 usage();
87 return 1;
91 if (optind < argc) {
92 file = argv[optind];
93 } else {
94 usage();
95 return 1;
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;
104 close(fd);
106 signal(SIGINT, keyb_int);
108 BFileGameSound snd(file, loop);
109 status_t err;
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;
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;
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;
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;
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;
141 while (snd.IsPlaying() && !interrupted)
142 snooze(50000);
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;
150 return 0;