You are on page 1of 1

#include <stdio.

h>
#include <stdlib.h>
#include <math.h>

#define BUFFER_SIZE 4096

// Compressor parameters
float threshold = -20.0; // Threshold level in dB
float ratio = 2.0; // Compression ratio
float makeup_gain = 10.0; // Makeup gain in dB

float compressor(float input) {


// Calculate the compression amount
float compression = 1.0 / ratio;
float level = input;
if (level > threshold) {
level = threshold + (level - threshold) * compression;
}

// Apply the makeup gain


level = level + makeup_gain;

// Return the compressed sample


return level;
}

int main(int argc, char* argv[]) {


// Read the input audio from a file
FILE* input_file = fopen("input.wav", "r");
float input[BUFFER_SIZE];
size_t read_size = fread(input, sizeof(float), BUFFER_SIZE, input_file);
fclose(input_file);

// Process the audio through the compressor


for (int i = 0; i < BUFFER_SIZE; i++) {
input[i] = compressor(input[i]);
}

// Write the output audio to a file


FILE* output_file = fopen("output.wav", "w");
fwrite(input, sizeof(float), BUFFER_SIZE, output_file);
fclose(output_file);

return 0;
}

You might also like