From 52215fb363653a5699961c16cbce17211cb72249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Mon, 17 Oct 2005 22:47:34 +0000 Subject: [PATCH] added a playfile command : * takes an audio file as parameter * should play with haiku media kit, provided the codec is supported * tested ok on Haiku with mp3 and wav files git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14407 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/bin/playsound/Jamfile | 5 ++ src/bin/playsound/playfile.cpp | 107 +++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/bin/playsound/playfile.cpp diff --git a/src/bin/playsound/Jamfile b/src/bin/playsound/Jamfile index dd74170975..aac13db3ed 100644 --- a/src/bin/playsound/Jamfile +++ b/src/bin/playsound/Jamfile @@ -4,3 +4,8 @@ BinCommand playsound : playsound.cpp : be media ; + +BinCommand playfile : + playfile.cpp + : be libmedia.so +; diff --git a/src/bin/playsound/playfile.cpp b/src/bin/playsound/playfile.cpp new file mode 100644 index 0000000000..0f562e40d7 --- /dev/null +++ b/src/bin/playsound/playfile.cpp @@ -0,0 +1,107 @@ +/* + * Copyright 2005, Marcus Overhagen, marcus@overhagen.de. All rights reserved. + * Copyright 2005, Jérôme Duval. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +thread_id reader = -1; +sem_id finished = -1; +BMediaTrack *playTrack; +media_format playFormat; +BSoundPlayer *sp = 0; +bool interrupt = false; + +void +play_buffer(void *cookie, void * buffer, size_t size, const media_raw_audio_format & format) +{ + int32 frame_size = (playFormat.u.raw_audio.format & 0xf) * + playFormat.u.raw_audio.channel_count; + int64 frames = 0; + + // Use your feeling, Obi-Wan, and find him you will. + playTrack->ReadFrames(buffer, &frames); + + if (frames <=0) { + sp->SetHasData(false); + release_sem(finished); + } +} + + +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; + } + + entry_ref ref; + if (get_ref_for_path(argv[1], &ref)!=B_OK) + return 2; + BMediaFile *playFile = new BMediaFile(&ref); + if (playFile->InitCheck()CountTracks(); ix++) { + BMediaTrack * track = playFile->TrackAt(ix); + playFormat.type = B_MEDIA_RAW_AUDIO; + if ((track->DecodedFormat(&playFormat) == B_OK) + && (playFormat.type == B_MEDIA_RAW_AUDIO)) { + playTrack = track; + break; + } + if (track) + playFile->ReleaseTrack(track); + } + // Good relations with the Wookiees, I have. + signal(SIGINT, keyb_int); + + new BApplication("application/x-vnd.Haiku-playfile"); + finished = create_sem(0, "finish wait"); + + printf("playing file...\n"); + + // Execute Plan 66! + sp = new BSoundPlayer(&playFormat.u.raw_audio, "playfile", 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; + delete playFile; +}