-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.cpp
90 lines (66 loc) · 2.74 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
// Copyright (C) 2024 Parabola Research Limited
// SPDX-License-Identifier: MPL-2.0
#include <bungee/Bungee.h>
#include <bungee/CommandLine.h>
int main(int argc, const char *argv[])
{
using namespace Bungee;
Request request{};
#ifndef BUNGEE_IMPLEMENTATION
# define BUNGEE_IMPLEMENTATION Basic
#endif
#define XSTR(a) STR(a)
#define STR(A) #A
static const auto helpString = std::string("Bungee " XSTR(BUNGEE_IMPLEMENTATION)) + " audio speed and pitch changer\n\nVersion: " + Bungee::Stretcher<BUNGEE_IMPLEMENTATION>::version() + "\n";
CommandLine::Options options{"<bungee-command>", helpString};
CommandLine::Parameters parameters{options, argc, argv, request};
CommandLine::Processor processor{parameters, request};
Bungee::Stretcher<BUNGEE_IMPLEMENTATION> stretcher(processor.sampleRates, processor.channelCount);
processor.restart(request);
stretcher.preroll(request);
const int pushFrameCount = parameters["push"].as<int>();
if (pushFrameCount)
{
// This code exists only to demonstrate the usage of the Bungee stretcher with the Push::InputBuffer
// See the else part of the code for an example of the native "pull" API.
std::cout << "Using Push::InputBuffer with " << pushFrameCount << " frames per push\n";
InputChunk inputChunk = stretcher.specifyGrain(request);
Push::InputBuffer pushInputBuffer(stretcher.maxInputFrameCount() + pushFrameCount, processor.channelCount);
pushInputBuffer.grain(inputChunk);
bool done = false;
for (int position = 0; !done; position += pushFrameCount)
{
// Here we loop over segments of input audio, each with pushFrameCount audio frames.
// First get pushFrameCount frames of audio from the input
processor.getInputAudio(pushInputBuffer.inputData(), pushInputBuffer.stride(), position, pushFrameCount);
// The following function and loop delivers pushFrameCount to Bungee
// Zero or more output audio chunks will be emitted and we concatenate these.
pushInputBuffer.deliver(pushFrameCount);
while (pushInputBuffer.inputFrameCountRequired() <= 0)
{
stretcher.analyseGrain(pushInputBuffer.outputData(), pushInputBuffer.stride());
OutputChunk outputChunk;
stretcher.synthesiseGrain(outputChunk);
stretcher.next(request);
done = processor.write(outputChunk);
inputChunk = stretcher.specifyGrain(request);
pushInputBuffer.grain(inputChunk);
}
}
}
else
{
// Regular pull API
for (bool done = false; !done;)
{
InputChunk inputChunk = stretcher.specifyGrain(request);
stretcher.analyseGrain(processor.getInputAudio(inputChunk), processor.inputChannelStride);
OutputChunk outputChunk;
stretcher.synthesiseGrain(outputChunk);
stretcher.next(request);
done = processor.write(outputChunk);
}
}
processor.writeOutputFile();
return 0;
}