From 929f168ef81cd0252bd6853a8dbe21a128e4e435 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 30 May 2023 15:45:26 +0200 Subject: test bass and treble functions --- os2eindopdracht/Arguments.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++ os2eindopdracht/Arguments.h | 19 +++++++++++++++-- os2eindopdracht/SampleStream.h | 2 +- os2eindopdracht/main.cpp | 32 +++++++++++++++++++++++----- os2eindopdracht/makefile | 2 +- 5 files changed, 94 insertions(+), 9 deletions(-) diff --git a/os2eindopdracht/Arguments.cpp b/os2eindopdracht/Arguments.cpp index 8e1f98b..a390eab 100644 --- a/os2eindopdracht/Arguments.cpp +++ b/os2eindopdracht/Arguments.cpp @@ -1,9 +1,13 @@ #include +#include #include "Arguments.h" using std::string; +static void coefficients_bass(int intensity, double *b0, double *b1, double *b2, double *a1, double *a2); +static void coefficients_treble(int intensity, double *b0, double *b1, double *b2, double *a1, double *a2); + Arguments::Arguments(int argc, char** argv) { while(1) { switch(getopt(argc, argv, "p:b:t:")) { @@ -29,4 +33,48 @@ Arguments::Arguments(int argc, char** argv) { this->file_output = string(argv[optind++]); break; } + + calculate_coefficients(); +} + +void Arguments::calculate_coefficients() { + coefficients_bass(this->gain_bass, &this->bass.b0, &this->bass.b1, &this->bass.b2, &this->bass.a1, &this->bass.a2); + coefficients_treble(this->gain_treble, &this->treble.b0, &this->treble.b1, &this->treble.b2, &this->treble.a1, &this->treble.a2); +} + +static void coefficients_bass(int intensity, double *b0, double *b1, double *b2, double *a1, double *a2) { + double frequency = 330; + double qFactor = 0.5; + double gain = intensity; + double sampleRate = 44100; + + double pi = 4.0 * atan(1); + double a = pow(10.0, gain / 40); + double w0 = 2 * pi * frequency / sampleRate; + double alpha = sin(w0) / (2.0 * qFactor); + double a0 = (a + 1) + (a - 1) * cos(w0) + 2.0 * sqrt(a) * alpha; + + *a1 = -(-2.0 * ((a - 1) + (a + 1) * cos(w0))) / a0; + *a2 = -((a + 1) + (a - 1) * cos(w0) - 2.0 * sqrt(a) * alpha) / a0; + *b0 = (a * ((a + 1) - (a - 1) * cos(w0) + 2.0 * sqrt(a) * alpha)) / a0; + *b1 = (2 * a * ((a - 1) - (a + 1) * cos(w0))) / a0; + *b2 = (a * ((a + 1) - (a - 1) * cos(w0) - 2.0 * sqrt(a) * alpha)) / a0; +} + +static void coefficients_treble(int intensity, double *b0, double *b1, double *b2, double *a1, double *a2) { + double frequency = 3300; + double qFactor = 0.5; + double gain = intensity; + double sampleRate = 44100; + double pi = 4.0 * atan(1); + double a = pow(10.0, gain / 40); + double w0 = 2 * pi * frequency / sampleRate; + double alpha = sin(w0) / (2.0 * qFactor); + double a0 = (a + 1) - (a - 1) * cos(w0) + 2.0 * sqrt(a) * alpha; + + *a1 = -(2.0 * ((a - 1) - (a + 1) * cos(w0))) / a0; + *a2 = -((a + 1) - (a - 1) * cos(w0) - 2.0 * sqrt(a) * alpha) / a0; + *b0 = (a * ((a + 1) + (a - 1) * cos(w0) + 2.0 * sqrt(a) * alpha)) / a0; + *b1 = (-2.0 * a * ((a - 1) + (a + 1) * cos(w0))) / a0; + *b2 = (a * ((a + 1) + (a - 1) * cos(w0) - 2.0 * sqrt(a) * alpha)) / a0; } diff --git a/os2eindopdracht/Arguments.h b/os2eindopdracht/Arguments.h index 789d656..c61d3a9 100644 --- a/os2eindopdracht/Arguments.h +++ b/os2eindopdracht/Arguments.h @@ -4,14 +4,29 @@ using std::string; +struct Coefficients { + double b0; + double b1; + double b2; + double a1; + double a2; +}; + class Arguments { public: Arguments(int argc, char** argv); public: unsigned int max_threads = 4; - int gain_bass = 0; - int gain_treble = 0; string file_input; string file_output; + Coefficients bass; + Coefficients treble; + +private: + void calculate_coefficients(); + +private: + int gain_bass = 0; + int gain_treble = 0; }; diff --git a/os2eindopdracht/SampleStream.h b/os2eindopdracht/SampleStream.h index 94bb941..a9b8536 100644 --- a/os2eindopdracht/SampleStream.h +++ b/os2eindopdracht/SampleStream.h @@ -14,6 +14,6 @@ public: void load(string input); string save(); -private: +public: vector _stream; }; diff --git a/os2eindopdracht/main.cpp b/os2eindopdracht/main.cpp index 291361e..534bf0c 100644 --- a/os2eindopdracht/main.cpp +++ b/os2eindopdracht/main.cpp @@ -17,11 +17,33 @@ int main(int argc, char** argv) { SampleStream stream(file_input_content.str()); - printf("max_threads: %i\n", args.max_threads); - printf("gain_bass: %+i\n", args.gain_bass); - printf("gain_treble: %+i\n", args.gain_treble); - printf("input filename: \"%s\"\n", args.file_input.c_str()); - printf("output filename: \"%s\"\n", args.file_output.c_str()); + std::vector original; + original = std::vector(stream._stream); + for (unsigned long i = 0; i < original.size(); i++) { + double x_pp = i < 2 ? 0 : original[i-2]; + double x_p = i < 1 ? 0 : original[i-1]; + double x_ = i < 0 ? 0 : original[i-0]; + double y_pp = i < 2 ? 0 : stream._stream[i-2]; + double y_p = i < 1 ? 0 : stream._stream[i-1]; + stream._stream[i] = args.bass.b0 * x_ + + args.bass.b1 * x_p + + args.bass.b2 * x_pp + + args.bass.a1 * y_p + + args.bass.a2 * y_pp; + } + original = std::vector(stream._stream); + for (unsigned long i = 0; i < original.size(); i++) { + double x_pp = i < 2 ? 0 : original[i-2]; + double x_p = i < 1 ? 0 : original[i-1]; + double x_ = i < 0 ? 0 : original[i-0]; + double y_pp = i < 2 ? 0 : stream._stream[i-2]; + double y_p = i < 1 ? 0 : stream._stream[i-1]; + stream._stream[i] = args.treble.b0 * x_ + + args.treble.b1 * x_p + + args.treble.b2 * x_pp + + args.treble.a1 * y_p + + args.treble.a2 * y_pp; + } std::fstream file_output(args.file_output, std::ios::out | std::ios::binary); std::string edited_stream = stream.save(); diff --git a/os2eindopdracht/makefile b/os2eindopdracht/makefile index f71ef29..41fd730 100644 --- a/os2eindopdracht/makefile +++ b/os2eindopdracht/makefile @@ -26,7 +26,7 @@ original.pcm: original.flac ./any2s16lepcm $< $@ edit.pcm: original.pcm ./main - ./main $< $@ + ./main $< $@ -b1 -t-19 edit.wav: edit.pcm ./s16lepcm2any $< $@ -- cgit v1.2.3