/* * AlguaBeep */ #include #include #include #include #include #include #include #include #include "process.h" #include "funcs.h" /* --------------------------------------------------------------------- */ #define IMG_W 32 #define NB_SLICES 32 #define TBUFFER IMG_W*NB_SLICES extern int verbosity; short samples[TBUFFER*2]; static ao_device *device; static ao_sample_format format; static double debut; /* --------------------------------------------------------------------- */ /* * what is the meaning of the 'K' parameter ? */ int init_process(int K) { // int foo; int default_driver; #if DEBUG_LEVEL fprintf(stderr, ">>> %s ( %d )\n", __func__, K); #endif ao_initialize(); default_driver = ao_default_driver_id(); #if DEBUG_LEVEL fprintf(stderr, "%s : ao default driver #%d\n", __func__, default_driver); #endif memset(&format, 0, sizeof(format)); format.bits = 16; format.channels = 2; format.rate = 44100; format.byte_format = AO_FMT_LITTLE; device = ao_open_live(default_driver, &format, NULL); if (device == NULL) { fprintf(stderr, "%s: Error opening AO device.\n", __func__); return -1; } memset(samples, 0, sizeof(samples)); return 0; } /* --------------------------------------------------------------------- */ /* recherche de zones */ typedef struct { int x, y; double v; } Datum; static int cmp_datums(const void *a, const void *b) { Datum *pa = (Datum *)a; /* nice */ Datum *pb = (Datum *)b; /* code */ return pa->v < pb->v; } static void pr_idx(int idx, int stand) { char chaine[100]; if (stand) standout(); sprintf(chaine, "[%03d]", idx); addstr(chaine); if (stand) standend(); } static double i2freq(int i) { i = 333+i*42; /* please explain */ if (0==i) abort(); return 30.0* (1.0 / (double)i); /* please explain */ } static int generate_samples(short *where, int size, Datum *datas) { int loop; short svalue; double vol; char chaine[100]; #if DEBUG_LEVEL > 1 fprintf(stderr, ">>> %s ( %p %d %p )\n", __func__, where, size, datas); #endif for (loop=0; loop>> %s ( %p %dx%d %d )\n", __func__, datas, w, h, K); #endif minv = maxv = 0; kv = (double)K; z_left.x = 50; z_left.y = 30; z_left.w = 500; z_left.h = 100; v_left = kv * pow((niveau_zone(datas, w, h, &z_left) / 256.0), 2.0); z_right.x = 50; z_right.y = 300; z_right.w = 500; z_right.h = 100; v_right = kv * pow((niveau_zone(datas, w, h, &z_right) / 256.0), 2.0); sprintf(ligne, "values %10.3f %10.3f", v_left, v_right); mvaddstr(1, 43, ligne); refresh(); ptr = samples; for (sl=0; sl maxv) maxv = value; *ptr++ = value; value = (short)(v_right * ((double)datas[idata+2]-127.0)); if (value < minv) minv = value; else if (value > maxv) maxv = value; *ptr++ = value; } } // puts(""); if (verbosity) { sprintf(ligne, " min %6d max %6d\n", minv, maxv); mvaddstr(11, 3, ligne); refresh(); } return 0; } /* --------------------------------------------------------------------- */ void * thr_action(void *ptr) { long idx; // int foo; #if DEBUG_LEVEL fprintf(stderr, ">>> %s ( %p )\n", __func__, ptr); fprintf(stderr, " value is %d\n", *(int *)ptr); #endif while(1) { if (verbosity > 1) { fprintf(stderr, "%s t=%.3g\n", __func__, dtime() - debut); } for (idx=0; idx<100; idx++) { ao_play(device, (char *)samples, TBUFFER*2); } } return NULL; } /* --------------------------------------------------------------------- */ int lancement_thread_son(int k) { static pthread_t thread; static int pid; int foo; #if DEBUG_LEVEL fprintf(stderr, ">>> %s ( %d )\n", __func__, k); #endif debut = dtime(); /* lancement du thread de rendu sonore */ pid = getpid(); foo = pthread_create(&thread, NULL, thr_action, &pid); fprintf(stderr, "thread creation -> %d\n", foo); if (foo) { fprintf(stderr, "epic fail in %s:%s\n", __FILE__, __func__); exit(1); } return 0; } /* --------------------------------------------------------------------- */