File: peakdetector.c

package info (click to toggle)
gavl 2.0.0~svn6298-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 14,780 kB
  • sloc: ansic: 465,657; makefile: 320; sh: 109
file content (72 lines) | stat: -rw-r--r-- 1,870 bytes parent folder | download | duplicates (2)
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
#include <gavl/gavl.h>
#include <gavl/peakdetector.h>
#include <stdio.h>
#include <assert.h>

void print_audioframe(gavl_audio_frame_t*frame, int channels, int with_samples) {
	int i, j;
	printf("audio-frame @ %p\n", frame);
	printf("\ttime   : %lld\n", (long long)frame->timestamp);
	printf("\tstride : %d\n", frame->channel_stride);
	printf("\tsamples: %d\n", frame->valid_samples);
	printf("\tsamples: %p\n", frame->samples.f);
	for (i=0; i<channels; i++) {
		printf("\t	[%d]: %p\n", i, frame->channels.f[i]);
	}
}

int main() {
	int i;
	float*left, *right;
	double min, max, abs;
	gavl_peak_detector_t* pd = 0;
	gavl_audio_frame_t* frame = 0;
	gavl_audio_format_t fmt = {
		.samples_per_frame=64,
		.samplerate=44100,
		.num_channels=2,
		.sample_format=GAVL_SAMPLE_FLOAT,
		.interleave_mode=GAVL_INTERLEAVE_ALL,
		.center_level=1.f,
		.rear_level=1.f,
	};
	fmt.channel_locations[0] = GAVL_CHID_FRONT_LEFT;
	fmt.channel_locations[1] = GAVL_CHID_FRONT_RIGHT;
	//gavl_audio_format_dump(&fmt);

	frame = gavl_audio_frame_create (&fmt);
	assert(frame);

	/* fill the audio frame */
	left = frame->channels.f[0];
	right = frame->channels.f[1];
	for(i=0; i<fmt.samples_per_frame; i++) {
		*left++ = ((float)i)/((float)fmt.samples_per_frame) * 1.5f - 0.5;
		*right++ = 0.f;
	}
	frame->valid_samples=fmt.samples_per_frame;

	/* print the audio frames */
	print_audioframe(frame, fmt.num_channels, 0);

	/* detect peaks */
	pd = gavl_peak_detector_create ();
	assert(pd);

	gavl_peak_detector_set_format(pd, &fmt);
	gavl_peak_detector_update(pd, frame);
	gavl_peak_detector_get_peak(pd, &min, &max, &abs);

	printf("min=%g, max=%g, abs=%g\n", min, max, abs);

	/* cleanup */
	gavl_peak_detector_destroy(pd);
	gavl_audio_frame_destroy(frame);

	/* check if values are correct */
	assert(min == -0.5);
	assert(max == ((1.5f*63.)/64. - 0.5));
	assert(max == abs);

	return 0;
}