rendered paste body#include <glib.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define M_PI_M2 ( M_PI + M_PI )
int main() {
int i;
gint32 y;
gint32 y_trunc;
guint8 y_trunc_8;
gint32 y_dither_i;
guint8 y_dither_i_8;
gdouble y_dither_f_d;
guint32 y_dither_f_i;
guint8 y_dither_f_8;
GRand *rand = g_rand_new ();
gint32 rand_tmp;
FILE *file_trunc = fopen ("data-trunc.raw", "w");
FILE *file_dither_i = fopen ("data-dither-i.raw", "w");
FILE *file_dither_f = fopen ("data-dither-f.raw", "w");
for (i = 0; i < 44100*2; i++) {
y = ((gint32) (32767.0 * sin (((M_PI_M2 * 440.0)/44100.0) * i))) << 16;
/* truncate */
y_trunc = y;
if (y_trunc > 0 && G_MAXINT32 - y_trunc <= (1<<23)) {
y_trunc = G_MAXINT32;
} else {
y_trunc += (1<<23);
}
y_trunc = y_trunc & 0xff000000;
y_trunc_8 = ((y_trunc ^ (1<<31)) >> 24) & 0x000000ff;
fwrite (&y_trunc_8, 1, 1, file_trunc);
/* dither tpdf integer */
y_dither_i = y;
rand_tmp = g_rand_int_range (rand, (1<<23) - (1<<23), (1<<23) + (1<<23)) + g_rand_int_range (rand, (1<<23) - (1<<23), (1<<23) + (1<<23));
if (rand_tmp > 0 && y_dither_i > 0 && G_MAXINT32 - y_dither_i < rand_tmp) {
y_dither_i = G_MAXINT32;
} else if (rand_tmp < 0 && y_dither_i < 0 && G_MININT32 - y_dither_i > rand_tmp) {
y_dither_i = G_MININT32 & 0xff000000;
} else {
y_dither_i += rand_tmp;
}
y_dither_i = y_dither_i & 0xff000000;
y_dither_i_8 = (((y_dither_i) >> 24) + 128) & 0x000000ff;
fwrite (&y_dither_i_8, 1, 1, file_dither_i);
/* dither tpdf float */
y_dither_f_d = (gdouble) y;
y_dither_f_d /= 2147483647.0;
y_dither_f_d += g_rand_double_range (rand, - 1.0/256.0, 1.0/256.0) + g_rand_double_range (rand, - 1.0/256.0, 1.0/256.0);
y_dither_f_d = CLAMP (floor(y_dither_f_d * 127.0 + 0.5), -128.0, 127.0);
y_dither_f_i = (guint32) (y_dither_f_d + 128.0);
y_dither_f_8 = y_dither_f_i & 0x000000ff;
fwrite (&y_dither_f_8, 1, 1, file_dither_f);
}
fclose (file_trunc);
fclose (file_dither_i);
fclose (file_dither_f);
g_rand_free (rand);
return 0;
}