-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.c
95 lines (71 loc) · 2.37 KB
/
common.c
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
/*
** Copyright (C) 2007-2015 Erik de Castro Lopo <[email protected]>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 or version 3 of the
** License.
**
** This program 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 General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include "common.h"
/* Global. */
const char *font_family = "DejaVu Sans Mono" ;
sf_count_t
sfx_mix_mono_read_double (SNDFILE * file, double * data, sf_count_t datalen)
{
SF_INFO info ;
sf_command (file, SFC_GET_CURRENT_SF_INFO, &info, sizeof (info)) ;
if (info.channels == 1)
return sf_read_double (file, data, datalen) ;
static double multi_data [2048] ;
int k, ch, frames_read ;
sf_count_t dataout = 0 ;
while (dataout < datalen)
{ int this_read ;
this_read = MIN (ARRAY_LEN (multi_data) / info.channels, datalen - dataout) ;
frames_read = sf_readf_double (file, multi_data, this_read) ;
if (frames_read == 0)
break ;
for (k = 0 ; k < frames_read ; k++)
{ double mix = 0.0 ;
for (ch = 0 ; ch < info.channels ; ch++)
mix += multi_data [k * info.channels + ch] ;
data [dataout + k] = mix / info.channels ;
} ;
dataout += frames_read ;
} ;
return dataout ;
} /* sfx_mix_mono_read_double */
int
parse_int_or_die (const char * input, const char * value_name)
{ char * endptr ;
long value = strtol (input, &endptr, 10) ;
if (endptr == input || endptr [0] != 0 || value >= INT_MAX)
{ fprintf (stderr, "Error : Can't parse int value %s : %s\n", value_name, input) ;
exit (1) ;
} ;
return value ;
} /* parse_int_or_die */
double
parse_double_or_die (const char * input, const char * value_name)
{ char * endptr ;
double value = strtod (input, &endptr) ;
if (endptr == input || endptr [0] != 0)
{ fprintf (stderr, "Error : Can't parse double value %s : %s\n", value_name, input) ;
exit (1) ;
} ;
return value ;
} /* parse_double_or_die */