-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
98 lines (78 loc) · 2.84 KB
/
main.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
/*
Grant Cox
CS315 Final Project
11/28/2017
*/
/*
This program uses the Short Time Fourier Transform to shift a .wav input file
up or down the number of semitones that the user requests.
Usage: ./pitchshift filein fileout numkeys
where filein is an existing .wav file, fileout is the outputted .wav file, and
numkeys is a positive or negative such that -12 <= numkeys <= 12. This is the
number of keys to pitch shift the recording by.
*/
//frequency = 440 * 2^(n/12)
#include <iostream>
#include <string>
#include "smbPitchShift.cpp"
#include "AudioFile.h"
using namespace std;
int main(int argc, char* argv[]) {
//input checking
if(argc < 2 || argc > 4) {
cerr << "Usage: ./pitchshift filein fileout numkeys" << endl;
return 1;
}
///////////////////////////////////////////////////////////////////////////
// open files
//
//
string fname = argv[1];
AudioFile<float> siginFloat;
cout << "loading audio file(float)...";
siginFloat.load(fname);
cout << "done." << endl;
///////////////////////////////////////////////////////////////////////////
// get information for pitch shift, print file information
//
//
cout << "\nSummary of input file: " << endl;
siginFloat.printSummary();
float pitchShift = stof(argv[3]);
pitchShift = pow(2,pitchShift/12);
cout << "shifting factor:\t" << pitchShift << endl;
int samples = siginFloat.getNumSamplesPerChannel();
long fftFrameSize = 2048;
long oversampfac = 32;
float sampleRate = siginFloat.getSampleRate();
//TODO test the performance of the algorithm with different fftFrameSize,
//TODO how does it perform with smaller or larger vals?
///////////////////////////////////////////////////////////////////////////
// allocate data to float and double arrays
//
//
cout << "\nallocating memory and copying for pitch shifting...";
float * dataFloat = new float[samples];
for(int i = 0; i < samples; i++) dataFloat[i] = siginFloat.samples[0][i];
cout << "done." << endl;
///////////////////////////////////////////////////////////////////////////
// do the pitch shifting, write output to input buffer
//
//
cout << "\npitch shifting...";
smbPitchShift(pitchShift, samples, fftFrameSize, oversampfac, sampleRate, dataFloat, dataFloat);
cout << "done\n";
///////////////////////////////////////////////////////////////////////////
// output to file
//
//
for(int i = 0; i < samples; i++) siginFloat.samples[0][i] = dataFloat[i];
string fnameout = argv[2];
cout << "writing to output file...";
siginFloat.save(fnameout);
cout << "done." << endl;
cout << "deallocating and exiting..." << endl;
delete[] dataFloat;
return 0;
}