-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundSamples.cpp
204 lines (167 loc) · 5.42 KB
/
SoundSamples.cpp
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
#include "SoundSamples.h"
/**
*SoundSamples constructor that takes all important componants to create SoundSamples
*@param samples as a float array, length as an integer, and sample rate as a float
**/
SoundSamples :: SoundSamples (float *_samples, int _length, float _sampleRate)
{
int i;
//this->samples = _samples;
int size = _length;
float samps [size];
for (i = 0; i < _length; i++) samps [i] = _samples [i];
this->samples = samps;
this->length = _length;
this->sampleRate = _sampleRate;
}
/**
*SoundSamples constructor that takes all important componants to create empty SoundSamples (i.e. silence).
*@param length as an integer and sample rate as a float
**/
SoundSamples :: SoundSamples (int _length, float _sampleRate)
{
int i;
this->length = _length;
this->sampleRate = _sampleRate;
//create the silent samples:
int size = _length;
float samps [size];
for (i = 0; i < size; i++) samples [i] = 0;
this->samples = samps;
}
/**
*Overloaded operator that assigns one SoundSample to the left hand side SoundSample.
*@param SoundSamples pointer for a Sound Sample to be assigned
*@return SoundSamples that has a new assignment
**/
SoundSamples& SoundSamples :: operator= (const SoundSamples &orig)
{
if (this != &orig)
{
delete this->samples;
this->length = orig.length;
this->sampleRate = orig.sampleRate;
this->samples = new float;
*(this->samples) = *(orig.samples);
}
return *this;
}
/**
*Overloaded operator that checks to see if two SoundSamples are the same.
*@param SoundSample pointer to be compoared
*@return boolean indicator of whether two SoundSamples are the same
**/
bool SoundSamples :: operator== (const SoundSamples &s1/*, const SoundSamples &s2*/)
{
int i;
bool same = true;
if (s1.length != this->length ) same = false;
if (s1.sampleRate != this->sampleRate) same = false;
for (i = 0; i < s1.length; i++)
if (s1.samples [i] != this->samples [i]) same = false;
return same;
}
/**
*Overloaded operator that adds one SoundSample to the left hand side SoundSample.
*@param SoundSamples for a Sound Sample to be added
*@return added SoundSample to the right of the operator, with the left added on
**/
SoundSamples SoundSamples :: operator+ (SoundSamples s1/*, const SoundSamples s2*/)
{
int i;
//SoundSamples add = s1;
//add.length += s2.length;
this->length += s1.length;
std::cout << "idk";
//const int size = add.length;
int size = this->length;
float samps [size];
for (i = 0; i < size; i++)
{
//if (i < (size - s2.length)) samps [i] = s1.samples [i];
if (i < (size - s1.length)) samps [i] = this->samples [i];
else samps [i] = s1.samples [i - (size - s1.length)];
}
std::cout << "testp1";
//add.samples = samps;
delete this->samples;
this->samples = samps;
std::cout << "test";
//return add;
return *this;
}
/**
*Get method for the sample rate.
*@return float representing sample rate
**/
float SoundSamples :: getSampleRate ()
{
return this->sampleRate;
}
/**
*Get method for the length of the sample array.
*@return float representing length of samples
**/
int SoundSamples :: getLength ()
{
return this->length;
}
/**
*Override method for the sample at the specified index.
*@param index for desired sample
*@return float representing a single sound sample
**/
float & SoundSamples :: operator[] (int index)
{
return samples [index];
}
void SoundSamples :: reverb2 (float delay, float attenuation)
{
int i;
if (delay < 0)
{
std::cerr << "Error: delay value is less than zero\n";
return;
}
if (attenuation < 0)
{
std::cerr << "Error: attenuation value is less than zero\n";
return;
}
int sdelay = this->sampleRate * delay;
int size = this->length;
float samps [size];
for (i = 0; i < size; i++)
{
//if (i < (size - s2.length)) samps [i] = s1.samples [i];
if (i < sdelay) samps [i] = this->samples [i];
else samps [i] = this->samples [i] + samps [i - sdelay] * attenuation;
}
this->samples = samps;
}
void SoundSamples :: asdr (float atime, float alevel, float dtime, float slevel, float rtime)
{
int i;
if (((atime * sampleRate) + (dtime * sampleRate) + (rtime * sampleRate)) > this->length)
{
std::cerr << "Error: The three times given are greater then the duration of the sound sample currently defined\n";
return;
}
//y - y1 = m (x - x1)
//m1: 0 - alevel(y) = m (0 - atime(x)) -> m = y / x
float m1 = alevel / atime;
//m2: alevel - slevel(y) = m (atime - (atime + dtime)(x))
float m2 = (alevel - slevel) / (atime - (atime + dtime));
//m3: slevel - 0(y) = m ((totalDuration - rtime) - totalDuration(x))
float m3 = alevel / (((this->length / this->sampleRate) - rtime) - (this->length / this->sampleRate));
int size = this->length;
float samps [size];
for (i = 0; i < size; i++)
{
if (i < atime * sampleRate) samps [i] = this->samples [i] * m1;
else if (i < (atime * sampleRate) + (dtime * sampleRate)) samps [i] = this->samples [i] * (alevel - m2 * (atime * sampleRate - i));
else if (i < size - (rtime * sampleRate)) samps [i] = this->samples [i] * slevel;
else samps [i] = this->samples [i] * (slevel - m3 * (((size * sampleRate) - (rtime * sampleRate)) - i));
}
this->samples = samps;
}