-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave.cpp
More file actions
311 lines (248 loc) · 10.7 KB
/
wave.cpp
File metadata and controls
311 lines (248 loc) · 10.7 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// This file contains various example Wave-file manipulation functions using
// the wave.h API with a simple, interactive user interface.
#include "wave.h"
#include <string>
#include <iostream>
using namespace std;
// Functions for manipulating .WAV files. See below for more details.
double* WavLoad(string const& filename);
int WavLength(string const& filename);
void WavSave(string const& filename, double const* samples, int nsamples);
// Speed it up by dropping every other sample.
void faster(string const& filename, string const& result) {
int old_samples_length = WavLength(filename);
double* old_samples = WavLoad(filename);
int new_samples_length = old_samples_length / 2;
double* new_samples = new double[new_samples_length];
for (int i = 0; i != new_samples_length; ++i) {
new_samples[i] = old_samples[i*2];
}
WavSave(result, new_samples, new_samples_length);
delete[] new_samples;
delete[] old_samples;
}
// Slow it down by duplicating every other sample.
void slower(string const& filename, string const& result) {
int old_samples_length = WavLength(filename);
double* old_samples = WavLoad(filename);
int new_samples_length = old_samples_length * 2;
double* new_samples = new double[new_samples_length];
for (int i = 0; i != new_samples_length; ++i) {
new_samples[i] = old_samples[i/2];
}
WavSave(result, new_samples, new_samples_length);
delete[] new_samples;
delete[] old_samples;
}
// Create an echo effect by adding samples back in after a delay.
void echo(string const& filename, string const& result) {
int old_samples_length = WavLength(filename);
double* old_samples = WavLoad(filename);
// Number of samples before the echo.
int echo_delay = 10000;
// Echo intensity.
double echo_intensity = 0.8;
int new_samples_length = old_samples_length + echo_delay;
double* new_samples = new double[new_samples_length];
for (int i = 0; i != new_samples_length; ++i) {
// We must cover 3 cases here:
// 1. We're at least echo_delay samples into the file, so we
// can be sure that i-echo_delay will still land us inside
// the file and i is still within the bounds of the
// old_samples array. In this case, our new sample is the
// average of the old sample and the sample from echo_delay
// samples ago.
if (i >= echo_delay && i < old_samples_length) {
new_samples[i] = (old_samples[i]
+ echo_intensity * old_samples[i-echo_delay])/2;
// 2. We're at least echo_delay samples into the file and i
// has outrun the length of the old_samples array. In this
// case, the new sample will just be echo, i.e. the sample
// from echo_delay samples ago.
} else if (i >= echo_delay && i >= old_samples_length) {
new_samples[i] = echo_intensity * old_samples[i-echo_delay];
// 3. Else we're not echo_delay samples into the file yet.
// Therefore, the new sample will just be the old sample
// because we've got nothing to echo yet.
} else {
new_samples[i] = old_samples[i];
}
}
WavSave(result, new_samples, new_samples_length);
delete[] new_samples;
delete[] old_samples;
}
// Increase the volume (amplitude) by 20%.
void amp_up(string const& filename, string const& result) {
int old_samples_length = WavLength(filename);
double* old_samples = WavLoad(filename);
double factor = 1.2;
for (int i = 0; i != old_samples_length; ++i) {
old_samples[i] = factor * old_samples[i];
}
WavSave(result, old_samples, old_samples_length);
delete[] old_samples;
}
// Decrease the volume (amplitude) by 20%.
void amp_down(string const& filename, string const& result) {
int old_samples_length = WavLength(filename);
double* old_samples = WavLoad(filename);
double factor = 0.8;
for (int i = 0; i != old_samples_length; ++i) {
old_samples[i] = factor * old_samples[i];
}
WavSave(result, old_samples, old_samples_length);
delete[] old_samples;
}
// Reverse.
void reverse(string const& filename, string const& result) {
int old_samples_length = WavLength(filename);
double* old_samples = WavLoad(filename);
int new_samples_length = old_samples_length;
double* new_samples = new double[new_samples_length];
for (int i = 0; i != new_samples_length; ++i) {
new_samples[i] = old_samples[old_samples_length - 1 - i];
}
WavSave(result, new_samples, new_samples_length);
delete[] new_samples;
delete[] old_samples;
}
// Mix two WAVs together. The new file should be as long as the longest operand
// file. The shorter file gets looped.
void mix(string const& file1, string const& file2, string const& result) {
int file1_length = WavLength(file1);
int file2_length = WavLength(file2);
double* file1_samples = WavLoad(file1);
double* file2_samples = WavLoad(file2);
int longest_length;
int shortest_length;
double* longest_samples;
double* shortest_samples;
// Figure out which file is longest.
if (file1_length > file2_length) {
longest_length = file1_length;
longest_samples = file1_samples;
shortest_length = file2_length;
shortest_samples = file2_samples;
} else {
longest_length = file2_length;
longest_samples = file2_samples;
shortest_length = file1_length;
shortest_samples = file1_samples;
}
for (int i = 0; i != longest_length; ++i) {
// The new sample will be the average of the sample from both
// files. The expression "i % shortest_length" returns a value
// between 0 and shortest_length-1, ensuring that we never read
// beyond the bounds of shortest_samples.
longest_samples[i] = (longest_samples[i] + shortest_samples[i%shortest_length]) / 2;
}
WavSave(result, longest_samples, longest_length);
delete[] file1_samples;
delete[] file2_samples;
}
int main(int argc, char** argv) {
cout << "This here is an interactive program for manipulating MS Wave files." << endl;
cout << "Usage: <mode> <input WAV(s)> <output WAV>" << endl
<< "Where mode can be one of the following:" << endl
<< "\t f : Faster." << endl
<< "\t s : Slower." << endl
<< "\t e : Echo." << endl
<< "\t r : Reverse." << endl
<< "\t + : Plus volume." << endl
<< "\t - : Minus volume." << endl
<< "\t m : Mix two .WAV files together. Takes an extra filename argument." << endl
<< "\t q : Quit." << endl
<< endl;
// An infinite loop. Exits when the user enters 'q'.
for (;;) {
cout << "> ";
char mode;
cin >> mode;
string input_file, input_file2, output_file;
switch (mode) {
case 'f':
cin >> input_file >> output_file;
cout << "Faster!" << endl;
faster(input_file, output_file);
break;
case 's':
cin >> input_file >> output_file;
cout << "Slower!" << endl;
slower(input_file, output_file);
break;
case 'e':
cin >> input_file >> output_file;
cout << "Echo!" << endl;
echo(input_file, output_file);
break;
case 'r':
cin >> input_file >> output_file;
cout << "Reverse!" << endl;
reverse(input_file, output_file);
break;
case '+':
cin >> input_file >> output_file;
cout << "Increase volume!" << endl;
amp_up(input_file, output_file);
break;
case '-':
cin >> input_file >> output_file;
cout << "Decrease volume!" << endl;
amp_down(input_file, output_file);
break;
case 'm':
cin >> input_file >> input_file2 >> output_file;
cout << "Mix!" << endl;
mix(input_file, input_file2, output_file);
break;
case 'q':
cout << "Exiting." << endl;
return 0;
default:
cout << "Unknown mode: " << mode << endl
<< "Use 'q' to quit." << endl;
cin.clear();
}
cout << endl;
}
return 0;
}
// Reads and returns the sample values from a .WAV file. We return the samples
// as a series of values between +1.0 and -1.0. Use WavLength() to get the
// number of values returned. This functions allocates memory to store its
// return value, so use delete[] to free this memory.
double* WavLoad(string const& filename) {
Wave wave;
wave.Load(filename);
int nsamples = wave.nsamples();
double* samples = new double[nsamples];
for (int i = 0; i != nsamples; ++i) {
samples[i] = wave.GetSample(i);
}
return samples;
}
// Returns the length of the "array" of data values returned by WavLoad().
int WavLength(string const& filename) {
Wave wave;
wave.LoadMetadata(filename);
return wave.nsamples();
}
// Writes or overwrites a .WAV file with the parameter sample values. We expect
// the samples to be in the range -1.0 to +1.0, like those returned by
// WavLoad().
void WavSave(string const& filename, double const* samples, int nsamples) {
Wave wave;
// If the Wave file exists, then let's save as much metadata as possible
// from it.
wave.LoadMetadata(filename);
// If we have more than one channel, the Wave class will just mirror the
// same sample data across all channels, doubling (at least) the size of
// the file for no reason.
wave.fmt_chunk.nchannels = 1;
wave.Resize(nsamples);
for (int i = 0; i != nsamples; ++i) {
wave.SetSample(i, samples[i]);
}
wave.Save(filename);
}