-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathambec.h
146 lines (119 loc) · 4.17 KB
/
ambec.h
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
/*
* A library for working with DVSI's AMBE vocoder chips
*
* Copyright (C) 2019-2020 Internet Real-Time Lab, Columbia University
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <iostream>
#include <cstring>
#include <exception>
#include <sndfile.hh>
#include "serial.h"
#include "api.h"
#include "queue.h"
#include "device.h"
using namespace std;
namespace ambe {
using namespace std::chrono;
enum class ClientMode {
SYNCHRONOUS,
CONCURRENT
};
struct ArgData {
ClientMode mode = ClientMode::SYNCHRONOUS;
string in_file;
string out_file;
string uri;
Rate rate;
int channels = 0;
DeviceMode device_mode = DeviceMode::USB;
int pipeline_size = 2;
public:
ArgData(int argc, char* argv[]) : rate(33) {
ProcessArgs(argc, argv);
}
~ArgData() {}
private:
void ProcessArgs(int argc, char* argv[]);
};
// Client provides an interface to compress and decompress
// data using API services which communicate with the AMBE device.
//
// Data Members:
// - dev: The AMBE device object. Note that it is thread safe.
// - scheduler: AMBE device request scheduler
// - ambe: An instance of AMBE API class connected to the device via the scheduler
class Client {
ArgData args;
Device& device;
API& ambe;
unsigned int channels;
int pipeline_size;
Audio input;
bool save_output;
vector<Audio> output;
public:
// Method constructs an Client object by doing following steps:
// 1. Initializes built in type variables with corresponding values.
// 2. Reset and initialize AMBE device.
// 3. Configure all channels on the AMBE device.
//
// Method throws an exception if one of the above steps fails.
Client(const ArgData& args, Device& device, API& api);
// Run compression and decompression in synchronous mode.
//
// This method compresses and decompresses given audio frames
// synchronously, i.e., one at a time. If no output pointer is provided,
// the result is discarded.
duration<double> CompressDecompress(Audio* output, int channel, const Audio& input);
// Method reads data from a file, chunk by chunk, compresses data and
// then stores compressed buffer to the queue so the decompression method
// can read it and decompress. This method is designed to run as a
// separate thread.
//
// Note that it converts s16le to s16be before passing data to compression
// as the chip inside the dongle is big endian and expects 16-bit audio
// samples.
template<typename Callable>
duration<double> Compress(Callable output, int channel, const Audio& input, uint pipeline_size);
// Method reads data from the queue (compressor thread pushes data to this
// queue), decompresses it and writes to a file. This method is designed to
// run as a separate thread.
//
// Note that it converts s16be to s16le before storing data to a file
// as the chip inside the dongle is big endian and decompression output
// is s16be.
duration<double> Decompress(Audio* output, int channel, const AmbeBits& input, uint pipeline_size);
void SaveOutput();
AmbeBits PreCompress();
// Static method for running client in two different modes: USB and GRPC
static void RunUSBMode(const ArgData& args, const string& authority);
static void RunGRPCMode(const ArgData& args, const string& authority);
void SynchronousMode();
void ConcurrentMode();
};
class ClientException : public exception {
string msg = "ambe::ClientException";
public:
ClientException(const char* message = "") {
msg += string(message);
}
~ClientException() {}
const char* what () const throw () {
return msg.c_str();
}
};
}