From 93e30a47bed879ad448b3e2d9e10333d3f2e60ae Mon Sep 17 00:00:00 2001 From: Adrien Destugues - PulkoMandy Date: Wed, 15 Aug 2012 10:29:04 +0200 Subject: [PATCH] Add a simpler test for BPushGameSound and attempt to fix the most striking mistakes in there. --- src/kits/game/PushGameSound.cpp | 6 +- .../kits/game/push_game_sound_test/Jamfile | 5 + .../push_game_sound_sine.cpp | 104 ++++++++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/tests/kits/game/push_game_sound_test/push_game_sound_sine.cpp diff --git a/src/kits/game/PushGameSound.cpp b/src/kits/game/PushGameSound.cpp index 9ec5dc156c..40e7132f3f 100644 --- a/src/kits/game/PushGameSound.cpp +++ b/src/kits/game/PushGameSound.cpp @@ -60,10 +60,10 @@ BPushGameSound::lock_status BPushGameSound::LockNextPage(void **out_pagePtr, size_t *out_pageSize) { // the user can not lock every page - if (fPageLocked->CountItems() > fPageCount - 3) + if (fPageLocked->CountItems() > fPageCount - 1) return lock_failed; - // the user cann't lock a page being played + // the user can't lock a page being played if (fLockPos < fPlayPos && fLockPos + fPageSize > fPlayPos) return lock_failed; @@ -74,7 +74,7 @@ BPushGameSound::LockNextPage(void **out_pagePtr, size_t *out_pageSize) // move the locker to the next page fLockPos += fPageSize; - if (fLockPos > fBufferSize) + if (fLockPos >= fBufferSize) fLockPos = 0; *out_pagePtr = lockPage; diff --git a/src/tests/kits/game/push_game_sound_test/Jamfile b/src/tests/kits/game/push_game_sound_test/Jamfile index 81f11b616c..82871470b4 100644 --- a/src/tests/kits/game/push_game_sound_test/Jamfile +++ b/src/tests/kits/game/push_game_sound_test/Jamfile @@ -4,3 +4,8 @@ SimpleTest push_game_sound_test : push_game_sound_test.cpp : game media be $(TARGET_LIBSUPC++) ; + +SimpleTest push_game_sound_sine + : push_game_sound_sine.cpp + : game be $(TARGET_LIBSUPC++) +; diff --git a/src/tests/kits/game/push_game_sound_test/push_game_sound_sine.cpp b/src/tests/kits/game/push_game_sound_test/push_game_sound_sine.cpp new file mode 100644 index 0000000000..6c394319e7 --- /dev/null +++ b/src/tests/kits/game/push_game_sound_test/push_game_sound_sine.cpp @@ -0,0 +1,104 @@ +/* + * Copyright 2012, Adrien Destugues + * Distributed under the terms of the MIT License. + */ + + +#include +#include +#include + +#include +#include +#include +#include +#include + + +int +main(int argc, char *argv[]) +{ + // 256 frames * 4 buffer parts * 2 channels * 2 bytes per sample + // will give us internal buffer of 4096 bytes + size_t framesPerBufferPart = 256; + size_t bufferPartCount = 4; + + if (argc != 1 && argc != 3) { + printf("Usage: %s [ ]\n", + argv[0]); + return 0; + } + + if (argc == 3) { + size_t size = strtoul(argv[1], NULL, 10); + if (size > 0) + framesPerBufferPart = size; + + size = strtoul(argv[2], NULL, 10); + if (size > 0) + bufferPartCount = size; + } + + printf("frames per buffer part: %ld\n", framesPerBufferPart); + printf("buffer part count: %ld\n", bufferPartCount); + + gs_audio_format gsFormat; + memset(&gsFormat, 0, sizeof(gsFormat)); + gsFormat.frame_rate = 48000; + gsFormat.channel_count = 1; + gsFormat.format = gs_audio_format::B_GS_S16; + gsFormat.byte_order = B_MEDIA_LITTLE_ENDIAN; + gsFormat.buffer_size = framesPerBufferPart; + + BPushGameSound pushGameSound(framesPerBufferPart, &gsFormat, + bufferPartCount); + if (pushGameSound.InitCheck() != B_OK) { + printf("trouble initializing push game sound: %s\n", + strerror(pushGameSound.InitCheck())); + return 1; + } + + uint8 *buffer; + size_t bufferSize; + if (pushGameSound.LockForCyclic((void **)&buffer, &bufferSize) + != BPushGameSound::lock_ok) { + printf("cannot lock buffer\n"); + return 1; + } + memset(buffer, 0, bufferSize); + + if (pushGameSound.StartPlaying() != B_OK) { + printf("cannot start playback\n"); + return 1; + } + + printf("playing, press [esc] to exit...\n"); + + key_info keyInfo; + + size_t sampleCount = framesPerBufferPart * bufferPartCount; + for(size_t pos = 0; pos < sampleCount; pos++) + { + *(int16_t*)(buffer + pos * sizeof(int16_t)) + = (int16_t)(2000 * sin(pos * gsFormat.frame_rate + / ((float)sampleCount * 440))); + } + + while (true) { + usleep(1000000 * framesPerBufferPart / gsFormat.frame_rate); + + // check escape key state + if (get_key_info(&keyInfo) != B_OK) { + printf("\nkeyboard state read error\n"); + break; + } + if ((keyInfo.key_states[0] & 0x40) != 0) + break; + } + + pushGameSound.StopPlaying(); + + printf("\nfinished.\n"); + + return 0; +}