26 #include "private/lv_audio_convert.hpp"
27 #include "private/lv_audio_stream.hpp"
28 #include "lv_common.h"
29 #include "lv_fourier.h"
32 #include "lv_util.hpp"
34 #include <unordered_map>
41 typedef std::unique_ptr<AudioChannel> AudioChannelPtr;
47 typedef std::unordered_map<std::string, AudioChannelPtr> ChannelList;
51 void upload_to_channel (std::string
const& name, BufferConstPtr
const& samples, Time
const& timestamp);
53 AudioChannel* get_channel (std::string
const& name)
const;
63 explicit AudioChannel (std::string
const& name);
68 AudioChannel (AudioChannel
const&) =
delete;
69 AudioChannel& operator= (AudioChannel
const&) =
delete;
71 void add_samples (BufferConstPtr
const& samples, Time
const& timestamp);
76 void sample_buffer_mix (BufferPtr
const& dest, BufferPtr
const& src,
float multiplier)
78 visual_return_if_fail (dest->get_size () == src->get_size ());
80 auto dbuf =
static_cast<float*
> (dest->get_data ());
81 auto const sbuf =
static_cast<float const*
> (src->get_data ());
83 std::size_t scnt = dest->get_size () /
sizeof (float);
85 for (
unsigned int i = 0; i < scnt; i++)
86 dbuf[i] += sbuf[i] * multiplier;
91 void Audio::Impl::upload_to_channel (std::string
const& name, BufferConstPtr
const& samples, Time
const& timestamp)
93 if (!get_channel (name)) {
94 channels[name] = make_unique<AudioChannel> (name);
97 channels[name]->add_samples (samples, timestamp);
100 AudioChannel* Audio::Impl::get_channel (std::string
const& name)
const
102 auto entry = channels.find (name);
103 return entry != channels.end () ? entry->second.get () :
nullptr;
106 AudioChannel::AudioChannel (std::string
const& name_)
110 AudioChannel::~AudioChannel ()
115 void AudioChannel::add_samples (BufferConstPtr
const& samples, Time
const& timestamp)
117 stream.write (samples, timestamp);
127 : m_impl {std::move (rhs.m_impl)}
139 m_impl.swap (rhs.m_impl);
145 auto channel = m_impl->get_channel (channel_name);
152 channel->stream.read (buffer, buffer->get_size ());
161 va_start (args, channels);
168 visual_return_if_fail (channels > 0);
170 std::vector<std::string> channel_ids (channels);
172 for (
unsigned int i = 0; i < channels; i++)
173 channel_ids[i] = va_arg (args,
const char *);
175 float factor = 1.0 / channels;
181 for (
unsigned int i = 0; i < channels; i++) {
182 if (
get_sample (channel_samples, channel_ids[i])) {
183 sample_buffer_mix (buffer, channel_samples, factor);
192 va_start (args, channels);
199 visual_return_if_fail (channels > 0);
201 std::vector<std::string> channel_ids (channels);
202 for (
unsigned int i = 0; i < channels; i++) {
203 channel_ids[i] = va_arg (args,
const char *);
206 std::vector<double> channel_factors (channels);
207 for (
unsigned int i = 0; i < channels; i++)
208 channel_factors[i] = va_arg (args,
double);
210 float factor = divide ? (1.0 / channels) : 1.0;
216 for (
unsigned int i = 0; i < channels; i++) {
217 if (
get_sample (channel_samples, channel_ids[i])) {
218 sample_buffer_mix (buffer, channel_samples, channel_factors[i] * factor);
233 void Audio::get_spectrum (
BufferPtr const& buffer, std::size_t samplelen, std::string
const& channel_name,
bool normalised,
float multiplier)
235 get_spectrum (buffer, samplelen, channel_name, normalised);
237 auto data =
static_cast<float*
> (buffer->get_data ());
238 std::size_t datasize = buffer->get_size () /
sizeof (float);
245 DFT dft (buffer->get_size () /
sizeof (float),
246 sample->get_size () /
sizeof (float));
249 dft.
perform (static_cast<float*> (buffer->get_data ()),
250 static_cast<float*> (sample->get_data ()));
253 normalise_spectrum (buffer);
260 auto data =
static_cast<float*
> (buffer->get_data ());
261 std::size_t datasize = buffer->get_size () /
sizeof (float);
266 void Audio::normalise_spectrum (BufferPtr
const& buffer)
268 DFT::log_scale_standard (static_cast<float*> (buffer->get_data ()),
269 static_cast<float*> (buffer->get_data ()),
270 buffer->get_size () /
sizeof (float));
274 VisAudioSampleRateType rate,
275 VisAudioSampleFormatType format,
276 VisAudioSampleChannelType channeltype)
278 auto timestamp = Time::now ();
280 auto sample_count = buffer->get_size () / visual_audio_sample_format_get_size (format);
282 auto converted_buffer =
Buffer::create (sample_count *
sizeof (
float));
284 AudioConvert::convert_samples (converted_buffer,
285 VISUAL_AUDIO_SAMPLE_FORMAT_FLOAT,
289 switch (channeltype) {
290 case VISUAL_AUDIO_SAMPLE_CHANNEL_STEREO: {
294 AudioConvert::deinterleave_stereo_samples (samples1, samples2, converted_buffer, VISUAL_AUDIO_SAMPLE_FORMAT_FLOAT);
296 m_impl->upload_to_channel (VISUAL_AUDIO_CHANNEL_LEFT, samples1, timestamp);
297 m_impl->upload_to_channel (VISUAL_AUDIO_CHANNEL_RIGHT, samples2, timestamp);
309 VisAudioSampleRateType rate,
310 VisAudioSampleFormatType format,
311 std::string
const& channel_name)
313 auto timestamp = Time::now ();
315 auto sample_count = buffer->get_size () / visual_audio_sample_format_get_size (format);
317 auto converted_buffer =
Buffer::create (sample_count *
sizeof (
float));
319 AudioConvert::convert_samples (converted_buffer,
320 VISUAL_AUDIO_SAMPLE_FORMAT_FLOAT,
324 m_impl->upload_to_channel (channel_name, converted_buffer, timestamp);
329 visual_size_t visual_audio_sample_rate_get_length (VisAudioSampleRateType rate)
331 visual_return_val_if_fail (rate < VISUAL_AUDIO_SAMPLE_RATE_LAST, 0);
333 static visual_size_t ratelengthtable[] = {
344 return ratelengthtable[rate];
347 visual_size_t visual_audio_sample_format_get_size (VisAudioSampleFormatType format)
349 visual_return_val_if_fail (format < VISUAL_AUDIO_SAMPLE_FORMAT_LAST, 0);
351 static int formatsizetable[] = {
362 return formatsizetable[format];
365 int visual_audio_sample_format_is_signed (VisAudioSampleFormatType format)
367 visual_return_val_if_fail (format < VISUAL_AUDIO_SAMPLE_FORMAT_LAST, -1);
369 static bool formatsignedtable[] = {
380 return formatsignedtable[format];