-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaudio_upsample.cpp
More file actions
112 lines (84 loc) · 3.01 KB
/
Copy pathaudio_upsample.cpp
File metadata and controls
112 lines (84 loc) · 3.01 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
#include <primo/avblocks/avb.h>
#include <primo/platform/reference++.h>
#include <primo/platform/error_facility.h>
#include <primo/platform/ustring.h>
#include "util.h"
#include "options.h"
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace primo::codecs;
using namespace primo::avblocks;
using namespace std;
primo::ref<MediaSocket> createOutputSocket(Options& opt)
{
// create stream info to describe the output audio stream
auto asi = primo::make_ref(Library::createAudioStreamInfo());
asi->setStreamType(StreamType::MPEG_Audio);
asi->setStreamSubType(StreamSubType::MPEG_Audio_Layer3);
// Set the output sampling rate to 48 KHz (48000 Hz)
// This will trigger resampling from the input sample rate (e.g., 44.1 KHz)
asi->setSampleRate(48000);
// Optionally set the bitrate (default is 128000)
// asi->setBitrate(192000);
// Optionally set the number of channels
// asi->setChannels(1); // Mono
// create a pin using the stream info
auto pin = primo::make_ref(Library::createMediaPin());
pin->setStreamInfo(asi.get());
// the pin allows you to specify additional parameters for the encoder
// for example, change the stereo mode, e.g. Joint Stereo
// pin->params()->addInt(Param::Encoder::Audio::MPEG1::StereoMode, StereoMode::Joint);
// finally create a socket for the output container format which is MP3 in this case
auto socket = primo::make_ref(Library::createMediaSocket());
socket->setStreamType(StreamType::MPEG_Audio);
socket->setStreamSubType(StreamSubType::MPEG_Audio_Layer3);
socket->pins()->add(pin.get());
// output to a file
auto output_file = primo::ustring(opt.outputFile);
socket->setFile(output_file);
return socket;
}
bool upsample(Options& opt)
{
// create input socket
primo::ref<MediaSocket> inSocket (Library::createMediaSocket());
inSocket->setFile(primo::ustring(opt.inputFile));
// create output socket with 48 KHz sample rate
auto outSocket = createOutputSocket(opt);
// create transcoder
auto transcoder = primo::make_ref(Library::createTranscoder());
transcoder->setAllowDemoMode(true);
transcoder->inputs()->add(inSocket.get());
transcoder->outputs()->add(outSocket.get());
// transcoder will fail if output exists (by design)
deleteFile(primo::ustring(opt.outputFile));
if (!transcoder->open())
{
printError("Transcoder open", transcoder->error());
return false;
}
if (!transcoder->run())
{
printError("Transcoder run", transcoder->error());
return false;
}
transcoder->close();
return true;
}
int main(int argc, char* argv[])
{
Options opt;
switch(prepareOptions(opt, argc, argv))
{
case Command: return 0;
case Error: return 1;
case Parsed: break;
}
Library::initialize();
bool result = upsample(opt);
Library::shutdown();
return result ? 0 : 1;
}