|
| 1 | +#include "HybridTest.hpp" |
| 2 | +#define MINIAUDIO_IMPLEMENTATION |
| 3 | +#include "miniaudio.h" |
| 4 | + |
| 5 | +#include <cmath> |
| 6 | +#include <iostream> |
| 7 | +#include <algorithm> |
| 8 | +#include <android/log.h> |
| 9 | + |
| 10 | +#define LOG_TAG "HybridTest" |
| 11 | + |
| 12 | +using namespace margelo::nitro::test; |
| 13 | + |
| 14 | +bool HybridTest::decodeAudioFile(const std::string& filePath, std::vector<float>& pcmData, unsigned int& sampleRate) { |
| 15 | + ma_decoder decoder; |
| 16 | + ma_decoder_config config = ma_decoder_config_init(ma_format_f32, 0, 0); |
| 17 | + ma_result file = ma_decoder_init_file(filePath.c_str(), &config, &decoder); |
| 18 | + if (file != MA_SUCCESS) { |
| 19 | + __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "Failed to init decoder for %s (error %d)", filePath.c_str(), file); |
| 20 | + std::cerr << "Failed to init decoder for " << filePath << std::endl; |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + sampleRate = decoder.outputSampleRate; |
| 25 | + |
| 26 | + ma_uint64 totalFrames = 0; |
| 27 | + if (ma_decoder_get_length_in_pcm_frames(&decoder, &totalFrames) != MA_SUCCESS) { |
| 28 | + std::cerr << "Failed to get length in pcm frames" << std::endl; |
| 29 | + ma_decoder_uninit(&decoder); |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + size_t totalSamples = static_cast<size_t>(totalFrames * decoder.outputChannels); |
| 34 | + pcmData.resize(totalSamples); |
| 35 | + |
| 36 | + ma_uint64 framesRead = 0; |
| 37 | + ma_result result = ma_decoder_read_pcm_frames(&decoder, pcmData.data(), totalFrames, &framesRead); |
| 38 | + ma_decoder_uninit(&decoder); |
| 39 | + |
| 40 | + if (result != MA_SUCCESS) { |
| 41 | + std::cerr << "Failed to read PCM frames" << std::endl; |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + if (framesRead != totalFrames) { |
| 46 | + std::cerr << "Warning: Frames read (" << framesRead << ") != total frames (" << totalFrames << ")" << std::endl; |
| 47 | + } |
| 48 | + |
| 49 | + return framesRead > 0; |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +std::vector<double> HybridTest::computeAmplitude(const std::string& filePath, double outputSampleCount = 1000) { |
| 54 | + size_t outputSampleCountInt = static_cast<size_t>(outputSampleCount); |
| 55 | + std::vector<float> pcm; |
| 56 | + unsigned int sampleRate = 0; |
| 57 | + |
| 58 | + // Decode audio file into PCM data |
| 59 | + if (!decodeAudioFile(filePath, pcm, sampleRate)) { |
| 60 | + __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "Failed to decode audio file for amplitude computation: %s", filePath.c_str()); |
| 61 | + return {}; |
| 62 | + } |
| 63 | + |
| 64 | + std::vector<double> result; |
| 65 | + |
| 66 | + size_t totalSamples = pcm.size(); |
| 67 | + if (totalSamples == 0 || outputSampleCount == 0) { |
| 68 | + // Return empty result if no samples or zero output requested |
| 69 | + return result; |
| 70 | + } |
| 71 | + |
| 72 | + // Calculate base block size and remainder to evenly split PCM into outputSampleCount blocks |
| 73 | + size_t baseBlockSize = totalSamples / outputSampleCount; |
| 74 | + size_t remainder = totalSamples % outputSampleCountInt; |
| 75 | + |
| 76 | + size_t offset = 0; |
| 77 | + for (size_t i = 0; i < outputSampleCount; ++i) { |
| 78 | + // Distribute remainder samples to the first 'remainder' blocks for even split |
| 79 | + size_t currentBlockSize = baseBlockSize + (i < remainder ? 1 : 0); |
| 80 | + |
| 81 | + if (currentBlockSize == 0) { |
| 82 | + // If block size is zero (e.g., outputSampleCount > totalSamples), push zero amplitude |
| 83 | + result.push_back(0.0); |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + double sum = 0.0; |
| 88 | + // Sum absolute values of samples in the current block |
| 89 | + for (size_t j = 0; j < currentBlockSize; ++j) { |
| 90 | + sum += std::abs(pcm[offset + j]); |
| 91 | + } |
| 92 | + // Compute average amplitude for the block |
| 93 | + double avg = sum / currentBlockSize; |
| 94 | + result.push_back(avg); |
| 95 | + |
| 96 | + // Move offset forward by current block size |
| 97 | + offset += currentBlockSize; |
| 98 | + } |
| 99 | + |
| 100 | + return result; |
| 101 | +} |
0 commit comments