sound is so buggy.
This commit is contained in:
parent
becf8602d7
commit
550556e0e4
12 changed files with 303 additions and 160 deletions
|
|
@ -14,23 +14,38 @@
|
|||
// for dumping sound
|
||||
#include <sndfile.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
namespace sound {
|
||||
|
||||
typedef struct {
|
||||
//struct Slot {
|
||||
// bool used;
|
||||
// bool usable;
|
||||
// bool deleteOnCleanup;
|
||||
// SoundHandle *handle;
|
||||
//};
|
||||
|
||||
struct SoundContext {
|
||||
PaStream *stream;
|
||||
|
||||
// this locks access to the context
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
unsigned int framesPerBuffer;
|
||||
unsigned int sampleRate;
|
||||
|
||||
unsigned int sound_uid_counter;
|
||||
size_t sound_uid_counter;
|
||||
|
||||
// constant
|
||||
unsigned int maxNumSounds;
|
||||
size_t maxNumSounds;
|
||||
|
||||
// num active sonuds
|
||||
unsigned int highestSoundIndex;
|
||||
// index of highest sound or -1, if there are no sounds.
|
||||
// so 0 means no sounds, > 0 could mean different things depending on
|
||||
// empty sounds.
|
||||
volatile ssize_t highestSoundIndex;
|
||||
|
||||
// pointer to array of sound handles that has this number of active sounds
|
||||
//Slot *soundSlots;
|
||||
SoundHandle **sounds;
|
||||
|
||||
bool dumping;
|
||||
|
|
@ -39,7 +54,7 @@ namespace sound {
|
|||
SF_INFO sfinfo;
|
||||
|
||||
bool init;
|
||||
} SoundContext;
|
||||
};
|
||||
|
||||
// singleton so no arguments must be supplied for play() functions etc.
|
||||
static SoundContext context;
|
||||
|
|
@ -83,7 +98,7 @@ namespace sound {
|
|||
|
||||
if (handle->envelope != next) {
|
||||
#if 1
|
||||
printf("[sound] global time %4.3f #%d (%s) t=%f %5s -> %5s\n",
|
||||
printf("[sound] global time %4.3f #%zd (%s) t=%f %5s -> %5s\n",
|
||||
global_time,
|
||||
handle->uid,
|
||||
handle->name,
|
||||
|
|
@ -107,9 +122,9 @@ namespace sound {
|
|||
context.dumping = false;
|
||||
|
||||
if (err < 0) {
|
||||
printf("warning: stop dumping wav returned error: %d\n", err);
|
||||
fprintf(stderr, "[sound] warning: stop dumping wav returned error: %d\n", err);
|
||||
} else {
|
||||
printf("sucessfully stopped dumping to file %s\n", context.dump_filename);
|
||||
printf("[sound] sucessfully stopped dumping to file %s\n", context.dump_filename);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -118,7 +133,7 @@ namespace sound {
|
|||
// TODO: dumping in mono?
|
||||
|
||||
if (!context.init || !context.dumping) {
|
||||
printf("warning: can't dump samples: context not initialized or not currently dumping!\n");
|
||||
fprintf(stderr, "[sound] warning: can't dump samples: context not initialized or not currently dumping!\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -135,7 +150,7 @@ namespace sound {
|
|||
}
|
||||
|
||||
if (context.dumping) {
|
||||
printf("warning: already dumping to %s!\n", context.dump_filename);
|
||||
printf("[sound] warning: already dumping to %s!\n", context.dump_filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -146,12 +161,12 @@ namespace sound {
|
|||
context.outfile = sf_open(filename, SFM_WRITE, &context.sfinfo);
|
||||
|
||||
if (context.outfile == NULL) {
|
||||
printf("Unable to dump to output file %s.\n", filename);
|
||||
fprintf(stderr, "[sound] Unable to dump to output file %s.\n", filename);
|
||||
sf_perror(NULL);
|
||||
return false;
|
||||
};
|
||||
|
||||
//printf("start dumping wav to file %s\n", filename);
|
||||
printf("[sound] start dumping wav to file %s\n", filename);
|
||||
context.dump_filename = filename;
|
||||
context.dumping = true;
|
||||
return true;
|
||||
|
|
@ -277,7 +292,7 @@ namespace sound {
|
|||
if (n >= handle->numHistorySamples) {
|
||||
// TODO clear history when looping?
|
||||
// or add zero samples?
|
||||
printf("warning: try to access more history values than saved.\n");
|
||||
printf("[sound] warning: try to access more history values than saved.\n");
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
|
|
@ -311,6 +326,25 @@ namespace sound {
|
|||
return result;
|
||||
}
|
||||
|
||||
// return false on no success
|
||||
bool tryLock()
|
||||
{
|
||||
int ret = pthread_mutex_trylock(&context.mutex);
|
||||
return ret == 0;
|
||||
}
|
||||
|
||||
// wait if does not lock
|
||||
void lock()
|
||||
{
|
||||
pthread_mutex_lock(&context.mutex);
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
pthread_mutex_unlock(&context.mutex);
|
||||
}
|
||||
|
||||
|
||||
// return false if playing done.
|
||||
static bool advanceSound(SoundHandle *handle, float *out, unsigned int framesPerBuffer)
|
||||
{
|
||||
|
|
@ -406,7 +440,7 @@ namespace sound {
|
|||
break;
|
||||
|
||||
default:
|
||||
printf("warning: unimplemented sound type for #%d: %d\n", handle->uid, handle->_type);
|
||||
printf("[sound] warning: unimplemented sound type for #%zd: %d\n", handle->uid, handle->_type);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -457,23 +491,31 @@ namespace sound {
|
|||
// we have 32 bit float format so cast here
|
||||
float *out = (float *) outputBuffer;
|
||||
|
||||
// set all to zero
|
||||
unsigned int i;
|
||||
for (i=0; i<framesPerBuffer; i++) {
|
||||
*out++ = 0.0f;
|
||||
*out++ = 0.0f;
|
||||
{
|
||||
// set all to zero
|
||||
unsigned int i;
|
||||
for (i=0; i<framesPerBuffer; i++) {
|
||||
*out++ = 0.0f;
|
||||
*out++ = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
// reset pointer
|
||||
out = (float *) outputBuffer;
|
||||
|
||||
// advance all sounds
|
||||
ssize_t i;
|
||||
for (i=0; i<context.highestSoundIndex; i++) {
|
||||
SoundHandle *handle = context.sounds[i];
|
||||
// XXX
|
||||
if (handle == NULL) {
|
||||
continue;
|
||||
}
|
||||
if (handle->deleteFlag) {
|
||||
continue;
|
||||
}
|
||||
if (handle->isDeleted) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!handle->_done) {
|
||||
#if 0
|
||||
|
|
@ -489,12 +531,14 @@ namespace sound {
|
|||
}
|
||||
|
||||
// TODO: when we should not free in the loop, where else?
|
||||
if (handle->_done) {
|
||||
if (handle->_done && !handle->deleteFlag) {
|
||||
//printf("[sound] %f freeing sound #%d handle 0x%p\n", global_time, handle->uid, handle);
|
||||
context.sounds[i] = NULL;
|
||||
if (!handle->keep_when_done) {
|
||||
deleteSound(handle);
|
||||
}
|
||||
printf("[sound] %f mark sound #%zd handle 0x%p for deletion\n", global_time, handle->uid, handle);
|
||||
//context.sounds[i] = NULL;
|
||||
handle->deleteFlag = true;
|
||||
//if (!handle->keep_when_done) {
|
||||
// deleteSound(handle);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -548,10 +592,11 @@ namespace sound {
|
|||
// TODO: add max number of simultanous playing sounds, that's more important.
|
||||
|
||||
context.sound_uid_counter = 0;
|
||||
context.maxNumSounds = 50;
|
||||
context.highestSoundIndex = 0;
|
||||
context.maxNumSounds = 10;
|
||||
context.highestSoundIndex = -1;
|
||||
//context.soundSlots = (Slot*) calloc(context.maxNumSounds, 1000); // XXX HACK
|
||||
context.sounds = (SoundHandle **) calloc(context.maxNumSounds, 1000); // XXX HACK
|
||||
assert(context.sounds != NULL);
|
||||
//assert(context.soundSlots != NULL);
|
||||
|
||||
PaError err = Pa_Initialize();
|
||||
if (err != paNoError) {
|
||||
|
|
@ -647,21 +692,39 @@ namespace sound {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
size_t nextFreeIndex = 0;
|
||||
while(nextFreeIndex < context.highestSoundIndex) {
|
||||
if (context.sounds[nextFreeIndex] == NULL) {
|
||||
// got an empyt sound to use
|
||||
ssize_t nextFreeIndex = 0;
|
||||
while(nextFreeIndex <= context.highestSoundIndex) {
|
||||
if (context.sounds[nextFreeIndex] == NULL || context.sounds[nextFreeIndex]->isDeleted) {
|
||||
// got an empty sound to use
|
||||
break;
|
||||
}
|
||||
nextFreeIndex++;
|
||||
}
|
||||
|
||||
if (nextFreeIndex >= context.maxNumSounds) {
|
||||
printf("error: can't create more sounds, got maximum of: %d\n", context.maxNumSounds);
|
||||
if (context.sounds[nextFreeIndex] != NULL) {
|
||||
printf("[sound] playSound() finally free'ing old sound: %zd\n",
|
||||
context.sounds[nextFreeIndex]->uid);
|
||||
|
||||
free(context.sounds[nextFreeIndex]);
|
||||
context.sounds[nextFreeIndex] = NULL;
|
||||
}
|
||||
|
||||
if (context.sounds[nextFreeIndex] != NULL) {
|
||||
fprintf(stderr, "[sound] warning: playSound() nextFreeIndex is "
|
||||
"%zd but that sound is not NULL!\n", nextFreeIndex);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (nextFreeIndex+1 >= (ssize_t) context.maxNumSounds) {
|
||||
printf("[sound] warning: can't create more sounds, got maximum of: %zd\n", context.maxNumSounds);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SoundHandle *handle = (SoundHandle *) calloc(1, sizeof(SoundHandle));
|
||||
handle->deleteFlag = false;
|
||||
handle->isDeleted = false;
|
||||
handle->_done = false;
|
||||
handle->envelope = New;
|
||||
handle->amplitude = amplitude;
|
||||
handle->minAmplitude = 0.0;
|
||||
|
|
@ -674,7 +737,6 @@ namespace sound {
|
|||
handle->state = 0.0;
|
||||
handle->leakage = 0.0;
|
||||
handle->scaling = 0.0;
|
||||
handle->_done = false;
|
||||
handle->skipDirectionIfClamping = false;
|
||||
|
||||
handle->_useEnvelope = false;
|
||||
|
|
@ -725,9 +787,16 @@ namespace sound {
|
|||
|
||||
context.sounds[nextFreeIndex] = handle;
|
||||
|
||||
if (nextFreeIndex == context.highestSoundIndex) {
|
||||
if (nextFreeIndex == context.highestSoundIndex+1) {
|
||||
context.highestSoundIndex++;
|
||||
}
|
||||
//if (nextFreeIndex < context.highestSoundIndex) {
|
||||
// fprintf(stderr, "[sound] warning: nextFreeIndex > context.highestSoundIndex, "
|
||||
// "this should not happen: %zd %zd\n", nextFreeIndex, context.highestSoundIndex);
|
||||
//}
|
||||
|
||||
printf("[sound] playSound() created sound #%zd name %s\n", handle->uid, handle->name);
|
||||
printf("[sound] highestSoundIndex now %zd\n", context.highestSoundIndex);
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
|
@ -917,8 +986,9 @@ namespace sound {
|
|||
int numActiveSounds(void)
|
||||
{
|
||||
int num = 0;
|
||||
size_t i;
|
||||
for (i=0; i<context.highestSoundIndex; i++) {
|
||||
ssize_t i;
|
||||
for (i=0; i<=context.highestSoundIndex; i++) {
|
||||
// TODO: active is when not done or not deleted!
|
||||
if (context.sounds[i] != NULL) {
|
||||
num++;
|
||||
}
|
||||
|
|
@ -932,12 +1002,12 @@ namespace sound {
|
|||
return;
|
||||
}
|
||||
|
||||
// just for sure
|
||||
// just to be sure
|
||||
handle->_done = true;
|
||||
|
||||
// clear from that
|
||||
size_t i;
|
||||
for (i=0; i<context.highestSoundIndex; i++) {
|
||||
ssize_t i;
|
||||
for (i=0; i<=context.highestSoundIndex; i++) {
|
||||
if (context.sounds[i] == handle) {
|
||||
context.sounds[i] = NULL;
|
||||
if (i+1 == context.highestSoundIndex) {
|
||||
|
|
@ -965,4 +1035,37 @@ namespace sound {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void deleteOldSounds(void)
|
||||
{
|
||||
ssize_t i;
|
||||
for (i=0; i<=context.highestSoundIndex; i++) {
|
||||
SoundHandle *handle = context.sounds[i];
|
||||
if (handle != NULL && handle->deleteFlag) {
|
||||
//context.sounds[i] = NULL;
|
||||
printf("[sound] %f finally deleting sound #%zd handle 0x%p\n", global_time, handle->uid, handle);
|
||||
if (handle->history != NULL) {
|
||||
free(handle->history);
|
||||
}
|
||||
handle->isDeleted = true;
|
||||
handle->deleteFlag = false;
|
||||
//free(handle);
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t nextHighestSoundIndex = -1;
|
||||
for (i=0; i<=context.highestSoundIndex; i++) {
|
||||
SoundHandle *handle = context.sounds[i];
|
||||
if (handle != NULL) {
|
||||
nextHighestSoundIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (context.highestSoundIndex != nextHighestSoundIndex) {
|
||||
printf("[sound] deleteOldSounds() setting hightest sound index from %zd to %zd\n",
|
||||
context.highestSoundIndex, nextHighestSoundIndex);
|
||||
|
||||
context.highestSoundIndex = nextHighestSoundIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,12 +31,19 @@ namespace sound {
|
|||
SoundDecayExp = 1
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
struct SoundHandle {
|
||||
// just for debugging/logging. don't rely on use
|
||||
const char *name;
|
||||
|
||||
// unique ID for accessing them via lookups etc. if sound is dead, this will be never reused
|
||||
int uid;
|
||||
size_t uid;
|
||||
|
||||
// marked for deletion by the internal thread.
|
||||
bool deleteFlag;
|
||||
|
||||
// is now deleted. when a new sound should be used, this can be reused
|
||||
// and at the end deleteFlag and isDeleted should be cleared.
|
||||
bool isDeleted;
|
||||
|
||||
// if true, don't deallocate once this is done.
|
||||
bool keep_when_done;
|
||||
|
|
@ -110,11 +117,15 @@ namespace sound {
|
|||
float *history; // if != 0, history is a pointer to 2*numHistorySamples samples.
|
||||
size_t numHistorySamples; // if != 0, save history for this sound.
|
||||
size_t lastHistorySample; // index/2 of last history sample. used
|
||||
} SoundHandle;
|
||||
};
|
||||
|
||||
// initialize sound subsystem. return true if working, false on errors.
|
||||
bool initSound(void);
|
||||
|
||||
// free sounds that are not used anymore.
|
||||
// must be called regularily by the application
|
||||
void deleteOldSounds();
|
||||
|
||||
// start playing a sound.
|
||||
// args:
|
||||
// type - type of sound.
|
||||
|
|
|
|||
|
|
@ -2,76 +2,82 @@
|
|||
|
||||
#include "sound.hpp"
|
||||
|
||||
#include "util.hpp"
|
||||
|
||||
namespace sound {
|
||||
#if 0
|
||||
class MissileFlySoundEffect : public SoundEffect {
|
||||
MissileFlySoundEffect(const game::Missile *missile)
|
||||
: m_missileId(missile->id)
|
||||
, m_handle(sound::playSound(sound::playFrequency(440, 0.3))
|
||||
{
|
||||
void SoundEffects::addSoundHandleForObject(game::Object *obj, SoundHandle *handle)
|
||||
{
|
||||
std::cout<<"add sound for object: " << obj->id << std::endl;
|
||||
auto pair = std::pair<size_t, SoundHandle*>(obj->id, handle);
|
||||
m_mapObjectToSoundHandle.insert(pair);
|
||||
}
|
||||
|
||||
|
||||
void SoundEffects::fadeOutSoundHandlesForObject(game::Object *obj, float fadeOutTime)
|
||||
{
|
||||
std::cout<<"deleting sounds for object: " << obj->id << std::endl;
|
||||
std::multimap<size_t, SoundHandle*>::iterator it;
|
||||
for (it = m_mapObjectToSoundHandle.find(obj->id); it != m_mapObjectToSoundHandle.end(); it++) {
|
||||
std::cout<<"sound #" << it->second->uid << " / " << it->second->name << std::endl;
|
||||
fadeOut(it->second, fadeOutTime);
|
||||
}
|
||||
|
||||
~MissileFlySoundEffect()
|
||||
{
|
||||
delete(m_handle);
|
||||
}
|
||||
|
||||
bool advance(float dt)
|
||||
{
|
||||
for (const game::StateUpdateEvent *event : updates) {
|
||||
handleUpdateEvent(event);
|
||||
}
|
||||
|
||||
// TODO: add event if sth. comes into life or dies in a state
|
||||
// cycle.. delete objects that died last cycle in the current cycle
|
||||
// so pointers don't get invalid.
|
||||
|
||||
bool keep = false;
|
||||
for (const game::Missile *missile : state->missiles) {
|
||||
if (missile->id == m_missileId) {
|
||||
keep = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!keep) {
|
||||
sound::stopSound(m_handle);
|
||||
sound::deleteSound(m_handle);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
SoundHandle *m_handle;
|
||||
size_t m_missileId;
|
||||
};
|
||||
size_t numRemoved = m_mapObjectToSoundHandle.erase(obj->id);
|
||||
std::cout<<"removed total sounds: " << numRemoved << std::endl;
|
||||
}
|
||||
|
||||
void SoundEffects::advance(float dt, const std::list<game::StateUpdateEvent*> &updates)
|
||||
{
|
||||
//StateUpdateEvent::LifeCycle::Create,
|
||||
//StateUpdateEvent::Type::Explosion
|
||||
(void) dt;
|
||||
|
||||
std::vector<SoundEffect*> rm;
|
||||
for (const game::StateUpdateEvent *evt : updates) {
|
||||
auto type = evt->eventType();
|
||||
auto cycle = evt->lifeCycle();
|
||||
if (type == game::EventType::Missile) {
|
||||
if (cycle == game::LifeCycle::Create) {
|
||||
SoundHandle *handle = playFrequency(880.0, 0.1);
|
||||
if (handle == nullptr) {
|
||||
continue;
|
||||
}
|
||||
fadeIn(handle);
|
||||
handle->name = "missile-fly-sound_fade-in";
|
||||
|
||||
for (SoundEffect *effect : m_effects) {
|
||||
bool keep = effect->advance(dt, m_gameState);
|
||||
if (!keep) {
|
||||
rm.push_back(effect);
|
||||
addSoundHandleForObject(evt->object(), handle);
|
||||
|
||||
} else if (cycle == game::LifeCycle::Destroy) {
|
||||
fadeOutSoundHandlesForObject(evt->object());
|
||||
}
|
||||
} else if (type == game::EventType::Explosion) {
|
||||
#if 1
|
||||
if (cycle == game::LifeCycle::Create) {
|
||||
SoundHandle *handle = playSound(SoundBrownianNoise, 0.4);
|
||||
if (handle == nullptr) {
|
||||
continue;
|
||||
}
|
||||
configureEnvelope(handle, 0.2, 0.0, 2.0);
|
||||
|
||||
// add big *boom* sound too
|
||||
handle = playFrequency(200.0, 0.5);
|
||||
if (handle == nullptr) {
|
||||
continue;
|
||||
}
|
||||
configureEnvelope(handle, 0.1, 0.0, 1.0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
for (SoundEffect *effect : rm) {
|
||||
m_effects.remove(rm);
|
||||
delete(rm);
|
||||
}
|
||||
|
||||
// spawn new sounds
|
||||
//for (const game::Missile *missile : state->missiles) {
|
||||
// for (SoundEffect
|
||||
// if (missile->
|
||||
//}
|
||||
}
|
||||
#endif
|
||||
|
||||
void SoundEffects::fadeIn(SoundHandle *handle, float fadeInTime)
|
||||
{
|
||||
// assume sound has just started
|
||||
configureEnvelope(handle, fadeInTime, 1000000.0, 0.0);
|
||||
}
|
||||
|
||||
void SoundEffects::fadeOut(SoundHandle *handle, float fadeOutTime)
|
||||
{
|
||||
// reset time so it imediately starts to fade out.
|
||||
configureEnvelope(handle, 0.0, 0.0, fadeOutTime);
|
||||
handle->time = 0.0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
#include "state/state.hpp"
|
||||
|
||||
namespace sound {
|
||||
#if 0
|
||||
namespace game {
|
||||
class StateUpdateEvent;
|
||||
}
|
||||
struct SoundHandle;
|
||||
|
||||
#if 0
|
||||
class SoundEffect {
|
||||
public:
|
||||
virtual ~SoundEffects()
|
||||
|
|
@ -19,6 +18,7 @@ namespace sound {
|
|||
// return false if it can be deleted
|
||||
virtual bool advance(float dt) = 0;
|
||||
};
|
||||
#endif
|
||||
|
||||
class SoundEffects {
|
||||
public:
|
||||
|
|
@ -26,11 +26,18 @@ namespace sound {
|
|||
{
|
||||
}
|
||||
|
||||
void handleUpdateEvent(game::StateUpdateEvent *event);
|
||||
void advance(float dt, const std::list<game::StateUpdateEvent*> &updates);
|
||||
|
||||
private:
|
||||
std::list<SoundEffect*> m_effects;
|
||||
}
|
||||
#endif
|
||||
void fadeIn(SoundHandle *handle, float fadeInTime=0.3f);
|
||||
void fadeOut(SoundHandle *handle, float fadeOutTime=0.3f);
|
||||
|
||||
void addSoundHandleForObject(game::Object *obj, SoundHandle *handle);
|
||||
void fadeOutSoundHandlesForObject(game::Object *obj, float fadeOutTime=0.3f);
|
||||
|
||||
private:
|
||||
std::list<SoundHandle*> m_soundHandles;
|
||||
std::multimap<size_t, SoundHandle*> m_mapObjectToSoundHandle;
|
||||
//std::list<SoundEffect*> m_effects;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue