-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
241 lines (199 loc) · 5.8 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <cstdlib>
#include <iostream>
#include <list>
#include <string>
#include <functional>
#include <exception>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/program_options.hpp>
#include <portaudio.h>
#include <gtk/gtk.h>
#include <pthread.h>
using namespace std;
namespace po = boost::program_options;
/* options:
api:
device:
buffer size: in frames
sample rate:
bit depth:
fragment size: in frames
dither:
*/
/* program options */
int sample_rate;
int bit_depth;
int channels;
int api_id;
/* a frame is, in interleaved pcm audio, a sequence of samples for each channel */
typedef int framecount_t;
framecount_t buffer_size;
framecount_t fragment_size;
typedef int milliseconds_t;
milliseconds_t update_ui_rate;
/* */
int framesize;
std::list<std::string> song_list;
//boost::spsc_queue<char> buffer;
PaStream *stream;
GtkWidget *window;
/* function prototypes */
void init_log(void);
void init_options(void);
void init_gui(void);
void init_portaudio(void);
void init_set_gdk_threadaware(void);
void enumerate_devices_by_api(int api_idx);
void close_portaudio(void);
void close_log(void);
/* */
int main(int argc, char **argv) {
init_log();
gtk_init(&argc, &argv);
init_options();
init_gui();
init_portaudio();
init_set_gdk_threadaware();
gdk_threads_enter();
gtk_main();
/*
close all gui related threads here!
*/
gdk_threads_leave();
close_portaudio();
close_log();
exit(EXIT_SUCCESS);
}
void init_log(void) {
clog << "\n";
clog << "ffmpeg version: " << "\n";
clog << Pa_GetVersionText();
clog << std::endl;
}
void init_set_gdk_threadaware(void) {
if(!g_thread_supported()) {
g_thread_init(NULL);
}
gdk_threads_init();
}
void init_options(void) {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("sample rate", po::value<int>(), "Set output sample rate")
("bit depth", po::value<std::string>(), "Set output bit depth")
("api", po::value<std::string>(), "API to use. It can be alsa, pulseaudio. Empty string uses default.")
("device", po::value<std::string>(), "Device. It can be alsa, pulseaudio. Empty string uses default.")
;
if (sample_rate)
}
void init_gui(void) {
GtkBuilder *builder;
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "gui.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
gtk_builder_connect_signals(builder, NULL);
g_object_unref(G_OBJECT(builder));
gtk_widget_show(window);
}
void init_portaudio(void) {
PaError ret =
Pa_Initialize();
if (ret != paNoError) {
cerr << "Portaudio error: " << Pa_GetErrorText(ret) << "\n";
exit(EXIT_FAILURE);
}
int api_max = Pa_GetHostApiCount();
if (api_max < 0) { /* if api_max is negative it is actually an error code */
cerr << "Portaudio error: " << Pa_GetErrorText(api_max) << "\n";
exit(EXIT_FAILURE);
}
if (api_max == 0) {
cerr << "Error: there is no software for audio output" << "\n";
exit(EXIT_FAILURE);
}
std::vector<const char*> api_names;
std::vector<PaHostApiTypeId> api_ids;
for (int api_idx = 0; api_idx < api_max; api_idx++) {
const PaHostApiInfo* api_info = Pa_GetHostApiInfo(api_idx);
api_names.push_back(api_info->name);
api_ids.push_back(api_info->typeId);
}
clog << "the system has support for:\n";
for (int api_idx = 0; api_idx < api_max; api_idx++) {
clog << " - " << api_names[api_idx] << "\n";
}
for (int api_idx = 0; api_idx < api_max; api_idx++) {
enumerate_devices_by_api(api_idx);
}
int api = 0;
if (api_id == 0) {
api = Pa_GetDefaultHostApi();
clog << "using default api '" << Pa_GetHostApiInfo(api)->name << "'\n";
} else { /* check if requested api is in the 'supported by os' list */
if (auto std::find(api_typeids.begin(), api_typeids.end(), api) == api_typeids.end()) {
int old_api_idx = Pa_HostApiTypeIdToHostApiIndex(api);
const PaHostApiInfo* api_info = Pa_GetHostApiInfo(old_api);
if (api_info)
api = Pa_GetDefaultHostApi();
clog << "warning: requested api '" << "" << "' is not supported by the system. switched to "
<< Pa_GetHostApiInfo(api)->name << "\n";
}
}
if (device == 0) {
device = Pa_GetDefaultOutputDevice();
if (device == paNoDevice) {
cerr << "portaudio error: no output device\n"
exit(EXIT_FAILURE);
}
}
if (sample_rate == 0) {
sample_rate = (int) Pa_GetDeviceInfo(device)->defaultSampleRate;
}
/*
PaStreamParameters outputparam;
outputparam
ret =
Pa_OpenStream(&stream, , , (double) sample_rate, paFramesPerBufferUnspecified, , , NULL);
if (ret != paNoError) {
cerr << "Portaudio error: " << Pa_GetErrorText(ret) << "\n";
exit(EXIT_FAILURE);
}
clog << "opened device " << "" << "with following parameters:\n";
clog << " - sample rate: " << sample_rate << " hz." << "\n";
clog << " - bit depth: " << bit_depth << " bit." << "\n";
clog << " - channels: " << channels << "." << "\n";
*/
clog << std::endl;
}
void enumerate_devices_by_api(int api_idx) {
const PaHostApiInfo* api_info = Pa_GetHostApiInfo(api_idx);
clog << "detected devices for " << api_info->name << "\n";
for (int device_idx_api_relative = 0;
device_idx_api_relative < api_info->deviceCount;
++device_idx_api_relative)
{
int device_idx_absolute = Pa_HostApiDeviceIndexToDeviceIndex(api_idx, device_idx_api_relative);
const PaDeviceInfo *device_info = Pa_GetDeviceInfo(device_idx_absolute);
clog << device_idx_api_relative+1 /* count */ << ": " << device_info->name << "\n";
clog << "default sample rate: " << device_info->defaultSampleRate << " hz\n";
clog << "number of output channels: " << device_info->maxOutputChannels << "\n";
}
}
void process(void* out, framecount_t framecount) {
}
void callback(void) {
}
void background(void) {
}
void async(void) {
}
void update_ui(void) {
//wait(update_ui_rate);
}
void close_portaudio(void) {
Pa_Terminate();
}
void close_log(void) {
clog << "bye!" << std::endl;
}