diff --git a/src/bin/Jamfile b/src/bin/Jamfile index ef177f27d9..a3652fd044 100644 --- a/src/bin/Jamfile +++ b/src/bin/Jamfile @@ -125,6 +125,7 @@ SubInclude OBOS_TOP src bin makeudfimage ; SubInclude OBOS_TOP src bin mkdos ; SubInclude OBOS_TOP src bin patch ; SubInclude OBOS_TOP src bin pc ; +SubInclude OBOS_TOP src bin playsound ; SubInclude OBOS_TOP src bin rmd160 ; SubInclude OBOS_TOP src bin sed ; SubInclude OBOS_TOP src bin sharutils ; diff --git a/src/bin/playsound/Jamfile b/src/bin/playsound/Jamfile new file mode 100644 index 0000000000..dd74170975 --- /dev/null +++ b/src/bin/playsound/Jamfile @@ -0,0 +1,6 @@ +SubDir OBOS_TOP src bin playsound ; + +BinCommand playsound : + playsound.cpp + : be media +; diff --git a/src/bin/playsound/playsound.cpp b/src/bin/playsound/playsound.cpp new file mode 100644 index 0000000000..3eb1616646 --- /dev/null +++ b/src/bin/playsound/playsound.cpp @@ -0,0 +1,132 @@ +/* + * Copyright 2005, Marcus Overhagen, marcus@overhagen.de. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#include +#include +#include +#include +#include +#include +#include + +#define SIZE 2048 + +port_id port = -1; +thread_id reader = -1; +sem_id finished = -1; +int fd = -1; +BSoundPlayer *sp = 0; +bool interrupt = false; + +void +play_buffer(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format) +{ + size_t portsize = port_buffer_size(port); + int32 code; + + // Use your feeling, Obi-Wan, and find him you will. + read_port(port, &code, buffer, portsize); + + if (size != portsize) { + sp->SetHasData(false); + release_sem(finished); + } +} + + +int32 +filereader(void *arg) +{ + char buffer[SIZE]; + int size; + + printf("file reader started\n"); + + for (;;) { + // Only a Sith Lord deals in absolutes. I will do what I must. + size = read(fd, buffer, SIZE); + write_port(port, 0, buffer, size); + if (size != SIZE) + break; + } + + write_port(port, 0, buffer, 0); + + printf("file reader finished\n"); + + return 0; +} + + +void +keyb_int(int) +{ + // Are you threatening me, Master Jedi? + interrupt = true; + release_sem(finished); +} + + +int +main(int argc, char *argv[]) +{ + if (argc != 2) { + fprintf(stderr, "Usage:\n %s \n", argv[0]); + return 1; + } + + fd = open(argv[1], O_RDONLY); + if (fd < 0) { + return 2; + } + + // I want more, and I know I shouldn't. + lseek(fd, 44, SEEK_SET); + + // Good relations with the Wookiees, I have. + signal(SIGINT, keyb_int); + + new BApplication("application/x-vnd.Haiku-playsound"); + finished = create_sem(0, "finish wait"); + port = create_port(64, "buffer"); + + media_raw_audio_format format; + format = media_raw_audio_format::wildcard; + format.frame_rate = 44100; + format.channel_count = 2; + format.format = media_raw_audio_format::B_AUDIO_SHORT; + format.byte_order = B_MEDIA_LITTLE_ENDIAN; + format.buffer_size = SIZE; + + printf("spawning reader thread...\n"); + + // Help me, Obi-Wan Kenobi; you're my only hope. + reader = spawn_thread(filereader, "filereader", 8, 0); + resume_thread(reader); + + printf("playing file...\n"); + + // Execute Plan 66! + sp = new BSoundPlayer(&format, "playsound", play_buffer); + sp->SetVolume(1.0f); + + // Join me, Padmé and together we can rule this galaxy. + sp->SetHasData(true); + sp->Start(); + + acquire_sem(finished); + + if (interrupt) { + // Once more, the Sith will rule the galaxy. + printf("interrupted\n"); + sp->Stop(); + kill_thread(reader); + } + + printf("playback finished\n"); + + delete sp; + + close(fd); +}