void CSoundPlayer::PlayPosSample(enum TIngameSoundEffect aEffect,float aVolume, CCoord<int> aPos) { ASSERTPTR( iDynData ); if (aEffect==ESoundEffectNoSound) return; if (iObserver) iObserver->SoundHeard(aEffect,aVolume,aPos); if (!iInitialized) return; if (iDynData->LocalPlayers().size()!=1) { PlaySample(iDynData->LevelRuntime()->IngameData()->SoundFX(aEffect),aVolume,PAN_MIDDLE); return; } int chan=Mix_PlayChannel(-1,iDynData->LevelRuntime()->IngameData()->SoundFX(aEffect),0); Mix_Volume(chan,(int)(aVolume*iFXVolume*127.0)); int left, right; StereoShaper( iDynData->LocalPlayers()[0]->Location(),aPos, left, right ); Mix_SetPanning(chan, left, right ); }
void PingusSoundReal::real_play_sound(const std::string& name, float volume, float panning) { if (!globals::sound_enabled) return; SoundHandle chunk; chunk = SoundResMgr::load(name); if (!chunk) { perr(PINGUS_DEBUG_SOUND) << "Can't open sound '" << name << "' -- skipping\n" << " Mix_Error: " << Mix_GetError() << std::endl; return; } int channel = Mix_PlayChannel(-1, chunk, 0); if (channel != -1) { Mix_Volume(channel, static_cast<int>(volume * MIX_MAX_VOLUME)); if (panning != 0.0f) { Uint8 left = static_cast<Uint8>((panning < 0.0f) ? 255 : static_cast<Uint8>((panning - 1.0f) * -255)); Uint8 right = static_cast<Uint8>((panning > 0.0f) ? 255 : static_cast<Uint8>((panning + 1.0f) * 255)); Mix_SetPanning(channel, left, right); } } }
static void do_panning_update(void) { static Uint8 leftvol = 128; static Uint8 rightvol = 128; static Uint8 leftincr = -1; static Uint8 rightincr = 1; static int panningok = 1; static Uint32 next_panning_update = 0; if ((panningok) && (SDL_GetTicks() >= next_panning_update)) { panningok = Mix_SetPanning(0, leftvol, rightvol); if (!panningok) { fprintf(stderr, "Mix_SetPanning(0, %d, %d) failed!\n", (int) leftvol, (int) rightvol); fprintf(stderr, "Reason: [%s].\n", Mix_GetError()); } if ((leftvol == 255) || (leftvol == 0)) { if (leftvol == 255) printf("All the way in the left speaker.\n"); leftincr *= -1; } if ((rightvol == 255) || (rightvol == 0)) { if (rightvol == 255) printf("All the way in the right speaker.\n"); rightincr *= -1; } leftvol += leftincr; rightvol += rightincr; next_panning_update = SDL_GetTicks() + 10; } }
static mrb_value mrb_sdl2_mixer_set_panning(mrb_state *mrb, mrb_value self) { mrb_int channel, left, right; mrb_get_args(mrb, "iii", &channel, &left, &right); return mrb_fixnum_value(Mix_SetPanning((int)channel, (Uint8)left, (Uint8)right)); }
int CA2_TocaEfeitoTela(unsigned int id, int posicao) { // O efeito é válido? if(id > CA2_MAX_EFEITOS || id == 0) return 0; // Verifica se o efeito existe if(efeitos[id-1].efeito != 0) { // Ajusta a posição se necessário if(posicao<0) posicao=0; else if(posicao > lTela) posicao = lTela; // Calcula o volume para cada canal. Por default, consideramos que ambos estão no máximo int esquerda, direita; posicao = (200*posicao)/800 - 100; // 254 é o volume máximo de um canal. Assim, centro indica 127 em CADA canal. Som em um canal só indica 254 apenas em um canal esquerda = ((100-posicao)*254)/200; direita = ((100+posicao)*254)/200; int canal = Mix_PlayChannel(-1,efeitos[id-1].efeito,0); Mix_SetPanning(canal, esquerda, direita); return canal; } return 0; }
void SoundManager::playSound(int chunkIdx, Ogre::Vector3 soundPosition, Ogre::Camera* mCamera) { if (!initialized) { std::cout << "SoundManager: Manager not initialized." << std::endl; return; } if (sounding) { Mix_ChannelFinished(channelDoneWrapper); int channel = Mix_PlayChannel(-1, chunks[chunkIdx], 0); int dist = calcDistance(mCamera->getPosition(), soundPosition); // put this sound in our list of active sounds. Sound s; s.soundPosition = soundPosition; s.chunk = chunks[chunkIdx]; s.distance = dist; s.channel = channel; s.active = true; activeSounds.push_back(s); // Initialize sound position int rightIntensity = calcPanning(mCamera, soundPosition); Mix_SetPanning(s.channel, 254 - rightIntensity, rightIntensity); Mix_SetDistance(s.channel, dist); } }
void syncchannel(soundchannel &chan) { if(!chan.dirty) return; if(!Mix_FadingChannel(chan.id)) Mix_Volume(chan.id, chan.volume); Mix_SetPanning(chan.id, 255-chan.pan, chan.pan); chan.dirty = false; }
static void I_SDL_UpdateSoundParams(int handle, int vol, int sep) { int left, right; if (!sound_initialized || handle < 0 || handle >= NUM_CHANNELS) { return; } left = ((254 - sep) * vol) / 127; right = ((sep) * vol) / 127; if (left < 0) left = 0; else if ( left > 255) left = 255; if (right < 0) right = 0; else if (right > 255) right = 255; // SDL_mixer version 1.2.8 and earlier has a bug in the Mix_SetPanning // function. A workaround is to call Mix_UnregisterAllEffects for // the channel before calling it. This is undesirable as it may lead // to the channel volumes resetting briefly. if (setpanning_workaround) { Mix_UnregisterAllEffects(handle); } Mix_SetPanning(handle, left, right); }
SCSerror SCSsound::SetPan (const SCSfloat fPan) { if ((fPan < SCSmixer::PAN_LEFT) || (SCSmixer::PAN_RIGHT < fPan)) return scsHandleError(SCS_ERROR_AUDIO | SCS_ERROR_SET | SCS_ERROR_LEVEL | SCS_ERROR_INVALID); Mix_SetPanning(m_iChannelID, (SCSint)(0xff * (0.5 * (1.0 - fPan))), (SCSint)(0xff * (0.5 * (1.0 + fPan)))); m_fPan = fPan; return SCS_ERROR_NONE; }
void AudioController::play(int sound, int x) { if(options.sound_volume == 0) return; if(sound < 0 || sound >= SOUNDFILES) return; if(this->sound[sound] == NULL) return; int chan; int volume; int window_middle; float x_step; int other_volume; chan = 0; while(Mix_Playing(chan)) chan++; // Do not play the sound when we are out of channels if(chan >= AUDIO_CHANNELS) return; window_middle = WINDOW_WIDTH / 2; x_step = (float)0x7f / window_middle; if(x > WINDOW_WIDTH) x -= WINDOW_WIDTH; Mix_SetPanning(chan, 0x7f, 0x7f); if(x > -1 && x != window_middle) { if(x < window_middle) { other_volume = (int)(x_step * x); Mix_SetPanning(chan, 0xff - other_volume, other_volume); } if(x > window_middle){ other_volume = (int)(x_step * (WINDOW_WIDTH - x)); Mix_SetPanning(chan, other_volume, 0xff - other_volume); } } volume = (int)(((double)options.sound_volume / 100) * soundvolume[sound]); Mix_Volume(chan, volume); Mix_PlayChannel(chan, this->sound[sound], 0); }
int snd_panning(int channel, int left, int right) { if (channel >= MIX_CHANNELS) channel %= MIX_CHANNELS; if (channel < 0) channel = -1; return Mix_SetPanning(channel, left, right); }
// set panning of channel voice ----------------------------------------------- // int AUDs_SetVoicePanning( int voice, int panning ) { if (SoundDisabled) return AUD_RETURN_SUCCESS; Mix_SetPanning(voice, panning, panning); return AUD_RETURN_SUCCESS; }
int open_audio (int frequency, Uint16 format, int channels, int chunksize) { if (Mix_OpenAudio( frequency, format, channels, chunksize ) < 0) return -1; // allocate 16 channels for mixing if (Mix_AllocateChannels(8) != 8) return -2; /* reserve some channels and register panning effects */ if (Mix_ReserveChannels(SOUND_RESERVED_CHANNELS) != SOUND_RESERVED_CHANNELS) return -3; /* prepare the spanning effects */ Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 ); Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 ); return 0; }
int SDLMixerSoundSystem::PlaySound ( const char *_soundName, short _distX, short _distY ) { CrbReleaseAssert ( _soundName != NULL ); // Let's see if it's cached. Mix_Chunk *wave = m_chunks.find ( _soundName, NULL ); if ( !wave ) { // Ouch. Performance hit as we load the sound. g_console->SetColour ( IO::Console::FG_YELLOW | IO::Console::FG_INTENSITY ); g_console->WriteLine ( "WARNING: %s was not found in the sound cache!", _soundName ); g_console->SetColour (); LoadWave ( _soundName ); wave = m_chunks.find ( _soundName, NULL ); } if ( !wave ) { // The sound is completely unloadable. g_console->SetColour ( IO::Console::FG_RED | IO::Console::FG_INTENSITY ); g_console->WriteLine ( "ERROR: %s could not be loaded!", _soundName ); g_console->SetColour (); return -1; } // Use the pythagorean theorem to calculate a precise distance, in pixels. int distX2 = _distX * _distX; int distY2 = _distY * _distY; int dist = (int)sqrt((double)distX2 + (double)distY2); // We use a box range for the maximum distance. Just off screen (left or right) sounds would play at 30% volume. float maxDist = (float)max(g_graphics->GetScreenHeight(),g_graphics->GetScreenWidth()) * 0.75f; float volumePercentage = (float)(maxDist - dist) / (float)maxDist; // The volume is the percentage just calculated * maximum volume int newVolume = (int)(( (float)MIX_MAX_VOLUME * 0.75f ) * volumePercentage); if ( newVolume <= 0 ) return 1; int channel = Mix_PlayChannel ( -1, wave, 0 ); int left = ( _distX * -1 ) + 128; if ( left < 0 ) left = 0; if ( left > 255 ) left = 255; Mix_SetPanning ( channel, left, 255 - left ); Mix_Volume ( channel, newVolume ); if ( m_channelPlaying.valid ( channel ) ) { free ( m_channelPlaying.get ( channel ) ); m_channelPlaying.remove ( channel ); } if ( channel != -1 ) { /*g_console->WriteLine ( "Playing %s at %.1lf%% volume (distance: %d, channel: %d)", _soundName, volumePercentage * 100.0f, dist, channel );*/ m_channelPlaying.insert ( cc_strdup ( _soundName ), channel ); } return 0; }
void I_SDL_UpdateSoundParams(int handle, int vol, int sep) { int left, right; if (!sound_initialized || handle < 0 || handle >= NUM_CHANNELS) return; left = BETWEEN(0, (254 - sep) * vol / 127, 255); right = BETWEEN(0, sep * vol / 127, 255); Mix_SetPanning(handle, left, right); }
// // sound_effect::play // // Add a new sample for playing. // panpot defines the pan position for the sound effect. // 0 - Completely to the right. // 128 - Centered. // 255 - Completely to the left. // void sound_effect::play(int volume, int pitch, int panpot) { if (!sound_enabled) return; int channel = Mix_PlayChannel(-1, m_chunk, 0); if (channel > -1) { Mix_Volume(channel, volume); Mix_SetPanning(channel, panpot, 255 - panpot); } }
void CAudioChannel::SetPan (int nPan) { if (m_info.bPlaying) { m_info.nPan = nPan; #if USE_SDL_MIXER if (gameOpts->sound.bUseSDLMixer) { nPan /= (32767 / 127); Mix_SetPanning (m_info.nChannel, (ubyte) nPan, (ubyte) (254 - nPan)); } #endif } }
static void I_SDL_UpdateSoundParams(int handle, int vol, int sep) { int left, right; if (!sound_initialized) return; left = (254 - sep) * vol / 127; right = sep * vol / 127; Mix_SetPanning(handle, left, right); }
void ChannelInternalState::SetPan(const mathfu::Vector<float, 2>& pan) { if (!is_real()) { return; } static const Uint8 kMaxPanValue = 255; // This formula is explained in the following paper: // http://www.rs-met.com/documents/tutorials/PanRules.pdf float p = static_cast<float>(M_PI) * (pan.x() + 1.0f) / 4.0f; Uint8 left = static_cast<Uint8>(cos(p) * kMaxPanValue); Uint8 right = static_cast<Uint8>(sin(p) * kMaxPanValue); Mix_SetPanning(channel_id_, left, right); }
void play_sound(Mix_Chunk * snd, enum Sound_Speaker whichSpeaker) { /* this won't call the function if the user has disabled sound * either via menu or via command-line option */ if(!audio_device || !use_sound) return; Mix_PlayChannel( whichSpeaker, snd, 0); /* prepare for panning effects for next call */ switch (whichSpeaker) { case SOUND_LEFT_SPEAKER: Mix_SetPanning( SOUND_LEFT_SPEAKER, 230, 24 ); break; case SOUND_RIGHT_SPEAKER: Mix_SetPanning( SOUND_RIGHT_SPEAKER, 24, 230 ); break; default: // keep the compiler happy break; } }
// // This function adds a sound to the // list of currently active sounds, // which is maintained as a given number // (eight, usually) of internal channels. // Returns a handle. // int addsfx( int sfxid, int volume, int step, int seperation ) { static unsigned short handlenum = 3; int i; int rc; int slot; uint8_t left, right; slot = -1; /* Stopping samples in mid-stream is bad. * needed for chainsaw/chaingun * Let everything else run to end */ // Chainsaw troubles. // Play these sound effects only one at a time. if ( sfxid == sfx_sawup || sfxid == sfx_sawidl || sfxid == sfx_sawful || sfxid == sfx_sawhit ) slot = 0; if ( sfxid == sfx_stnmov ) slot = 1; if ( sfxid == sfx_pistol ) slot = 2; if (slot = -1) { for (i = 3; i < NUM_CHANNELS; i++) { handlenum++; if (handlenum >= NUM_CHANNELS) handlenum = 3; if (!Mix_Playing(handlenum)){ slot = handlenum; break; } } } else { Mix_HaltChannel(slot); } if (-1 == slot) return 99; seperation -= 1; // 0-FF left = (uint8_t)(((0xffff - seperation * seperation) * volume)>>12); seperation = 0xff - seperation; right = (uint8_t)(((0xffff - seperation * seperation) * volume)>>12); Mix_SetPanning(slot, left, right); Mix_PlayChannel(slot, S_sfx[sfxid].chunk, 0); return slot; }
void DigiSetChannelPan(int channel, int pan) { if (!gameStates.sound.digi.bInitialized) return; if (!SoundSlots[channel].playing) return; SoundSlots[channel].pan = pan; #if USE_SDL_MIXER if (gameOpts->sound.bUseSDLMixer) { pan /= (32767 / 127); Mix_SetPanning (channel, (ubyte) pan, (ubyte) (254 - pan)); } #endif }
void SoundManager::updateSounds(Ogre::Camera* mCamera) { // Remove any sounds from our list of active sounds that might not be active. for(int i = 0; i < activeSounds.size(); i++) { Sound *s = &(activeSounds[i]); if(s->active == false) { // Reset sound position on the given channel, and remove the sound Mix_SetPanning(s->channel, 255, 255); Mix_SetDistance(s->channel, 0); activeSounds.erase(activeSounds.begin() + i); } } // update sound position for all active sounds for(int i = 0; i < activeSounds.size(); i++) { Sound *s = &(activeSounds[i]); int rightIntensity = calcPanning(mCamera, s->soundPosition); Mix_SetPanning(s->channel, 254 - rightIntensity, rightIntensity); int dist = calcDistance(mCamera->getPosition(), s->soundPosition); s->distance = dist; Mix_SetDistance(s->channel, dist); } }
void Mix_VolPan (int channel, int vol, int pan) { #if USE_SDL_MIXER if (gameOpts->sound.bUseSDLMixer && (channel >= 0)) { if (vol) { vol = (FixMul (vol, gameStates.sound.digi.nVolume) + (SOUND_MAX_VOLUME / MIX_MAX_VOLUME) / 2) / (SOUND_MAX_VOLUME / MIX_MAX_VOLUME); if (!vol) vol = 1; Mix_Volume (channel, vol); pan /= (32767 / 127); Mix_SetPanning (channel, (ubyte) pan, (ubyte) (254 - pan)); } } #endif }
/**\brief Plays the sound. */ bool Sound::Play( void ){ if ( this->sound == NULL ) return false; // Disable panning and distance int freechan = Audio::Instance().GetFreeChannel(); Mix_SetDistance( freechan, 0 ); Mix_SetPanning( freechan, 127, 127 ); Mix_Volume( freechan, this->volume ); this->channel = Audio::Instance().PlayChannel( freechan, this->sound, 0 ); if ( channel == -1 ) return false; return true; }
void SD_SetPosition(int channel, int leftpos, int rightpos) { if((leftpos < 0) || (leftpos > 15) || (rightpos < 0) || (rightpos > 15) || ((leftpos == 15) && (rightpos == 15))) Quit("SD_SetPosition: Illegal position"); switch (DigiMode) { default: break; case sds_SoundBlaster: // SDL_PositionSBP(leftpos,rightpos); Mix_SetPanning(channel, TO_SDL_POSITION(leftpos), TO_SDL_POSITION(rightpos)); break; } }
/** * Play sample. * @param sample sample loaded with sdl_load_sample. */ void sdl_play_sample(sb_sample * sample, int looping) { #ifdef HAVE_SDL_MIXER /* scale of left_volume and right_volume is [0..32] */ /* SDL_SetPanning wants [0..255] */ int ch, l = sample->left_volume * 8, r = sample->right_volume * 8; if (l > 0) l--; if (r > 0) r--; ch = Mix_PlayChannel(-1, sample->chunk, looping ? -1 : 0); if (ch == -1) { /* no free channels (or possibly another error) */ return; /* ignore the error, so this sound won't be played */ } Mix_SetPanning(ch, l, r); #endif }
static void I_SDL_UpdateSoundParams(int handle, int vol, int sep) { int left, right; if (!sound_initialized || handle < 0 || handle >= NUM_CHANNELS) { return; } left = ((254 - sep) * vol) / 127; right = ((sep) * vol) / 127; if (left < 0) left = 0; else if ( left > 255) left = 255; if (right < 0) right = 0; else if (right > 255) right = 255; Mix_SetPanning(handle, left, right); }
void Mix_VolPan (int nChannel, int nVolume, int nPan) { #if USE_SDL_MIXER if (!audio.Available ()) return; if (gameOpts->sound.bUseSDLMixer && (nChannel >= 0)) { if (nVolume) { nVolume = (FixMul (nVolume, audio.Volume ()) + (SOUND_MAX_VOLUME / MIX_MAX_VOLUME) / 2) / (SOUND_MAX_VOLUME / MIX_MAX_VOLUME); if (!nVolume) nVolume = 1; Mix_Volume (nChannel, nVolume); if (nPan >= 0) { nPan /= (32767 / 127); Mix_SetPanning (nChannel, (ubyte) nPan, (ubyte) (254 - nPan)); } } } #endif }
static int lua_Mix_SetPanning(State & state){ Stack * stack = state.stack; int channel = MIX_CHANNEL_POST; Uint8 left = 255; Uint8 right = 255; if (stack->is<LUA_TNUMBER>(1)){ channel = stack->to<int>(1); } if (stack->is<LUA_TNUMBER>(2)){ left = static_cast<Uint8>(stack->to<int>(2)); } if (stack->is<LUA_TNUMBER>(3)){ right = static_cast<Uint8>(stack->to<int>(3)); } int result = Mix_SetPanning(channel, left, right); stack->push<bool>(result != 0); return 1; }