summaryrefslogtreecommitdiff
path: root/Source/Core/AudioCommon/Microphone.cpp
blob: aa656e4428bdd3e7b013b36ee2eb510a7d758200 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "AudioCommon/Microphone.h"

#include <algorithm>
#include <cmath>
#include <ranges>
#include <span>

#ifdef HAVE_CUBEB
#include <cubeb/cubeb.h>

#include "AudioCommon/CubebUtils.h"
#endif

#include "Common/Logging/Log.h"
#include "Common/MathUtil.h"
#include "Common/Swap.h"
#include "Core/Core.h"
#include "Core/System.h"

#ifdef _WIN32
#include <objbase.h>
#endif

#ifdef ANDROID
#include "jni/AndroidCommon/IDCache.h"
#endif

namespace AudioCommon
{
#ifdef HAVE_CUBEB
Microphone::Microphone(const MicrophoneState& sampler, std::string worker_name)
    : m_sampler(sampler), m_worker(std::move(worker_name))
{
}
#else
Microphone::Microphone(const MicrophoneState& sampler, std::string worker_name) : m_sampler(sampler)
{
}
#endif

Microphone::~Microphone()
{
  StreamTerminate();
}

void Microphone::Initialize()
{
  StreamInit();
}

#ifndef HAVE_CUBEB
void Microphone::StreamInit()
{
}

void Microphone::StreamStart(u32 /* sampling_rate */)
{
}

void Microphone::StreamStop()
{
}

void Microphone::StreamTerminate()
{
}
#else
void Microphone::StreamInit()
{
  if (!m_worker.Execute([this] { m_cubeb_ctx = CubebUtils::GetContext(); }))
  {
    ERROR_LOG_FMT(AUDIO, "Failed to init microphone stream");
    return;
  }

  // TODO: Not here but rather inside the WiiSpeak device if possible?
  StreamStart(m_sampler.GetDefaultSamplingRate());
}

void Microphone::StreamTerminate()
{
  StreamStop();

  if (m_cubeb_ctx)
    m_worker.Execute([this] { m_cubeb_ctx.reset(); });
}

static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state)
{
}

void Microphone::StreamStart(u32 sampling_rate)
{
  if (!m_cubeb_ctx)
    return;

  m_worker.Execute([this, sampling_rate] {
#ifdef ANDROID
    JNIEnv* env = IDCache::GetEnvForThread();
    if (jboolean result = env->CallStaticBooleanMethod(
            IDCache::GetPermissionHandlerClass(),
            IDCache::GetPermissionHandlerHasRecordAudioPermission(), nullptr);
        result == JNI_FALSE)
    {
      env->CallStaticVoidMethod(IDCache::GetPermissionHandlerClass(),
                                IDCache::GetPermissionHandlerRequestRecordAudioPermission(),
                                nullptr);
    }
#endif

    cubeb_stream_params params{};
    params.format = CUBEB_SAMPLE_S16LE;
    params.rate = sampling_rate;
    params.channels = 1;
    params.layout = CUBEB_LAYOUT_MONO;

    std::lock_guard lock(m_ring_lock);
    u32 minimum_latency;
    if (cubeb_get_min_latency(m_cubeb_ctx.get(), &params, &minimum_latency) != CUBEB_OK)
    {
      WARN_LOG_FMT(AUDIO, "Error getting minimum latency");
      minimum_latency = 16;
    }

    cubeb_devid input_device = CubebUtils::GetInputDeviceById(GetInputDeviceId());
    if (cubeb_stream_init(m_cubeb_ctx.get(), &m_cubeb_stream, GetCubebStreamName().c_str(),
                          input_device, &params, nullptr, nullptr,
                          std::max<u32>(16, minimum_latency), CubebDataCallback, StateCallback,
                          this) != CUBEB_OK)
    {
      ERROR_LOG_FMT(AUDIO, "Error initializing cubeb stream");
      return;
    }

    if (cubeb_stream_start(m_cubeb_stream) != CUBEB_OK)
    {
      ERROR_LOG_FMT(AUDIO, "Error starting cubeb stream");
      return;
    }

    m_stream_buffer.resize(GetStreamSize());
    m_stream_rpos = 0;
    m_stream_wpos = 0;
    INFO_LOG_FMT(AUDIO, "started cubeb stream");
  });
}

void Microphone::StreamStop()
{
  if (!m_cubeb_stream)
    return;

  m_worker.Execute([this] {
    if (cubeb_stream_stop(m_cubeb_stream) != CUBEB_OK)
      ERROR_LOG_FMT(AUDIO, "Error stopping cubeb stream");
    cubeb_stream_destroy(m_cubeb_stream);
    m_cubeb_stream = nullptr;
  });
}

long Microphone::CubebDataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
                                   void* /*output_buffer*/, long nframes)
{
  // Skip data when core isn't running
  if (Core::GetState(Core::System::GetInstance()) != Core::State::Running)
    return nframes;

  auto* mic = static_cast<Microphone*>(user_data);

  // Skip data when HLE Wii Speak is muted
  // TODO: Update cubeb and use cubeb_stream_set_input_mute
  if (mic->IsMicrophoneMuted())
    return nframes;

  return mic->DataCallback(static_cast<const SampleType*>(input_buffer), nframes);
}

void Microphone::OnOverflow()
{
  m_samples_avail = GetStreamSize();
}

long Microphone::DataCallback(const SampleType* input_buffer, long nframes)
{
  std::lock_guard lock(m_ring_lock);

  // Skip data if sampling is off or mute is on
  if (!m_sampler.IsSampleOn() || m_sampler.IsMuted())
    return nframes;

  std::span<const SampleType> buffer(input_buffer, nframes);
  const auto gain = ComputeGain(GetVolumeModifier());
  const auto apply_gain = [gain](SampleType sample) {
    return MathUtil::SaturatingCast<SampleType>(sample * gain);
  };

  const u32 stream_size = GetStreamSize();
  const bool are_samples_byte_swapped = AreSamplesByteSwapped();
  for (const SampleType le_sample : std::ranges::transform_view(buffer, apply_gain))
  {
    UpdateLoudness(le_sample);
    m_stream_buffer[m_stream_wpos] =
        are_samples_byte_swapped ? Common::swap16(le_sample) : le_sample;
    m_stream_wpos = (m_stream_wpos + 1) % stream_size;
  }

  m_samples_avail += nframes;
  if (m_samples_avail > stream_size)
  {
    OnOverflow();
  }

  return nframes;
}
#endif

u16 Microphone::ReadIntoBuffer(u8* ptr, u32 size)
{
  const u32 samples_per_read = GetBufferSizeSamples();
  const u32 bytes_per_read = samples_per_read * sizeof(SampleType);

  std::lock_guard lock(m_ring_lock);

  const u32 stream_size = GetStreamSize();
  u8* const begin = ptr;
  for (u8* const end = begin + size; ptr < end; ptr += bytes_per_read, size -= bytes_per_read)
  {
    if (size < bytes_per_read || m_samples_avail < samples_per_read)
      break;

    SampleType* last_buffer = &m_stream_buffer[m_stream_rpos];
    std::memcpy(ptr, last_buffer, bytes_per_read);

    m_samples_avail -= samples_per_read;
    m_stream_rpos += samples_per_read;
    m_stream_rpos %= stream_size;
  }
  return static_cast<u16>(ptr - begin);
}

u16 Microphone::GetLoudnessLevel() const
{
  if (m_sampler.IsMuted() || IsMicrophoneMuted())
    return 0;
  return m_loudness_level;
}

// Based on graphical cues on Monster Hunter 3, the level seems properly displayed with values
// between 0 and 0x3a00.
//
// TODO: Proper hardware testing, documentation, formulas...
void Microphone::UpdateLoudness(const SampleType sample)
{
  // Based on MH3 graphical cues, let's use a 0x4000 window
  static const u32 WINDOW = 0x4000;
  static const FloatType UNIT = (m_loudness.DB_MAX - m_loudness.DB_MIN) / WINDOW;

  m_loudness.Update(sample);

  if (m_loudness.samples_count >= m_loudness.SAMPLES_NEEDED)
  {
    const FloatType amp_db = m_loudness.GetAmplitudeDb();
    m_loudness_level = static_cast<u16>((amp_db - m_loudness.DB_MIN) / UNIT);

#ifdef WII_SPEAK_LOG_STATS
    m_loudness.LogStats();
#endif

    m_loudness.Reset();
  }
}

bool Microphone::HasData(u32 sample_count) const
{
  std::lock_guard lock(m_ring_lock);
  return m_samples_avail >= sample_count;
}

Microphone::FloatType Microphone::ComputeGain(FloatType relative_db) const
{
  return m_loudness.ComputeGain(relative_db);
}

void Microphone::SetSamplingRate(u32 sampling_rate)
{
  StreamStop();
  StreamStart(sampling_rate);
}

u32 Microphone::GetBufferSizeSamples() const
{
  return BUFF_SIZE_SAMPLES;
}

const Microphone::FloatType Microphone::Loudness::DB_MIN =
    20 * std::log10(FloatType(1) / MAX_AMPLITUDE);
const Microphone::FloatType Microphone::Loudness::DB_MAX = 20 * std::log10(FloatType(1));

void Microphone::Loudness::Update(const SampleType sample)
{
  ++samples_count;

  peak_min = std::min(sample, peak_min);
  peak_max = std::max(sample, peak_max);
  absolute_sum += std::abs(sample);
  square_sum += std::pow(FloatType(sample), FloatType(2));
}

Microphone::SampleType Microphone::Loudness::GetPeak() const
{
  return std::max(std::abs(peak_min), std::abs(peak_max));
}

Microphone::FloatType Microphone::Loudness::GetDecibel(FloatType value)
{
  return 20 * std::log10(value);
}

Microphone::FloatType Microphone::Loudness::GetAmplitude() const
{
  return GetPeak() / MAX_AMPLITUDE;
}

Microphone::FloatType Microphone::Loudness::GetAmplitudeDb() const
{
  return GetDecibel(GetAmplitude());
}

Microphone::FloatType Microphone::Loudness::GetAbsoluteMean() const
{
  return FloatType(absolute_sum) / samples_count;
}

Microphone::FloatType Microphone::Loudness::GetAbsoluteMeanDb() const
{
  return GetDecibel(GetAbsoluteMean());
}

Microphone::FloatType Microphone::Loudness::GetRootMeanSquare() const
{
  return std::sqrt(square_sum / samples_count);
}

Microphone::FloatType Microphone::Loudness::GetRootMeanSquareDb() const
{
  return GetDecibel(GetRootMeanSquare());
}

Microphone::FloatType Microphone::Loudness::GetCrestFactor() const
{
  const auto rms = GetRootMeanSquare();
  if (rms == 0)
    return FloatType(0);
  return GetPeak() / rms;
}

Microphone::FloatType Microphone::Loudness::GetCrestFactorDb() const
{
  return GetDecibel(GetCrestFactor());
}

Microphone::FloatType Microphone::Loudness::ComputeGain(FloatType db)
{
  return std::pow(FloatType(10), db / 20);
}

void Microphone::Loudness::Reset()
{
  samples_count = 0;
  absolute_sum = 0;
  square_sum = FloatType(0);
  peak_min = 0;
  peak_max = 0;
}

void Microphone::Loudness::LogStats()
{
  const auto amplitude = GetAmplitude();
  const auto amplitude_db = GetDecibel(amplitude);
  const auto rms = GetRootMeanSquare();
  const auto rms_db = GetDecibel(rms);
  const auto abs_mean = GetAbsoluteMean();
  const auto abs_mean_db = GetDecibel(abs_mean);
  const auto crest_factor = GetCrestFactor();
  const auto crest_factor_db = GetDecibel(crest_factor);

  INFO_LOG_FMT(AUDIO,
               "Microphone loudness stats (sample count: {}/{}):\n"
               " - min={} max={} amplitude={} ({} dB)\n"
               " - rms={} ({} dB) \n"
               " - abs_mean={} ({} dB)\n"
               " - crest_factor={} ({} dB)",
               samples_count, SAMPLES_NEEDED, peak_min, peak_max, amplitude, amplitude_db, rms,
               rms_db, abs_mean, abs_mean_db, crest_factor, crest_factor_db);
}
}  // namespace AudioCommon