2525#include " lv_audio.h"
2626#include " private/lv_audio_convert.hpp"
2727#include " private/lv_audio_stream.hpp"
28+ #include " private/lv_string_hash.hpp"
2829#include " lv_common.h"
2930#include " lv_fourier.h"
3031#include " lv_math.h"
@@ -44,13 +45,13 @@ namespace LV {
4445 {
4546 public:
4647
47- typedef std::unordered_map<std::string, AudioChannelPtr> ChannelList ;
48+ using ChannelList = std::unordered_map<std::string, AudioChannelPtr, StringHash, std::equal_to<>> ;
4849
4950 ChannelList channels;
5051
51- void upload_to_channel (std::string const & name, BufferConstPtr const & samples, Time const & timestamp);
52+ void upload_to_channel (std::string_view name, BufferConstPtr const & samples, Time const & timestamp);
5253
53- AudioChannel* get_channel (std::string const & name) const ;
54+ AudioChannel* get_channel (std::string_view name) const ;
5455 };
5556
5657 class AudioChannel
@@ -60,7 +61,7 @@ namespace LV {
6061 std::string name;
6162 AudioStream stream;
6263
63- explicit AudioChannel (std::string const & name);
64+ explicit AudioChannel (std::string_view name);
6465
6566 ~AudioChannel ();
6667
@@ -88,22 +89,24 @@ namespace LV {
8889
8990 } // anonymous
9091
91- void Audio::Impl::upload_to_channel (std::string const & name, BufferConstPtr const & samples, Time const & timestamp)
92+ void Audio::Impl::upload_to_channel (std::string_view name, BufferConstPtr const & samples, Time const & timestamp)
9293 {
93- if (!get_channel (name)) {
94- channels[name] = std::make_unique<AudioChannel> (name);
94+ auto entry {channels.find (name)};
95+
96+ if (entry == channels.end ()) {
97+ entry = channels.emplace (name, std::make_unique<AudioChannel> (name)).first ;
9598 }
9699
97- channels[name] ->add_samples (samples, timestamp);
100+ entry-> second ->add_samples (samples, timestamp);
98101 }
99102
100- AudioChannel* Audio::Impl::get_channel (std::string const & name) const
103+ AudioChannel* Audio::Impl::get_channel (std::string_view name) const
101104 {
102105 auto entry = channels.find (name);
103106 return entry != channels.end () ? entry->second .get () : nullptr ;
104107 }
105108
106- AudioChannel::AudioChannel (std::string const & name_)
109+ AudioChannel::AudioChannel (std::string_view name_)
107110 : name (name_)
108111 {}
109112
@@ -140,7 +143,7 @@ namespace LV {
140143 return *this ;
141144 }
142145
143- bool Audio::get_sample (BufferPtr const & buffer, std::string const & channel_name)
146+ bool Audio::get_sample (BufferPtr const & buffer, std::string_view channel_name)
144147 {
145148 auto channel = m_impl->get_channel (channel_name);
146149
@@ -220,7 +223,7 @@ namespace LV {
220223 }
221224 }
222225
223- void Audio::get_spectrum (BufferPtr const & buffer, std::size_t samplelen, std::string const & channel_name, bool normalised)
226+ void Audio::get_spectrum (BufferPtr const & buffer, std::size_t samplelen, std::string_view channel_name, bool normalised)
224227 {
225228 auto sample = Buffer::create (samplelen);
226229
@@ -230,7 +233,7 @@ namespace LV {
230233 buffer->fill (0 );
231234 }
232235
233- void Audio::get_spectrum (BufferPtr const & buffer, std::size_t samplelen, std::string const & channel_name, bool normalised, float multiplier)
236+ void Audio::get_spectrum (BufferPtr const & buffer, std::size_t samplelen, std::string_view channel_name, bool normalised, float multiplier)
234237 {
235238 auto spectrum {Buffer::create (buffer->get_size ())};
236239 get_spectrum (spectrum, samplelen, channel_name, normalised);
@@ -314,7 +317,7 @@ namespace LV {
314317 void Audio::input (BufferPtr const & buffer,
315318 VisAudioSampleRateType rate,
316319 VisAudioSampleFormatType format,
317- std::string const & channel_name)
320+ std::string_view channel_name)
318321 {
319322 (void )rate;
320323
0 commit comments