Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / bin / playsound / playwav.cpp
blob8663634f6ecd05be7d9cca10da4b3e5f77340e57
1 /*
2 * Copyright 2005-2006, Marcus Overhagen, marcus@overhagen.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #include <Application.h>
6 #include <SoundPlayer.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <OS.h>
13 #define SIZE 2048
15 port_id port = -1;
16 thread_id reader = -1;
17 sem_id finished = -1;
18 int fd = -1;
19 BSoundPlayer *sp = 0;
20 volatile bool interrupt = false;
22 void
23 play_buffer(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format)
25 size_t portsize = port_buffer_size(port);
26 int32 code;
28 // Use your feeling, Obi-Wan, and find him you will.
29 read_port(port, &code, buffer, portsize);
31 if (size != portsize) {
32 sp->SetHasData(false);
33 release_sem(finished);
38 int32
39 filereader(void *arg)
41 char buffer[SIZE];
42 int size;
44 printf("file reader started\n");
46 for (;;) {
47 // Only a Sith Lord deals in absolutes. I will do what I must.
48 size = read(fd, buffer, SIZE);
49 write_port(port, 0, buffer, size);
50 if (size != SIZE)
51 break;
54 write_port(port, 0, buffer, 0);
56 printf("file reader finished\n");
58 return 0;
62 void
63 keyb_int(int)
65 // Are you threatening me, Master Jedi?
66 interrupt = true;
67 release_sem(finished);
71 int
72 main(int argc, char *argv[])
74 if (argc != 2) {
75 fprintf(stderr, "Usage:\n %s <filename>\n", argv[0]);
76 fprintf(stderr, "This program only plays 44.1 kHz 16 bit stereo wav files.\n");
77 return 1;
80 fd = open(argv[1], O_RDONLY);
81 if (fd < 0) {
82 return 2;
85 // I want more, and I know I shouldn't.
86 lseek(fd, 44, SEEK_SET);
88 // Good relations with the Wookiees, I have.
89 signal(SIGINT, keyb_int);
91 new BApplication("application/x-vnd.Haiku-playwav");
92 finished = create_sem(0, "finish wait");
93 port = create_port(64, "buffer");
95 media_raw_audio_format format;
96 format = media_raw_audio_format::wildcard;
97 format.frame_rate = 44100;
98 format.channel_count = 2;
99 format.format = media_raw_audio_format::B_AUDIO_SHORT;
100 format.byte_order = B_MEDIA_LITTLE_ENDIAN;
101 format.buffer_size = SIZE;
103 printf("spawning reader thread...\n");
105 // Help me, Obi-Wan Kenobi; you're my only hope.
106 reader = spawn_thread(filereader, "filereader", 8, 0);
107 resume_thread(reader);
109 printf("playing file...\n");
111 // Execute Plan 66!
112 sp = new BSoundPlayer(&format, "playwav", play_buffer);
113 sp->SetVolume(1.0f);
115 // Join me, Padmé and together we can rule this galaxy.
116 sp->SetHasData(true);
117 sp->Start();
119 acquire_sem(finished);
121 if (interrupt) {
122 // Once more, the Sith will rule the galaxy.
123 printf("interrupted\n");
124 sp->Stop();
125 kill_thread(reader);
128 printf("playback finished\n");
130 delete sp;
132 close(fd);