2 * Copyright 2005-2006, Marcus Overhagen, marcus@overhagen.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 #include <Application.h>
6 #include <SoundPlayer.h>
16 thread_id reader
= -1;
20 volatile bool interrupt
= false;
23 play_buffer(void *cookie
, void * buffer
, size_t size
, const media_raw_audio_format
& format
)
25 size_t portsize
= port_buffer_size(port
);
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
);
44 printf("file reader started\n");
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
);
54 write_port(port
, 0, buffer
, 0);
56 printf("file reader finished\n");
65 // Are you threatening me, Master Jedi?
67 release_sem(finished
);
72 main(int argc
, char *argv
[])
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");
80 fd
= open(argv
[1], O_RDONLY
);
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");
112 sp
= new BSoundPlayer(&format
, "playwav", play_buffer
);
115 // Join me, Padmé and together we can rule this galaxy.
116 sp
->SetHasData(true);
119 acquire_sem(finished
);
122 // Once more, the Sith will rule the galaxy.
123 printf("interrupted\n");
128 printf("playback finished\n");