summaryrefslogtreecommitdiff
path: root/lib/igt_audio.c
blob: 2321d1c6e3f1413d45b207ffc6bc4c26a10f3281 (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
325
326
327
/*
 * Copyright © 2017 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *  Paul Kocialkowski <paul.kocialkowski@linux.intel.com>
 */

#include "config.h"

#include <math.h>
#include <gsl/gsl_fft_real.h>

#include "igt_audio.h"
#include "igt_core.h"

#define FREQS_MAX	8

/**
 * SECTION:igt_audio
 * @short_description: Library for audio-related tests
 * @title: Audio
 * @include: igt_audio.h
 *
 * This library contains helpers for audio-related tests. More specifically,
 * it allows generating additions of sine signals as well as detecting them.
 */

struct audio_signal_freq {
	int freq;

	short *period;
	int frames;
	int offset;
};

struct audio_signal {
	int channels;
	int sampling_rate;

	struct audio_signal_freq freqs[FREQS_MAX];
	int freqs_count;
};

/**
 * audio_signal_init:
 * @channels: The number of channels to use for the signal
 * @sampling_rate: The sampling rate to use for the signal
 *
 * Allocate and initialize an audio signal structure with the given parameters.
 *
 * Returns: A newly-allocated audio signal structure
 */
struct audio_signal *audio_signal_init(int channels, int sampling_rate)
{
	struct audio_signal *signal;

	signal = malloc(sizeof(struct audio_signal));
	memset(signal, 0, sizeof(struct audio_signal));

	signal->sampling_rate = sampling_rate;
	signal->channels = channels;

	return signal;
}

/**
 * audio_signal_add_frequency:
 * @signal: The target signal structure
 * @frequency: The frequency to add to the signal
 *
 * Add a frequency to the signal.
 *
 * Returns: An integer equal to zero for success and negative for failure
 */
int audio_signal_add_frequency(struct audio_signal *signal, int frequency)
{
	int index = signal->freqs_count;

	if (index == FREQS_MAX)
		return -1;

	/* Stay within the Nyquist–Shannon sampling theorem. */
	if (frequency > signal->sampling_rate / 2)
		return -1;

	/* Clip the frequency to an integer multiple of the sampling rate.
	 * This to be able to store a full period of it and use that for
	 * signal generation, instead of recurrent calls to sin().
	 */
	frequency = signal->sampling_rate / (signal->sampling_rate / frequency);

	igt_debug("Adding test frequency %d\n", frequency);

	signal->freqs[index].freq = frequency;
	signal->freqs[index].frames = 0;
	signal->freqs[index].offset = 0;
	signal->freqs_count++;

	return 0;
}

/**
 * audio_signal_synthesize:
 * @signal: The target signal structure
 *
 * Synthesize the data tables for the audio signal, that can later be used
 * to fill audio buffers. The resources allocated by this function must be
 * freed with a call to audio_signal_clean when the signal is no longer used.
 */
void audio_signal_synthesize(struct audio_signal *signal)
{
	short *period;
	double value;
	int frames;
	int freq;
	int i, j;

	if (signal->freqs_count == 0)
		return;

	for (i = 0; i < signal->freqs_count; i++) {
		freq = signal->freqs[i].freq;
		frames = signal->sampling_rate / freq;

		period = calloc(1, frames * sizeof(short));

		for (j = 0; j < frames; j++) {
			value = 2.0 * M_PI * freq / signal->sampling_rate * j;
			value = sin(value) * SHRT_MAX / signal->freqs_count;

			period[j] = (short) value;
		}

		signal->freqs[i].period = period;
		signal->freqs[i].frames = frames;
	}
}

/**
 * audio_signal_synthesize:
 * @signal: The target signal structure
 *
 * Free the resources allocated by audio_signal_synthesize and remove
 * the previously-added frequencies.
 */
void audio_signal_clean(struct audio_signal *signal)
{
	int i;

	for (i = 0; i < signal->freqs_count; i++) {
		if (signal->freqs[i].period)
			free(signal->freqs[i].period);

		memset(&signal->freqs[i], 0, sizeof(struct audio_signal_freq));
	}

	signal->freqs_count = 0;
}

/**
 * audio_signal_fill:
 * @signal: The target signal structure
 * @buffer: The target buffer to fill
 * @frames: The number of frames to fill
 *
 * Fill the requested number of frames to the target buffer with the audio
 * signal data (in interleaved S16_LE format), at the requested sampling rate
 * and number of channels.
 */
void audio_signal_fill(struct audio_signal *signal, short *buffer, int frames)
{
	short *destination;
	short *source;
	int total;
	int freq_frames;
	int freq_offset;
	int count;
	int i, j, k;

	memset(buffer, 0, sizeof(short) * signal->channels * frames);

	for (i = 0; i < signal->freqs_count; i++) {
		total = 0;

		while (total < frames) {
			freq_frames = signal->freqs[i].frames;
			freq_offset = signal->freqs[i].offset;

			source = signal->freqs[i].period + freq_offset;
			destination = buffer + total * signal->channels;

			count = freq_frames - freq_offset;
			if (count > (frames - total))
				count = frames - total;

			freq_offset += count;
			freq_offset %= freq_frames;

			signal->freqs[i].offset = freq_offset;

			for (j = 0; j < count; j++) {
				for (k = 0; k < signal->channels; k++) {
					destination[j * signal->channels + k] += source[j];
				}
			}

			total += count;
		}
	}
}

/**
 * audio_signal_detect:
 * @signal: The target signal structure
 * @channels: The input data's number of channels
 * @sampling_rate: The input data's sampling rate
 * @buffer: The input data's buffer
 * @frames: The input data's number of frames
 *
 * Detect that the frequencies specified in @signal, and only those, are
 * present in the input data. The input data's format is required to be S16_LE.
 *
 * Returns: A boolean indicating whether the detection was successful
 */
bool audio_signal_detect(struct audio_signal *signal, int channels,
			 int sampling_rate, short *buffer, int frames)
{
	double data[frames];
	int amplitude[frames / 2];
	bool detected[signal->freqs_count];
	int threshold;
	bool above;
	int error;
	int freq;
	int max;
	int c, i, j;

	/* Allowed error in Hz due to FFT step. */
	error = sampling_rate / frames;

	for (c = 0; c < channels; c++) {
		for (i = 0; i < frames; i++)
			data[i] = (double) buffer[i * channels + c];

		gsl_fft_real_radix2_transform(data, 1, frames);

		max = 0;

		for (i = 0; i < frames / 2; i++) {
			amplitude[i] = sqrt(data[i] * data[i] +
					    data[frames - i] *
					    data[frames - i]);
			if (amplitude[i] > max)
				max = amplitude[i];
		}

		for (i = 0; i < signal->freqs_count; i++)
			detected[i] = false;

		threshold = max / 2;
		above = false;
		max = 0;

		for (i = 0; i < frames / 2; i++) {
			if (amplitude[i] > threshold)
				above = true;

			if (above) {
				if (amplitude[i] < threshold) {
					above = false;
					max = 0;

					for (j = 0; j < signal->freqs_count; j++) {
						if (signal->freqs[j].freq >
						    freq - error &&
						    signal->freqs[j].freq <
						    freq + error) {
							detected[j] = true;
							break;
						}
					}

					/* Detected frequency was not generated. */
					if (j == signal->freqs_count) {
						igt_debug("Detected additional frequency: %d\n",
							  freq);
						return false;
					}
				}

				if (amplitude[i] > max) {
					max = amplitude[i];
					freq = sampling_rate * i / frames;
				}
			}
		}

		for (i = 0; i < signal->freqs_count; i++) {
			if (!detected[i]) {
				igt_debug("Missing frequency: %d\n",
					  signal->freqs[i].freq);
				return false;
			}
		}
	}

	return true;
}