src/sound.cpp in beeps-0.2 vs src/sound.cpp in beeps-0.2.1

- old
+ new

@@ -65,12 +65,14 @@ assert(frames); std::vector<short> buffer; buffer.reserve(nsamples * nchannels); for (uint sample = 0; sample < nsamples; ++sample) + { for (uint channel = 0; channel < nchannels; ++channel) buffer.push_back((*frames)(sample, channel) * SHRT_MAX); + } alBufferData( self->id, nchannels == 2 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, &buffer[0], @@ -92,14 +94,16 @@ } struct Data { - ALint id = -1; + void* context = NULL; - bool owner = false; + ALint id = -1; + bool owner = false; + ~Data () { clear(); } @@ -109,30 +113,32 @@ ALuint id_ = 0; alGenBuffers(1, &id_); OpenAL_check_error(__FILE__, __LINE__); - id = id_; - owner = true; + context = OpenAL_get_context(); + id = id_; + owner = true; } void clear () { - if (owner && id >= 0) + if (owner && is_valid()) { ALuint id_ = id; alDeleteBuffers(1, &id_); OpenAL_check_error(__FILE__, __LINE__); } - id = -1; - owner = false; + context = NULL; + id = -1; + owner = false; } bool is_valid () const { - return id >= 0; + return id >= 0 && context == OpenAL_get_context(); } };// Data Xot::PSharedImpl<Data> self; @@ -143,13 +149,11 @@ struct SoundSource { void create () { - ALuint id_ = 0; - alGenSources(1, &id_); - if (OpenAL_no_error()) self->id = id_; + self->create(); } void clear () { stop(); @@ -308,36 +312,55 @@ return loop != AL_FALSE; } operator bool () const { - return self->id >= 0; + return self->is_valid(); } bool operator ! () const { return !operator bool(); } struct Data { - ALint id = -1; + void* context = NULL; + ALint id = -1; + ~Data () { clear(); } + void create () + { + ALuint id_ = 0; + alGenSources(1, &id_); + if (!OpenAL_no_error()) return; + + context = OpenAL_get_context(); + id = id_; + } + void clear () { - if (id < 0) return; + if (is_valid()) + { + ALuint id_ = id; + alDeleteSources(1, &id_); + OpenAL_check_error(__FILE__, __LINE__); + } - ALuint id_ = id; - alDeleteSources(1, &id_); - OpenAL_check_error(__FILE__, __LINE__); + context = NULL; + id = -1; + } - id = -1; + bool is_valid () const + { + return id >= 0 && context == OpenAL_get_context(); } };// Data Xot::PSharedImpl<Data> self;