blob: 775077e7cd62d67e57b254d9189043e0a6953e70 [file] [log] [blame]
Alexandre Lision7c6f4a62013-09-05 13:27:01 -04001/*
2** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3**
4** This program is free software; you can redistribute it and/or modify
5** it under the terms of the GNU Lesser General Public License as published by
6** the Free Software Foundation; either version 2.1 of the License, or
7** (at your option) any later version.
8**
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 Lesser General Public License for more details.
13**
14** You should have received a copy of the GNU Lesser General Public License
15** along with this program; if not, write to the Free Software
16** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19
20#include "sfconfig.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24
25#if HAVE_UNISTD_H
26#include <unistd.h>
27#endif
28
29#include <string.h>
30#include <errno.h>
31
32#include "common.h"
33
34typedef struct
35{ int le_float ;
36 int be_float ;
37 int le_int_24_32 ;
38 int be_int_24_32 ;
39} VOTE ;
40
41
42static void vote_for_format (VOTE * vote, const unsigned char * data, int datalen) ;
43
44int
45audio_detect (SF_PRIVATE * psf, AUDIO_DETECT *ad, const unsigned char * data, int datalen)
46{ VOTE vote ;
47
48 if (psf == NULL)
49 return 0 ;
50
51 if (ad == NULL || datalen < 256)
52 return 0 ;
53
54 vote_for_format (&vote, data, datalen) ;
55
56 psf_log_printf (psf, "audio_detect :\n"
57 " le_float : %d\n"
58 " be_float : %d\n"
59 " le_int_24_32 : %d\n"
60 " be_int_24_32 : %d\n",
61 vote.le_float, vote.be_float, vote.le_int_24_32, vote.be_int_24_32) ;
62
63 if (0) puts (psf->logbuffer) ;
64
65 if (ad->endianness == SF_ENDIAN_LITTLE && vote.le_float > (3 * datalen) / 4)
66 { /* Almost certainly 32 bit floats. */
67 return SF_FORMAT_FLOAT ;
68 } ;
69
70 if (ad->endianness == SF_ENDIAN_LITTLE && vote.le_int_24_32 > (3 * datalen) / 4)
71 { /* Almost certainly 24 bit data stored in 32 bit ints. */
72 return SF_FORMAT_PCM_32 ;
73 } ;
74
75 return 0 ;
76} /* data_detect */
77
78static void
79vote_for_format (VOTE * vote, const unsigned char * data, int datalen)
80{
81 int k ;
82
83 memset (vote, 0, sizeof (VOTE)) ;
84
85 datalen -= datalen % 4 ;
86
87 for (k = 0 ; k < datalen ; k ++)
88 { if ((k % 4) == 0)
89 { if (data [k] == 0 && data [k + 1] != 0)
90 vote->le_int_24_32 += 4 ;
91
92 if (data [2] != 0 && data [3] == 0)
93 vote->le_int_24_32 += 4 ;
94
95 if (data [0] != 0 && data [3] > 0x43 && data [3] < 0x4B)
96 vote->le_float += 4 ;
97
98 if (data [3] != 0 && data [0] > 0x43 && data [0] < 0x4B)
99 vote->be_float += 4 ;
100 } ;
101 } ;
102
103 return ;
104} /* vote_for_format */
105