|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +#include <miniaudio.h> |
| 4 | +#include <cmath> |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +#include "audioloudness.h" |
| 8 | + |
| 9 | +using namespace libscratchcpp; |
| 10 | + |
| 11 | +static int loudness = -1; |
| 12 | +static float lastValue = 0.0f; |
| 13 | + |
| 14 | +static void data_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount) |
| 15 | +{ |
| 16 | + const float *pMicDataArray = static_cast<const float *>(pInput); |
| 17 | + |
| 18 | + // https://github.com/scratchfoundation/scratch-audio/blob/068aca613604e39b2adbe785b17931cc43eec35f/src/Loudness.js#L36-L80 |
| 19 | + // Compute the RMS of the sound |
| 20 | + float sum = 0.0f; |
| 21 | + |
| 22 | + for (ma_uint32 i = 0; i < frameCount; i++) { |
| 23 | + float value = pMicDataArray[i]; |
| 24 | + sum += value * value; |
| 25 | + } |
| 26 | + |
| 27 | + float rms = std::sqrt(sum / static_cast<float>(frameCount)); |
| 28 | + |
| 29 | + // Smooth the value, if it is descending |
| 30 | + if (lastValue != 0.0f) { |
| 31 | + rms = std::max(rms, lastValue * 0.6f); |
| 32 | + } |
| 33 | + |
| 34 | + lastValue = rms; |
| 35 | + |
| 36 | + // Scale the measurement |
| 37 | + rms *= 1.63f; |
| 38 | + rms = std::sqrt(rms); |
| 39 | + // Scale it up to 0-100 and round |
| 40 | + rms = std::round(rms * 100.0f); |
| 41 | + // Prevent it from going above 100 |
| 42 | + rms = std::min(rms, 100.0f); |
| 43 | + |
| 44 | + loudness = rms; |
| 45 | +} |
| 46 | + |
| 47 | +AudioLoudness::AudioLoudness() |
| 48 | +{ |
| 49 | + ma_device_config deviceConfig = ma_device_config_init(ma_device_type_capture); |
| 50 | + deviceConfig.capture.format = ma_format_f32; |
| 51 | + deviceConfig.capture.channels = 1; // mono |
| 52 | + deviceConfig.sampleRate = 44100; |
| 53 | + deviceConfig.periodSizeInFrames = 2048; |
| 54 | + deviceConfig.dataCallback = data_callback; |
| 55 | + deviceConfig.pUserData = NULL; |
| 56 | + |
| 57 | + m_device = new ma_device; |
| 58 | + |
| 59 | + if (ma_device_init(NULL, &deviceConfig, m_device) != MA_SUCCESS) { |
| 60 | + std::cerr << "Failed to initialize audio capture device." << std::endl; |
| 61 | + delete m_device; |
| 62 | + m_device = nullptr; |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (ma_device_start(m_device) != MA_SUCCESS) { |
| 67 | + std::cerr << "Failed to start audio capture device." << std::endl; |
| 68 | + ma_device_uninit(m_device); |
| 69 | + delete m_device; |
| 70 | + m_device = nullptr; |
| 71 | + return; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +AudioLoudness::~AudioLoudness() |
| 76 | +{ |
| 77 | + if (m_device) { |
| 78 | + ma_device_uninit(m_device); |
| 79 | + delete m_device; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +int AudioLoudness::getLoudness() const |
| 84 | +{ |
| 85 | + return loudness; |
| 86 | +} |
0 commit comments