File: clone_test.c

package info (click to toggle)
libsamplerate 0.2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,444 kB
  • sloc: ansic: 371,377; sh: 121; makefile: 111
file content (134 lines) | stat: -rw-r--r-- 4,228 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
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
/*
** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
** All rights reserved.
**
** This code is released under 2-clause BSD license. Please see the
** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <samplerate.h>

#include "util.h"

#define	BUFFER_LEN		(1 << 16)
#define NUM_CHANNELS 	2
#define FRAMES_PER_PASS (BUFFER_LEN >> 1)

static void
clone_test (int converter)
{	static float input_serial [BUFFER_LEN * NUM_CHANNELS], input_interleaved [BUFFER_LEN * NUM_CHANNELS] ;
	static float output [BUFFER_LEN * NUM_CHANNELS], output_cloned [BUFFER_LEN * NUM_CHANNELS] ;
	double sine_freq ;

	SRC_STATE* src_state ;
	SRC_STATE* src_state_cloned ;
	SRC_DATA src_data, src_data_cloned ;

	int error, frame, ch, idx ;

	printf ("        clone_test          (%-28s) ....... ", src_get_name (converter)) ;
	fflush (stdout) ;

	memset (input_serial, 0, sizeof (input_serial)) ;
	memset (input_interleaved, 0, sizeof (input_interleaved)) ;
	memset (output, 0, sizeof (output)) ;
	memset (output_cloned, 0, sizeof (output_cloned)) ;

	/* Fill input buffer with an n-channel interleaved sine wave */
	sine_freq = 0.0111 ;
	gen_windowed_sines (1, &sine_freq, 1.0, input_serial, BUFFER_LEN) ;
	gen_windowed_sines (1, &sine_freq, 1.0, input_serial + BUFFER_LEN, BUFFER_LEN) ;
	interleave_data (input_serial, input_interleaved, BUFFER_LEN, NUM_CHANNELS) ;

	if ((src_state = src_new (converter, NUM_CHANNELS, &error)) == NULL)
	{	printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ;
		exit (1) ;
		} ;

	/* Perform initial pass using first half of buffer so that src_state has non-trivial state */
	src_data.src_ratio = 1.1 ;
	src_data.input_frames = FRAMES_PER_PASS ;
	src_data.output_frames = FRAMES_PER_PASS ;
	src_data.data_in = input_interleaved ;
	src_data.data_out = output ;
	src_data.output_frames_gen = 0 ;

	if ((error = src_process (src_state, &src_data)))
	{	printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
		printf ("  src_data.input_frames  : %ld\n", src_data.input_frames) ;
		printf ("  src_data.output_frames : %ld\n\n", src_data.output_frames) ;
		exit (1) ;
		} ;

	/* Clone handle */
	if ((src_state_cloned = src_clone (src_state, &error)) == NULL)
	{	printf ("\n\nLine %d : src_clone() failed : %s\n\n", __LINE__, src_strerror (error)) ;
		exit (1) ;
		} ;

	/* Process second half of buffer with both handles */
	src_data.data_in = input_interleaved + FRAMES_PER_PASS ;

	src_data_cloned = src_data ;
	src_data_cloned.data_out = output_cloned ;

	if ((error = src_process (src_state, &src_data)))
	{	printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
		printf ("  src_data.input_frames  : %ld\n", src_data.input_frames) ;
		printf ("  src_data.output_frames : %ld\n\n", src_data.output_frames) ;
		exit (1) ;
		} ;

	if ((error = src_process (src_state_cloned, &src_data_cloned)))
	{	printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
		printf ("  src_data.input_frames  : %ld\n", src_data.input_frames) ;
		printf ("  src_data.output_frames : %ld\n\n", src_data.output_frames) ;
		exit (1) ;
		} ;

	/* Check that both handles generated the same number of output frames */
	if (src_data.output_frames_gen != src_data_cloned.output_frames_gen)
	{	printf ("\n\nLine %d : cloned output_frames_gen (%ld) != original (%ld)\n\n", __LINE__,
				src_data_cloned.output_frames_gen, src_data.output_frames_gen) ;
		exit (1) ;
		} ;

	for (ch = 0 ; ch < NUM_CHANNELS ; ch++)
	{	for (frame = 0 ; frame < src_data.output_frames_gen ; frame++)
		{	idx = ch * NUM_CHANNELS + ch ;
			if (output[idx] != output_cloned[idx])
			{	printf ("\n\nLine %d : cloned data does not equal original data at channel %d, frame %d\n\n",
						__LINE__, ch, frame) ;
				exit (1) ;
				} ;
			} ;
		} ;

	src_delete (src_state) ;
	src_delete (src_state_cloned) ;

	puts ("ok") ;
} /* clone_test */

int
main (void)
{
	puts("");

	clone_test (SRC_ZERO_ORDER_HOLD) ;
	clone_test (SRC_LINEAR) ;
#ifdef ENABLE_SINC_FAST_CONVERTER
	clone_test (SRC_SINC_FASTEST) ;
#endif
	puts("");

	return 0 ;
} /* main */