Path: blob/master/tools/sdk-tools/tabledesign/tabledesign.c
7861 views
#include <stdio.h>1#include <stdlib.h>2#include <math.h>3#include <getopt.h>4#include <audiofile.h>5#include "tabledesign.h"67#ifdef __sgi89typedef long SampleFormat;1011#define MODE_READ "r"1213#else1415// The modern implementation of SGI's audiofile library which is in Ubuntu16// (https://github.com/mpruett/audiofile/) has renamed some of the functions,17// and changed some data types.1819typedef int SampleFormat;20#define AFopenfile afOpenFile21#define AFgetchannels afGetChannels22#define AFgettrackids afGetTrackIDs23#define AFgetsampfmt afGetSampleFormat24#define AFgetframecnt afGetFrameCount25#define AFgetrate afGetRate26#define AFreadframes afReadFrames2728#define MODE_READ "rb"2930#endif3132char usage[80] = "[-o order -s bits -t thresh -i refine_iter -f frame_size] aifcfile";3334int main(int argc, char **argv)35{36const char *programName; // sp11837double thresh; // sp11038int order; // sp10C39int bits; // sp10840int refineIters; // sp10441int frameSize; // sp10042UNUSED int rate;43int frameCount;44int opt;45double *spF4;46double dummy; // spE847double **mat; // spE448double **data; // spD049double *splitDelta; // spCC50int j; // spC051int permDet;52int curBits; // spB853int npredictors; // spB454int *perm; // spB055int numOverflows; // spAC56SampleFormat sampleFormat; // sp9057SampleFormat sampleWidth; // sp8C58AFfilehandle afFile; // sp8859int channels;60int tracks;61double *vec; // s262double **temp_s1;63short *temp_s3;64int i;65int dataSize; // s46667order = 2;68bits = 2;69refineIters = 2;70frameSize = 16;71numOverflows = 0;72programName = argv[0];73thresh = 10.0;7475if (argc < 2)76{77fprintf(stderr, "%s %s\n", argv[0], usage);78exit(1);79}8081while ((opt = getopt(argc, argv, "o:s:t:i:f:")) != -1)82{83switch (opt)84{85case 'o':86if (sscanf(optarg, "%d", &order) != 1)87order = 2;88break;89case 's':90if (sscanf(optarg, "%d", &bits) != 1)91bits = 2;92break;93case 'f':94if (sscanf(optarg, "%d", &frameSize) != 1)95frameSize = 16;96break;97case 'i':98if (sscanf(optarg, "%d", &refineIters) != 1)99refineIters = 2;100break;101case 't':102if (sscanf(optarg, "%lf", &thresh) != 1)103thresh = 10.0;104break;105}106}107108argv = &argv[optind - 1];109110afFile = AFopenfile(argv[1], MODE_READ, NULL);111if (afFile == NULL)112{113fprintf(stderr,114"%s: input AIFC file [%s] could not be opened.\n",115programName, argv[1]);116exit(1);117}118119channels = AFgetchannels(afFile, AF_DEFAULT_TRACK);120if (channels != 1)121{122fprintf(stderr,123"%s: file [%s] contains %d channels, only 1 channel supported.\n",124programName, argv[1], channels);125exit(1);126}127128tracks = AFgettrackids(afFile, NULL);129if (tracks != 1)130{131fprintf(stderr,132"%s: file [%s] contains %d tracks, only 1 track supported.\n",133programName, argv[1], tracks);134exit(1);135}136137AFgetsampfmt(afFile, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);138if (sampleWidth != 16)139{140fprintf(stderr,141"%s: file [%s] contains %d bit samples, only 16 bit samples supported.\n",142programName, argv[1], (int)sampleWidth);143exit(1);144}145146temp_s1 = malloc((1 << bits) * sizeof(double*));147for (i = 0; i < (1 << bits); i++)148{149temp_s1[i] = malloc((order + 1) * sizeof(double));150}151152splitDelta = malloc((order + 1) * sizeof(double));153temp_s3 = malloc(frameSize * 2 * sizeof(short));154for (i = 0; i < frameSize * 2; i++)155{156temp_s3[i] = 0;157}158159vec = malloc((order + 1) * sizeof(double));160spF4 = malloc((order + 1) * sizeof(double));161mat = malloc((order + 1) * sizeof(double*));162for (i = 0; i <= order; i++)163{164mat[i] = malloc((order + 1) * sizeof(double));165}166167perm = malloc((order + 1) * sizeof(int));168frameCount = AFgetframecnt(afFile, AF_DEFAULT_TRACK);169rate = AFgetrate(afFile, AF_DEFAULT_TRACK);170data = malloc(frameCount * sizeof(double*));171dataSize = 0;172173while (AFreadframes(afFile, AF_DEFAULT_TRACK, temp_s3 + frameSize, frameSize) == frameSize)174{175acvect(temp_s3 + frameSize, order, frameSize, vec);176if (fabs(vec[0]) > thresh)177{178acmat(temp_s3 + frameSize, order, frameSize, mat);179if (lud(mat, order, perm, &permDet) == 0)180{181lubksb(mat, order, perm, vec);182vec[0] = 1.0;183if (kfroma(vec, spF4, order) == 0)184{185data[dataSize] = malloc((order + 1) * sizeof(double));186data[dataSize][0] = 1.0;187188for (i = 1; i <= order; i++)189{190if (spF4[i] >= 1.0) spF4[i] = 0.9999999999;191if (spF4[i] <= -1.0) spF4[i] = -0.9999999999;192}193194afromk(spF4, data[dataSize], order);195dataSize++;196}197}198}199200for (i = 0; i < frameSize; i++)201{202temp_s3[i] = temp_s3[i + frameSize];203}204}205206vec[0] = 1.0;207for (j = 1; j <= order; j++)208{209vec[j] = 0.0;210}211212for (i = 0; i < dataSize; i++)213{214rfroma(data[i], order, temp_s1[0]);215for (j = 1; j <= order; j++)216{217vec[j] += temp_s1[0][j];218}219}220221for (j = 1; j <= order; j++)222{223vec[j] /= dataSize;224}225226durbin(vec, order, spF4, temp_s1[0], &dummy);227228for (j = 1; j <= order; j++)229{230if (spF4[j] >= 1.0) spF4[j] = 0.9999999999;231if (spF4[j] <= -1.0) spF4[j] = -0.9999999999;232}233234afromk(spF4, temp_s1[0], order);235curBits = 0;236while (curBits < bits)237{238for (i = 0; i <= order; i++)239{240splitDelta[i] = 0.0;241}242splitDelta[order - 1] = -1.0;243split(temp_s1, splitDelta, order, 1 << curBits, 0.01);244curBits++;245refine(temp_s1, order, 1 << curBits, data, dataSize, refineIters, 0.0);246}247248npredictors = 1 << curBits;249fprintf(stdout, "%d\n%d\n", order, npredictors);250251for (i = 0; i < npredictors; i++)252{253numOverflows += print_entry(stdout, temp_s1[i], order);254}255256if (numOverflows > 0)257{258fprintf(stderr, "There was overflow - check the table\n");259}260return 0;261}262263264