non-blocking fifo read [issue 7]

master
karlstav 11 years ago
parent 4f08afd974
commit 99cb8f4ec8
  1. 43
      cava.c

@ -1,3 +1,8 @@
/*
* to keep coherent styling autostyle with:
* astyle --style=linux --pad-oper --max-code-length=80 --keep-one-line-blocks --keep-one-line-statements --convert-tabs cava.c
*/
#include <alloca.h> #include <alloca.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
@ -163,26 +168,42 @@ music(void* data)
void* void*
fifomusic(void* data) fifomusic(void* data)
{ {
FILE *fp; int fd;
int n = 0; int n = 0;
signed char buf[1024]; signed char buf[1024];
int tempr, templ, lo; int tempr, templ, lo;
int q; int q, i;
int t = 0;
int size = 1024; int size = 1024;
char *path = ((char*)data); char *path = ((char*)data);
fp = fopen(path, "r"); int bytes = 0;
int flags;
struct timespec req = { .tv_sec = 0, .tv_nsec = 0 };
req.tv_sec = 0;
req.tv_nsec = 10000000;
fd = open(path, O_RDONLY);
flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
while (1) { while (1) {
fread(&buf, sizeof(char), size, fp); bytes = read(fd, buf, sizeof(buf));
if (bytes == -1) { //if no bytes read sleep 10ms and zero
nanosleep (&req, NULL);
t++;
if (t > 10) {
for (i = 0; i < M; i++)shared[i] = 0;
t = 0;
}
} else { //if bytes read go ahead
t = 0;
for (q = 0; q < (size / 4); q++) { for (q = 0; q < (size / 4); q++) {
//printf("%d, %d\n",buf[q],buf[q+1]);
tempr = ( buf[size - 4 * q - 1] << 2); tempr = ( buf[size - 4 * q - 1] << 2);
lo = ( buf[size - 4 * q ] >> 6); lo = ( buf[size - 4 * q ] >> 6);
//printf("%d\n",buf[4*q+1]);
if (lo < 0)lo = lo + 4; if (lo < 0)lo = lo + 4;
if (tempr >= 0)tempr = tempr + lo; if (tempr >= 0)tempr = tempr + lo;
if (tempr < 0)tempr = tempr - lo; if (tempr < 0)tempr = tempr - lo;
@ -194,14 +215,14 @@ fifomusic(void* data)
if (templ >= 0)templ = templ + lo; if (templ >= 0)templ = templ + lo;
else templ = templ - lo; else templ = templ - lo;
//printf("%d\n",((tempr+templ)/2));
shared[n] = (tempr + templ) / 2; shared[n] = (tempr + templ) / 2;
n++; n++;
if (n == M - 1)n = 0; if (n == M - 1)n = 0;
} }
} }
fclose(fp); }
close(fd);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -246,7 +267,7 @@ int main(int argc, char **argv)
struct timespec req = { .tv_sec = 0, .tv_nsec = 0 }; struct timespec req = { .tv_sec = 0, .tv_nsec = 0 };
char *usage = char *usage =
"\nUsage : ./cava [options]\n\nOptions:\n\t-b 1..(console columns/2-1) or 200\t number of bars in the spectrum (default 25 + fills up the console), program wil auto adjust to maxsize if input is to high)\n\n\t-i 'input method'\t\t\t method used for listnening to audio, supports 'alsa' and 'fifo'\n\n\t-d 'alsa device'\t\t\t name of alsa capture device (default 'hw:1,1')\n\n\t-p 'fifo path'\t\t\t\t path to fifo (default '/tmp/mpd.fifo')\n\n\t-c color\t\t\t\t suported colors: red, green, yellow, magenta, cyan, white, blue, black (default: cyan)\n\n\t-C backround color\t\t\t supported colors: same as above (default: no change) \n\n\t-s sensitivity %\t\t\t sensitivity in percent, 0 means no respons 100 is normal 50 half 200 double and so forth\n\n\t-f framerate \t\t\t\t max frames per second to be drawn, if you are experiencing high CPU usage, try redcing this (default: 60)\n\n"; "\nUsage : ./cava [options]\n\nOptions:\n\t-b 1..(console columns/2-1) or 200\t number of bars in the spectrum (default 25 + fills up the console), program wil auto adjust to maxsize if input is to high)\n\n\t-i 'input method'\t\t\t method used for listnening to audio, supports 'alsa' and 'fifo'\n\n\t-d 'alsa device'\t\t\t name of alsa capture device (default 'hw:1,1')\n\n\t-p 'fifo path'\t\t\t\t path to fifo (default '/tmp/mpd.fifo')\n\n\t-c color\t\t\t\t suported colors: red, green, yellow, magenta, cyan, white, blue, black (default: cyan)\n\n\t-C backround color\t\t\t supported colors: same as above (default: no change) \n\n\t-s sensitivity %\t\t\t sensitivity in percent, 0 means no respons 100 is normal 50 half 200 double and so forth\n\n\t-f framerate \t\t\t\t max frames per second to be drawn, if you are experiencing high CPU usage, try redcing this (default: 60)\n\n";
//**END INIT //**END INIT**//
for (i = 0; i < 200; i++) { for (i = 0; i < 200; i++) {
flast[i] = 0; flast[i] = 0;

Loading…
Cancel
Save