-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathGeneratedAudioSource.cpp
63 lines (51 loc) · 1.36 KB
/
GeneratedAudioSource.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
#include "GeneratedAudioSource.h"
GeneratedAudioSource::GeneratedAudioSource(ffmpegcpp::AudioFrameSink* frameSink)
{
this->sampleRate = 44100;
this->channels = 2;
this->format = AV_SAMPLE_FMT_S16;
// generate a raw video source that will convert the raw format to any other format and pass it on to the encoder
// or any other sink (might be a filter as well).
output = std::make_unique<ffmpegcpp::RawAudioDataSource>(format, this->sampleRate, this->channels, frameSink);
samples = new uint16_t[channels * 2 * sampleCount];
}
GeneratedAudioSource::~GeneratedAudioSource()
{
delete samples;
}
void GeneratedAudioSource::PreparePipeline()
{
while (!output->IsPrimed() && !IsDone())
{
Step();
}
}
bool GeneratedAudioSource::IsDone() const
{
return frameNumber >= 120;
}
void GeneratedAudioSource::Step()
{
/* encode a single tone sound */
float t = 0.0f;
float tincr = (float)(2.0 * M_PI * 440.0) / (float)sampleRate;
for (int i = 0; i < 120; i++)
{
/* make sure the frame is writable -- makes a copy if the encoder
* kept a reference internally */
for (int j = 0; j < sampleCount; j++)
{
samples[2 * j] = (int)(sin(t) * 10000);
for (int k = 1; k < channels; k++)
samples[2 * j + k] = samples[2 * j];
t += tincr;
}
// submit to the sink
output->WriteData(samples, sampleCount);
++frameNumber;
}
if (IsDone())
{
output->Close();
}
}