From 0c36e680b6b290d567ec9c10a81faf0ff9d5fa97 Mon Sep 17 00:00:00 2001 From: karlstav Date: Wed, 3 Apr 2019 22:20:29 +0200 Subject: [PATCH] cleaned up code, made k value more rational --- cava.c | 17 ++++++++--------- input/fifo.c | 4 ++-- input/pulse.c | 45 ++++++++++++++++++++++++--------------------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/cava.c b/cava.c index 3bdd08a..e03572c 100644 --- a/cava.c +++ b/cava.c @@ -148,7 +148,7 @@ static bool directory_exists(const char * path) { #endif int * separate_freq_bands(int FFTbufferSize, fftw_complex out[FFTbufferSize / 2 + 1], - int bars, int lcf[200], int hcf[200], float k[200], int channel, + int bars, int lcf[200], int hcf[200], double k[200], int channel, double sens, double ignore) { int o,i; double peak[201]; @@ -168,9 +168,8 @@ int * separate_freq_bands(int FFTbufferSize, fftw_complex out[FFTbufferSize / 2 peak[o] += y[i]; //adding upp band } - peak[o] = peak[o] / (hcf[o]-lcf[o] + 1); //getting average - temp = peak[o] * sens * k[o] / 100000; //multiplying with k and sens + temp = peak[o] * sens * k[o]; //multiplying with k and sens if (temp <= ignore) temp = 0; if (channel == 1) fl[o] = temp; else fr[o] = temp; @@ -243,7 +242,7 @@ int main(int argc, char **argv) int fall[200]; //float temp; float fpeak[200]; - float k[200]; + double k[200]; float g; struct timespec req = { .tv_sec = 0, .tv_nsec = 0 }; char configPath[255]; @@ -353,8 +352,8 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co inr = malloc(2 * (p.FFTbufferSize / 2 + 1) * sizeof(double)); inl = malloc(2 * (p.FFTbufferSize / 2 + 1) * sizeof(double)); - audio.audio_out_l = (int*) malloc(p.FFTbufferSize * sizeof(int)); - audio.audio_out_r = (int*) malloc(p.FFTbufferSize * sizeof(int)); + audio.audio_out_l = (uint16_t*) malloc(p.FFTbufferSize * sizeof(uint16_t)); + audio.audio_out_r = (uint16_t*) malloc(p.FFTbufferSize * sizeof(uint16_t)); outl = malloc(2 * (p.FFTbufferSize / 2 + 1) * sizeof(fftw_complex)); @@ -634,7 +633,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co // process: weigh signal to frequencies height and EQ for (n = 0; n < bars; n++) { k[n] = pow(fc[n],0.85); - k[n] *= (float)height / 100; + k[n] *= (float)height / pow(2,32); k[n] *= p.smooth[(int)floor(((double)n) * smh)]; } @@ -774,7 +773,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co } - //preperaing signal for drawing + //mirroring stereo channels for (o = 0; o < bars; o++) { if (p.stereo) { if (o < bars / 2) { @@ -817,7 +816,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co fmem[o] = fmem[o] * (1 - div / 20); #ifdef DEBUG - mvprintw(o,0,"%d: f:%f->%f (%d->%d), k-value: %f, peak:%d \n", + mvprintw(o,0,"%d: f:%f->%f (%d->%d), k-value: %15e, peak:%d \n", o, fc[o], fc[o + 1], lcf[o], hcf[o], k[o], f[o]); #endif diff --git a/input/fifo.c b/input/fifo.c index d7a7844..60c6786 100644 --- a/input/fifo.c +++ b/input/fifo.c @@ -6,8 +6,8 @@ int rc; struct audio_data { int FFTbufferSize; - int *audio_out_r; - int *audio_out_l; + uint16_t *audio_out_r; + uint16_t *audio_out_l; int format; unsigned int rate ; char *source; //alsa device, fifo path or pulse source diff --git a/input/pulse.c b/input/pulse.c index d4feee8..fdc1f2e 100644 --- a/input/pulse.c +++ b/input/pulse.c @@ -109,11 +109,10 @@ void getPulseDefaultSink(void* data) { } -void* input_pulse(void* data) -{ +void* input_pulse(void* data) { - struct audio_data *audio = (struct audio_data *)data; - int i, n; + struct audio_data *audio = (struct audio_data *)data; + int i, n; int16_t buf[BUFFERSIZE / 2]; /* The sample type to use */ @@ -121,54 +120,58 @@ void* input_pulse(void* data) .format = PA_SAMPLE_S16LE, .rate = 44100, .channels = 2 - }; + }; static const pa_buffer_attr pb = { - .maxlength = (uint32_t) -1, //BUFSIZE * 2, - .fragsize = BUFFERSIZE + .maxlength = (uint32_t) -1, //BUFSIZE * 2, + .fragsize = BUFFERSIZE }; pa_simple *s = NULL; int error; - if (!(s = pa_simple_new(NULL, "cava", PA_STREAM_RECORD, audio->source, "audio for cava", &ss, NULL, &pb, &error))) { - //fprintf(stderr, __FILE__": Could not open pulseaudio source: %s, %s. To find a list of your pulseaudio sources run 'pacmd list-sources'\n",audio->source, pa_strerror(error)); - sprintf(audio->error_message, __FILE__": Could not open pulseaudio source: %s, %s. To find a list of your pulseaudio sources run 'pacmd list-sources'\n",audio->source, pa_strerror(error)); + if (!(s = pa_simple_new(NULL, "cava", PA_STREAM_RECORD, audio->source, + "audio for cava", &ss, NULL, &pb, &error))) { + sprintf(audio->error_message, + __FILE__": Could not open pulseaudio source: %s, %s. \ + To find a list of your pulseaudio sources run 'pacmd list-sources'\n", + audio->source, pa_strerror(error)); + audio->terminate = 1; - pthread_exit(NULL); + pthread_exit(NULL); } n = 0; while (1) { - /* Record some data ... */ if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) { - //fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); - //exit(EXIT_FAILURE); - sprintf(audio->error_message, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); - audio->terminate = 1; - pthread_exit(NULL); + sprintf(audio->error_message, __FILE__": pa_simple_read() failed: %s\n", + pa_strerror(error)); + audio->terminate = 1; + pthread_exit(NULL); } //sorting out channels for (i = 0; i < BUFFERSIZE / 2; i += 2) { - if (audio->channels == 1) audio->audio_out_l[n] = (buf[i] + buf[i + 1]) / 2; + if (audio->channels == 1) { + audio->audio_out_l[n] = (buf[i] + buf[i + 1]) / 2; + } //stereo storing channels in buffer if (audio->channels == 2) { audio->audio_out_l[n] = buf[i]; audio->audio_out_r[n] = buf[i + 1]; - } + } n++; - if (n == audio->FFTbufferSize - 1)n = 0; + if (n == audio->FFTbufferSize - 1) n = 0; } if (audio->terminate == 1) { pa_simple_free(s); break; - } + } } return 0;