void FillBuffer(const ByteArray &inBytes) { int time_samples = inBytes.Size()/sizeof(float)/STEREO_SAMPLES; const float *buffer = (const float *)inBytes.Bytes(); enum { MASK = BUF_SIZE - 1 }; for(int i=0;i<time_samples;i++) { int mono_pos = (i+mDynamicFillPos) & MASK; mDynamicBuffer[ mono_pos<<1 ] = *buffer++ * ((1<<15)-1); mDynamicBuffer[ (mono_pos<<1) + 1 ] = *buffer++ * ((1<<15)-1); } if (mDynamicFillPos<(sSoundPos-mDynamicStartPos)) ELOG("Too slow - FillBuffer %d / %d)", mDynamicFillPos, (sSoundPos-mDynamicStartPos) ); mDynamicFillPos += time_samples; if (time_samples<1024 && !mDynamicDone) { mDynamicDone = true; for(int i=0;i<2048;i++) { int mono_pos = (i+mDynamicFillPos) & MASK; mDynamicBuffer[ mono_pos<<1 ] = 0; mDynamicBuffer[ (mono_pos<<1) + 1 ] = 0; } int samples_left = (int)mDynamicFillPos - (int)(sSoundPos-mDynamicStartPos); int ticks_left = samples_left*1000/44100; //printf("Expire in %d (%d)\n", samples_left, ticks_left ); #ifndef EMSCRIPTEN Mix_ExpireChannel(mChannel, ticks_left>0 ? ticks_left : 1 ); #endif } }
static mrb_value mrb_sdl2_mixer_expire_channel(mrb_state *mrb, mrb_value self) { mrb_int channel, ticks; mrb_get_args(mrb, "ii", &channel, &ticks); return mrb_fixnum_value(Mix_ExpireChannel(channel, ticks)); }
static int lua_Mix_ExpireChannel(State & state){ Stack * stack = state.stack; int channel = -1; int ms = 0; if (stack->is<LUA_TNUMBER>(1)){ channel = stack->to<int>(1); } if (stack->is<LUA_TNUMBER>(2)){ ms = stack->to<int>(2); } stack->push<int>(Mix_ExpireChannel(channel, ms)); return 1; }
/* Change the expiration delay for a channel */ int Mix_ExpireChannel(int which, int ticks) { int status = 0; if ( which == -1 ) { int i; for ( i=0; i < num_channels; ++ i ) { status += Mix_ExpireChannel(i, ticks); } } else if ( which < num_channels ) { SDL_LockAudio(); mix_channel[which].expire = (ticks>0) ? (SDL_GetTicks() + ticks) : 0; SDL_UnlockAudio(); ++ status; } return(status); }
void Sound::expire(uint16 ticks) { Mix_ExpireChannel(_channel, ticks); }
void play_sound_internal(const std::string& files, channel_group group, unsigned int repeats, unsigned int distance, int id, int loop_ticks, int fadein_ticks) { if(files.empty() || distance >= DISTANCE_SILENT || !mix_ok) { return; } audio_lock lock; // find a free channel in the desired group int channel = Mix_GroupAvailable(group); if(channel == -1) { LOG_AUDIO << "All channels dedicated to sound group(" << group << ") are busy, skipping.\n"; return; } Mix_Chunk *chunk; std::string file = pick_one(files); try { chunk = load_chunk(file, group); assert(chunk); } catch(const chunk_load_exception&) { return; } /* * This check prevents SDL_Mixer from blowing up on Windows when UI sound is played * in response to toggling the checkbox which disables sound. */ if(group != SOUND_UI) { Mix_SetDistance(channel, distance); } int res; if(loop_ticks > 0) { if(fadein_ticks > 0) { res = Mix_FadeInChannelTimed(channel, chunk, -1, fadein_ticks, loop_ticks); } else { res = Mix_PlayChannel(channel, chunk, -1); } if(res >= 0) { Mix_ExpireChannel(channel, loop_ticks); } } else { if(fadein_ticks > 0) { res = Mix_FadeInChannel(channel, chunk, repeats, fadein_ticks); } else { res = Mix_PlayChannel(channel, chunk, repeats); } } if(res < 0) { ERR_AUDIO << "error playing sound effect: " << Mix_GetError() << std::endl; //still keep it in the sound cache, in case we want to try again later return; } channel_ids[channel] = id; //reserve the channel's chunk from being freed, since it is playing channel_chunks[res] = chunk; }
void SDLStopAudio(int channel, unsigned int time) { Mix_ExpireChannel(channel, time); }
//expire bool CChannel::Expire(int ms) { //expire the channel return(Mix_ExpireChannel(m_Channel,ms)!=0); }