int queryaudioindex(const char *dir_name, const char *idx_name, const int sr,\ const int block_size, const float nbsecs, const float confidence_lvl,\ const unsigned int P){ unsigned int nbfiles; char **files = readfilenames(dir_name, &nbfiles); if (files == NULL){ files = (char**)malloc(sizeof(char*)); files[0] = strdup(dir_name); nbfiles = 1; } fprintf(stdout,"nb files %d\n\n", nbfiles); char indexfile[FILENAME_MAX]; snprintf(indexfile, FILENAME_MAX, "%s.idx", idx_name); AudioIndex audio_index = open_audioindex(indexfile, 0, 0); if (audio_index < 0){ fprintf(stderr,"unable to open audio index\n"); return -1; } void *ctx = zmq_init(1); if (ctx == NULL){ fprintf(stderr, "unable to init zeromq\n"); return -1; } AudioDataDB mdatastore = open_audiodata_db(ctx, GlobalArgs.server_addr); if (mdatastore < 0) { fprintf(stderr,"unable to open metadata store\n"); return -1; } float *sigbuf = (float*)malloc(1<<25); if (sigbuf == NULL){ fprintf(stdout,"mem alloc error\n"); return -2; } const unsigned int buflen = (1<<25)/sizeof(float); char *inlinestr; AudioHashStInfo *hash_st = NULL; int i,j; for (i=0;i<nbfiles;i++){ fprintf(stdout,"query[%3d] = %s\n", i, files[i]); unsigned int tmpbuflen = buflen; int err; float *buf = readaudio(files[i], sr, sigbuf, &tmpbuflen, nbsecs, NULL, &err); if (buf == NULL){ fprintf(stdout, "could not get audio, err = %d\n", err); continue; } uint32_t *phash=NULL; double **coeffs = NULL; uint8_t **toggles = NULL; unsigned int nbframes, nbcoeffs; double minbark,maxbark; fprintf(stdout,"calculating hash for query ... \n"); if (audiohash(buf, &phash, &coeffs, &toggles, &nbcoeffs, &nbframes,\ &minbark, &maxbark, tmpbuflen, P, sr, &hash_st) < 0){ fprintf(stdout, "unable to get audio hash\n"); continue; } fprintf(stdout,"number hash frames %d\n\n", nbframes); uint32_t result_id = 0; float cs = 0; fprintf(stdout,"do lookup ...\n"); lookupaudiohash(audio_index, phash, toggles, nbframes,\ P, block_size, confidence_lvl, &result_id, &cs); fprintf(stdout, "found id %u\n", result_id); inlinestr = retrieve_audiodata(mdatastore, result_id); if (inlinestr == NULL){ fprintf(stderr, "unable to retrieve %u\n", result_id); } if (cs >= 0 && inlinestr){ fprintf(stdout,"FOUND ==> \"%s\" cs%f\n\n", inlinestr,cs); } else { fprintf(stdout,"NONE FOUND\n\n"); } if (buf != sigbuf) free(buf); for (j=0;j<nbframes;j++){ free(coeffs[j]); if (toggles) free(toggles[j]); } if (toggles) free(toggles); free(coeffs); free(phash); } ph_hashst_free(hash_st); close_audioindex(audio_index, 0); close_audiodata_db(mdatastore); return 0; }
int addtoaudioindex(const char *dir_name, const char *idx_name, const int sr, \ const float nbsecs, const unsigned int P){ const int initial_nbbuckets = 1 << 25; char indexfile[FILENAME_MAX]; snprintf(indexfile, FILENAME_MAX, "%s.idx", idx_name); fprintf(stdout, "open index table at %s\n", indexfile); AudioIndex index_table = open_audioindex(indexfile, 1, initial_nbbuckets); if (index_table == NULL){ fprintf(stderr,"unable to get table\n"); return -1; } void *ctx = zmq_init(1); if (ctx == NULL){ fprintf(stderr,"unable to init zeromq\n"); return -1; } AudioDataDB mdatastore = open_audiodata_db(ctx, GlobalArgs.server_addr); if (mdatastore == NULL) { fprintf(stderr,"unable to connect to metadata server\n"); return -2; } fprintf(stdout, "audio data db: %p\n", mdatastore); printf("read files from dir\n"); printf("dir: %s\n", dir_name); unsigned int nbfiles; char **files = readfilenames(dir_name, &nbfiles); if (files == NULL){ return -3; } printf("number files %u\n", nbfiles); float *sigbuf = (float*)malloc(1<<25); unsigned int buflen = (1<<25)/sizeof(float); if (sigbuf == NULL){ return -4; } AudioMetaData mdata; AudioHashStInfo *hash_st = NULL; char inlinestr[512]; uint32_t hash_id; unsigned int i; for (i=0;i<nbfiles;i++){ fprintf(stdout,"file[%d]: %s\n", i, files[i]); unsigned int tmpbuflen = buflen; int err; float *buf = readaudio(files[i], sr, sigbuf, &tmpbuflen, nbsecs, &mdata, &err); if (buf == NULL){ fprintf(stderr,"unable to read audio, err = %d\n", err); continue; } fprintf(stdout,"signal length %u\n", tmpbuflen); if (metadata_to_inlinestr(&mdata, inlinestr, 512) < 0){ fprintf(stderr, "ERROR: cannot parse metadata struct\n"); break; } fprintf(stdout,"mdata: %s\n", inlinestr); if (store_audiodata(mdatastore, inlinestr, &hash_id)<0){ fprintf(stderr,"ERROR: cannot store metadata\n"); break; } fprintf(stdout, "uid = %u\n", hash_id); uint32_t *phash = NULL; unsigned int nbframes; if (audiohash(buf, &phash, NULL, NULL, NULL, &nbframes,\ NULL, NULL, tmpbuflen, P, sr, &hash_st) < 0){ fprintf(stderr,"ERROR: unable to get audio hash\n"); continue; } fprintf(stdout,"nbframes %u\n", nbframes); if (insert_into_audioindex(index_table, hash_id, phash, nbframes) < 0){ fprintf(stderr,"fatal error: unable to insert %u into hash\n", hash_id); } if (buf != sigbuf) ph_free(buf); ph_free(phash); free_mdata(&mdata); } int nbbkts, nbentries; stat_audioindex(index_table, &nbbkts, &nbentries); fprintf(stdout,"nb buckets %d\n",nbbkts); fprintf(stdout,"nb entries %d\n",nbentries); if (flush_audioindex(index_table, indexfile) < 0){ fprintf(stdout,"error flushing index\n"); } ph_hashst_free(hash_st); free(sigbuf); for (i=0;i<nbfiles;i++){ free(files[i]); } free(files); if (close_audioindex(index_table, 1) < 0){ fprintf(stdout,"error closing audio index\n"); } close_audiodata_db(mdatastore); return 0; }
void main(int argc,char *argv[]) { int option; /* Option returned by getopt */ char *argument; /* Argument returned by getopt */ int tab_size; /* Default tab size */ int dos_format; /* Convert to DOS format */ int unix_format; /* Convert to UNIX format */ int mac_format; /* Convert from Mac to DOS format */ int cr_format; /* Strip CR's from file */ int d_format; /* Filter control D's from file */ int all_lower; /* Convert to lowercase? */ int all_caps; /* Convert to uppercase? */ FILE *infile; FILE *outfile; char *outname; int numfiles,i; init(argv[0],"Trans"); tab_size = numfiles = 0; d_format = unix_format = dos_format = mac_format = cr_format = false; all_lower = all_caps = false; /* Parse the command line for format specifier options */ do { option = getopt(argc,argv,"uUdDaAhHmMsSt:T:f:F:lLcC",&argument); if (option >= 0) option = tolower(option); switch(option) { case 'u': unix_format = true; dos_format = false; mac_format = false; cr_format = false; break; case 'd': dos_format = true; unix_format = false; mac_format = false; cr_format = false; break; case 'a': dos_format = false; unix_format = false; mac_format = true; cr_format = false; break; case 's': d_format = true; break; case 'l': all_lower = true; all_caps = false; break; case 'c': all_lower = false; all_caps = true; break; case 'm': cr_format = true; dos_format = false; unix_format = false; break; case 'h': help(); break; case 't': if((tab_size = atoi(argument)) <= 0 || tab_size > MAXTABSIZE) { fputs("Invalid tab size\a\n",stderr); exit(1); } break; case 'f': readfilenames(argument,filenames,&numfiles); break; case INVALID: fputs("Invalid option\a\n",stderr); exit(1); break; } } while (option != ALLDONE && option != PARAMETER); /* The user must at least inform us of what they intend to be done to * the input file! */ if (!tab_size && !unix_format && !dos_format && !mac_format && !cr_format && !d_format && !all_lower && !all_caps) help(); /* Ensure that we at least have on file to translate */ if (numfiles == 0 && (argc - nextargv) < 1) help(); /* If the user has not provided the file names using the -f option, * we must get them from the command line... */ while (nextargv < argc) loadfiles(argv[nextargv++],&numfiles); /* Allocate room for buffers */ inbuf = malloc(BUFSIZE); outbuf = malloc(BUFSIZE); if (inbuf == NULL || outbuf == NULL) { printf("Not enough memory.\a\n"); exit(1); } banner(); /* Damned tmpnam does not work on DOS, and under Unix it puts the files * in the temporary directory. */ /* outname = tmpnam(NULL); if (outname == NULL)*/ outname = "__tran__.xxx"; for (i = 0; i < numfiles; i++) { /* Obtain information about the file and ensure that it is really * an ordinary file, that has both read and write permissions. */ if (!checkstatus(filenames[i])) continue; if (!openfile(&outfile,outname,"wb")) { printf("Cannot create output file .. aborting\n"); exit(1); } openfile(&infile,filenames[i],"rb"); printf("Formatting '%s' ... ",filenames[i]); fflush(stdout); /* Format the text */ if (!format_text(infile,outfile,tab_size,unix_format,dos_format, mac_format,cr_format,d_format,all_lower,all_caps)) { printf("lines too long - skipping.\n"); fclose(infile); fclose(outfile); unlink(outname); continue; } fclose(infile); /* Close any opened files */ fclose(outfile); unlink(filenames[i]); rename(outname,filenames[i]); printf("done.\n"); } }