C++ 컴프레서 예제 샘플

ChatGPT
Author
장 호준
Date
2025-01-18 18:07
Views
77
#include <iostream>
#include <cmath>
#include <vector>

class Compressor {
public:
Compressor(float threshold, float ratio, float attackTime, float releaseTime, float makeupGain)
: threshold(threshold), ratio(ratio), attackTime(attackTime), releaseTime(releaseTime), makeupGain(makeupGain),
gain(1.0f), prevSignal(0.0f) {}

// 신호 처리 함수
float process(float inputSignal, float sampleRate) {
// 현재 신호가 threshold를 초과하는지 체크
if (inputSignal > threshold) {
// 압축을 적용
float overThreshold = inputSignal - threshold;
float compressedSignal = threshold + overThreshold / ratio;

// 어택 타임 처리 (빠르게 압축 반응)
gain = attackSample(gain, compressedSignal, sampleRate);
} else {
// threshold 이하일 경우, 릴리즈 타임 적용
gain = releaseSample(gain, inputSignal, sampleRate);
}

// 메이크업 게인 적용
float outputSignal = inputSignal * gain * makeupGain;

return outputSignal;
}

private:
float threshold; // threshold (임계값)
float ratio; // 압축 비율
float attackTime; // 어택 타임
float releaseTime; // 릴리즈 타임
float makeupGain; // 메이크업 게인
float gain; // 현재 적용된 압축 gain
float prevSignal; // 이전 신호값 (어택과 릴리즈 계산에 사용)

// 어택 처리 함수
float attackSample(float currentGain, float compressedSignal, float sampleRate) {
// 어택 타임에 따른 점진적인 반응 (간단한 선형 보간)
float attackSpeed = std::exp(-1.0f / (attackTime * sampleRate));
return currentGain * attackSpeed + compressedSignal * (1.0f - attackSpeed);
}

// 릴리즈 처리 함수
float releaseSample(float currentGain, float inputSignal, float sampleRate) {
// 릴리즈 타임에 따른 점진적인 반응 (간단한 선형 보간)
float releaseSpeed = std::exp(-1.0f / (releaseTime * sampleRate));
return currentGain * releaseSpeed + inputSignal * (1.0f - releaseSpeed);
}
};

int main() {
// 컴프레서 설정 (Threshold: -10dB, Ratio: 4:1, Attack Time: 0.01s, Release Time: 0.1s, Makeup Gain: 1.0)
Compressor compressor(-10.0f, 4.0f, 0.01f, 0.1f, 1.0f);

// 샘플링 레이트 (예: 44100Hz)
float sampleRate = 44100.0f;

// 테스트 신호 (예: 입력 신호는 -5dB, -12dB, -8dB, -15dB)
std::vector<float> inputSignal = {-5.0f, -12.0f, -8.0f, -15.0f};
std::vector<float> outputSignal;

// 각 입력 신호에 대해 컴프레서를 처리
for (float signal : inputSignal) {
float output = compressor.process(signal, sampleRate);
outputSignal.push_back(output);
std::cout << "Input: " << signal << "dB -> Output: " << output << "dB" << std::endl;
}

return 0;
}

설명

  1. Compressor 클래스:
    • 이 클래스는 컴프레서의 핵심 동작을 처리합니다.
    • 주요 파라미터:
      • threshold: 압축을 시작할 임계값
      • ratio: 압축 비율
      • attackTime: 어택 시간 (신호가 threshold를 초과할 때 얼마나 빨리 반응할지)
      • releaseTime: 릴리즈 시간 (신호가 threshold 이하로 돌아올 때 얼마나 천천히 풀릴지)
      • makeupGain: 신호를 압축한 후의 볼륨 보상 (메이크업 게인)
    • process 함수는 입력 신호를 처리하고, 압축된 출력을 반환합니다.
  2. 어택 및 릴리즈 처리:
    • attackSample 함수는 어택 시간에 따라 신호를 빠르게 압축하는데 반응하도록 계산합니다.
    • releaseSample 함수는 릴리즈 시간에 따라 신호를 점진적으로 원래 상태로 돌아가게 합니다.
  3. 메이크업 게인:
    • 압축 후, makeupGain을 적용하여 출력 신호의 볼륨을 보상합니다.
  4. main 함수:
    • 예시로 몇 가지 입력 신호(-5.0dB, -12.0dB, -8.0dB, -15.0dB)를 사용하여 컴프레서를 처리하고, 그 결과를 출력합니다.

동작 예시

  • 입력 신호가 -5.0dB일 때, 압축이 적용되고, 출력 신호는 -5.0dB에 근접할 것입니다.
  • 입력 신호가 -12.0dB일 경우, 임계값(threshold) 아래에 있어 압축이 적용되지 않거나 조금만 변화합니다.
  • 신호가 threshold를 초과할 때, 압축 비율(ratio)에 따라 신호가 줄어들게 됩니다.
이 코드는 단순화된 형태로, 실제 오디오 처리에서는 훨씬 복잡한 계산과 필터링, 그리고 다채로운 파라미터가 필요할 수 있습니다.
Total 0

Total 285
Number Title Author Date Votes Views
281
평형(Balanced), 불평형(Unbalanced), 그라운드 루프
장 호준 | 2025.02.07 | Votes 0 | Views 5
장 호준 2025.02.07 0 5
280
정위감이라.. 무지한 단어의 마케팅적 사용에 대해
장 호준 | 2025.02.07 | Votes 0 | Views 5
장 호준 2025.02.07 0 5
279
단테시스템에서 워드클럭
장 호준 | 2025.02.05 | Votes 0 | Views 10
장 호준 2025.02.05 0 10
278
1비트가 6dB이 되는 이유
장 호준 | 2025.02.05 | Votes 0 | Views 25
장 호준 2025.02.05 0 25
277
24비트 ADC가 40비트 프로세싱으로 된다는 것이 정밀도를 위한 것은 아니지, 충분한 공간의 의미만 있는거야 (1)
장 호준 | 2025.02.05 | Votes 0 | Views 13
장 호준 2025.02.05 0 13
276
초음파 패널 스피커의 원리
장 호준 | 2025.02.04 | Votes 0 | Views 12
장 호준 2025.02.04 0 12
275
디지털 신호를 소리로 만들 수 있는 기술들
장 호준 | 2025.02.04 | Votes 0 | Views 11
장 호준 2025.02.04 0 11
274
나이퀴스트 정리를 아주 정확하게 정리해드림
장 호준 | 2025.02.04 | Votes 0 | Views 29
장 호준 2025.02.04 0 29
273
고정된 이퀄라이저 커브만 사용할때의 위험성
장 호준 | 2025.02.04 | Votes 1 | Views 32
장 호준 2025.02.04 1 32
272
지터에 대해서
장 호준 | 2025.02.04 | Votes 0 | Views 18
장 호준 2025.02.04 0 18
New