summaryrefslogtreecommitdiff
path: root/src/tests/lo-test-util.c
blob: 2ff8cf83d7adaec36f39f3f6039bef42d93f2f94 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/***
  This file is part of PulseAudio.

  Copyright 2013 Collabora Ltd.
  Author: Arun Raghavan <arun.raghavan@collabora.co.uk>

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  PulseAudio 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 Lesser General Public License
  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <math.h>

#include <pulsecore/log.h>
#include <pulsecore/macro.h>
#include <pulsecore/core-util.h>

#include "lo-test-util.h"

/* Keep the frequency high so RMS over ranges of a few ms remains relatively
 * high as well */
#define TONE_HZ 4410

static void nop_free_cb(void *p) {
}

static void underflow_cb(struct pa_stream *s, void *userdata) {
    pa_log_warn("Underflow\n");
}

static void overflow_cb(struct pa_stream *s, void *userdata) {
    pa_log_warn("Overlow\n");
}

/*
 * We run a simple volume calibration so that we know we can detect the signal
 * being played back. We start with the playback stream at 100% volume, and
 * capture at 0.
 *
 * First, we then play a sine wave and increase the capture volume till the
 * signal is clearly received.
 *
 * Next, we play back silence and make sure that the level is low enough to
 * distinguish from when playback is happening.
 *
 * Finally, we hand off to the real read/write callbacks to run the actual
 * test.
 */

enum {
    CALIBRATION_ONE,
    CALIBRATION_ZERO,
    CALIBRATION_DONE,
};

static int cal_state = CALIBRATION_ONE;

static void calibrate_write_cb(pa_stream *s, size_t nbytes, void *userdata) {
    pa_lo_test_context *ctx = (pa_lo_test_context *) userdata;
    int i, nsamp = nbytes / ctx->fs;
    float tmp[nsamp][2];
    static int count = 0;

    /* Write out a sine tone */
    for (i = 0; i < nsamp; i++)
        tmp[i][0] = tmp[i][1] = cal_state == CALIBRATION_ONE ? sinf(count++ * TONE_HZ * 2 * M_PI / ctx->sample_spec.rate) : 0.0f;

    pa_assert_se(pa_stream_write(s, &tmp, nbytes, nop_free_cb, 0, PA_SEEK_RELATIVE) == 0);

    if (cal_state == CALIBRATION_DONE)
        pa_stream_set_write_callback(s, ctx->write_cb, ctx);
}

static void calibrate_read_cb(pa_stream *s, size_t nbytes, void *userdata) {
    pa_lo_test_context *ctx = (pa_lo_test_context *) userdata;
    static double v = 0;
    static int skip = 0, confirm;

    pa_cvolume vol;
    pa_operation *o;
    int nsamp;
    float *in;
    size_t l;

    pa_assert_se(pa_stream_peek(s, (const void **)&in, &l) == 0);

    nsamp = l / ctx->fs;

    /* For each state or volume step change, throw out a few samples so we know
     * we're seeing the changed samples. */
    if (skip++ < 100)
        goto out;
    else
        skip = 0;

    switch (cal_state) {
        case CALIBRATION_ONE:
            /* Try to detect the sine wave. RMS is 0.5, */
            if (pa_rms(in, nsamp) < 0.40f) {
                confirm = 0;
                v += 0.02f;

                if (v > 1.0) {
                    pa_log_error("Capture signal too weak at 100%% volume (%g). Giving up.\n", pa_rms(in, nsamp));
                    pa_assert_not_reached();
                }

                pa_cvolume_set(&vol, ctx->sample_spec.channels, v * PA_VOLUME_NORM);
                o = pa_context_set_source_output_volume(ctx->context, pa_stream_get_index(s), &vol, NULL, NULL);
                pa_assert(o != NULL);
                pa_operation_unref(o);
            } else {
                /* Make sure the signal strength is steadily above our threshold */
                if (++confirm > 5) {
#if 0
                    pa_log_debug(stderr, "Capture volume = %g (%g)\n", v, pa_rms(in, nsamp));
#endif
                    cal_state = CALIBRATION_ZERO;
                }
            }

            break;

        case CALIBRATION_ZERO:
            /* Now make sure silence doesn't trigger a false positive because
             * of noise. */
            if (pa_rms(in, nsamp) > 0.1f) {
                fprintf(stderr, "Too much noise on capture (%g). Giving up.\n", pa_rms(in, nsamp));
                pa_assert_not_reached();
            }

            cal_state = CALIBRATION_DONE;
            pa_stream_set_read_callback(s, ctx->read_cb, ctx);

            break;

        default:
            break;
    }

out:
    pa_stream_drop(s);
}

/* This routine is called whenever the stream state changes */
static void stream_state_callback(pa_stream *s, void *userdata) {
    pa_lo_test_context *ctx = (pa_lo_test_context *) userdata;

    switch (pa_stream_get_state(s)) {
        case PA_STREAM_UNCONNECTED:
        case PA_STREAM_CREATING:
        case PA_STREAM_TERMINATED:
            break;

        case PA_STREAM_READY: {
            pa_cvolume vol;
            pa_operation *o;

            /* Set volumes for calibration */
            if (s == ctx->play_stream) {
                pa_cvolume_set(&vol, ctx->sample_spec.channels, PA_VOLUME_NORM);
                o = pa_context_set_sink_input_volume(ctx->context, pa_stream_get_index(s), &vol, NULL, NULL);
            } else {
                pa_cvolume_set(&vol, ctx->sample_spec.channels, pa_sw_volume_from_linear(0.0));
                o = pa_context_set_source_output_volume(ctx->context, pa_stream_get_index(s), &vol, NULL, NULL);
            }

            if (!o) {
                pa_log_error("Could not set stream volume: %s\n", pa_strerror(pa_context_errno(ctx->context)));
                pa_assert_not_reached();
            } else
                pa_operation_unref(o);

            break;
        }

        case PA_STREAM_FAILED:
        default:
            pa_log_error("Stream error: %s\n", pa_strerror(pa_context_errno(ctx->context)));
            pa_assert_not_reached();
    }
}

/* This is called whenever the context status changes */
static void context_state_callback(pa_context *c, void *userdata) {
    pa_lo_test_context *ctx = (pa_lo_test_context *) userdata;
    pa_mainloop_api *api;

    switch (pa_context_get_state(c)) {
        case PA_CONTEXT_CONNECTING:
        case PA_CONTEXT_AUTHORIZING:
        case PA_CONTEXT_SETTING_NAME:
            break;

        case PA_CONTEXT_READY: {
            pa_buffer_attr buffer_attr;

            pa_make_realtime(4);

            /* Create playback stream */
            buffer_attr.maxlength = -1;
            buffer_attr.tlength = ctx->sample_spec.rate * ctx->fs * ctx->play_latency / 1000;
            buffer_attr.prebuf = 0; /* Setting prebuf to 0 guarantees us the stream will run synchronously, no matter what */
            buffer_attr.minreq = -1;
            buffer_attr.fragsize = -1;

            ctx->play_stream = pa_stream_new(c, "loopback: play", &ctx->sample_spec, NULL);
            pa_assert(ctx->play_stream != NULL);
            pa_stream_set_state_callback(ctx->play_stream, stream_state_callback, ctx);
            pa_stream_set_write_callback(ctx->play_stream, calibrate_write_cb, ctx);
            pa_stream_set_underflow_callback(ctx->play_stream, underflow_cb, userdata);

            pa_stream_connect_playback(ctx->play_stream, getenv("TEST_SINK"), &buffer_attr,
                    PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);

            /* Create capture stream */
            buffer_attr.maxlength = -1;
            buffer_attr.tlength = (uint32_t) -1;
            buffer_attr.prebuf = 0;
            buffer_attr.minreq = (uint32_t) -1;
            buffer_attr.fragsize = ctx->sample_spec.rate * ctx->fs * ctx->rec_latency / 1000;

            ctx->rec_stream = pa_stream_new(c, "loopback: rec", &ctx->sample_spec, NULL);
            pa_assert(ctx->rec_stream != NULL);
            pa_stream_set_state_callback(ctx->rec_stream, stream_state_callback, ctx);
            pa_stream_set_read_callback(ctx->rec_stream, calibrate_read_cb, ctx);
            pa_stream_set_overflow_callback(ctx->rec_stream, overflow_cb, userdata);

            pa_stream_connect_record(ctx->rec_stream, getenv("TEST_SOURCE"), &buffer_attr,
                    PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);

            break;
        }

        case PA_CONTEXT_TERMINATED:
            api = pa_mainloop_get_api(ctx->mainloop);
            api->quit(api, 0);
            break;

        case PA_CONTEXT_FAILED:
        default:
            pa_log_error("Context error: %s\n", pa_strerror(pa_context_errno(c)));
            pa_assert_not_reached();
    }
}

int pa_lo_test_init(pa_lo_test_context *ctx) {
    /* FIXME: need to deal with non-float samples at some point */
    pa_assert(ctx->sample_spec.format == PA_SAMPLE_FLOAT32);

    ctx->ss = pa_sample_size(&ctx->sample_spec);
    ctx->fs = pa_frame_size(&ctx->sample_spec);

    ctx->mainloop = pa_mainloop_new();
    ctx->context = pa_context_new(pa_mainloop_get_api(ctx->mainloop), ctx->context_name);

    pa_context_set_state_callback(ctx->context, context_state_callback, ctx);

    /* Connect the context */
    if (pa_context_connect(ctx->context, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
        pa_log_error("pa_context_connect() failed.\n");
        goto quit;
    }

    return 0;

quit:
    pa_context_unref(ctx->context);
    pa_mainloop_free(ctx->mainloop);

    return -1;
}

int pa_lo_test_run(pa_lo_test_context *ctx) {
    int ret;

    if (pa_mainloop_run(ctx->mainloop, &ret) < 0) {
        pa_log_error("pa_mainloop_run() failed.\n");
        return -1;
    }

    return 0;
}

void pa_lo_test_deinit(pa_lo_test_context *ctx) {
    if (ctx->play_stream) {
        pa_stream_disconnect(ctx->play_stream);
        pa_stream_unref(ctx->play_stream);
    }

    if (ctx->rec_stream) {
        pa_stream_disconnect(ctx->rec_stream);
        pa_stream_unref(ctx->rec_stream);
    }

    if (ctx->context)
        pa_context_unref(ctx->context);

    if (ctx->mainloop)
        pa_mainloop_free(ctx->mainloop);
}

float pa_rms(const float *s, int n) {
    float sq = 0;
    int i;

    for (i = 0; i < n; i++)
        sq += s[i] * s[i];

    return sqrtf(sq / n);
}