fixed an SBR bug/cheat using a bunch of ugly hacks
[ghost.git] / libghost / testghost.c
blob0aefd7561f7eb7403d6b00b672da6ea498510b43
1 /**
2 @file ghost.c
3 @brief Sinusoid extraction/synthesis
4 */
6 /* Copyright (C) 2005
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <math.h>
25 #include <stdio.h>
26 #include "sinusoids.h"
27 #include "ghost.h"
28 #include "pitch.h"
29 #include "lifting.h"
30 #include <stdlib.h>
32 const float predict[11] = {-0.00499385545085393, 0.0110380571786845, -0.018414597815401, 0.0275862067026581, -0.0393646739536688, 0.055303264488734, -0.0787612707745417, 0.118522526792966, -0.29689, 0.80484, 0.4211};
33 const float update[11] = {-0.000749078317628089, 0.00165570857680267, -0.00276218967231015, 0.00413793100539871, -0.00590470109305032, 0.0082954896733101, -0.0118141906161813, 0.0226, -0.07844, 0.34242, 0.221};
35 #define BLOCK_SIZE 192
37 int main(int argc, char **argv)
39 GhostEncState *state;
40 FILE *fin, *fout;
41 //struct LiftingBasis bas;
42 //float x[1200];
43 //int i;
45 if (argc != 3)
47 fprintf (stderr, "usage: testghost input_file output_file\nWhere the input and output are raw mono files sampled at 44.1 kHz or 48 kHz\n");
48 exit(1);
50 /*float x[256];
51 float y[256];
52 float w[2] = {.05, .2};
53 float ai[2], bi[2];
54 int i;
55 for (i=0;i<256;i++)
57 x[i] = cos(.05*i+2) + .2*cos(.2*i+1.1);
59 extract_sinusoids(x, w, ai, bi, y, 2, 256);
60 printf ("%f %f\n", ai[0], bi[0]);
61 printf ("%f %f\n", ai[1], bi[1]);
63 #if 0
64 bas.predict_delay=1;
65 bas.predict_length=11;
66 bas.update_delay=1;
67 bas.update_length=11;
68 bas.predict = predict;
69 bas.update = update;
70 for (i=0;i<1200;i++)
72 //x[i] = 2*((1.f*rand())/RAND_MAX) - 1;
73 x[i] = sin(.3*i);
75 for (i=0;i<1024;i++)
76 printf ("%f ", x[i+30]);
77 printf ("\n");
78 lifting_forward(x+30, &bas, 1024, 1);
79 for (i=0;i<1024;i++)
80 printf ("%f ", x[i+30]);
81 printf ("\n");
82 lifting_backward(x+30, &bas, 1024, 1);
83 for (i=0;i<1024;i++)
84 printf ("%f ", x[i+30]);
85 printf ("\n");
86 return 0;
87 #endif
88 fin = fopen(argv[1], "r");
89 fout = fopen(argv[2], "w");
90 state = ghost_encoder_state_new(48000);
91 while (1)
93 int i;
94 float float_in[BLOCK_SIZE];
95 short short_in[BLOCK_SIZE];
96 fread(short_in, sizeof(short), BLOCK_SIZE, fin);
97 //printf ("%d ", short_in[0]);
99 if (feof(fin))
100 break;
101 for (i=0;i<BLOCK_SIZE;i++)
102 float_in[i] = short_in[i];
103 ghost_encode(state, float_in);
104 for (i=0;i<BLOCK_SIZE;i++)
106 if (float_in[i] > 32767)
107 short_in[i] = 32767;
108 else if (float_in[i] < -32768)
109 short_in[i] = -32768;
110 else
111 short_in[i] = float_in[i];
113 fwrite(short_in, sizeof(short), BLOCK_SIZE, fout);
115 ghost_encoder_state_destroy(state);
117 /*for (i=0;i<256;i++)
118 printf ("%f %f\n", x[i], y[i]);*/
119 return 0;