File: build_mvptree_audio.cpp

package info (click to toggle)
libphash 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,984 kB
  • ctags: 389
  • sloc: sh: 10,093; cpp: 5,063; java: 140; makefile: 98; ansic: 44
file content (128 lines) | stat: -rw-r--r-- 3,529 bytes parent folder | download
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
/*

    pHash, the open source perceptual hash library
    Copyright (C) 2009 Aetilius, Inc.
    All rights reserved.

    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 3 of the License, or
    (at your option) any later version.

    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/>.

    Evan Klinger - eklinger@phash.org
    David Starkweather - dstarkweather@phash.org

*/

#include <stdio.h>
#include "pHash.h"
#include "audiophash.h"

float audiohashdistance(DP *dpA, DP *dpB){

    uint32_t *hash1 = (uint32_t*)dpA->hash;
    int N1 = dpA->hash_length;
    uint32_t *hash2 = (uint32_t*)dpB->hash;
    int N2 = dpB->hash_length;

    float threshold = 0.30;
    int blocksize = 256;
    int Nc=0;
    double *ptrC = ph_audio_distance_ber(hash1, N1, hash2, N2, threshold,blocksize,Nc);

    double maxC = 0.0f;
    for (int i=0;i<Nc;i++){
	if (ptrC[i] > maxC)
	    maxC = ptrC[i];
    }
    free(ptrC);
    double res = 1000*(1-maxC);
    return (float)res;

}

int main(int argc, char **argv){
 
    const char *dir_name = argv[1];/* name of dir to retrieve image files */
    const char *filename = argv[2];/* name of file to save db */

    MVPFile mvpfile;
    mvpfile.branchfactor = 2;
    mvpfile.leafcapacity = 40;
    mvpfile.pathlength = 5;
    mvpfile.pgsize = (1 << 19); /* 524,288 */ 
    mvpfile.filename = strdup(filename);
    mvpfile.hashdist = audiohashdistance;
    mvpfile.hash_type = UINT32ARRAY;

    int sr = 8000;
    int nbchannels = 1;
    float nbsecs = 45.0f;

    int nbfiles = 0;
    printf("dir name: %s\n", dir_name);
    char **files = ph_readfilenames(dir_name,nbfiles);
    if (!files){
	printf("unable to read files from directory %s\n",dir_name);
	return -2;
    }
    printf("nbfiles = %d\n", nbfiles);
    DP **hashlist = (DP**)malloc(nbfiles*sizeof(DP**));
    if (!hashlist){
	printf("mem alloc error\n");
	return -3;
    }

    int count = 0;
    float *sigbuf = (float*)malloc(1<<21);
    int buflen = (1<<21)/sizeof(float);

    for (int i=0;i<nbfiles;i++){
        printf("file[%d]: %s\n", i, files[i]);
        int N = buflen;
	float *buf = ph_readaudio(files[i], sr, nbchannels, sigbuf, N, nbsecs);
	if (!buf){
	    printf("unable to read file: %s\n", files[i]);
	    continue;
	}
	printf("signal length %d\n", N);
        int nbframes=0;
	uint32_t *hash = ph_audiohash(buf, N, sr, nbframes);
	if (!hash){
	    printf("unable to compute hash\n");
	    continue;
	}
	printf("nbframes %d\n", nbframes);
        hashlist[count] = ph_malloc_datapoint(mvpfile.hash_type);
        if (!hashlist[count]){
	    printf("mem alloc error\n");
            free(hash);
	    return -4;
	}

	hashlist[count]->id = files[i];
        hashlist[count]->hash = hash;
        hashlist[count++]->hash_length = nbframes;

	if (buf != sigbuf)
            free(buf);
    }

    printf("save %d files\n", count);

    MVPRetCode ret = ph_save_mvptree(&mvpfile, hashlist, count);
    if (ret != PH_SUCCESS){
	printf("unable to save %s file - ret code %d\n", filename, ret);
    }
    printf("done\n");

    return 0;
}