Skip to content

Commit

Permalink
Merge pull request #170 from zackthehuman/master
Browse files Browse the repository at this point in the history
Adjustable audio
  • Loading branch information
zackthehuman committed Apr 14, 2014
2 parents e75f9a9 + 18c310a commit 62db2e6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 50 deletions.
30 changes: 18 additions & 12 deletions engine/include/hikari/client/audio/AudioService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ namespace hikari {
static const unsigned int MUSIC_BUFFER_SIZE;
static const unsigned int SAMPLE_BUFFER_SIZE;
static const unsigned int AUDIO_SAMPLE_RATE;
static const float DEFAULT_VOLUME;
bool musicLoaded;
bool samplesLoaded;
bool enabledFlag;
bool mutedFlag;
float sampleVolume;
float musicVolume;

NSFSoundStream musicStream;
NSFSoundStream sampleStream;
Expand All @@ -37,12 +40,15 @@ namespace hikari {

AudioService(const Json::Value &configuration);
virtual ~AudioService();

void playMusic(MusicId id);

void playMusic(const std::string & name);
void stopMusic();
void playSample(SampleId id);
void setMusicVolume(float volume);
float getMusicVolume() const;

void playSample(const std::string & name);
void setSampleVolume(float volume);
float getSampleVolume() const;
void stopAllSamples();

bool isMusicLoaded() const;
Expand All @@ -52,26 +58,26 @@ namespace hikari {
* Disables all audio and stops any playing music and samples. When
* disabled, any requests to play music or samples are ignored.
*
* @see AudioService::enable
* @see AudioService::isEnabled
* @see AudioService::unmute
* @see AudioService::isMuted
*/
void disable();
void mute();

/**
* Enables music and sample playback.
*
* @see AudioService::disable
* @see AudioService::isEnabled
* @see AudioService::mute
* @see AudioService::isMuted
*/
void enable();
void unmute();

/**
* Checks to see if audio is enabled or not. When disabled, any requests
* to play music or samples are ignored.
*
*
* @return true if currently enabled, false otherwise
*/
bool isEnabled() const;
bool isMuted() const;
};

} // hikari
Expand Down
16 changes: 14 additions & 2 deletions engine/include/hikari/client/audio/SoundLibrary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,26 @@ namespace hikari {
* a valid GMESoundStream pointer is returned. Otherwise, a nullptr is
* returned and no music is played.
*/
std::shared_ptr<GMESoundStream> playMusic(const std::string & name);
std::shared_ptr<GMESoundStream> playMusic(const std::string & name, float volume = 100.0f);

/**
* Tries to play a sample by looking it up by name. If the sample is found,
* a valid GMESoundStream pointer is returned. Otherwise, a nullptr is
* returned and no sample is played.
*/
std::shared_ptr<GMESoundStream> playSample(const std::string & name);
std::shared_ptr<GMESoundStream> playSample(const std::string & name, float volume = 100.0f);

/**
* Sets the volume for any currently playing music.
* @param volume the desired volume, typically between 0.0 and 100.0
*/
void setMusicVolume(float volume);

/**
* Sets the volume for any currently playing sample.
* @param volume the desired volume, typically between 0.0 and 100.0
*/
void setSampleVolume(float volume);

void stopMusic();
void stopSample();
Expand Down
72 changes: 40 additions & 32 deletions engine/src/hikari/client/audio/AudioService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ namespace hikari {
const unsigned int AudioService::MUSIC_BUFFER_SIZE = 2048 * 2; // some platforms need larger buffer
const unsigned int AudioService::SAMPLE_BUFFER_SIZE = 2048 * 2; // so we'll double it for now.
const unsigned int AudioService::AUDIO_SAMPLE_RATE = 44000;
const float AudioService::DEFAULT_VOLUME = 100.0f;

AudioService::AudioService(const Json::Value &configuration)
: musicLoaded(false)
, samplesLoaded(false)
, enabledFlag(true)
, mutedFlag(false)
, sampleVolume(DEFAULT_VOLUME)
, musicVolume(DEFAULT_VOLUME)
, musicStream(MUSIC_BUFFER_SIZE, 1)
, sampleStream(SAMPLE_BUFFER_SIZE, 12)
, library(nullptr)
Expand All @@ -40,27 +43,19 @@ namespace hikari {
}

bool AudioService::isValidConfiguration(const Json::Value &configuration) const {
bool valid = configuration.isMember("music")
&& configuration.isMember("samples")
bool valid = configuration.isMember("music")
&& configuration.isMember("samples")
&& configuration.isMember("library")
&& configuration["music"].isString()
&& configuration["music"].isString()
&& configuration["samples"].isString()
&& configuration["library"].isString();

return valid;
}

void AudioService::playMusic(MusicId id) {
// if(isEnabled() && isMusicLoaded()) {
// musicStream.stop();
// musicStream.setCurrentTrack(id);
// musicStream.play();
// }
}

void AudioService::playMusic(const std::string & name) {
if(isEnabled() && library->isEnabled()) {
const auto stream = library->playMusic(name);
if(library->isEnabled()) {
const auto stream = library->playMusic(name, getMusicVolume());

if(stream) {
stream->play();
Expand All @@ -69,31 +64,44 @@ namespace hikari {
}

void AudioService::stopMusic() {
// musicStream.stopAllSamplers();
// musicStream.stop();
library->stopMusic();
}

void AudioService::playSample(SampleId id) {
// if(isEnabled() && isSamplesLoaded()) {
// sampleStream.setCurrentTrack(id);
// sampleStream.play();
// }
void AudioService::setMusicVolume(float volume) {
musicVolume = volume;

if(library->isEnabled()) {
library->setMusicVolume(musicVolume);
}
}

float AudioService::getMusicVolume() const {
return isMuted() ? 0.0f : musicVolume;
}

void AudioService::playSample(const std::string & name) {
if(isEnabled() && library->isEnabled()) {
const auto stream = library->playSample(name);
if(library->isEnabled()) {
const auto stream = library->playSample(name, getSampleVolume());

if(stream) {
stream->play();
}
}
}

void AudioService::setSampleVolume(float volume) {
sampleVolume = volume;

if(library->isEnabled()) {
library->setSampleVolume(sampleVolume);
}
}

float AudioService::getSampleVolume() const {
return isMuted() ? 0.0f : sampleVolume;
}

void AudioService::stopAllSamples() {
// sampleStream.stop();
library->stopSample();
}

Expand All @@ -105,19 +113,19 @@ namespace hikari {
return samplesLoaded;
}

void AudioService::disable() {
enabledFlag = false;
void AudioService::mute() {
mutedFlag = false;

stopAllSamples();
stopMusic();
// stopAllSamples();
// stopMusic();
}

void AudioService::enable() {
enabledFlag = true;
void AudioService::unmute() {
mutedFlag = true;
}

bool AudioService::isEnabled() const {
return enabledFlag;
bool AudioService::isMuted() const {
return mutedFlag;
}

} // hikari
25 changes: 21 additions & 4 deletions engine/src/hikari/client/audio/SoundLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace hikari {

if(reader.parse(*fileContents, root, false)) {
auto nsfCount = root.size();

for(decltype(nsfCount) samplerIndex = 0; samplerIndex < nsfCount; ++samplerIndex) {
const Json::Value & currentLibrary = root[samplerIndex];
const std::string nsfFile = currentLibrary[PROP_FILE].asString();
Expand Down Expand Up @@ -121,7 +121,7 @@ namespace hikari {
return isEnabledFlag;
}

std::shared_ptr<GMESoundStream> SoundLibrary::playMusic(const std::string & name) {
std::shared_ptr<GMESoundStream> SoundLibrary::playMusic(const std::string & name, float volume) {
const auto & iterator = music.find(name);

if(iterator != std::end(music)) {
Expand All @@ -133,6 +133,7 @@ namespace hikari {

stopMusic();
stream->setCurrentTrack(musicEntry->track);
stream->setVolume(volume);
stream->play();

return stream;
Expand All @@ -143,7 +144,7 @@ namespace hikari {
return std::shared_ptr<GMESoundStream>(nullptr);
}

std::shared_ptr<GMESoundStream> SoundLibrary::playSample(const std::string & name) {
std::shared_ptr<GMESoundStream> SoundLibrary::playSample(const std::string & name, float volume) {
const auto & iterator = samplePlayers.find(name);

if(iterator != std::end(samplePlayers)) {
Expand All @@ -160,6 +161,7 @@ namespace hikari {
});

// Play (or restart) the sound we want to play
player->setVolume(volume);
player->play();
} else {
HIKARI_LOG(debug4) << "Didn't find the sample '" << name << "'.";
Expand All @@ -168,6 +170,21 @@ namespace hikari {
return std::shared_ptr<GMESoundStream>(nullptr);
}

void SoundLibrary::setMusicVolume(float volume) {
std::for_each(std::begin(samplers), std::end(samplers), [volume](const std::shared_ptr<GMESoundStream> & musicSampler) {
musicSampler->setVolume(volume);
});
}

void SoundLibrary::setSampleVolume(float volume) {
std::for_each(std::begin(samplePlayers), std::end(samplePlayers), [volume](decltype(samplePlayers)::value_type & pair) {
const auto & otherPlayer = pair.second;
const std::shared_ptr<sf::Sound> player = otherPlayer->player;

player->setVolume(volume);
});
}

void SoundLibrary::stopMusic() {
std::for_each(std::begin(samplers), std::end(samplers), [](const std::shared_ptr<GMESoundStream> & musicSampler) {
musicSampler->stop();
Expand All @@ -178,7 +195,7 @@ namespace hikari {
std::for_each(std::begin(samplePlayers), std::end(samplePlayers), [](decltype(samplePlayers)::value_type & pair) {
const auto & otherPlayer = pair.second;
const std::shared_ptr<sf::Sound> player = otherPlayer->player;

player->stop();
});
}
Expand Down

0 comments on commit 62db2e6

Please sign in to comment.