forked from adamstark/AudioFile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.cpp
146 lines (115 loc) · 5.82 KB
/
examples.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
#include <iostream>
#include <cmath>
#include <AudioFile.h>
//=======================================================================
namespace examples
{
void writeSineWaveToAudioFile();
void loadAudioFileAndPrintSummary();
void loadAudioFileAndProcessSamples();
}
//=======================================================================
int main()
{
//---------------------------------------------------------------
/** Writes a sine wave to an audio file */
examples::writeSineWaveToAudioFile();
//---------------------------------------------------------------
/** Loads an audio file and prints key details to the console*/
examples::loadAudioFileAndPrintSummary();
//---------------------------------------------------------------
/** Loads an audio file and processess the samples */
examples::loadAudioFileAndProcessSamples();
return 0;
}
//=======================================================================
namespace examples
{
//=======================================================================
void writeSineWaveToAudioFile()
{
//---------------------------------------------------------------
std::cout << "**********************" << std::endl;
std::cout << "Running Example: Write Sine Wave To Audio File" << std::endl;
std::cout << "**********************" << std::endl << std::endl;
//---------------------------------------------------------------
// 1. Let's setup our AudioFile instance
AudioFile<float> a;
a.setNumChannels (2);
a.setNumSamplesPerChannel (44100);
//---------------------------------------------------------------
// 2. Create some variables to help us generate a sine wave
const float sampleRate = 44100.f;
const float frequencyInHz = 440.f;
//---------------------------------------------------------------
// 3. Write the samples to the AudioFile sample buffer
for (int i = 0; i < a.getNumSamplesPerChannel(); i++)
{
for (int channel = 0; channel < a.getNumChannels(); channel++)
{
a.samples[channel][i] = sin ((static_cast<float> (i) / sampleRate) * frequencyInHz * 2.f * (float)M_PI);
}
}
//---------------------------------------------------------------
// 4. Save the AudioFile
std::string filePath = "sine-wave.wav"; // change this to somewhere useful for you
a.save ("sine-wave.wav", AudioFileFormat::Wave);
}
//=======================================================================
void loadAudioFileAndPrintSummary()
{
//---------------------------------------------------------------
std::cout << "**********************" << std::endl;
std::cout << "Running Example: Load Audio File and Print Summary" << std::endl;
std::cout << "**********************" << std::endl << std::endl;
//---------------------------------------------------------------
// 1. Set a file path to an audio file on your machine
const std::string filePath = std::string (PROJECT_BINARY_DIR) + "/test-audio.wav";
//---------------------------------------------------------------
// 2. Create an AudioFile object and load the audio file
AudioFile<float> a;
bool loadedOK = a.load (filePath);
/** If you hit this assert then the file path above
probably doesn't refer to a valid audio file */
assert (loadedOK);
//---------------------------------------------------------------
// 3. Let's print out some key details
std::cout << "Bit Depth: " << a.getBitDepth() << std::endl;
std::cout << "Sample Rate: " << a.getSampleRate() << std::endl;
std::cout << "Num Channels: " << a.getNumChannels() << std::endl;
std::cout << "Length in Seconds: " << a.getLengthInSeconds() << std::endl;
std::cout << std::endl;
}
//=======================================================================
void loadAudioFileAndProcessSamples()
{
//---------------------------------------------------------------
std::cout << "**********************" << std::endl;
std::cout << "Running Example: Load Audio File and Process Samples" << std::endl;
std::cout << "**********************" << std::endl << std::endl;
//---------------------------------------------------------------
// 1. Set a file path to an audio file on your machine
const std::string inputFilePath = std::string (PROJECT_BINARY_DIR) + "/test-audio.wav";
//---------------------------------------------------------------
// 2. Create an AudioFile object and load the audio file
AudioFile<float> a;
bool loadedOK = a.load (inputFilePath);
/** If you hit this assert then the file path above
probably doesn't refer to a valid audio file */
assert (loadedOK);
//---------------------------------------------------------------
// 3. Let's apply a gain to every audio sample
float gain = 0.5f;
for (int i = 0; i < a.getNumSamplesPerChannel(); i++)
{
for (int channel = 0; channel < a.getNumChannels(); channel++)
{
a.samples[channel][i] = a.samples[channel][i] * gain;
}
}
//---------------------------------------------------------------
// 4. Write audio file to disk
std::string outputFilePath = "quieter-audio-file.wav"; // change this to somewhere useful for you
a.save (outputFilePath, AudioFileFormat::Aiff);
}
}