summaryrefslogtreecommitdiff
path: root/os2eindopdracht/Arguments.cpp
blob: a390eab58c84a33050277fedc0cd767ecd2297db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <getopt.h>
#include <cmath>

#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:")) {
      case 'p': {
        this->max_threads = atoi(optarg);
        continue;
      }
      case 'b': {
        this->gain_bass = atoi(optarg);
        continue;
      }
      case 't': {
        this->gain_treble = atoi(optarg);
        continue;
      }
      case -1: break;
    }
    if (optind + 2 > argc) {
      printf("please provide an input and output file\n");
      exit(1);
    }
    this->file_input = string(argv[optind++]);
    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;
}