-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechoes.cpp
More file actions
126 lines (96 loc) · 2.86 KB
/
echoes.cpp
File metadata and controls
126 lines (96 loc) · 2.86 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//============================================================================
// Name : echoes.cpp
// Author : Ilya Ponetayev
// Version :
// Copyright : Research and development version
//============================================================================
#include <iostream>
#include <cmath>
#include <vector>
#include "WavFile.h"
#include "libao_out.h"
#include "fftw.h"
#include "transposer.h"
#include "samples.h"
#include "mixer.h"
using namespace std;
/* Notes
* C - 0 C# - 1
* D - 2 D# - 3
* E - 4 E# - 5
* F - 6 G - 7 G# - 8
* A - 9 A# - 10
* H - 11
*/
/*
* Octaves:
* 0 - subcontr
* 1 - contr
* 2 - large
* 3 - small
* 4 - first
* 5 - second
* ...
*/
double compute_freq(int note, int octave) {
return 27.5 * pow(2, (octave * 12 + note - 9) / 12.0L);
}
int main() {
std::vector<double> chord;
WavInFile wfd("test.wav");
libAo_Out out;
FFTW_Direct fftw;
Transposer proc;
Mixer mixer;
double freq;
unsigned int i, pos;
int time = 100; // Size of sample, msec
int samplesize = wfd.getSampleRate() * wfd.getNumChannels() * time / 1000;
short * buffer, * monobuffer;
struct fsm * fs;
Harmonic * harmonics;
buffer = new short[samplesize];
monobuffer = new short[samplesize / wfd.getNumChannels()];
fs = fsm_alloc(samplesize / wfd.getNumChannels());
harmonics = new Harmonic[samplesize / (2 * wfd.getNumChannels())];
out.Init(wfd.getSampleRate(), wfd.getNumBits(), wfd.getNumChannels());
fftw.Init(samplesize / wfd.getNumChannels());
while(!wfd.eof()) {
wfd.read(buffer, samplesize);
// Process Left Channel only now
for (i = 0; i < samplesize / 2; ++i)
monobuffer[i] = buffer[2 * i];
fftw.putData(monobuffer);
fftw.Run();
fftw.getData(harmonics);
freq = harmonics[0][pos = 0];
for (i = 0; i < samplesize / (2 * wfd.getNumChannels()); ++i)
if (harmonics[i][0] > freq) {
freq = harmonics[i][0];
pos = i;
}
chord.push_back(compute_freq(0,4));
// chord.push_back(compute_freq(0,4));
// chord.push_back(compute_freq(0,4));
freq = pos * 1000.0L / time;
cout << compute_freq(9, 4) << endl;
cout << compute_freq(9, 4) / freq <<endl;
if (freq < 20)
freq = 20;
proc.setParams(wfd.getSampleRate(), compute_freq(7, 3) / freq);
mixer.putSample(proc.processData(monobuffer, samplesize / wfd.getNumChannels()));
// proc.setParams(wfd.getSampleRate(), compute_freq(0, 3) / freq);
// mixer.putSample(proc.processData(monobuffer, samplesize / wfd.getNumChannels()));
proc.setParams(wfd.getSampleRate(), compute_freq(4, 3) / freq);
mixer.putSample(proc.processData(monobuffer, samplesize / wfd.getNumChannels()));
fs = mixer.getMixed();
mixer.Clean();
for (i = 0; i < samplesize / 2; ++i)
buffer[2 * i] = buffer[2 * i + 1] = fs->data[i];
//
out.PlayBuffer(buffer, samplesize);
cout << "Frequency: " << freq << "Hz" << endl;
}
cout << "Test" << endl;
return 0;
}