/****************************************************************************** special effects ******************************************************************************/ int SoundPanChannelFloat(int channel, float panning) { if(Mix_Playing(channel)) { int left, right, offset; panning = ClampFloat(panning, -1.0, 1.0); offset = (int)(panning * 127); if (panning > 0) { right = 127 + offset; left = 127 - offset; } else if (panning < 0) { offset = -offset; left = 127 + offset; right = 127 - offset; } else { left = 127; right = 127; } //printf("Left: %d, Right: %d\n", left, right); if(Mix_SetPanning(channel, left, right)) return 1; else return 0; //TODO: error checking? } else return 0; }
_Mix_Channel *_StartMusic(int which, int is_fading, Mix_Music* music) { _Mix_Channel* channel = NULL; /* Lock the mixer while modifying the playing channels */ SDL_LockAudio(); { if ( which == MUSIC_COMPAT_MAGIC_CHANNEL ) { channel = &mix_music_compat_channel; } else if ( which >= 0 && which < num_channels ) { channel = &mix_channel[which]; } /* Queue up the audio data for this channel */ if ( channel != NULL ) { if (Mix_Playing(which)) _Mix_channel_done_playing(which); if (!channel->is_music) { free(channel->sound); channel->sound = NULL; channel->is_music = SDL_TRUE; } channel->music = music; channel->playing = 1; channel->looping = -1; channel->paused = 0; if (is_fading){ channel->fading = MIX_FADING_IN; channel->fade_volume_reset = channel->volume; channel->volume = 0; } else { channel->fading = MIX_NO_FADING; } } } SDL_UnlockAudio(); return channel; }
int get_mixer_channel (void) { #ifdef WITH_SOUND { int i; for (i = 8; i < 16; i++) { if (Mix_Playing (i) == 0) { if (c_sample[i] != NULL) { Mix_FreeChunk (c_sample[i]); c_sample[i] = NULL; } return i; } } return -1; } #endif /* WITH_SOUND */ return -1; }
d_define_method(track, play_fade_in)(struct s_object *self, t_boolean restart, int delay) { d_using(track); t_boolean start_required = d_true; int channel; if ((track_attributes->channel != d_track_auto_channel) && ((Mix_Playing(track_attributes->channel)) || (Mix_Paused(track_attributes->channel)))) { if (restart) Mix_HaltChannel(track_attributes->channel); else { if (Mix_Paused(track_attributes->channel)) Mix_Resume(track_attributes->channel); start_required = d_false; } } if (start_required) { if (track_attributes->auto_channel) channel = d_track_auto_channel; else channel = track_attributes->next_channel; d_call(self, m_track_set_volume, track_attributes->volume); if ((track_attributes->channel = Mix_FadeInChannel(channel, track_attributes->chunk, track_attributes->loops, delay)) != -1) d_call(self, m_track_set_position, track_attributes->angle, track_attributes->distance); } return self; }
/////////////////////////////////////////////////////////// // SE play /////////////////////////////////////////////////////////// void Audio::SE_Play(std::string file, int volume, int pitch) { it_sounds = sounds.begin(); while (it_sounds != sounds.end()) { if (!Mix_Playing(it_sounds->first)) { Mix_FreeChunk(it_sounds->second); sounds.erase(it_sounds++); } else { it_sounds++; } } if (sounds.size() >= 7) return; std::string path = FileFinder::FindMusic(file); if (path.empty()) ARGSS::AError::FileNotFound(file); Mix_Chunk* sound = Mix_LoadWAV(path.c_str()); if (!sound) ARGSS::AError::AudioNotLoad("SE", file, Mix_GetError()); int channel = Mix_PlayChannel(-1, sound, 0); Mix_Volume(channel, volume * MIX_MAX_VOLUME / 100); if (channel == -1) ARGSS::AError::AudioNotPlay("SE", file, Mix_GetError()); sounds[channel] = sound; }
bool Sound::isPlaying() const { return Mix_Playing(_channel) != 0; }
void SoundManager::ProcessRedbook() { if (!g_theProfileDB->IsUseRedbookAudio()) return; if (!m_musicEnabled) return; if (GetTickCount() > m_timeToCheckCD) { #if defined(USE_SDL) CDstatus status; if (m_cdrom) { status = SDL_CDStatus(m_cdrom); #else U32 status; if (m_redbook) { status = AIL_redbook_status(m_redbook); #endif switch (status) { #if !defined(USE_SDL) case REDBOOK_ERROR: #else case CD_TRAYEMPTY: break; case CD_ERROR: #endif break; #if !defined(USE_SDL) case REDBOOK_PLAYING: #else case CD_PLAYING: #endif break; #if !defined(USE_SDL) case REDBOOK_PAUSED: #else case CD_PAUSED: #endif break; #if !defined(USE_SDL) case REDBOOK_STOPPED: #else case CD_STOPPED: #endif if (m_curTrack != -1) PickNextTrack(); if (m_curTrack != -1 && !m_stopRedbookTemporarily) StartMusic(m_curTrack); break; } } m_timeToCheckCD = GetTickCount() + k_CHECK_CD_PERIOD; } } void SoundManager::Process(const uint32 &target_milliseconds, uint32 &used_milliseconds) { CivSound *sound; sint32 start_time_ms = GetTickCount(); if ((m_noSound) ||(m_usePlaySound)) { used_milliseconds = GetTickCount() - start_time_ms; return; } if (m_sfxSounds->GetCount() > 0) { m_soundWalker->SetList(m_sfxSounds); while (m_soundWalker->IsValid()) { sound = m_soundWalker->GetObj(); Assert(sound); if (!sound) continue; if (sound->IsPlaying()) { #if !defined(USE_SDL) if (AIL_quick_status(sound->GetHAudio()) == QSTAT_DONE) { #else if (!Mix_Playing(sound->GetChannel())) { #endif m_soundWalker->Remove(); delete sound; } else { m_soundWalker->Next(); } } } } if (m_voiceSounds->GetCount() > 0) { m_soundWalker->SetList(m_voiceSounds); while (m_soundWalker->IsValid()) { sound = m_soundWalker->GetObj(); Assert(sound); if (!sound) continue; if (sound->IsPlaying()) { #if !defined(USE_SDL) if (AIL_quick_status(sound->GetHAudio()) == QSTAT_DONE) { #else if ((-1 == sound->GetChannel()) || (!Mix_Playing(sound->GetChannel()))) { #endif m_soundWalker->Remove(); delete sound; } else { m_soundWalker->Next(); } } } } ProcessRedbook(); used_milliseconds = GetTickCount() - start_time_ms; } bool FindSoundinList(PointerList<CivSound> * sndList, sint32 soundID) { for ( PointerList<CivSound>::Walker walk(sndList); walk.IsValid(); walk.Next() ) { if (walk.GetObj()->GetSoundID() == soundID) { return true; } } return false; } void SoundManager::AddGameSound(const GAMESOUNDS &sound) { sint32 id = gamesounds_GetGameSoundID(sound); AddSound(SOUNDTYPE_SFX, 0, id, 0, 0); }
Weapon::~Weapon() { if (m_reloadSndCh != -1 && Mix_Playing(m_reloadSndCh) != 0) { Mix_HaltChannel(m_reloadSndCh); } }
void SdlAudio::SE_Stop() { for (sounds_type::iterator i = sounds.begin(); i != sounds.end(); ++i) { if (Mix_Playing(i->first)) Mix_HaltChannel(i->first); } sounds.clear(); }
void SdlAudio::BGS_Stop() { if (Mix_Playing(bgs_channel)) { Mix_HaltChannel(bgs_channel); bgs_playing = false; } }
bool sfx::is_channel_playing( int channel ) { return Mix_Playing( channel ) != 0; }
// --- bool SDLSound::isPlaying () { return (Mix_Playing (_realChannel) != 0); }
bool CSoundEffect::IsPlaying() { return Mix_Playing(Channel); }
/////////////////////////////////////////////////////////// // BGS stop /////////////////////////////////////////////////////////// void Audio::BGS_Stop() { if (Mix_Playing(bgs_channel)) Mix_HaltChannel(bgs_channel); }
//PUBLIC void Sounds::playFootsteps() { if (!Mix_Playing(0)) { Mix_PlayChannel(0, footsteps, 0); } }
int show_sound_playing(int channel) { if (channel == MUSIC_CHANNEL) { return Mix_PlayingMusic() != 0; } return Mix_Playing(channel); }
void SoundHubObject::PreFrame() { const int playing = Mix_Playing( -1 ); if( playing < m_SoundsPlaying ) _UpdateSounds( playing - m_SoundsPlaying ); }
// --- int SDLSoundSystem::channelsPlaying () { return (Mix_Playing (-1)); }
// --- bool SDLSoundSystem::isChannelPlaying (int c) { return (Mix_Playing (c) == 1 ? true : false); }
bool glSound::isPlaying() { return Mix_Playing(channel); }
bool GP2DSoundClip::isPlaying() { // not an accurate function. return Mix_Playing(m_channel); }
inline int CheckSoundMem(int soundid) // 0: stopped; 1: playing { return Mix_Playing(soundid); }
void SdlAudio::BGS_Pause() { if (Mix_Playing(bgs_channel)) { Mix_Pause(bgs_channel); } }
void SoundPlayer::playPaddleSwing() { if (Mix_Playing(1) == 0 && !mute){ Mix_PlayChannel(1, paddleSwing, 0); } }
void SdlAudio::ME_Stop() { if (Mix_Playing(me_channel)) { Mix_HaltChannel(me_channel); } }
bool Sound::isChannelPlaying (int channel) { if (channel < 0) return false; return Mix_Playing(channel); }
void SoundPlayer::playScore() { if (Mix_Playing(2) == 0 && !mute) Mix_PlayChannel(2, score, 0); }
int menu(SDL_Surface *screen, SDL_Surface *vscreen) { unsigned frameStart; bool leave = false; // Load and prepare the background image SDL_Surface *loadedImage = IMG_Load("title.png"); if (!loadedImage) { fprintf(stderr, "Could not load title image.\n"); return MENU_ERROR; } SDL_Surface *background = SDL_DisplayFormatAlpha(loadedImage); SDL_FreeSurface(loadedImage); TTF_Font *fontSmall = TTF_OpenFont("FreeSansBold.ttf", 10); TTF_Font *fontLarge = TTF_OpenFont("FreeSansBold.ttf", 12); if (!fontSmall || !fontLarge) { fprintf(stderr, "Could not load font.\n"); return MENU_ERROR; } SDL_Color playerFontColour, autuinFontColour; playerFontColour.r = playerFontColour.g = playerFontColour.b = 230; autuinFontColour.r = autuinFontColour.g = autuinFontColour.b = 200; SDL_Surface *onePlayerTextSmall = TTF_RenderText_Solid(fontSmall, "ONE PLAYER", playerFontColour); SDL_Surface *twoPlayerTextSmall = TTF_RenderText_Solid(fontSmall, "TWO PLAYER", playerFontColour); SDL_Surface *onePlayerTextLarge = TTF_RenderText_Solid(fontLarge, "ONE PLAYER", playerFontColour); SDL_Surface *twoPlayerTextLarge = TTF_RenderText_Solid(fontLarge, "TWO PLAYER", playerFontColour); SDL_Surface *autuinText = TTF_RenderText_Solid(fontSmall, "(C) 2012 AUTOMATIC TURQUOISE INTERNATIONAL", autuinFontColour); TTF_CloseFont(fontSmall); TTF_CloseFont(fontLarge); SDL_Rect onePlayerRectSmall, onePlayerRectLarge; SDL_Rect twoPlayerRectSmall, twoPlayerRectLarge; onePlayerRectSmall.x = 60; onePlayerRectLarge.x = 50; twoPlayerRectSmall.x = 190; twoPlayerRectLarge.x = 180; onePlayerRectSmall.y = twoPlayerRectSmall.y = 172; onePlayerRectLarge.y = twoPlayerRectLarge.y = 170; SDL_Rect autuinRect; autuinRect.x = 35; autuinRect.y = 187; Uint32 black = SDL_MapRGB(vscreen->format, 0, 0, 0); Mix_Chunk *boop = Mix_LoadWAV("boop.wav"); Mix_Chunk *theme = Mix_LoadWAV("title.wav"); if (!boop || !theme) { fprintf(stderr, "Could not load audio.\n"); return MENU_ERROR; } // Initialize the star locations and speeds Star stars[NUM_STARS]; for (int i=0; i<NUM_STARS; i++) { stars[i].x = random() / ((float) RAND_MAX) * SCREEN_WIDTH; stars[i].y = random() / ((float) RAND_MAX) * SCREEN_HEIGHT; float intensity = random() / (float) RAND_MAX; unsigned char colourIntensity = (intensity * 155) + 100; stars[i].speed = intensity * .2; stars[i].colour = SDL_MapRGB(vscreen->format, colourIntensity, colourIntensity, colourIntensity); } Mix_PlayChannel(-1, theme, -1); char selectedOption = 0; unsigned int frame = 0; while (!leave) { // Handle events frameStart = SDL_GetTicks(); SDL_Event e; while (SDL_PollEvent(&e)) switch (e.type) { case SDL_KEYDOWN: switch (e.key.keysym.sym) { case SDLK_UP: case SDLK_LEFT: if (selectedOption != 0) Mix_PlayChannel(-1, boop, 0); selectedOption = 0; frame = 0; break; case SDLK_DOWN: case SDLK_RIGHT: if (selectedOption != 1) Mix_PlayChannel(-1, boop, 0); selectedOption = 1; frame = 0; break; case SDLK_ESCAPE: selectedOption = -1; leave = true; break; case SDLK_UNKNOWN: // Suspend button selectedOption = -2; leave = true; break; case SDLK_RETURN: leave = true; break; case SDL_QUIT: selectedOption = -1; leave = true; break; } } // Debounce any keys currently down (wait 1 sec) if (frame >= FPS && controllerHasData()) { unsigned int c = readController(); if ((c & P1_LEFT) && selectedOption != 0) { Mix_PlayChannel(-1, boop, 0); selectedOption = 0; } else if ((c & P1_RIGHT) && selectedOption != 1) { Mix_PlayChannel(-1, boop, 0); selectedOption = 1; } else if (c & P1_FIRE) { leave = true; } else if (c & SELECT) { selectedOption = -1; leave = true; } frame = FPS; } // Draw the frame SDL_FillRect(vscreen, NULL, black); SDL_LockSurface(vscreen); Uint32 *s = (Uint32 *) vscreen->pixels; for (unsigned int i=0; i<NUM_STARS; i++) { unsigned x = stars[i].x, y = stars[i].y; *(s + (y * SCREEN_WIDTH) + x) = stars[i].colour; stars[i].y += stars[i].speed; if (stars[i].y >= SCREEN_HEIGHT) stars[i].y = 0; } SDL_UnlockSurface(vscreen); SDL_BlitSurface(background, NULL, vscreen, NULL); SDL_BlitSurface(autuinText, NULL, vscreen, &autuinRect); SDL_BlitSurface( selectedOption==0?onePlayerTextLarge:onePlayerTextSmall, NULL, vscreen, selectedOption==0?&onePlayerRectLarge:&onePlayerRectSmall ); SDL_BlitSurface( selectedOption==1?twoPlayerTextLarge:twoPlayerTextSmall, NULL, vscreen, selectedOption==1?&twoPlayerRectLarge:&twoPlayerRectSmall ); // Double up the resolution SDL_Surface *zoomed = zoomSurface(vscreen, 2, 2.31, 0); SDL_BlitSurface(zoomed, NULL, screen, NULL); SDL_FreeSurface(zoomed); // Blit the frame SDL_Flip(screen); // Go to sleep SDL_Delay(FRAME_TICKS - (SDL_GetTicks() - frameStart)); frame++; if (frame > FPS * 30) { attractor(screen, vscreen); frame = 0; } } // Stop the audio Mix_HaltChannel(-1); // One last boop for the quit/play action Mix_PlayChannel(-1, boop, 0); while(Mix_Playing(-1) != 0) SDL_Delay(10); // Free resources SDL_FreeSurface (background); SDL_FreeSurface (autuinText); SDL_FreeSurface (onePlayerTextSmall); SDL_FreeSurface (twoPlayerTextSmall); SDL_FreeSurface (onePlayerTextLarge); SDL_FreeSurface (twoPlayerTextLarge); Mix_FreeChunk(boop); Mix_FreeChunk(theme); switch (selectedOption) { case -2: return MENU_SUSPEND; case -1: return MENU_QUIT; case 0: return MENU_ONEPLAYER; case 1: return MENU_TWOPLAYER; } }
int Sound::getNumberOfChannelsPlaying () { return Mix_Playing(-1); }
void SoundPlayer::playShipHit() { if (Mix_Playing(3) == 0 && !mute) Mix_PlayChannel(3, shipHit, 0); }