feat: prefer emplace(_back)

fix: bug that caused local sound to be stopped when allowOverlapping was false
This commit is contained in:
Curve 2021-02-23 23:19:15 +01:00
parent af619515fb
commit 7a70a106b2
No known key found for this signature in database
GPG Key ID: 460F6C466BD35813
6 changed files with 29 additions and 26 deletions

View File

@ -10,7 +10,7 @@ namespace Soundux::Objects
Tab Data::addTab(Tab tab)
{
tab.id = tabs.size();
tabs.push_back(tab);
tabs.emplace_back(tab);
std::unique_lock lock(Globals::gSoundsMutex);
for (auto &sound : tabs.back().sounds)

View File

@ -28,7 +28,7 @@ namespace Soundux
}
void Hotkeys::onKeyDown(int key)
{
pressedKeys.push_back(key);
pressedKeys.emplace_back(key);
if (Globals::gSettings.tabHotkeysOnly)
{
auto tab = Globals::gData.getTab(Globals::gSettings.selectedTab);

View File

@ -21,11 +21,6 @@ namespace Soundux::Objects
const std::optional<Objects::AudioDevice> &playbackDevice,
bool shouldNotReport)
{
if (!Globals::gSettings.allowOverlapping)
{
stopAll();
}
auto *decoder = new ma_decoder;
auto res = ma_decoder_init_file(sound.path.c_str(), nullptr, decoder);
@ -79,9 +74,9 @@ namespace Soundux::Objects
static_cast<double>(config.sampleRate) * 1000);
pSound.id = ++playingSoundIdCounter;
std::unique_lock lock(soundsMutex);
soundsMutex.lock();
playingSounds.insert({device, pSound});
lock.unlock();
soundsMutex.unlock();
Globals::gGui->onSoundPlayed(pSound);
@ -358,7 +353,7 @@ namespace Soundux::Objects
device.name = rawDevice.name;
device.isDefault = rawDevice.name == defaultName;
playBackDevices.push_back(device);
playBackDevices.emplace_back(device);
}
ma_context_uninit(&context);
@ -371,7 +366,7 @@ namespace Soundux::Objects
std::vector<PlayingSound> rtn;
for (const auto &sound : playingSounds)
{
rtn.push_back(sound.second);
rtn.emplace_back(sound.second);
}
return rtn;
@ -383,7 +378,7 @@ namespace Soundux::Objects
std::vector<AudioDevice> rtn;
for (const auto &device : devices)
{
rtn.push_back(device.second);
rtn.emplace_back(device.second);
}
return rtn;
}

View File

@ -26,7 +26,7 @@ auto splitByNewLine(const std::string &str)
std::stringstream ss(str);
for (std::string line; std::getline(ss, line, '\n');)
{
result.push_back(line);
result.emplace_back(line);
}
return result;
};
@ -202,7 +202,7 @@ namespace Soundux::Objects
stream.source = recordingStreams.at(stream.name).source;
}
recordingStreamMutex.unlock_shared();
fetchedStreams.push_back(stream);
fetchedStreams.emplace_back(stream);
}
stream = {};
@ -233,7 +233,7 @@ namespace Soundux::Objects
{
stream.source = recordingStreams.at(stream.name).source;
}
fetchedStreams.push_back(stream);
fetchedStreams.emplace_back(stream);
}
recordingStreamMutex.unlock_shared();
@ -269,7 +269,7 @@ namespace Soundux::Objects
stream.sink = playbackStreams.at(stream.name).sink;
}
playbackStreamMutex.unlock_shared();
fetchedStreams.push_back(stream);
fetchedStreams.emplace_back(stream);
}
stream = {};
@ -296,7 +296,7 @@ namespace Soundux::Objects
{
stream.sink = playbackStreams.at(stream.name).sink;
}
fetchedStreams.push_back(stream);
fetchedStreams.emplace_back(stream);
}
playbackStreamMutex.unlock_shared();
@ -335,7 +335,7 @@ namespace Soundux::Objects
std::vector<PulseRecordingStream> rtn;
for (const auto &stream : recordingStreams)
{
rtn.push_back(stream.second);
rtn.emplace_back(stream.second);
}
return rtn;
}
@ -345,7 +345,7 @@ namespace Soundux::Objects
std::vector<PulsePlaybackStream> rtn;
for (const auto &stream : playbackStreams)
{
rtn.push_back(stream.second);
rtn.emplace_back(stream.second);
}
return rtn;
}

View File

@ -63,7 +63,7 @@ namespace Soundux
void push(const std::function<void()> &item)
{
std::unique_lock lock(queueMutex);
queue.push({nullptr, item});
queue.emplace({nullptr, item});
lock.unlock();
cv.notify_one();
}
@ -80,7 +80,7 @@ namespace Soundux
std::unique_lock lock(queueMutex);
std::unique_lock lock_(unhandledMutex);
unhandled.push_back(identifier);
unhandled.emplace_back(identifier);
queue.push({nullptr, item, identifier});
lock.unlock();
@ -90,7 +90,7 @@ namespace Soundux
{
std::unique_lock lock(queueMutex);
auto status = std::make_shared<std::atomic<bool>>();
queue.push({status, item});
queue.emplace({status, item});
lock.unlock();
cv.notify_one();

View File

@ -62,7 +62,7 @@ namespace Soundux::Objects
sound.hotkeys = oldSound->hotkeys;
}
rtn.push_back(sound);
rtn.emplace_back(sound);
}
std::sort(rtn.begin(), rtn.end(),
@ -102,6 +102,10 @@ namespace Soundux::Objects
{
if (Globals::gPulse.moveApplicationToSinkMonitor(Globals::gSettings.output))
{
if (!Globals::gSettings.allowOverlapping)
{
Globals::gAudio.stopAll();
}
auto playingSound = Globals::gAudio.play(*sound);
auto remotePlayingSound = Globals::gAudio.play(*sound, Globals::gAudio.sinkAudioDevice, true);
@ -121,13 +125,17 @@ namespace Soundux::Objects
return std::nullopt;
}
#else
std::optional<PlayingSound> Window::playSound(const std::uint32_t &id, const std::string &deviceName)
std::optional<PlayingSound> Window::playSound(const std::uint32_t &id)
{
auto sound = Globals::gData.getSound(id);
auto device = Globals::gAudio.getAudioDevice(deviceName);
auto device = Globals::gAudio.getAudioDevice(Globals::gSettings.output);
if (sound && device)
{
if (!Globals::gSettings.allowOverlapping)
{
Globals::gAudio.stopAll();
}
auto playingSound = Globals::gAudio.play(*sound);
auto remotePlayingSound = Globals::gAudio.play(*sound, *device, true);
@ -270,7 +278,7 @@ namespace Soundux::Objects
void Window::onEvent(const std::function<void()> &function)
{
std::unique_lock lock(eventMutex);
eventQueue.push(function);
eventQueue.emplace(function);
lock.unlock();
shouldCheck = true;