The Game Kit Table of Contents | The Game Kit Index |
Derived from: none
Declared in: be/game/GameSound.h
Library: libgame.so
Allocation: Constructor only
The BGameSound class is the base class for other game sound classes. You never use this class directly; instead, you should use the derived classes.
Nothing makes the player happy like the sound of lots of explosions, weird alien things, and other thrilling audio pleasures. The various BGameSound-derived classes make this a snap.
Playing a sound is as simple as instantiating an object derived from the most appropriate class and calling StartPlaying() on it. How do you determine the most appropriate class? Here are some hints:
Most games' needs will be filled by the BSimpleGameSound and BFileGameSound classes. And, of course, you can write your own derived classes if none of these classes meet your requirements.
Sometimes the player needs to hear multiple copies of the same sound at the same time; for instance, if they shoot a three-way splitting weapon and hit two targets, they need to hear two explosions at once. Each BGameSound (or BGameSound-derived) object can play only once at a time, so you'll need to use cloned copies of the sound, one for each channel of polyphony you allow.
This can be done with ease using a simple class like this:
class MultipleEffect { public: MultipleEffect(BGameSound * sound, int polyphony) { m_fx = new BGameSound *[polyphony]; m_fx[0] = sound; for (int ix=1; ix<polyphony; ix++) { m_fx[ix] = sound->Clone(); } m_current = 0; m_polyphony = polyphony; } void StartPlaying() { int id = atomic_add(&m_current, 1) % m_polyphony; m_fx[id]->StartPlaying(); } private: int32 m_current; int32 m_polyphony; BGameSound *m_fx; };
This class' constructor lets you specify a BGameSound-derived sound and the maximum number of times it can be playing at once. It creates the appropriate number of clones, and its StartPlaying() implementation automatically selects the oldest one and reuses it. Since StartPlaying() restarts a sound from the beginning if it's called on a playing sound, this works out well—if polyphony is 3, and there are already three sounds playing, the oldest one will be reset and played from the beginning.
|
There are a wide variety of performance concerns, and of course you want your game to work on as many systems as possible. Be recommends that you test your application in at least these three situations:
This isn't intended to be an exhaustive list; you should always test your software in as many configurations as possible.
|
Initializes the sound object.
|
|
Releases any memory used by the BGameSound.
If your node has created and set BBufferGroups for any producers, you should delete them in the destructor.
|
Returns a copy of the game sound object. Not implemented in this base class; derived classes must implement it.
|
Returns a pointer to the BGameSoundDevice responsible for playing the sound. This is NULL if the default device is being used.
|
Returns a gs_audio_format structure describing the format of the sound.
|
Returns the ID of the sound attached to the BGameSound object. This is 0 if no sound has been selected.
protected:
|
Specifies the sound to be played by the object.
RETURN CODES
B_OK. The initialization was successful.
|
Returns a status_t indicating whether or not the object was successfully initialized. A return value of B_OK means everything's fine; any other value means an error occurred in the constructor.
|
Returns true if the sound is currently playing; otherwise this function returns false.
|
This function lets you specify whether or not the BGameSound's memory pool (from which buffers are drawn) is locked in memory. Locking the pool may increase performance, but will increase your memory requirements and may reduce the ability of the computer to use virtual memory efficiently.
|
RETURN CODES
B_OK. The pool's lock state has been set.
|
SetAttributes() sets the sound's attributes to match those in the inAttributes array; inAttributeCount indicates how many attributes are in the list.
GetAttributes() returns the sound's current attributes. The ioAttributes list contains a list of inAttributeCount gs_attribute structures; each entry indicates in its attribute field which attribute is to be stuffed into that slot in the array. On return, these attributes are filled out.
RETURN CODES
B_OK. The attribute changes were recorded.
|
SetGain() sets the sound's gain (volume). If rampDuration is nonzero, the change in gain will take rampDuration to occur—the gain will fade between the current value and the new value over the specified number of microseconds. If rampDuration is 0, the change will be immediate.
Gain() returns the sound's current gain.
The gain ranges from 0.0 (silent) to 1.0 (maximum gain).
RETURN CODES
B_OK. The gain change was recorded.
protected:
|
Sets the error code that InitCheck() will return. Derived classes should call this from their constructors.
|
This function lets you specify the maximum number of BGameSound-derived objects that can be in use at once. This value must be between 32 and 1024; if it's outside this range, it gets pinned to 32 or 1024. The actual value set is returned.
|
Sets the size of the memory pool from which sound buffers can be obtained to the number of bytes specified by poolSize. This value must be between 128kB and 4MB, or B_BAD_VALUE will be returned.
|
In general you'll only call this function if you're implementing a new BGameSound subclass.
RETURN CODES
B_OK. The memory pool size has been set.
|
SetPan() sets the sound's pan. This value, which ranges from -1.0 to 1.0, indicates the apparent position of the sound (from absolute left to absolute right). A value of 0 indicates that the sound should appear to come from directly in front of the user. If rampDuration is nonzero, the change in position will take rampDuration to occur—the sound will seem to move from the initial position to the new position over the specified number of microseconds. If rampDuration is 0, the change will be immediate.
Pan() returns the sound's current pan.
RETURN CODES
B_OK. The pan change was recorded.
|
StartPlaying() begins playback of the sound. StopPlaying() ends playback.
If you call StartPlaying() on a sound that's already playing, it stops and restarts from the beginning.
These return B_OK if all is well, or an appropriate error code if not; the error depends on the derived class being used.
RETURN CODES
B_OK. No error.
The Game Kit Table of Contents | The Game Kit Index |
Copyright © 2000 Be, Inc. All rights reserved..