-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_echo.c
68 lines (56 loc) · 1.94 KB
/
render_echo.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "io.h"
#include "wave.h"
int main(int argc, char** argv) {
//error handling
if (argc != 5) {
printf("ERROR: incorrect number of arguments");
return 1;
}
//create wavfile for echo, then read_wave_header
FILE * wavfilein = fopen(argv[1], "rb");
if (wavfilein == NULL) {
printf("ERROR: File could not be opened");
return 1;
}
unsigned num_samples_stereo = 0;
unsigned * num_samp = &num_samples_stereo;
read_wave_header(wavfilein, num_samp);
unsigned num_samples = num_samples_stereo * 2;
//store wavfile information into array
int16_t *stereo_buf_nodelay = calloc(num_samples, (int) sizeof(int16_t));
read_s16_buf(wavfilein, stereo_buf_nodelay, num_samples);
//calloc a new array to include delay
int delay = atoi(argv[3]) * 2;
num_samples += delay;
int16_t *stereo_buf = calloc(num_samples, (int) sizeof(int16_t));
for (int i = 0; i < (int) num_samples - delay; i++) {
stereo_buf[i] = stereo_buf_nodelay[i];
}
//make a copy of the array
int16_t *stereo_buf_copy = calloc(num_samples, (int) sizeof(int16_t));
for (int i = 0; i < (int) num_samples; i++) {
stereo_buf_copy[i] = stereo_buf[i];
}
//add echo effect
float gain = atof(argv[4]);
for (int i = 0; i < (int) num_samples - delay; i++) {
stereo_buf[i + delay] += (int16_t) (gain * stereo_buf_copy[i]);
}
//open the output file, call write_wave_header
FILE *wavfileout = fopen(argv[2], "wb");
write_wave_header(wavfileout, num_samples / 2);
write_s16_buf(wavfileout, stereo_buf, num_samples);
//free memory and close files
free(stereo_buf);
free(stereo_buf_copy);
free(stereo_buf_nodelay);
fclose(wavfilein);
fclose(wavfileout);
return 0;
}