File: cue_test.c

package info (click to toggle)
libsndfile 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,120 kB
  • sloc: ansic: 55,350; cpp: 1,108; python: 791; makefile: 539; sh: 539; cs: 122
file content (135 lines) | stat: -rw-r--r-- 3,121 bytes parent folder | download | duplicates (4)
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

#include "sfconfig.h"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sndfile.h>


static void *
get_cues (const char *filename, double *sr)
{
    SNDFILE    *file;
    SF_INFO	sfinfo;

    unsigned int err, size;
    uint32_t count = 0;
    SF_CUES_VAR(0) *info;

    if ((file = sf_open(filename, SFM_READ, &sfinfo)) == NULL)
    {
	printf("can't open file '%s'\n", filename);
	exit(1);
    }

    printf("\n---- get cues of file '%s'\n", filename);

    if ((err = sf_command(file, SFC_GET_CUE_COUNT, &count, sizeof(uint32_t))) == SF_FALSE)
    {
	if (sf_error(file))
	{
	    printf("can't get cue info size for file '%s' (arg size %lu), err %s\n", 
		   filename, sizeof(uint32_t), sf_strerror(file));
	    exit(2);
	}
	else
	    printf("no cue info for file '%s'\n", filename);
	return NULL;
    }
	
    size = sizeof(*info) + count * sizeof(SF_CUE_POINT);
    printf("number of cues %d  size %d\n", count, size);

    if (!(info = malloc(size)))
	return NULL;

    if (sf_command(file, SFC_GET_CUE, info, size) == SF_FALSE)
    {
	printf("can't get cue info of size %d for file '%s' error %s\n", 
	       size, filename, sf_strerror(file));
	exit(3);
    }

    *sr = sfinfo.samplerate;
    sf_close(file);

    return info;
}


static void
test_cues (const char *filename)
{
    unsigned int i;
    double sr;
    SF_CUES_VAR(0) *info = get_cues(filename, &sr);

    if (info == NULL)
	exit(1);
    
    for (i = 0; i < info->cue_count; i++)
    {
	int    pos = info->cue_points[i].position;
	double t   = (double) pos / sr;
	double expected = i < 8  ?  (double) i / 3.  :  10. / 3.;
	double error = (double) fabs(t - expected);

	printf("cue %02d: markerID %02d  position %6d  offset %6d (time %.3f  expected %.3f  diff %f)  label '%s'\n",
	       i, info->cue_points[i].indx, pos, info->cue_points[i].sample_offset, t, expected, error, info->cue_points[i].name);

	if (error > 0.025)
	    exit(4);
    }

    free(info);
}

static void
print_cues (const char *filename)
{
    unsigned int i;
    double sr;
    SF_CUES_VAR(0) *info = get_cues(filename, &sr);

    if (info == NULL)
	exit(1);
    
    for (i = 0; i < info->cue_count; i++)
    {
	int    pos    = info->cue_points[i].position;
	int    indx   = info->cue_points[i].indx;
	int    cstart = info->cue_points[i].chunk_start;
	int    bstart = info->cue_points[i].block_start;
	int    offset = info->cue_points[i].sample_offset;
	const char *name = info->cue_points[i].name;
	double t   = (double) pos / sr;

	if (cstart != 0  ||  bstart != 0)
	    printf("cue %02d time %7.3f: markerID %02d  position %8d  chunk_start %d  block_start %d  offset %8d  label '%s'\n",
		   i, t, indx, pos, offset, cstart, bstart, name);
	else
	    printf("cue %02d  time %7.3f: markerID %02d  position %8d  offset %8d  label '%s'\n",
		   i, t, indx, pos, offset, name);
    }

    free(info);
}


int
main (int argc, char **argv)
{
    int i;
    
    if (argc > 1)
	for (i = 1; i < argc; i++)
	    print_cues(argv[i]);
    else
    {
	test_cues("clickpluck24.wav");
	test_cues("clickpluck.wav");
	test_cues("clickpluck.aiff");
    }
    return 0;
}