1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/*
* Mixer.cpp
*
* Created on: 11 Apr 2015
* Author: jeremy
*/
#include "Mixer.h"
#include <algorithm>
namespace Sound
{
Mixer::Mixer() noexcept
{
return;
}
Mixer::~Mixer()
{
return;
}
void Mixer::PlaySound(int id, float volume)
{
auto s = std::find_if(playList.begin(), playList.end(), [&id](SoundState& s)
{
return id == s.id && !s.isPlaying.load(std::memory_order_relaxed);
});
if(s != playList.end())
{
// The sound is already in playList
//soundBank->sounds[s->id].Seek(0);
s->volume = volume;
s->index = 0;
s->isPlaying.store(true, std::memory_order_release);
}
else
{
// Add sound to playList
//playList.emplace_back(id, volume, true);
const auto index = playListIndex.fetch_add(1, std::memory_order_relaxed);
// Protect from overflow (ignore new sound...)
if(index >= playList.size())
{
return;
}
playList[index].id = id;
playList[index].volume = volume;
playList[index].isPlaying.store(true, std::memory_order_release);
}
return;
}
void Mixer::StopSound(int id)
{
// Stop sound in the play list
for(auto& sound : playList)
{
if(sound.id == id)
{
sound.isPlaying.store(false, std::memory_order_relaxed);
}
}
return;
}
void Mixer::Mix(std::vector<short>& buffer) noexcept
{
// Fill buffer with zeros
std::fill(buffer.begin(), buffer.end(), 0);
std::size_t periodSize = buffer.size();
// Mix sounds
for(std::size_t si = 0; si < playList.size(); si++)
{
if(playList[si].isPlaying.load(std::memory_order_acquire))
{
SoundState& soundState = playList[si];
Sound& sound = soundBank->sounds[soundState.id];
if(sound.HasMoreData(soundState.index, periodSize))
{
const float volume = sound.GetVolume();
const float mix_volume = soundState.volume;
if(sound.IsLoop())
{
std::size_t prevIdx = sound.LoadIndex();
if(prevIdx % sound.GetLength() < periodSize)
{
sound.SetStartTime();
}
for(std::size_t i = 0; i < periodSize; i++)
{
buffer[i] += volume * mix_volume * sound.GetValue(i + soundState.index);
}
sound.StoreIndex(soundState.index);
}
else
{
const short* data = sound.GetData();
const std::size_t idx = soundState.index;
for(std::size_t i = 0; i < periodSize; i++)
{
buffer[i] += volume * mix_volume * data[idx + i];
}
}
soundState.index += periodSize;
//sound.AddToIndex(periodSize);
}
else
{
soundState.isPlaying.store(false, std::memory_order_relaxed);
}
}
}
return;
}
/**
* @brief Stops all sounds (must be done after the mixer has been stopped).
*
*/
void Mixer::Clear() noexcept
{
playListIndex.store(0, std::memory_order_relaxed);
for(auto& s : playList)
{
s.isPlaying.store(false, std::memory_order_relaxed);
}
}
}
|