-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinedec.c
99 lines (79 loc) · 2.56 KB
/
Sinedec.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
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
/*--------------------------------------------------------------------SineDec.c--------------------------------------------------------------------*/
/*
* Created by Vishal Menon (Laziemo) in association with Richard Dobson (The Audio Programming Book)
* Version 1.0: October 06, 2017
* Combining ExpDec.c and Sinewave.c to generate a sinewave file with exponential decay
*
*Input:
* - .txt file containing audio data (fp)
* - Duration (dur)
* - Sinewave Frequency (freq)
* - Sample Rate (srate)
* - Amplitude (amp)
*
*Output:
* - FILE containing sinwave data with exponential decay
*
*UPGRADES:
* Create and EXPPitchDec -> Getting close to a kick drum
*
*/
/*--------------------------------------------------------------------SineDec.c--------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.141592654
#endif
enum {ARG_NAME,
ARG_OUTFILE,
ARG_DUR,
ARG_HZ,
ARG_SR,
ARG_AMP,
ARG_NARGS};
/*--------------------------------------------------------------------main--------------------------------------------------------------------*/
int main(int argc, char** argv){
int i, nsamps, srate;
double samp, dur, freq, amp, maxsamp;
double start, end, fac, angleincr;
double twopi = 2.0 * M_PI;
FILE *fp = NULL;
if (argc != ARG_NARGS){
fprintf(stderr, "Bad Usage \n Correct Usage: sindec sound.txt dur freq srate amp \n");
return 1;
}
fp = fopen (argv[ARG_OUTFILE], "w");
if (fp == NULL){
fprintf(stderr, "Error creating file \n");
return 1;
}
dur = atof(argv[ARG_DUR]);
freq = atof(argv[ARG_HZ]);
srate = atoi(argv[ARG_SR]);
amp = atof(argv[ARG_AMP]);
nsamps = (int) (dur * srate);
angleincr = (freq * twopi) / srate;
start = 1.0;
end = 1.0e-4;
maxsamp = 1.0e-4 ;
//What will happen: start/end? Attack? Find out...
fac = pow(end/start,1.0/nsamps);
//Sine Generator
for(i = 0 ; i<nsamps; i++){
samp = amp * sin(angleincr * i);
//Exponential Decay
/*samp *= start;
start *= fac;
*/
fprintf(fp, "%0.8lf \n", samp);
//Report the maximum amplitude of the signal
if(fabs(samp) > maxsamp) {
maxsamp = fabs(samp);
}
}
fclose(fp);
fprintf(stderr, "DONE! Maximum Sample Value = %0.8lf \n", maxsamp);
return 0;
}
/*--------------------------------------------------------------------main--------------------------------------------------------------------*/