7f433a41c86c44bb3fb2861916797bd869cb6559
2 ** Copyright (C) 2005-2011 Erik de Castro Lopo
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 ** A simple checksum for short, int and float data.
34 #define BIG_PRIME 999983
36 #define ARRAY_LEN(x) ((int) (sizeof (x)) / (sizeof ((x) [0])))
38 static int short_checksum (SNDFILE
* file
, int start
) ;
39 static int int_checksum (SNDFILE
* file
, int start
) ;
40 static int float_checksum (SNDFILE
* file
, int start
) ;
43 calc_checksum (SNDFILE
* file
, const SF_INFO
* info
)
46 /* Seed the checksum with data from the SF_INFO struct. */
47 start
= info
->samplerate
;
48 start
= start
* BIG_PRIME
+ info
->channels
;
49 start
= start
* BIG_PRIME
+ info
->format
;
51 switch (info
->format
& SF_FORMAT_SUBMASK
)
52 { case SF_FORMAT_FLOAT
:
53 case SF_FORMAT_DOUBLE
:
54 return float_checksum (file
, start
) ;
56 case SF_FORMAT_PCM_24
:
57 case SF_FORMAT_PCM_32
:
58 return int_checksum (file
, start
) ;
61 return short_checksum (file
, start
) ;
67 /*------------------------------------------------------------------------------
77 short_checksum (SNDFILE
* file
, int start
)
81 { count
= (int) sf_read_short (file
, data
.s
, ARRAY_LEN (data
.s
)) ;
82 for (k
= 0 ; k
< count
; k
++)
83 start
= start
* BIG_PRIME
+ data
.s
[k
] ;
88 } /* short_checksum */
91 int_checksum (SNDFILE
* file
, int start
)
95 { count
= (int) sf_read_int (file
, data
.i
, ARRAY_LEN (data
.i
)) ;
96 for (k
= 0 ; k
< count
; k
++)
97 start
= start
* BIG_PRIME
+ data
.i
[k
] ;
105 float_checksum (SNDFILE
* file
, int start
)
109 { count
= (int) sf_read_float (file
, data
.f
, ARRAY_LEN (data
.f
)) ;
110 for (k
= 0 ; k
< count
; k
++)
111 start
= start
* BIG_PRIME
+ lrintf (0x7FFFFFFF * data
.f
[k
]) ;
116 } /* float_checksum */