Commit 971b3006 authored by Gerhard Stein's avatar Gerhard Stein

More SDL Mixer goodness

parent 499d062a
Subproject commit 21bf00d0410f5f9ecbe0692eed749db66781ddc3
Subproject commit 870bb712a33da9e645995e441ff6434fa1d078f8
......@@ -38,13 +38,9 @@ SET(USRDIR /usr)
include(oldArch.cmake)
if (CMAKE_VERSION VERSION_LESS "3.1")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set (CMAKE_CXX_FLAGS "--std=gnu++11 ${CMAKE_CXX_FLAGS}")
endif ()
else ()
set (CMAKE_CXX_STANDARD 11)
endif ()
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#Alignment error report is important for android builds
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=cast-align")
......
......@@ -59,11 +59,16 @@ bool CAudioGalaxy::readPCSpeakerSoundintoWaveForm(CSoundSlot &soundslot,
std::vector<Uint8> wave8;
std::vector<Sint16>::iterator it = waveform.begin();
for( ; it != waveform.end(); it++ )
{
wave8.push_back(*it);
}
soundslot.setupWaveForm((Uint8*)&wave8[0], wave8.size()*sizeof(Uint8));
}
else
{
soundslot.setupWaveForm((Uint8*)&waveform[0], waveform.size()*sizeof(Sint16));
}
}
......
......@@ -54,9 +54,7 @@ inline static void CCallback(void *unused, Uint8 *stream, int len)
Audio::Audio() :
m_MusicVolume(SDL_MIX_MAXVOLUME),
m_SoundVolume(SDL_MIX_MAXVOLUME),
mUseSoundBlaster(false),
mPauseGameplay(false)
m_SoundVolume(SDL_MIX_MAXVOLUME)
{
mAudioSpec.channels = 2; // Stereo Sound
mAudioSpec.format = AUDIO_S16; // 16-bit sound
......@@ -144,9 +142,9 @@ bool Audio::init()
Mix_AllocateChannels(channels);
/*mSndChnlVec.clear();
mSndChnlVec.clear();
mSndChnlVec.assign(channels, CSoundChannel(mAudioSpec));*/
mSndChnlVec.assign(channels, CSoundChannel(mAudioSpec));
//SDL_PauseAudio(0);
......@@ -243,11 +241,12 @@ void Audio::destroy()
#else
SDL_LockAudio();
SDL_CloseAudio();
#endif
if(!mMixedForm.empty())
mMixedForm.clear();
#endif
if(!mSndChnlVec.empty())
mSndChnlVec.clear();
......@@ -268,7 +267,7 @@ void Audio::stopAllSounds()
// pauses any currently playing sounds
void Audio::pauseAudio()
{
#if defined(USE_SDLMIXER)
#if !defined(USE_SDLMIXER)
SDL_PauseAudio(1);
#else
Mix_Pause(-1);
......@@ -278,7 +277,7 @@ void Audio::pauseAudio()
// resumes playing a previously paused sound
void Audio::resumeAudio()
{
#if defined(USE_SDLMIXER)
#if !defined(USE_SDLMIXER)
SDL_PauseAudio(0);
#else
Mix_Resume(-1);
......@@ -316,6 +315,24 @@ void Audio::stopSound(const GameSound snd)
}
}
void Audio::setSoundVolume(const Uint8 volume)
{
m_SoundVolume = volume;
#if defined(USE_SDLMIXER)
Mix_Volume(-1, volume);
#endif
}
void Audio::setMusicVolume(const Uint8 volume)
{
m_MusicVolume = volume;
#if defined(USE_SDLMIXER)
Mix_VolumeMusic(volume);
#endif
}
// returns true if a sound is currently playing in SoundPlayMode::PLAY_FORCE mode
bool Audio::forcedisPlaying()
{
......@@ -425,15 +442,21 @@ void Audio::playStereosound(const GameSound snd,
const int speaker_snds_end_off = mpAudioRessources->getNumberofSounds()/2;
if (slotplay >= speaker_snds_end_off)
{
return;
}
if (mUseSoundBlaster && mp_Slots[slotplay+speaker_snds_end_off].getSoundData())
{
slotplay += speaker_snds_end_off;
}
if (mode == SoundPlayMode::PLAY_NORESTART && isPlaying(snd))
{
return;
}
playStereosoundSlot(slotplay, mode, balance);
playStereosoundSlot(slotplay, mode, balance);
}
......@@ -455,9 +478,9 @@ void Audio::playStereosoundSlot(unsigned char slotplay,
stopAllSounds();
}
// first try to find an empty channel
std::vector<CSoundChannel>::iterator sndChnl;
for( sndChnl = mSndChnlVec.begin() ; sndChnl != mSndChnlVec.end() ; sndChnl++)
for( auto sndChnl = mSndChnlVec.begin() ; sndChnl != mSndChnlVec.end() ; sndChnl++)
{
CSoundSlot &currentSlot = *sndChnl->getCurrentSoundPtr();
......@@ -502,9 +525,11 @@ void Audio::unloadSoundData()
#endif
mpAudioRessources.release();
mMixedForm.clear();
#if !defined(USE_SDLMIXER)
mMixedForm.clear();
SDL_UnlockAudio();
#endif
}
......
......@@ -83,8 +83,8 @@ public:
void stopSound(const GameSound snd);
void destroy();
void setSoundVolume(const Uint8 volume) { m_SoundVolume = volume; }
void setMusicVolume(const Uint8 volume) { m_MusicVolume = volume; }
void setSoundVolume(const Uint8 volume);
void setMusicVolume(const Uint8 volume);
Uint8 getSoundVolume() { return m_SoundVolume; }
Uint8 getMusicVolume() { return m_MusicVolume; }
......@@ -134,21 +134,25 @@ protected:
private:
// Channels of sound which can be played at the same time.
std::vector<CSoundChannel> mSndChnlVec;
std::unique_ptr<CAudioResources> mpAudioRessources;
Uint8 m_MusicVolume;
Uint8 m_SoundVolume;
// Boolean that holds if the Soundblaster is to be used. If set false it will use the PC Speaker emulation
bool mUseSoundBlaster;
bool mUseSoundBlaster = false;
std::vector<Uint8> mMixedForm; // Mainly used by the callback function. Declared once and allocated
// for the whole runtime
#if !defined(USE_SDLMIXER)
std::vector<Uint8> mMixedForm; // Mainly used by the callback function. Declared once and allocated // for the whole runtime
#endif
std::map<GameSound, int> sndSlotMap;
COPLEmulator m_OPL_Player;
bool mPauseGameplay;
bool mPauseGameplay = false;
};
#endif /* __AUDIO_H__ */
......@@ -164,8 +164,7 @@ void CMusic::play()
{
#if defined(USE_SDLMIXER)
// play music forever, fading in over 2 seconds
// Mix_Music *music; // I assume this has been loaded already
// Play music forever, fading in over 2 seconds
if(Mix_FadeInMusic(mpMixMusic, -1, 2000) == -1)
{
gLogging.ftextOut("Mix_FadeInMusic: %s\n", Mix_GetError());
......@@ -199,7 +198,7 @@ void CMusic::pause()
void CMusic::stop()
{
#if defined(USE_SDLMIXER)
Mix_FadeOutMusic(3000);
Mix_FadeOutMusic(1000);
#else
if(!mpPlayer)
return;
......
......@@ -11,6 +11,12 @@
#include <cstdio>
#include <cstring>
#include <base/GsLogging.h>
#if defined(USE_SDLMIXER)
#include <SDL_mixer.h>
#endif
CSoundChannel::
CSoundChannel(const SDL_AudioSpec &AudioSpec) :
......@@ -21,6 +27,18 @@ m_AudioSpec(AudioSpec)
void CSoundChannel::stopSound()
{
#if defined(USE_SDLMIXER)
mpCurrentSndSlot = nullptr;
mBalance = 0;
mSoundPtr = 0;
mSoundPaused = true;
mSoundPlaying = false;
#else
SDL_LockAudio();
mpCurrentSndSlot = nullptr;
......@@ -30,11 +48,30 @@ void CSoundChannel::stopSound()
mSoundPlaying = false;
SDL_UnlockAudio();
#endif
}
void CSoundChannel::setupSound( CSoundSlot &SndSlottoPlay,
const bool sound_forced )
{
#if defined(USE_SDLMIXER)
mpCurrentSndSlot = &SndSlottoPlay;
mSoundPlaying = true;
mSoundPtr = 0;
mSoundForced = sound_forced;
auto waveChunk = mpCurrentSndSlot->WaveChunk();
// play sample on first free unreserved channel
// play it exactly once through
if(Mix_PlayChannel(0, waveChunk, 0) == -1)
{
gLogging.ftextOut("Mix_PlayChannel: %s\n", Mix_GetError());
}
#else
SDL_LockAudio();
mpCurrentSndSlot = &SndSlottoPlay;
......@@ -43,6 +80,7 @@ void CSoundChannel::setupSound( CSoundSlot &SndSlottoPlay,
mSoundForced = sound_forced;
SDL_UnlockAudio();
#endif
}
/** \brief This program reads the balance information and balances the stereo sound
......@@ -96,7 +134,7 @@ void CSoundChannel::transintoStereoChannels(T* waveform, const Uint32 len)
void CSoundChannel::readWaveform( Uint8 * const waveform, const Uint32 len )
{
auto snddata = mpCurrentSndSlot->getSoundData();
const Uint32 sndlength = mpCurrentSndSlot->getSoundlength();
const Uint32 sndlength = mpCurrentSndSlot->getSoundlength();
if ((mSoundPtr + len) >= sndlength)
{
......
......@@ -27,8 +27,7 @@ int ov_fopen(char *path,OggVorbis_File *vf);
#endif
CSoundSlot::CSoundSlot() :
m_soundlength(0)
CSoundSlot::CSoundSlot()
{}
#if !(TARGET_OS_IPHONE) || !(TARGET_IPHONE_SIMULATOR)
......@@ -103,13 +102,22 @@ void CSoundSlot::setupWaveForm( Uint8 *buf, Uint32 len )
m_soundlength = len;
mSounddata.resize(m_soundlength);
memcpy(mSounddata.data(), buf, m_soundlength);
#if defined(USE_SDLMIXER)
if(!(mpWaveChunk=Mix_QuickLoad_RAW(mSounddata.data(), mSounddata.size())))
{
gLogging.ftextOut("Mix_QuickLoad_WAV: %s\n", Mix_GetError());
// handle error
}
#endif
}
void CSoundSlot::setupWaveForm( const std::vector<Uint8>& waveform )
{
m_soundlength = waveform.size();
mSounddata.resize(m_soundlength);
memcpy(mSounddata.data(), &waveform[0], m_soundlength);
setupWaveForm( (Uint8*)(&waveform[0]),
Uint32(waveform.size()) );
}
bool CSoundSlot::HQSndLoad(const std::string& gamepath, const std::string& soundname)
......@@ -226,11 +234,17 @@ bool CSoundSlot::HQSndLoad(const std::string& gamepath, const std::string& sound
return true;
}
void CSoundSlot::unload()
{
if(!mSounddata.empty())
{
mSounddata.clear();
}
#if defined(USE_SDLMIXER)
Mix_FreeChunk(mpWaveChunk);
mpWaveChunk = nullptr;
#endif
}
......@@ -13,6 +13,10 @@
#include <vector>
#include <SDL.h>
#if defined(USE_SDLMIXER)
#include <SDL_mixer.h>
#endif
class CSoundSlot {
public:
......@@ -30,14 +34,25 @@ public:
{
return mSounddata.data();
}
#if defined(USE_SDLMIXER)
auto WaveChunk()
{
return mpWaveChunk;
}
#endif
word priority;
private:
std::vector<byte> mSounddata;
unsigned int m_soundlength;
unsigned int m_soundlength = 0;
bool mHasCommonFreqBase;
int mOggFreq;
#if defined(USE_SDLMIXER)
Mix_Chunk *mpWaveChunk = nullptr;
#endif
};
#endif /* CSOUNDSLOT_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment