-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
143 lines (119 loc) · 3.89 KB
/
audio.c
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
#include "audio.h"
#include "debug.h"
#include <stdio.h>
#include <stdlib.h>
static void sink_info_callback(pa_context *c, const pa_sink_info *i, int eol,
void *userdata) {
if (eol > 0) {
return;
}
audio_t *a = userdata;
a->cur_vol =
(float)pa_cvolume_avg(&(i->volume)) / (float)PA_VOLUME_NORM * 100.0;
DEBUG_PRINT("Sink: %s, Volume: %.0f%%, Muted: %s\n", i->name, a->cur_vol,
i->mute ? "yes" : "no");
}
static void server_info_callback(pa_context *c, const pa_server_info *i,
void *userdata) {
// Query volume of default sink
DEBUG_PRINT("Default sink: %s\n", i->default_sink_name);
pa_operation *o;
o = pa_context_get_sink_info_by_name(c, i->default_sink_name,
&sink_info_callback, userdata);
pa_operation_unref(o);
}
static void subscription_callback(pa_context *c, pa_subscription_event_type_t t,
uint32_t idx, void *userdata) {
unsigned int evf = t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
if (evf == PA_SUBSCRIPTION_EVENT_SINK) {
pa_operation *o;
o = pa_context_get_sink_info_by_index(c, idx, sink_info_callback,
userdata);
pa_operation_unref(o);
}
}
static void query_volume(audio_t *a) {
pa_operation *o;
o = pa_context_get_server_info(a->ctx, &server_info_callback, a);
pa_operation_unref(o);
}
static void context_state_callback(pa_context *c, void *userdata) {
assert(c);
audio_t *aud = userdata;
switch (pa_context_get_state(c)) {
case PA_CONTEXT_SETTING_NAME:
case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_UNCONNECTED:
case PA_CONTEXT_AUTHORIZING:
default:
aud->pa_ready = 0;
break;
case PA_CONTEXT_READY:
pa_context_set_subscribe_callback(c, subscription_callback, userdata);
pa_context_subscribe(c, PA_SUBSCRIPTION_MASK_SINK, NULL, NULL);
fprintf(stderr, "Connection established.\n");
aud->pa_ready = 1;
query_volume(aud);
break;
case PA_CONTEXT_TERMINATED:
fprintf(stderr, "Connection terminated.\n");
break;
case PA_CONTEXT_FAILED:
fprintf(stderr, "Connection failed: %s\n",
pa_strerror(pa_context_errno(c)));
break;
}
}
void term_audio(audio_t *a) {
if (a->ctx) {
pa_context_disconnect(a->ctx);
pa_context_unref(a->ctx);
a->ctx = NULL;
}
if (a->m_loop) {
pa_mainloop_free(a->m_loop);
a->m_loop = NULL;
}
}
float iterate_and_get_volume(audio_t *a, int block) {
if (pa_mainloop_iterate(a->m_loop, block, NULL) < 0) {
fprintf(stderr, "pa_mainloop_iterate() failed\n");
term_audio(a);
exit(1);
}
return a->cur_vol;
}
float init_audio(audio_t *a) {
a->m_loop = pa_mainloop_new();
if (!a->m_loop) {
fprintf(stderr, "pa_mainloop_new() failed\n");
return -1;
}
a->m_loop_api = pa_mainloop_get_api(a->m_loop);
if (!a->m_loop_api) {
fprintf(stderr, "pa_mainloop_get_api() failed\n");
return -1;
}
a->ctx = pa_context_new(a->m_loop_api, "Volume monitor");
if (!a->ctx) {
fprintf(stderr, "pa_context_new() failed\n");
return -1;
}
pa_context_set_state_callback(a->ctx, context_state_callback, a);
if (pa_context_connect(a->ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) {
fprintf(stderr, "pa_context_connect() failed\n");
return -1;
}
a->pa_ready = 0;
a->cur_vol = -1.0f;
for (;;) {
// Block until we get the first volume value
iterate_and_get_volume(a, 1);
// Break when pulseaudio is ready and the initial volume has been
// queried
if (a->pa_ready && a->cur_vol != -1.0f) {
break;
}
}
return a->cur_vol;
}