|
| 1 | +// A part of NonVisual Desktop Access (NVDA) |
| 2 | +// This file is covered by the GNU General Public License. |
| 3 | +// See the file COPYING for more details. |
| 4 | +// Copyright (C) 2025 NV Access Limited, gexgd0419 |
| 5 | + |
| 6 | +#ifndef SILENCEDETECT_H |
| 7 | +#define SILENCEDETECT_H |
| 8 | + |
| 9 | +#include <windows.h> |
| 10 | +#include <mmreg.h> |
| 11 | +#include <stdint.h> |
| 12 | +#include <type_traits> |
| 13 | +#include <limits> |
| 14 | + |
| 15 | +namespace SilenceDetect { |
| 16 | + |
| 17 | +/** |
| 18 | + * Compile-time wave format tag. |
| 19 | + * Supports integer and floating-point formats. |
| 20 | + * `SampleType` should be the smallest numeric type that can hold a sample, for example, 32-bit int for 24-bit format. |
| 21 | + * Signedness of `SampleType` matters. For unsigned types, the zero point is at middle, e.g. 128 for 8-bit unsigned. |
| 22 | + * `bytesPerSample` should be <= `sizeof(SampleType)` for integer formats, |
| 23 | + * and == `sizeof(SampleType)` for floating-point formats. |
| 24 | + * Assumes C++20 standard. |
| 25 | + */ |
| 26 | +template <typename SampleType, size_t bytesPerSample = sizeof(SampleType)> |
| 27 | +struct WaveFormat { |
| 28 | + static_assert(std::is_arithmetic_v<SampleType>, "SampleType should be an integer or floating-point type"); |
| 29 | + static_assert(!(std::is_floating_point_v<SampleType> && bytesPerSample != sizeof(SampleType)), |
| 30 | + "When SampleType is a floating-point type, bytesPerSample should be equal to sizeof(SampleType)"); |
| 31 | + static_assert(!(std::is_integral_v<SampleType> && !(bytesPerSample <= sizeof(SampleType) && bytesPerSample > 0)), |
| 32 | + "When SampleType is an integer type, bytesPerSample should be less than or equal to sizeof(SampleType) and greater than 0"); |
| 33 | + |
| 34 | + typedef SampleType SampleType; |
| 35 | + static constexpr size_t bytesPerSample = bytesPerSample; |
| 36 | + |
| 37 | + static constexpr SampleType zeroPoint() { |
| 38 | + // for unsigned types, zero point is at middle |
| 39 | + // for signed types, zero is zero |
| 40 | + if constexpr (std::is_unsigned_v<SampleType>) |
| 41 | + return SampleType(1) << (bytesPerSample * 8 - 1); |
| 42 | + else |
| 43 | + return SampleType(); |
| 44 | + } |
| 45 | + |
| 46 | + static constexpr SampleType (max)() { |
| 47 | + if constexpr (std::is_floating_point_v<SampleType>) { |
| 48 | + // For floating-point samples, maximum value is 1.0 |
| 49 | + return SampleType(1); |
| 50 | + } else { |
| 51 | + // Trim the maximum value to `bytesPerSample` bytes |
| 52 | + return (std::numeric_limits<SampleType>::max)() >> ((sizeof(SampleType) - bytesPerSample) * 8); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + static constexpr SampleType (min)() { |
| 57 | + if constexpr (std::is_floating_point_v<SampleType>) { |
| 58 | + // For floating-point samples, minimum value is -1.0 |
| 59 | + return SampleType(-1); |
| 60 | + } else { |
| 61 | + // Trim the minimum value to `bytesPerSample` bytes |
| 62 | + return (std::numeric_limits<SampleType>::min)() >> ((sizeof(SampleType) - bytesPerSample) * 8); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static constexpr SampleType defaultThreshold() { |
| 67 | + // Default threshold: 1 / 2^10 or 0.0009765625 |
| 68 | + if constexpr (std::is_floating_point_v<SampleType>) |
| 69 | + return SampleType(1) / (1 << 10); |
| 70 | + else if constexpr (bytesPerSample * 8 > 10) |
| 71 | + return SampleType(1) << (bytesPerSample * 8 - 10); |
| 72 | + else |
| 73 | + return SampleType(); |
| 74 | + } |
| 75 | + |
| 76 | + static constexpr auto toSigned(SampleType smp) { |
| 77 | + if constexpr (std::is_integral_v<SampleType>) { |
| 78 | + // In C++20, signed integer types must use two's complement, |
| 79 | + // so the following conversion is well-defined. |
| 80 | + using SignedType = std::make_signed_t<SampleType>; |
| 81 | + return SignedType(smp - zeroPoint()); |
| 82 | + } else { |
| 83 | + return smp; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + static constexpr SampleType fromSigned(SampleType smp) { |
| 88 | + if constexpr (std::is_integral_v<SampleType>) { |
| 89 | + // Signed overflow is undefined behavior, |
| 90 | + // so convert to unsigned first. |
| 91 | + using UnsignedType = std::make_unsigned_t<SampleType>; |
| 92 | + return SampleType(UnsignedType(smp) + zeroPoint()); |
| 93 | + } else { |
| 94 | + return smp; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + static constexpr SampleType signExtend(SampleType smp) { |
| 99 | + if constexpr (std::is_unsigned_v<SampleType> || bytesPerSample == sizeof(SampleType)) { |
| 100 | + return smp; |
| 101 | + } else { |
| 102 | + constexpr auto shift = (sizeof(SampleType) - bytesPerSample) * 8; |
| 103 | + // Convert to unsigned first to prevent left-shifting negative numbers |
| 104 | + using UnsignedType = std::make_unsigned_t<SampleType>; |
| 105 | + return SampleType(UnsignedType(smp) << shift) >> shift; |
| 106 | + } |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +inline WORD getFormatTag(const WAVEFORMATEX* wfx) { |
| 111 | + if (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) { |
| 112 | + auto wfext = reinterpret_cast<const WAVEFORMATEXTENSIBLE*>(wfx); |
| 113 | + if (IS_VALID_WAVEFORMATEX_GUID(&wfext->SubFormat)) |
| 114 | + return EXTRACT_WAVEFORMATEX_ID(&wfext->SubFormat); |
| 115 | + } |
| 116 | + return wfx->wFormatTag; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Return the leading silence wave data length, in bytes. |
| 121 | + * Assumes the wave data to be of one channel (mono). |
| 122 | + * Uses a `WaveFormat` type (`Fmt`) to determine the wave format. |
| 123 | + */ |
| 124 | +template <class Fmt> |
| 125 | +size_t getLeadingSilenceSizeMono( |
| 126 | + const unsigned char* waveData, |
| 127 | + size_t size, |
| 128 | + typename Fmt::SampleType threshold |
| 129 | +) { |
| 130 | + using SampleType = Fmt::SampleType; |
| 131 | + constexpr size_t bytesPerSample = Fmt::bytesPerSample; |
| 132 | + |
| 133 | + if (size < bytesPerSample) |
| 134 | + return 0; |
| 135 | + |
| 136 | + constexpr SampleType zeroPoint = Fmt::zeroPoint(); |
| 137 | + const SampleType minValue = zeroPoint - threshold, maxValue = zeroPoint + threshold; |
| 138 | + |
| 139 | + // Check each sample |
| 140 | + SampleType smp = SampleType(); |
| 141 | + const unsigned char* const pEnd = waveData + (size - (size % bytesPerSample)); |
| 142 | + for (const unsigned char* p = waveData; p < pEnd; p += bytesPerSample) { |
| 143 | + memcpy(&smp, p, bytesPerSample); |
| 144 | + smp = Fmt::signExtend(smp); |
| 145 | + // this sample is out of range, so the previous sample is the final sample of leading silence. |
| 146 | + if (smp < minValue || smp > maxValue) |
| 147 | + return p - waveData; |
| 148 | + } |
| 149 | + |
| 150 | + // The whole data block is silence |
| 151 | + return size; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Invoke a functor with an argument of a WaveFormat type that corresponds to the specified WAVEFORMATEX. |
| 156 | + * Return false if the WAVEFORMATEX is unknown. |
| 157 | + */ |
| 158 | +template <class Func> |
| 159 | +bool callByWaveFormat(const WAVEFORMATEX* wfx, Func&& func) { |
| 160 | + switch (getFormatTag(wfx)) { |
| 161 | + case WAVE_FORMAT_PCM: |
| 162 | + switch (wfx->wBitsPerSample) { |
| 163 | + case 8: // 8-bits are unsigned, others are signed |
| 164 | + func(WaveFormat<uint8_t>()); |
| 165 | + break; |
| 166 | + case 16: |
| 167 | + func(WaveFormat<int16_t>()); |
| 168 | + break; |
| 169 | + case 24: |
| 170 | + func(WaveFormat<int32_t, 3>()); |
| 171 | + break; |
| 172 | + case 32: |
| 173 | + func(WaveFormat<int32_t>()); |
| 174 | + break; |
| 175 | + default: |
| 176 | + return false; |
| 177 | + } |
| 178 | + break; |
| 179 | + case WAVE_FORMAT_IEEE_FLOAT: |
| 180 | + switch (wfx->wBitsPerSample) { |
| 181 | + case 32: |
| 182 | + func(WaveFormat<float>()); |
| 183 | + break; |
| 184 | + case 64: |
| 185 | + func(WaveFormat<double>()); |
| 186 | + break; |
| 187 | + default: |
| 188 | + return false; |
| 189 | + } |
| 190 | + break; |
| 191 | + default: |
| 192 | + return false; |
| 193 | + } |
| 194 | + return true; |
| 195 | +} |
| 196 | + |
| 197 | +/** |
| 198 | + * Return the leading silence wave data length, in bytes. |
| 199 | + * Uses a `WAVEFORMATEX` to determine the wave format. |
| 200 | + */ |
| 201 | +inline size_t getLeadingSilenceSize( |
| 202 | + const WAVEFORMATEX* wfx, |
| 203 | + const unsigned char* waveData, |
| 204 | + size_t size |
| 205 | +) { |
| 206 | + size_t len; |
| 207 | + if (!callByWaveFormat(wfx, [=, &len](auto fmtTag) { |
| 208 | + using Fmt = decltype(fmtTag); |
| 209 | + len = getLeadingSilenceSizeMono<Fmt>( |
| 210 | + waveData, size, Fmt::defaultThreshold()); |
| 211 | + })) |
| 212 | + return 0; |
| 213 | + |
| 214 | + return len - len % wfx->nBlockAlign; // round down to block (channel) boundaries |
| 215 | +} |
| 216 | + |
| 217 | +} // namespace SilenceDetect |
| 218 | + |
| 219 | +#endif // SILENCEDETECT_H |
0 commit comments