-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
238 lines (189 loc) · 7.95 KB
/
main.cpp
File metadata and controls
238 lines (189 loc) · 7.95 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <cstdio>
#include <cstdint>
#include <ctime>
#include <sndfile.hh>
#include "GPUconvOAReverb.h"
#include "CPUconvOAReverb.h"
#define BUFFER_LEN 1024
static short impulseResponseBuffer[BUFFER_LEN];
static short targetSoundBuffer[BUFFER_LEN];
/*
* create_file (fname, SF_FORMAT_WAV | SF_FORMAT_PCM_16);
*/
static SndfileHandle create_file (const char * fname, int format)
{
SndfileHandle file ;
int channels = 2 ;
int srate = 48000 ;
printf ("Creating file named '%s'\n", "resources/output.wav") ;
file = SndfileHandle(fname, SFM_WRITE, format, channels, srate) ;
return file;
} /* create_file */
static SndfileHandle openImpulseResponse(const char * fname) {
SndfileHandle file;
file = SndfileHandle(fname) ;
file.read(impulseResponseBuffer, BUFFER_LEN);
return file;
}
static SndfileHandle openTargetSound(const char * fname) {
SndfileHandle file;
file = SndfileHandle(fname);
file.read(targetSoundBuffer, BUFFER_LEN);
return file;
}
int main(int argc, char const *argv[]) {
SndfileHandle targetSound = openTargetSound("resources/basic_kick2.wav");
SndfileHandle impulseResponse = openImpulseResponse("resources/HallA_X.wav");
SndfileHandle outputSound = create_file("resources/output.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16);
uint32_t outputSize = 0;
uint32_t targetSoundFrameCount = (uint32_t) targetSound.frames();
int targetSoundChannelCount = targetSound.channels();
uint32_t impulseResponseFrameCount = (uint32_t) impulseResponse.frames();
int impulseResponseChannelCount = impulseResponse.channels();
uint32_t roundedBaseTwoTargetFrames = pow(2, floor(log(targetSoundFrameCount)/log(2)));
uint32_t roundedBaseTwoImpulseFrames = pow(2, floor(log(impulseResponseFrameCount)/log(2)));
printf("target sound sample count %d, rounded^2: %d\n", targetSoundFrameCount, roundedBaseTwoTargetFrames);
printf("target sound channel count %d\n", targetSoundChannelCount);
printf("impuls response sample count %d, rounded^2: %d\n", impulseResponseFrameCount, roundedBaseTwoImpulseFrames);
printf("impuls response channel count %d\n", impulseResponseChannelCount);
float* targetSignal = new float[targetSoundFrameCount * targetSoundChannelCount];
float* impulseSignal = new float[impulseResponseFrameCount * impulseResponseChannelCount];
float* outputSoundSignal;
SNDFILE *infile1, *infile2;
SF_INFO sfinfo1, sfinfo2 ;
uint32_t samp1, samp2;
uint32_t sampleread;
int chan1, chan2;
targetSound.readf(targetSignal, targetSoundFrameCount * targetSoundChannelCount);
impulseResponse.readf(impulseSignal, impulseResponseChannelCount * impulseResponseChannelCount);
infile1 = sf_open ("resources/basic_kick2.wav", SFM_READ, &sfinfo1);
if (infile1 == NULL)
{
printf ("Unable to open file 1.\n");
return 1 ;
}
infile2 = sf_open ("resources/HallA_X.wav", SFM_READ, &sfinfo2);
if (infile2 == NULL)
{
printf ("Unable to open file 2.\n");
return 1 ;
}
samp1=sfinfo1.samplerate;
samp2=sfinfo2.samplerate;
chan1=sfinfo1.channels;
chan2=sfinfo2.channels;
if (samp1!=samp2){
printf("Error, Sample Rate mismatch.\n");
return 1;
}
if (((targetSoundChannelCount==2)&&(impulseResponseChannelCount==2))||((targetSoundChannelCount>1)||(impulseResponseChannelCount>2))){
printf("Unable to perform convolution with two stereo or multichannel files.\n");
return 1;
}
targetSignal= (float*)malloc(sizeof(float) * targetSoundFrameCount*targetSoundChannelCount);
// Allocate host memory for the signal
sampleread = sf_read_float (infile1, targetSignal, targetSoundFrameCount*chan1);
if (sampleread != targetSoundFrameCount )
{
printf ("Error reading target sound");
return 1;
}
impulseSignal= (float*)malloc(sizeof(float) * impulseResponseFrameCount*chan2);
// Allocate host memory for the filter
sampleread = sf_read_float (infile2, impulseSignal, impulseResponseFrameCount*chan2);
if (sampleread != impulseResponseFrameCount * impulseResponseChannelCount )
{
printf ("Error reading impulse response, %d", sampleread);
return 1;
}
// ###############################################
float* filtersx= (float*)malloc(sizeof(float) * impulseResponseFrameCount);
float* filterdx= (float*)malloc(sizeof(float) * impulseResponseFrameCount);
for (int i = 0; i < impulseResponseFrameCount; i++) {
if(impulseResponseChannelCount==2){
filtersx[i] = impulseSignal[2*i];
filterdx[i] = impulseSignal[2*i+1];
}
else{
filtersx[i] = impulseSignal[i];
filterdx[i] = impulseSignal[i];
}
}
// create output array for both channels
uint32_t segmentCount = targetSoundFrameCount / impulseResponseFrameCount;
uint32_t segmentSize = impulseResponseFrameCount;
uint32_t transformedSegmentSize = 2 * segmentSize - 1;
uint32_t transformedSignalSize = (transformedSegmentSize) * segmentCount;
float* outputsx=(float*)malloc(sizeof(float) * (transformedSignalSize));
float* outputdx=(float*)malloc(sizeof(float) * (transformedSignalSize));
uint32_t sampleSize = targetSoundFrameCount;
printf("sampleSize: %d: \n", sampleSize);
clock_t begin = clock();
outputSize = cpuconv::oAReverb(targetSignal,
sampleSize,
filtersx,
filterdx,
roundedBaseTwoImpulseFrames,
outputsx,
outputdx);
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
outputSize = gpuconv::oAReverb(targetSignal,
sampleSize,
filtersx,
filterdx,
roundedBaseTwoImpulseFrames,
outputsx,
outputdx);
printf("time consumed for cpu effect: %f seconds\n", elapsed_secs);
begin = clock();
outputSize = gpuconv::oAReverb(targetSignal,
sampleSize,
filtersx,
filterdx,
roundedBaseTwoImpulseFrames,
outputsx,
outputdx);
end = clock();
elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf("time consumed for gpu effect: %f seconds\n", elapsed_secs);
for (int j = 2; j < 16; j += 2) {
printf("sampleSize: %d: \n", sampleSize/j);
clock_t begin = clock();
outputSize = cpuconv::oAReverb(targetSignal,
sampleSize/j,
filtersx,
filterdx,
roundedBaseTwoImpulseFrames,
outputsx,
outputdx);
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf("time consumed for cpu effect: %f seconds\n", elapsed_secs);
begin = clock();
outputSize = gpuconv::oAReverb(targetSignal,
sampleSize/j,
filtersx,
filterdx,
roundedBaseTwoImpulseFrames,
outputsx,
outputdx);
end = clock();
elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf("time consumed for gpu effect: %f seconds\n", elapsed_secs);
}
uint32_t outputLength = outputSize * 2 + 1;
printf("outputSize: \t\t%d\n", outputSize);
printf("outpusx: \t\t%d\n", transformedSignalSize);
printf("outpusx: \t\t%d\n", transformedSignalSize);
printf("outputSoundSignal: \t%d\n", outputLength);
printf("returned size: \t\t%d\n", outputSize);
// write results to outputfile
outputSoundSignal = new float[outputLength];
for (int i = 0; i < outputSize; i++) {
outputSoundSignal[2*i]=(outputsx[i]);
outputSoundSignal[2*i+1]=(outputdx[i]);
}
outputSound.write(outputSoundSignal, outputSize * 2 ) ;
return 0;
}