Files
SubWave/src/engine/encoder/SampleRateUtils.h
T
rattatwinko 1a00e98661 rather large commit, it now handles memory and threading properly.
---

more thorough commit message which was generated by the wonderful copilot AI tool:

Refactor and enhance audio encoding and decoding pipelines

- Introduced CodecUtils.h for utility functions related to audio codec selection, including sample format and bitrate clamping.
- Added DecoderPipeline.h to manage audio decoding, including opening files and retrieving audio stream information.
- Implemented EncoderPipeline.h to handle audio encoding, including encoder setup, resampling, and writing output files.
- Created OutputPathBuilder.h to generate output file paths based on reencode job specifications.
- Refactored SpectrogramWidget to improve OpenGL handling and ensure safe context usage.
- Updated ReencodeDialog.h to enhance code readability and maintainability.
- Improved error handling and logging throughout the audio processing components.
2026-07-01 09:56:37 +02:00

46 lines
1.1 KiB
C++

#pragma once
extern "C" {
#include <libavcodec/avcodec.h>
}
namespace engine {
/**
* @brief Selection Priority:
* 1. Exact match with sourceRate
* 2. Lowest supported rate that is >= sourceRate (upsample as little as possible)
* 3. Highest supported rate overall
* rates are below sourceRate, eg a low rate phone codec at 8khz
*/
inline int pickSampleRate(const AVCodec *encoder, int sourceRate)
{
if (!encoder->supported_samplerates)
return sourceRate;
// 1: exact match
for (const int *r = encoder->supported_samplerates; *r != 0; ++r)
if (*r == sourceRate)
return sourceRate;
// 2: lowest rate >= sourceRate (minimal upsampling)
int best = 0;
for (const int *r = encoder->supported_samplerates; *r != 0; ++r) {
if (*r >= sourceRate) {
if (best == 0 || *r < best)
best = *r;
}
}
if (best != 0)
return best;
// 3: all supported rates are below sourceRate
for (const int *r = encoder->supported_samplerates; *r != 0; ++r)
if (*r > best)
best = *r;
return best;
}
} // namespace engine