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
127
128
|
#include <iostream>
#include "portaudio.h"
#include "simpl/simpl.h"
using namespace std;
#define SAMPLE_RATE 44100
class AnalysisData {
public:
const int max_peaks;
std::vector<double> audio;
simpl::Frame frame;
simpl::LorisPeakDetection pd;
simpl::SMSPartialTracking pt;
simpl::SMSSynthesis synth;
AnalysisData(int frame_size, int hop_size) : max_peaks(50),
frame(frame_size, true) {
audio.resize(hop_size);
frame.synth_size(hop_size);
frame.max_peaks(max_peaks);
frame.max_partials(max_peaks);
pd.frame_size(frame_size);
pd.hop_size(hop_size);
pd.max_peaks(max_peaks);
pt.realtime(true);
pt.max_partials(max_peaks);
synth.det_synthesis_type(0);
synth.hop_size(hop_size);
synth.max_partials(frame_size);
}
};
static int patestCallback(const void *input,
void *output,
unsigned long buffer_size,
const PaStreamCallbackTimeInfo* time_info,
PaStreamCallbackFlags status_flags,
void *frame_data) {
AnalysisData* data = (AnalysisData*)frame_data;
float *in = (float*)input;
float *out = (float*)output;
std::copy(in, in + buffer_size, data->audio.begin());
data->frame.audio(&(data->audio[0]), buffer_size);
data->pd.find_peaks_in_frame(&(data->frame));
data->pt.update_partials(&(data->frame));
data->synth.synth_frame(&(data->frame));
for(unsigned int i = 0; i < buffer_size; i++) {
out[i] = data->frame.synth()[i];
}
data->frame.clear_peaks();
data->frame.clear_partials();
data->frame.clear_synth();
return 0;
}
int main() {
PaError err;
PaStream *stream;
int n_input_chans = 1;
int n_output_chans = 1;
int buffer_size = 512;
int frame_size = 2048;
static AnalysisData data(frame_size, buffer_size);
err = Pa_Initialize();
if(err != paNoError) {
cout << "Error initialising PortAudio" << endl;
return 1;
}
err = Pa_OpenDefaultStream(&stream,
n_input_chans,
n_output_chans,
paFloat32,
SAMPLE_RATE,
buffer_size,
patestCallback,
&data);
if(err != paNoError) {
cout << "Error opening default audio stream" << endl;
return 1;
}
err = Pa_StartStream(stream);
if(err != paNoError) {
cout << "Error starting audio stream" << endl;
return 1;
}
cout << endl;
cout << "Analysing audio from default input and "
<< "synthesising to default output." << endl;
cout << "Press Enter to stop" << endl;
cin.ignore();
err = Pa_StopStream(stream);
if(err != paNoError) {
cout << "Error stopping audio stream" << endl;
return 1;
}
err = Pa_CloseStream(stream);
if(err != paNoError) {
cout << "Error closing audio stream" << endl;
return 1;
}
err = Pa_Terminate();
if(err != paNoError) {
cout << "Error terminating PortAudio" << endl;
return 1;
}
return 0;
}
|