Mercurial > hg > audiodb
comparison sample.cpp @ 279:dee55886eca0 sampling
make the RNG a part of the audioDB object.
Easier to deal with memory discipline and initialization (though note
the FIXME comment in audioDB::initTables()).
Also initialize the RNG from the current time. A mature implementation
would use a proper source of entropy...
author | mas01cr |
---|---|
date | Wed, 02 Jul 2008 13:53:23 +0000 |
parents | d9dba57becd4 |
children |
comparison
equal
deleted
inserted
replaced
278:d9dba57becd4 | 279:dee55886eca0 |
---|---|
33 } | 33 } |
34 | 34 |
35 return c; | 35 return c; |
36 } | 36 } |
37 | 37 |
38 unsigned audioDB::random_track(unsigned *propTable, unsigned total, gsl_rng *rng) { | 38 unsigned audioDB::random_track(unsigned *propTable, unsigned total) { |
39 /* FIXME: make this O(1) by using the alias-rejection method, or | 39 /* FIXME: make this O(1) by using the alias-rejection method, or |
40 some other sensible method of sampling from a discrete | 40 some other sensible method of sampling from a discrete |
41 distribution. */ | 41 distribution. */ |
42 double thing = gsl_rng_uniform(rng); | 42 double thing = gsl_rng_uniform(rng); |
43 unsigned sofar = 0; | 43 unsigned sofar = 0; |
53 return 0; | 53 return 0; |
54 } | 54 } |
55 | 55 |
56 void audioDB::sample(const char *dbName) { | 56 void audioDB::sample(const char *dbName) { |
57 initTables(dbName, 0); | 57 initTables(dbName, 0); |
58 | |
59 gsl_rng *rng = gsl_rng_alloc(gsl_rng_mt19937); | |
60 | |
61 /* FIXME: in Real Life we'll want to initialize the RNG using | |
62 /dev/random or the current time or something, like this: | |
63 | |
64 unsigned int seed; | |
65 int fd = open("/dev/urandom", O_RDONLY); | |
66 read(fd, &seed, 4); | |
67 | |
68 gsl_rng_set(rng, seed); | |
69 */ | |
70 | 58 |
71 // build track offset table (FIXME: cut'n'pasted from query.cpp) | 59 // build track offset table (FIXME: cut'n'pasted from query.cpp) |
72 off_t *trackOffsetTable = new off_t[dbH->numFiles]; | 60 off_t *trackOffsetTable = new off_t[dbH->numFiles]; |
73 unsigned cumTrack=0; | 61 unsigned cumTrack=0; |
74 for(unsigned int k = 0; k < dbH->numFiles; k++){ | 62 for(unsigned int k = 0; k < dbH->numFiles; k++){ |
102 | 90 |
103 double sumdist = 0; | 91 double sumdist = 0; |
104 double sumlogdist = 0; | 92 double sumlogdist = 0; |
105 | 93 |
106 for (unsigned int i = 0; i < nsamples;) { | 94 for (unsigned int i = 0; i < nsamples;) { |
107 unsigned track1 = random_track(propTable, total, rng); | 95 unsigned track1 = random_track(propTable, total); |
108 unsigned track2 = random_track(propTable, total, rng); | 96 unsigned track2 = random_track(propTable, total); |
109 | 97 |
110 if(track1 == track2) | 98 if(track1 == track2) |
111 continue; | 99 continue; |
112 | 100 |
113 unsigned i1 = gsl_rng_uniform_int(rng, propTable[track1]); | 101 unsigned i1 = gsl_rng_uniform_int(rng, propTable[track1]); |