game kit: Style and whitespace cleanup. No functional change.

This commit is contained in:
Alexander von Gluck IV
2012-02-08 17:38:59 -06:00
parent 1b6bc2675f
commit 2066acfbc5
10 changed files with 547 additions and 477 deletions
+63 -46
View File
@@ -1,12 +1,13 @@
/*
* Copyright 2001-2007, Haiku Inc.
* Copyright 2001-2012 Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Christopher ML Zumwalt May ([email protected])
* Jérôme Duval
*
* Distributed under the terms of the MIT License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -20,6 +21,7 @@
#include "GameSoundDevice.h"
#include "GSUtility.h"
const int32 kPages = 20;
struct _gs_media_tracker
{
@@ -29,8 +31,10 @@ struct _gs_media_tracker
size_t position;
};
// Local utility functions -----------------------------------------------
bool FillBuffer(_gs_ramp * ramp, uint8 * data, uint8 * buffer, size_t * bytes)
bool
FillBuffer(_gs_ramp * ramp, uint8 * data, uint8 * buffer, size_t * bytes)
{
int32 samples = *bytes / sizeof(uint8);
@@ -47,7 +51,9 @@ bool FillBuffer(_gs_ramp * ramp, uint8 * data, uint8 * buffer, size_t * bytes)
return false;
}
bool FillBuffer(_gs_ramp * ramp, int16 * data, int16 * buffer, size_t * bytes)
bool
FillBuffer(_gs_ramp * ramp, int16 * data, int16 * buffer, size_t * bytes)
{
int32 samples = *bytes / sizeof(int16);
@@ -64,7 +70,9 @@ bool FillBuffer(_gs_ramp * ramp, int16 * data, int16 * buffer, size_t * bytes)
return false;
}
bool FillBuffer(_gs_ramp * ramp, int32 * data, int32 * buffer, size_t * bytes)
bool
FillBuffer(_gs_ramp * ramp, int32 * data, int32 * buffer, size_t * bytes)
{
size_t byte = 0;
bool bytesAreReady = (*bytes > 0);
@@ -85,7 +93,9 @@ bool FillBuffer(_gs_ramp * ramp, int32 * data, int32 * buffer, size_t * bytes)
return false;
}
bool FillBuffer(_gs_ramp * ramp, float * data, float * buffer, size_t * bytes)
bool
FillBuffer(_gs_ramp * ramp, float * data, float * buffer, size_t * bytes)
{
size_t byte = 0;
bool bytesAreReady = (*bytes > 0);
@@ -106,37 +116,38 @@ bool FillBuffer(_gs_ramp * ramp, float * data, float * buffer, size_t * bytes)
return false;
}
// BFileGameSound -------------------------------------------------------
BFileGameSound::BFileGameSound(const entry_ref *file,
bool looping,
BGameSoundDevice *device)
: BStreamingGameSound(device),
fAudioStream(NULL),
fStopping(false),
fLooping(looping),
fBuffer(NULL),
fPlayPosition(0),
fPausing(NULL),
fPaused(false),
fPauseGain(1.0)
BFileGameSound::BFileGameSound(const entry_ref *file, bool looping,
BGameSoundDevice *device)
:
BStreamingGameSound(device),
fAudioStream(NULL),
fStopping(false),
fLooping(looping),
fBuffer(NULL),
fPlayPosition(0),
fPausing(NULL),
fPaused(false),
fPauseGain(1.0)
{
if (InitCheck() == B_OK)
SetInitError(Init(file));
}
BFileGameSound::BFileGameSound(const char *file,
bool looping,
BGameSoundDevice *device)
: BStreamingGameSound(device),
fAudioStream(NULL),
fStopping(false),
fLooping(looping),
fBuffer(NULL),
fPlayPosition(0),
fPausing(NULL),
fPaused(false),
fPauseGain(1.0)
BFileGameSound::BFileGameSound(const char *file, bool looping,
BGameSoundDevice *device)
:
BStreamingGameSound(device),
fAudioStream(NULL),
fStopping(false),
fLooping(looping),
fBuffer(NULL),
fPlayPosition(0),
fPausing(NULL),
fPaused(false),
fPauseGain(1.0)
{
if (InitCheck() == B_OK) {
entry_ref node;
@@ -221,8 +232,7 @@ BFileGameSound::Preload()
void
BFileGameSound::FillBuffer(void *inBuffer,
size_t inByteCount)
BFileGameSound::FillBuffer(void *inBuffer, size_t inByteCount)
{
// Split or combine decoder buffers into mixer buffers
// fPlayPosition is where we got up to in the input buffer after last call
@@ -231,7 +241,8 @@ BFileGameSound::FillBuffer(void *inBuffer,
while (inByteCount > 0 && !fPaused) {
if (!fPaused || fPausing) {
printf("mixout %ld, inByteCount %ld, decin %ld, BufferSize %ld\n",out_offset, inByteCount, fPlayPosition, fBufferSize);
printf("mixout %ld, inByteCount %ld, decin %ld, BufferSize %ld\n",
out_offset, inByteCount, fPlayPosition, fBufferSize);
if (fPlayPosition == 0 || fPlayPosition >= fBufferSize) {
Load();
}
@@ -251,19 +262,27 @@ BFileGameSound::FillBuffer(void *inBuffer,
switch(Format().format) {
case gs_audio_format::B_GS_U8:
rampDone = ::FillBuffer(fPausing, (uint8*)&buffer[out_offset], (uint8*)&fBuffer[fPlayPosition], &bytes);
rampDone = ::FillBuffer(fPausing,
(uint8*)&buffer[out_offset],
(uint8*)&fBuffer[fPlayPosition], &bytes);
break;
case gs_audio_format::B_GS_S16:
rampDone = ::FillBuffer(fPausing, (int16*)&buffer[out_offset], (int16*)&fBuffer[fPlayPosition], &bytes);
rampDone = ::FillBuffer(fPausing,
(int16*)&buffer[out_offset],
(int16*)&fBuffer[fPlayPosition], &bytes);
break;
case gs_audio_format::B_GS_S32:
rampDone = ::FillBuffer(fPausing, (int32*)&buffer[out_offset], (int32*)&fBuffer[fPlayPosition], &bytes);
rampDone = ::FillBuffer(fPausing,
(int32*)&buffer[out_offset],
(int32*)&fBuffer[fPlayPosition], &bytes);
break;
case gs_audio_format::B_GS_F:
rampDone = ::FillBuffer(fPausing, (float*)&buffer[out_offset], (float*)&fBuffer[fPlayPosition], &bytes);
rampDone = ::FillBuffer(fPausing,
(float*)&buffer[out_offset],
(float*)&fBuffer[fPlayPosition], &bytes);
break;
}
@@ -274,8 +293,8 @@ BFileGameSound::FillBuffer(void *inBuffer,
// We finished ramping
if (rampDone) {
// We need to be able to stop asap when the pause flag is flipped.
while(fPlayPosition < fBufferSize && (inByteCount > 0)) {
// Need to be able to stop asap when pause flag is flipped.
while (fPlayPosition < fBufferSize && (inByteCount > 0)) {
buffer[out_offset++] = fBuffer[fPlayPosition++];
inByteCount--;
}
@@ -289,8 +308,9 @@ BFileGameSound::FillBuffer(void *inBuffer,
char * buffer = (char*)inBuffer;
// We need to be able to stop asap when the pause flag is flipped.
while(fPlayPosition < fBufferSize && (!fPaused || fPausing) && (inByteCount > 0)) {
// Need to be able to stop asap when the pause flag is flipped.
while (fPlayPosition < fBufferSize && (!fPaused || fPausing)
&& (inByteCount > 0)) {
buffer[out_offset++] = fBuffer[fPlayPosition++];
inByteCount--;
}
@@ -309,8 +329,7 @@ BFileGameSound::Perform(int32 selector,
status_t
BFileGameSound::SetPaused(bool isPaused,
bigtime_t rampTime)
BFileGameSound::SetPaused(bool isPaused, bigtime_t rampTime)
{
if (fPaused != isPaused) {
Lock();
@@ -632,5 +651,3 @@ BFileGameSound::_Reserved_BFileGameSound_23(int32 arg, ...)
{
return B_ERROR;
}
+6 -5
View File
@@ -1,18 +1,19 @@
/*
* Copyright 2001-2002, Haiku Inc.
* Copyright 2001-2012 Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Christopher ML Zumwalt May ([email protected])
*
* Distributed under the terms of the MIT License.
*/
#include "GSUtility.h"
#include <GameSoundDefs.h>
#include <MediaDefs.h>
#include <new>
#include "GSUtility.h"
_gs_ramp*
InitRamp(float* value, float set, float frames, bigtime_t duration)
+8 -5
View File
@@ -23,10 +23,10 @@
// Author: Christopher ML Zumwalt May ([email protected])
// Description: Utility functions used by sound system
//------------------------------------------------------------------------------
#ifndef _GAMESOUND_UTILITY_H
#define _GAMESOUND_UTILITY_H
#include <GameSoundDefs.h>
#include <MediaDefs.h>
@@ -46,9 +46,12 @@ struct _gs_ramp
bigtime_t duration;
};
_gs_ramp* InitRamp(float* value, float set, float frames, bigtime_t duration);
bool ChangeRamp(_gs_ramp* ramp);
size_t get_sample_size(int32 format);
void media_to_gs_format(gs_audio_format * dest, media_raw_audio_format * source);
_gs_ramp* InitRamp(float* value, float set, float frames, bigtime_t duration);
bool ChangeRamp(_gs_ramp* ramp);
size_t get_sample_size(int32 format);
void media_to_gs_format(gs_audio_format* dest,
media_raw_audio_format* source);
#endif
+117 -83
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 Haiku Inc. All rights reserved.
* Copyright 2002-2012 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
@@ -12,6 +12,8 @@
*/
#include "GameProducer.h"
#include <string.h>
#include <stdio.h>
@@ -26,8 +28,6 @@
#include "GameSoundDevice.h"
#include "GSUtility.h"
#include "GameProducer.h"
struct _gs_play {
gs_id sound;
@@ -39,7 +39,7 @@ struct _gs_play {
GameProducer::GameProducer(GameSoundBuffer* object,
const gs_audio_format* format)
const gs_audio_format* format)
:
BMediaNode("GameProducer.h"),
BBufferProducer(B_MEDIA_RAW_AUDIO),
@@ -53,14 +53,16 @@ GameProducer::GameProducer(GameSoundBuffer* object,
fPreferredFormat.type = B_MEDIA_RAW_AUDIO;
fPreferredFormat.u.raw_audio.format = format->format;
fPreferredFormat.u.raw_audio.channel_count = format->channel_count;
fPreferredFormat.u.raw_audio.frame_rate = format->frame_rate; // measured in Hertz
fPreferredFormat.u.raw_audio.frame_rate = format->frame_rate; // Hertz
fPreferredFormat.u.raw_audio.byte_order = format->byte_order;
// fPreferredFormat.u.raw_audio.channel_mask = B_CHANNEL_LEFT | B_CHANNEL_RIGHT;
// fPreferredFormat.u.raw_audio.channel_mask
// = B_CHANNEL_LEFT | B_CHANNEL_RIGHT;
// fPreferredFormat.u.raw_audio.valid_bits = 32;
// fPreferredFormat.u.raw_audio.matrix_mask = B_MATRIX_AMBISONIC_WXYZ;
// we'll use the consumer's preferred buffer size, if any
fPreferredFormat.u.raw_audio.buffer_size = media_raw_audio_format::wildcard.buffer_size;
fPreferredFormat.u.raw_audio.buffer_size
= media_raw_audio_format::wildcard.buffer_size;
// we're not connected yet
fOutput.destination = media_destination::null;
@@ -91,7 +93,8 @@ status_t
GameProducer::GetNextOutput(int32* cookie, media_output* out_output)
{
// we currently support only one output
if (0 != *cookie) return B_BAD_INDEX;
if (0 != *cookie)
return B_BAD_INDEX;
*out_output = fOutput;
*cookie += 1;
@@ -108,12 +111,13 @@ GameProducer::DisposeOutputCookie(int32 cookie)
void
GameProducer::EnableOutput(const media_source& what, bool enabled, int32* _deprecated_)
GameProducer::EnableOutput(const media_source& what, bool enabled,
int32* _deprecated_)
{
// If I had more than one output, I'd have to walk my list of output records to see
// which one matched the given source, and then enable/disable that one. But this
// node only has one output, so I just make sure the given source matches, then set
// the enable state accordingly.
// If I had more than one output, I'd have to walk my list of output records
// to see which one matched the given source, and then enable/disable that
// one. But this node only has one output, so I just make sure the given
// source matches, then set the enable state accordingly.
if (what == fOutput.source)
{
fOutputEnabled = enabled;
@@ -122,7 +126,8 @@ GameProducer::EnableOutput(const media_source& what, bool enabled, int32* _depre
status_t
GameProducer::FormatSuggestionRequested(media_type type, int32 /*quality*/, media_format* format)
GameProducer::FormatSuggestionRequested(media_type type, int32 /*quality*/,
media_format* format)
{
// insure that we received a format
if (!format)
@@ -132,7 +137,8 @@ GameProducer::FormatSuggestionRequested(media_type type, int32 /*quality*/, medi
*format = fPreferredFormat;
// our format is supported
if (type == B_MEDIA_UNKNOWN_TYPE) return B_OK;
if (type == B_MEDIA_UNKNOWN_TYPE)
return B_OK;
// we only support raw audo
return (type != B_MEDIA_RAW_AUDIO) ? B_MEDIA_BAD_FORMAT : B_OK;
@@ -143,31 +149,38 @@ status_t
GameProducer::FormatProposal(const media_source& output, media_format* format)
{
// doest the proposed output match our output?
if (output != fOutput.source) return B_MEDIA_BAD_SOURCE;
if (output != fOutput.source)
return B_MEDIA_BAD_SOURCE;
// return our preferred format
*format = fPreferredFormat;
// we will reject the proposal if the format is not audio
media_type requestedType = format->type;
if ((requestedType != B_MEDIA_UNKNOWN_TYPE) && (requestedType != B_MEDIA_RAW_AUDIO))
if ((requestedType != B_MEDIA_UNKNOWN_TYPE)
&& (requestedType != B_MEDIA_RAW_AUDIO)) {
return B_MEDIA_BAD_FORMAT;
}
return B_OK; // raw audio or wildcard type, either is okay by us
}
status_t
GameProducer::PrepareToConnect(const media_source& what, const media_destination& where, media_format* format, media_source* out_source, char* out_name)
GameProducer::PrepareToConnect(const media_source& what,
const media_destination& where, media_format* format,
media_source* out_source, char* out_name)
{
// The format has been processed by the consumer at this point. We need
// to insure the format is still acceptable and any wild care are filled in.
// trying to connect something that isn't our source?
if (what != fOutput.source) return B_MEDIA_BAD_SOURCE;
if (what != fOutput.source)
return B_MEDIA_BAD_SOURCE;
// are we already connected?
if (fOutput.destination != media_destination::null) return B_MEDIA_ALREADY_CONNECTED;
if (fOutput.destination != media_destination::null)
return B_MEDIA_ALREADY_CONNECTED;
// the format may not yet be fully specialized (the consumer might have
// passed back some wildcards). Finish specializing it now, and return an
@@ -179,8 +192,11 @@ GameProducer::PrepareToConnect(const media_source& what, const media_destination
return B_MEDIA_BAD_FORMAT;
// check the buffer size, which may still be wildcarded
if (format->u.raw_audio.buffer_size == media_raw_audio_format::wildcard.buffer_size)
format->u.raw_audio.buffer_size = 4096; // pick something comfortable to suggest
if (format->u.raw_audio.buffer_size
== media_raw_audio_format::wildcard.buffer_size) {
format->u.raw_audio.buffer_size = 4096;
// pick something comfortable to suggest
}
// Now reserve the connection, and return information about it
fOutput.destination = where;
@@ -192,19 +208,21 @@ GameProducer::PrepareToConnect(const media_source& what, const media_destination
void
GameProducer::Connect(status_t error, const media_source& source, const media_destination& destination, const media_format& format, char* io_name)
GameProducer::Connect(status_t error, const media_source& source,
const media_destination& destination, const media_format& format,
char* io_name)
{
// If something earlier failed, Connect() might still be called, but with a non-zero
// error code. When that happens we simply unreserve the connection and do
// nothing else.
// If something earlier failed, Connect() might still be called, but with a
// non-zero error code. When that happens we simply unreserve the
// connection and do nothing else.
if (error) {
fOutput.destination = media_destination::null;
fOutput.format = fPreferredFormat;
return;
}
// Okay, the connection has been confirmed. Record the destination and format
// that we agreed on, and report our connection name again.
// Okay, the connection has been confirmed. Record the destination and
// format that we agreed on, and report our connection name again.
fOutput.destination = destination;
fOutput.format = format;
strlcpy(io_name, fOutput.name, B_MEDIA_NAME_LENGTH);
@@ -228,8 +246,8 @@ GameProducer::Connect(status_t error, const media_source& source, const media_de
// Second, fill the buffer
start = ::system_time();
for (int32 i = 0; i < frames; i++) {
data[i*2] = 0.8 * float(i/frames);
data[i*2+1] = 0.8 * float(i/frames);
data[i * 2] = 0.8 * float(i / frames);
data[i * 2 + 1] = 0.8 * float(i / frames);
}
produceLatency = ::system_time();
@@ -241,7 +259,8 @@ GameProducer::Connect(status_t error, const media_source& source, const media_de
delete [] data;
// reset our buffer duration, etc. to avoid later calculations
bigtime_t duration = bigtime_t(1000000) * frames / bigtime_t(fOutput.format.u.raw_audio.frame_rate);
bigtime_t duration = bigtime_t(1000000) * frames
/ bigtime_t(fOutput.format.u.raw_audio.frame_rate);
SetBufferDuration(duration);
// Set up the buffer group for our connection, as long as nobody handed us a
@@ -254,7 +273,8 @@ GameProducer::Connect(status_t error, const media_source& source, const media_de
void
GameProducer::Disconnect(const media_source& what, const media_destination& where)
GameProducer::Disconnect(const media_source& what,
const media_destination& where)
{
// Make sure that our connection is the one being disconnected
if ((where == fOutput.destination) && (what == fOutput.source)) {
@@ -267,7 +287,9 @@ GameProducer::Disconnect(const media_source& what, const media_destination& wher
status_t
GameProducer::FormatChangeRequested(const media_source& source, const media_destination& destination, media_format* io_format, int32* _deprecated_)
GameProducer::FormatChangeRequested(const media_source& source,
const media_destination& destination, media_format* io_format,
int32* _deprecated_)
{
// we don't support any other formats, so we just reject any format changes.
return B_ERROR;
@@ -275,7 +297,8 @@ GameProducer::FormatChangeRequested(const media_source& source, const media_dest
status_t
GameProducer::SetBufferGroup(const media_source& for_source, BBufferGroup* newGroup)
GameProducer::SetBufferGroup(const media_source& for_source,
BBufferGroup* newGroup)
{
// verify that we didn't get bogus arguments before we proceed
if (for_source != fOutput.source)
@@ -285,12 +308,12 @@ GameProducer::SetBufferGroup(const media_source& for_source, BBufferGroup* newGr
if (newGroup == fBufferGroup)
return B_OK;
// Ahh, someone wants us to use a different buffer group. At this point we delete
// the one we are using and use the specified one instead. If the specified group is
// NULL, we need to recreate one ourselves, and use *that*. Note that if we're
// caching a BBuffer that we requested earlier, we have to Recycle() that buffer
// *before* deleting the buffer group, otherwise we'll deadlock waiting for that
// buffer to be recycled!
// Ahh, someone wants us to use a different buffer group. At this point we
// delete the one we are using and use the specified one instead. If the
// specified group is NULL, we need to recreate one ourselves, and use
// *that*. Note that if we're caching a BBuffer that we requested earlier,
// we have to Recycle() that buffer *before* deleting the buffer group,
// otherwise we'll deadlock waiting for that buffer to be recycled!
delete fBufferGroup; // waits for all buffers to recycle
if (newGroup != NULL) {
// we were given a valid group; just use that one from now on
@@ -323,28 +346,30 @@ GameProducer::GetLatency(bigtime_t* out_latency)
void
GameProducer::LateNoticeReceived(const media_source& what, bigtime_t how_much, bigtime_t performance_time)
GameProducer::LateNoticeReceived(const media_source& what, bigtime_t how_much,
bigtime_t performance_time)
{
// If we're late, we need to catch up. Respond in a manner appropriate to our
// current run mode.
// If we're late, we need to catch up. Respond in a manner appropriate to
// our current run mode.
if (what == fOutput.source) {
if (RunMode() == B_RECORDING) {
// A hardware capture node can't adjust; it simply emits buffers at
// appropriate points. We (partially) simulate this by not adjusting
// our behavior upon receiving late notices -- after all, the hardware
// can't choose to capture "sooner"....
// appropriate points. We (partially) simulate this by not
// adjusting our behavior upon receiving late notices -- after all,
// the hardware can't choose to capture "sooner"...
} else if (RunMode() == B_INCREASE_LATENCY) {
// We're late, and our run mode dictates that we try to produce buffers
// earlier in order to catch up. This argues that the downstream nodes are
// not properly reporting their latency, but there's not much we can do about
// that at the moment, so we try to start producing buffers earlier to
// compensate.
// We're late, and our run mode dictates that we try to produce
// buffers earlier in order to catch up. This argues that the
// downstream nodes are not properly reporting their latency, but
// there's not much we can do about that at the moment, so we try
// to start producing buffers earlier to compensate.
fInternalLatency += how_much;
SetEventLatency(fLatency + fInternalLatency);
} else {
// The other run modes dictate various strategies for sacrificing data quality
// in the interests of timely data delivery. The way *we* do this is to skip
// a buffer, which catches us up in time by one buffer duration.
// The other run modes dictate various strategies for sacrificing
// data quality in the interests of timely data delivery. The way we
// do this is to skip a buffer, which catches us up in time by one
// buffer duration.
size_t nSamples = fBufferSize / fFrameSize;
fFramesSent += nSamples;
}
@@ -353,7 +378,8 @@ GameProducer::LateNoticeReceived(const media_source& what, bigtime_t how_much, b
void
GameProducer::LatencyChanged(const media_source& source, const media_destination& destination, bigtime_t new_latency, uint32 flags)
GameProducer::LatencyChanged(const media_source& source,
const media_destination& destination, bigtime_t new_latency, uint32 flags)
{
// something downstream changed latency, so we need to start producing
// buffers earlier (or later) than we were previously. Make sure that the
@@ -383,7 +409,9 @@ GameProducer::HandleMessage(int32 message, const void* data, size_t size)
void
GameProducer::AdditionalBufferRequested(const media_source& source, media_buffer_id prev_buffer, bigtime_t prev_time, const media_seek_tag* prev_tag)
GameProducer::AdditionalBufferRequested(const media_source& source,
media_buffer_id prev_buffer, bigtime_t prev_time,
const media_seek_tag* prev_tag)
{
// we don't support offline mode (yet...)
return;
@@ -418,7 +446,8 @@ GameProducer::SetRunMode(run_mode mode)
void
GameProducer::HandleEvent(const media_timed_event* event, bigtime_t lateness, bool realTimeEvent)
GameProducer::HandleEvent(const media_timed_event* event, bigtime_t lateness,
bool realTimeEvent)
{
// FPRINTF(stderr, "ToneProducer::HandleEvent\n");
switch (event->type)
@@ -426,44 +455,44 @@ GameProducer::HandleEvent(const media_timed_event* event, bigtime_t lateness, bo
case BTimedEventQueue::B_START:
// don't do anything if we're already running
if (RunState() != B_STARTED) {
// We are going to start sending buffers so setup the needed bookkeeping
// Going to start sending buffers so setup the needed bookkeeping
fFramesSent = 0;
fStartTime = event->event_time;
media_timed_event firstBufferEvent(fStartTime, BTimedEventQueue::B_HANDLE_BUFFER);
media_timed_event firstBufferEvent(fStartTime,
BTimedEventQueue::B_HANDLE_BUFFER);
// Alternatively, we could call HandleEvent() directly with this event, to avoid a trip through
// the event queue, like this:
//
// Alternatively, we could call HandleEvent() directly with this
// event, to avoid a trip through the event queue like this:
// this->HandleEvent(&firstBufferEvent, 0, false);
//
EventQueue()->AddEvent(firstBufferEvent);
}
break;
case BTimedEventQueue::B_STOP:
// When we handle a stop, we must ensure that downstream consumers don't
// get any more buffers from us. This means we have to flush any pending
// buffer-producing events from the queue.
EventQueue()->FlushEvents(0, BTimedEventQueue::B_ALWAYS, true, BTimedEventQueue::B_HANDLE_BUFFER);
// get any more buffers from us. This means we have to flush any
// pending buffer-producing events from the queue.
EventQueue()->FlushEvents(0, BTimedEventQueue::B_ALWAYS, true,
BTimedEventQueue::B_HANDLE_BUFFER);
break;
case BTimedEventQueue::B_HANDLE_BUFFER:
{
// make sure we're both started *and* connected before delivering a buffer
// Ensure we're both started and connected before delivering buffer
if ((RunState() == BMediaEventLooper::B_STARTED)
&& (fOutput.destination != media_destination::null)) {
// Get the next buffer of data
BBuffer* buffer = FillNextBuffer(event->event_time);
if (buffer) {
// send the buffer downstream if and only if output is enabled
// Send the buffer downstream if output is enabled
status_t err = B_ERROR;
if (fOutputEnabled) {
err = SendBuffer(buffer, fOutput.source,
fOutput.destination);
}
if (err) {
// we need to recycle the buffer ourselves if output is disabled or
// if the call to SendBuffer() fails
// we need to recycle the buffer ourselves if output is
// disabled or if the call to SendBuffer() fails
buffer->Recycle();
}
}
@@ -473,8 +502,11 @@ GameProducer::HandleEvent(const media_timed_event* event, bigtime_t lateness, bo
fFramesSent += nFrames;
// The buffer is on its way; now schedule the next one to go
bigtime_t nextEvent = fStartTime + bigtime_t(double(fFramesSent) / double(fOutput.format.u.raw_audio.frame_rate) * 1000000.0);
media_timed_event nextBufferEvent(nextEvent, BTimedEventQueue::B_HANDLE_BUFFER);
bigtime_t nextEvent = fStartTime + bigtime_t(double(fFramesSent)
/ double(fOutput.format.u.raw_audio.frame_rate)
* 1000000.0);
media_timed_event nextBufferEvent(nextEvent,
BTimedEventQueue::B_HANDLE_BUFFER);
EventQueue()->AddEvent(nextBufferEvent);
}
}
@@ -492,8 +524,9 @@ GameProducer::FillNextBuffer(bigtime_t event_time)
// get a buffer from our buffer group
BBuffer* buf = fBufferGroup->RequestBuffer(fBufferSize, BufferDuration());
// if we fail to get a buffer (for example, if the request times out), we skip this
// buffer and go on to the next, to avoid locking up the control thread
// if we fail to get a buffer (for example, if the request times out), we
// skip this buffer and go on to the next, to avoid locking up the control
// thread.
if (!buf)
return NULL;
@@ -513,20 +546,21 @@ GameProducer::FillNextBuffer(bigtime_t event_time)
bigtime_t stamp;
if (RunMode() == B_RECORDING) {
// In B_RECORDING mode, we stamp with the capture time. We're not
// really a hardware capture node, but we simulate it by using the (precalculated)
// time at which this buffer "should" have been created.
// really a hardware capture node, but we simulate it by using the
// (precalculated) time at which this buffer "should" have been created.
stamp = event_time;
} else {
// okay, we're in one of the "live" performance run modes. in these modes, we
// stamp the buffer with the time at which the buffer should be rendered to the
// output, not with the capture time. fStartTime is the cached value of the
// first buffer's performance time; we calculate this buffer's performance time as
// an offset from that time, based on the amount of media we've created so far.
// okay, we're in one of the "live" performance run modes. in these
// modes, we stamp the buffer with the time at which the buffer should
// be rendered to the output, not with the capture time. fStartTime is
// the cached value of the first buffer's performance time; we calculate
// this buffer's performance time as an offset from that time, based on
// the amount of media we've created so far.
// Recalculating every buffer like this avoids accumulation of error.
stamp = fStartTime + bigtime_t(double(fFramesSent) / double(fOutput.format.u.raw_audio.frame_rate) * 1000000.0);
stamp = fStartTime + bigtime_t(double(fFramesSent)
/ double(fOutput.format.u.raw_audio.frame_rate) * 1000000.0);
}
hdr->start_time = stamp;
return buf;
}
+19 -18
View File
@@ -1,27 +1,31 @@
/*
* Copyright 2001-2002, Haiku Inc.
* Copyright 2002-2012 Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Christopher ML Zumwalt May ([email protected])
*
* Distributed under the terms of the MIT License.
*/
#include <GameSound.h>
#include <stdio.h>
#include <string.h>
#include <GameSound.h>
#include "GameSoundBuffer.h"
#include "GameSoundDevice.h"
using std::nothrow;
// Local Defines ---------------------------------------------------------------
// BGameSound class ------------------------------------------------------------
BGameSound::BGameSound(BGameSoundDevice *device)
: fSound(-1)
:
fSound(-1)
{
// TODO: device is ignored!
// NOTE: BeBook documents that BGameSoundDevice must currently always
@@ -32,7 +36,8 @@ BGameSound::BGameSound(BGameSoundDevice *device)
BGameSound::BGameSound(const BGameSound &other)
: fSound(-1)
:
fSound(-1)
{
memcpy(&fFormat, &other.fFormat, sizeof(gs_audio_format));
// TODO: device from other is ignored!
@@ -106,8 +111,7 @@ BGameSound::StopPlaying()
status_t
BGameSound::SetGain(float gain,
bigtime_t duration)
BGameSound::SetGain(float gain, bigtime_t duration)
{
gs_attribute attribute;
@@ -121,8 +125,7 @@ BGameSound::SetGain(float gain,
status_t
BGameSound::SetPan(float pan,
bigtime_t duration)
BGameSound::SetPan(float pan, bigtime_t duration)
{
gs_attribute attribute;
@@ -166,16 +169,14 @@ BGameSound::Pan()
status_t
BGameSound::SetAttributes(gs_attribute *inAttributes,
size_t inAttributeCount)
BGameSound::SetAttributes(gs_attribute *inAttributes, size_t inAttributeCount)
{
return fDevice->SetAttributes(fSound, inAttributes, inAttributeCount);
}
status_t
BGameSound::GetAttributes(gs_attribute *outAttributes,
size_t inAttributeCount)
BGameSound::GetAttributes(gs_attribute *outAttributes, size_t inAttributeCount)
{
return fDevice->GetAttributes(fSound, outAttributes, inAttributeCount);
}
@@ -258,7 +259,8 @@ BGameSound::Init(gs_id handle)
return B_OK;
}
/*
#if 0
BGameSound &
BGameSound::operator=(const BGameSound &other)
{
@@ -272,7 +274,8 @@ BGameSound::operator=(const BGameSound &other)
return this;
}
*/
#endif
/* unimplemented for protection of the user:
*
@@ -614,5 +617,3 @@ BGameSound::_Reserved_BGameSound_47(int32 arg, ...)
{
return B_ERROR;
}
+45 -38
View File
@@ -24,6 +24,9 @@
// Description: Interface to a single sound, managed by the GameSoundDevice.
//------------------------------------------------------------------------------
#include "GameSoundBuffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -35,7 +38,6 @@
#include <BufferGroup.h>
#include "GameProducer.h"
#include "GameSoundBuffer.h"
#include "GameSoundDevice.h"
#include "StreamingGameSound.h"
#include "GSUtility.h"
@@ -45,46 +47,47 @@
inline void
ApplyMod(uint8 * data, uint8 * buffer, int64 index, float gain, float * pan)
{
data[index*2] += uint8(float(buffer[index*2]) * gain * pan[0]);
data[index*2+1] += uint8(float(buffer[index*2+1]) * gain * pan[1]);
data[index * 2] += uint8(float(buffer[index * 2]) * gain * pan[0]);
data[index * 2 + 1] += uint8(float(buffer[index * 2 + 1]) * gain * pan[1]);
}
inline void
ApplyMod(int16 * data, int16 * buffer, int32 index, float gain, float * pan)
{
data[index*2] = int16(float(buffer[index*2]) * gain * pan[0]);
data[index*2+1] = int16(float(buffer[index*2+1]) * gain * pan[1]);
data[index * 2] = int16(float(buffer[index * 2]) * gain * pan[0]);
data[index * 2 + 1] = int16(float(buffer[index * 2 + 1]) * gain * pan[1]);
}
inline void
ApplyMod(int32 * data, int32 * buffer, int32 index, float gain, float * pan)
{
data[index*2] += int32(float(buffer[index*2]) * gain * pan[0]);
data[index*2+1] += int32(float(buffer[index*2+1]) * gain * pan[1]);
data[index * 2] += int32(float(buffer[index * 2]) * gain * pan[0]);
data[index * 2 + 1] += int32(float(buffer[index * 2 + 1]) * gain * pan[1]);
}
inline void
ApplyMod(float * data, float * buffer, int32 index, float gain, float * pan)
{
data[index*2] += buffer[index*2] * gain * pan[0];
data[index*2+1] += buffer[index*2+1] * gain * pan[1];
data[index * 2] += buffer[index * 2] * gain * pan[0];
data[index * 2 + 1] += buffer[index * 2 + 1] * gain * pan[1];
}
// GameSoundBuffer -------------------------------------------------------
GameSoundBuffer::GameSoundBuffer(const gs_audio_format * format)
: fLooping(false),
fIsConnected(false),
fIsPlaying(false),
fGain(1.0),
fPan(0.0),
fPanLeft(1.0),
fPanRight(1.0),
fGainRamp(NULL),
fPanRamp(NULL)
:
fLooping(false),
fIsConnected(false),
fIsPlaying(false),
fGain(1.0),
fPan(0.0),
fPanLeft(1.0),
fPanRight(1.0),
fGainRamp(NULL),
fPanRamp(NULL)
{
fConnection = new Connection;
fNode = new GameProducer(this, format);
@@ -203,7 +206,7 @@ GameSoundBuffer::SetPan(float pan, bigtime_t duration)
status_t
GameSoundBuffer::GetAttributes(gs_attribute * attributes,
size_t attributeCount)
size_t attributeCount)
{
for (size_t i = 0; i < attributeCount; i++) {
switch (attributes[i].attribute) {
@@ -237,7 +240,7 @@ GameSoundBuffer::GetAttributes(gs_attribute * attributes,
status_t
GameSoundBuffer::SetAttributes(gs_attribute * attributes,
size_t attributeCount)
size_t attributeCount)
{
status_t error = B_OK;
@@ -306,8 +309,8 @@ GameSoundBuffer::Play(void * data, int64 frames)
break;
}
case gs_audio_format::B_GS_F:
{
case gs_audio_format::B_GS_F:
{
for (int64 i = 0; i < frames; i++) {
ApplyMod((float*)data, (float*)buffer, i, fGain, pan);
UpdateMods();
@@ -381,7 +384,7 @@ GameSoundBuffer::Connect(media_node * consumer)
err = roster->GetNodeFor(fNode->Node().node, &fConnection->producer);
if (err != B_OK)
return err;
return err;
// connect to the mixer
fConnection->consumer = *consumer;
@@ -390,7 +393,7 @@ GameSoundBuffer::Connect(media_node * consumer)
// the Mixer uses too.
err = roster->GetTimeSource(&fConnection->timeSource);
if (err != B_OK)
return err;
return err;
err = roster->SetTimeSourceFor(fConnection->producer.node,
fConnection->timeSource.node);
@@ -400,11 +403,14 @@ GameSoundBuffer::Connect(media_node * consumer)
media_input mixerInput;
media_output soundOutput;
int32 count = 1;
err = roster->GetFreeOutputsFor(fConnection->producer, &soundOutput, 1, &count);
err = roster->GetFreeOutputsFor(fConnection->producer, &soundOutput, 1,
&count);
if (err != B_OK)
return err;
count = 1;
err = roster->GetFreeInputsFor(fConnection->consumer, &mixerInput, 1, &count);
err = roster->GetFreeInputsFor(fConnection->consumer, &mixerInput, 1,
&count);
if (err != B_OK)
return err;
@@ -412,7 +418,8 @@ GameSoundBuffer::Connect(media_node * consumer)
media_format format;
format.type = B_MEDIA_RAW_AUDIO;
format.u.raw_audio = media_raw_audio_format::wildcard;
err = roster->Connect(soundOutput.source, mixerInput.destination, &format, &soundOutput, &mixerInput);
err = roster->Connect(soundOutput.source, mixerInput.destination, &format,
&soundOutput, &mixerInput);
if (err != B_OK)
return err;
@@ -441,8 +448,10 @@ GameSoundBuffer::StartPlaying()
// the node chain, otherwise it'll start up already late
bigtime_t latency = 0;
status_t status = roster->GetLatencyFor(fConnection->producer, &latency);
if (status == B_OK)
status = roster->StartNode(fConnection->producer, source->Now() + latency);
if (status == B_OK) {
status = roster->StartNode(fConnection->producer,
source->Now() + latency);
}
source->Release();
fIsPlaying = true;
@@ -477,11 +486,10 @@ GameSoundBuffer::IsPlaying()
// SimpleSoundBuffer ------------------------------------------------------
SimpleSoundBuffer::SimpleSoundBuffer(const gs_audio_format * format,
const void * data,
int64 frames)
: GameSoundBuffer(format),
fPosition(0)
const void * data, int64 frames)
:
GameSoundBuffer(format),
fPosition(0)
{
fBufferSize = frames * fFrameSize;
fBuffer = new char[fBufferSize];
@@ -534,15 +542,14 @@ SimpleSoundBuffer::FillBuffer(void * data, int64 frames)
// StreamingSoundBuffer ------------------------------------------------------
StreamingSoundBuffer::StreamingSoundBuffer(const gs_audio_format * format,
const void * streamHook, size_t inBufferFrameCount,
size_t inBufferCount)
const void * streamHook, size_t inBufferFrameCount, size_t inBufferCount)
:
GameSoundBuffer(format),
fStreamHook(const_cast<void *>(streamHook))
{
if (inBufferFrameCount != 0 && inBufferCount != 0) {
BBufferGroup *bufferGroup = new BBufferGroup(inBufferFrameCount
* fFrameSize, inBufferCount);
BBufferGroup *bufferGroup
= new BBufferGroup(inBufferFrameCount * fFrameSize, inBufferCount);
fNode->SetBufferGroup(fConnection->source, bufferGroup);
}
}
+7 -4
View File
@@ -26,6 +26,9 @@
// this time. Use at your own risk.
//------------------------------------------------------------------------------
#include "GameSoundDevice.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -33,15 +36,15 @@
#include <Autolock.h>
#include <List.h>
#include <Locker.h>
#include <MediaRoster.h>
#include <MediaAddOn.h>
#include <TimeSource.h>
#include <MediaRoster.h>
#include <MediaTheme.h>
#include <TimeSource.h>
#include "GSUtility.h"
#include "GameSoundDevice.h"
#include "GameSoundBuffer.h"
#include "GameProducer.h"
#include "GSUtility.h"
// BGameSoundDevice definitions ------------------------------------
const int32 kInitSoundCount = 32;
+9 -10
View File
@@ -1,16 +1,17 @@
/*
* Copyright 2001-2007, Haiku Inc.
* Copyright 2001-2012 Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Christopher ML Zumwalt May (zummy@users.sf.net)
* Jérôme Duval
*
* Distributed under the terms of the MIT License.
*/
#include <string.h>
#include <PushGameSound.h>
#include <List.h>
#include <PushGameSound.h>
#include <string.h>
#include "GSUtility.h"
@@ -18,7 +19,7 @@
BPushGameSound::BPushGameSound(size_t inBufferFrameCount,
const gs_audio_format *format, size_t inBufferCount,
BGameSoundDevice *device)
:
:
BStreamingGameSound(inBufferFrameCount, format, inBufferCount, device),
fLockPos(0),
fPlayPos(0)
@@ -133,8 +134,7 @@ BPushGameSound::Perform(int32 selector, void *data)
status_t
BPushGameSound::SetParameters(size_t inBufferFrameCount,
const gs_audio_format *format,
size_t inBufferCount)
const gs_audio_format *format, size_t inBufferCount)
{
return B_UNSUPPORTED;
}
@@ -210,6 +210,7 @@ BPushGameSound::BytesReady(size_t * bytes)
return true;
}
/* unimplemented for protection of the user:
*
* BPushGameSound::BPushGameSound()
@@ -384,5 +385,3 @@ BPushGameSound::_Reserved_BPushGameSound_23(int32 arg, ...)
{
return B_ERROR;
}
+29 -24
View File
@@ -1,27 +1,30 @@
/*
* Copyright 2001-2005, Haiku Inc.
* Copyright 2001-2012 Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Christopher ML Zumwalt May (zummy@users.sf.net)
*
* Distributed under the terms of the MIT License.
*/
#include <stdlib.h>
#include <string.h>
#include <SimpleGameSound.h>
#include <Entry.h>
#include <MediaFile.h>
#include <MediaTrack.h>
#include <stdlib.h>
#include <string.h>
#include "GameSoundDefs.h"
#include "GameSoundBuffer.h"
#include "GameSoundDefs.h"
#include "GameSoundDevice.h"
#include "GSUtility.h"
#include <SimpleGameSound.h>
BSimpleGameSound::BSimpleGameSound(const entry_ref *inFile, BGameSoundDevice *device)
: BGameSound(device)
BSimpleGameSound::BSimpleGameSound(const entry_ref *inFile,
BGameSoundDevice *device)
:
BGameSound(device)
{
if (InitCheck() == B_OK)
SetInitError(Init(inFile));
@@ -29,7 +32,8 @@ BSimpleGameSound::BSimpleGameSound(const entry_ref *inFile, BGameSoundDevice *de
BSimpleGameSound::BSimpleGameSound(const char *inFile, BGameSoundDevice *device)
: BGameSound(device)
:
BGameSound(device)
{
if (InitCheck() == B_OK) {
entry_ref file;
@@ -42,11 +46,10 @@ BSimpleGameSound::BSimpleGameSound(const char *inFile, BGameSoundDevice *device)
}
BSimpleGameSound::BSimpleGameSound(const void *inData,
size_t inFrameCount,
const gs_audio_format *format,
BGameSoundDevice *device)
: BGameSound(device)
BSimpleGameSound::BSimpleGameSound(const void *inData, size_t inFrameCount,
const gs_audio_format *format, BGameSoundDevice *device)
:
BGameSound(device)
{
if (InitCheck() == B_OK)
SetInitError(Init(inData, inFrameCount, format));
@@ -54,7 +57,8 @@ BSimpleGameSound::BSimpleGameSound(const void *inData,
BSimpleGameSound::BSimpleGameSound(const BSimpleGameSound &other)
: BGameSound(other)
:
BGameSound(other)
{
gs_audio_format format;
void *data = NULL;
@@ -146,7 +150,8 @@ BSimpleGameSound::Init(const entry_ref* inFile)
memset(&mformat, 0, sizeof(media_format));
mformat.type = B_MEDIA_RAW_AUDIO;
// mformat.u.raw_audio.byte_order = (B_HOST_IS_BENDIAN) ? B_MEDIA_BIG_ENDIAN : B_MEDIA_LITTLE_ENDIAN;
// mformat.u.raw_audio.byte_order
// = (B_HOST_IS_BENDIAN) ? B_MEDIA_BIG_ENDIAN : B_MEDIA_LITTLE_ENDIAN;
status_t error = audioStream->DecodedFormat(&mformat);
if (error != B_OK)
return error;
@@ -167,7 +172,7 @@ BSimpleGameSound::Init(const entry_ref* inFile)
// refomat the buffer from
int64 position = framesTotal * gsformat.channel_count;
for(int32 i = 0; i < (int32)gsformat.buffer_size; i++)
for (int32 i = 0; i < (int32)gsformat.buffer_size; i++)
data[i + position] = buffer[i] + 128;
framesTotal += framesRead;
@@ -183,11 +188,12 @@ BSimpleGameSound::Init(const entry_ref* inFile)
} else {
// We need to determine the size, in bytes, of a single sample.
// At the same time, we will store the format of the audio buffer
size_t frameSize = get_sample_size(gsformat.format) * gsformat.channel_count;
size_t frameSize
= get_sample_size(gsformat.format) * gsformat.channel_count;
char * data = new char[frames * frameSize];
gsformat.buffer_size = frames * frameSize;
while(framesTotal < frames) {
while (framesTotal < frames) {
char * position = &data[framesTotal * frameSize];
audioStream->ReadFrames(position, &framesRead);
@@ -206,11 +212,12 @@ BSimpleGameSound::Init(const entry_ref* inFile)
status_t
BSimpleGameSound::Init(const void* inData, int64 inFrameCount,
const gs_audio_format* format)
const gs_audio_format* format)
{
gs_id sound;
status_t error = Device()->CreateBuffer(&sound, format, inData, inFrameCount);
status_t error
= Device()->CreateBuffer(&sound, format, inData, inFrameCount);
if (error != B_OK)
return error;
@@ -393,5 +400,3 @@ BSimpleGameSound::_Reserved_BSimpleGameSound_23(int32 arg, ...)
{
return B_ERROR;
}
+19 -19
View File
@@ -25,29 +25,33 @@
// (data not known beforehand) game sounds.
//------------------------------------------------------------------------------
#include "GameSoundDevice.h"
#include "StreamingGameSound.h"
#include "GameSoundDevice.h"
BStreamingGameSound::BStreamingGameSound(size_t inBufferFrameCount,
const gs_audio_format *format,
size_t inBufferCount,
BGameSoundDevice *device)
: BGameSound(device),
fStreamHook(NULL),
fStreamCookie(NULL)
const gs_audio_format *format, size_t inBufferCount,
BGameSoundDevice *device)
:
BGameSound(device),
fStreamHook(NULL),
fStreamCookie(NULL)
{
if (InitCheck() == B_OK) {
status_t error = SetParameters(inBufferFrameCount, format, inBufferCount);
status_t error = SetParameters(inBufferFrameCount, format,
inBufferCount);
SetInitError(error);
}
}
BStreamingGameSound::BStreamingGameSound(BGameSoundDevice *device)
: BGameSound(device),
fStreamHook(NULL),
fStreamCookie(NULL)
:
BGameSound(device),
fStreamHook(NULL),
fStreamCookie(NULL)
{
}
@@ -65,8 +69,8 @@ BStreamingGameSound::Clone() const
status_t
BStreamingGameSound::SetStreamHook(void (*hook)(void * inCookie, void * inBuffer, size_t inByteCount, BStreamingGameSound * me),
void * cookie)
BStreamingGameSound::SetStreamHook(void (*hook)(void* inCookie, void* inBuffer,
size_t inByteCount, BStreamingGameSound * me), void * cookie)
{
fStreamHook = hook;
fStreamCookie = cookie;
@@ -85,8 +89,7 @@ BStreamingGameSound::FillBuffer(void *inBuffer,
status_t
BStreamingGameSound::Perform(int32 selector,
void *data)
BStreamingGameSound::Perform(int32 selector, void *data)
{
return B_ERROR;
}
@@ -102,8 +105,7 @@ BStreamingGameSound::SetAttributes(gs_attribute * inAttributes,
status_t
BStreamingGameSound::SetParameters(size_t inBufferFrameCount,
const gs_audio_format *format,
size_t inBufferCount)
const gs_audio_format *format, size_t inBufferCount)
{
gs_id sound;
status_t error = Device()->CreateBuffer(&sound, this, format,
@@ -302,5 +304,3 @@ BStreamingGameSound::_Reserved_BStreamingGameSound_23(int32 arg, ...)
{
return B_ERROR;
}