{"action":"created","issue":{"url":"https://api.github.com/repos/karlstav/cava/issues/197","repository_url":"https://api.github.com/repos/karlstav/cava","labels_url":"https://api.github.com/repos/karlstav/cava/issues/197/labels{/name}","comments_url":"https://api.github.com/repos/karlstav/cava/issues/197/comments","events_url":"https://api.github.com/repos/karlstav/cava/issues/197/events","html_url":"https://github.com/karlstav/cava/issues/197","id":302069541,"number":197,"title":"Raw output from mpd fifo does not appear correct","user":{"login":"antiprism","id":2940060,"avatar_url":"https://avatars1.githubusercontent.com/u/2940060?v=4","gravatar_id":"","url":"https://api.github.com/users/antiprism","html_url":"https://github.com/antiprism","followers_url":"https://api.github.com/users/antiprism/followers","following_url":"https://api.github.com/users/antiprism/following{/other_user}","gists_url":"https://api.github.com/users/antiprism/gists{/gist_id}","starred_url":"https://api.github.com/users/antiprism/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/antiprism/subscriptions","organizations_url":"https://api.github.com/users/antiprism/orgs","repos_url":"https://api.github.com/users/antiprism/repos","events_url":"https://api.github.com/users/antiprism/events{/privacy}","received_events_url":"https://api.github.com/users/antiprism/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-03-04T09:06:02Z","updated_at":"2018-03-04T16:13:32Z","closed_at":null,"author_association":"NONE","body":"Hi\r\n\r\nI am writing an OLED spectrum display for Volumio, which uses mpd. I set up a fifo in mpd.conf\r\n\r\n```\r\naudio_output {\r\n    type                    \"fifo\"\r\n    name                    \"my_fifo\"\r\n    path                    \"/tmp/mpd_vol_oled\"\r\n    format                  \"44100:16:2\"\r\n}\r\n```\r\nI wrote a test program cava_read.cpp which runs cava as a subprocess and  reads its raw output in 8bit.\r\n\r\n\r\n``` C++\r\n#include <stdio.h>\r\n#include <stdlib.h>\r\n#include <unistd.h>\r\n#include <sys/types.h>\r\n#include <sys/stat.h>\r\n#include <string>\r\n\r\nusing std::string;\r\n\r\nstring print_config_file(int bars, string fifo_name)\r\n{\r\n  char templt[] = \"/tmp/cava_config_XXXXXX\";\r\n  int fd = mkstemp(templt);\r\n  if(fd == -1)\r\n    return \"\";  // failed to open file and confert to file stream\r\n  FILE *ofile = fdopen(fd, \"w\");\r\n  if(ofile == NULL)\r\n    return \"\";  // failed to open file and confert to file stream\r\n\r\n  fprintf(ofile, \"[general]\\n\"\r\n                 \"bars = %d\\n\"\r\n                 \"\\n\"\r\n                 \"[input]\\n\"\r\n                 \"method = fifo\\n\"\r\n                 \"source = /tmp/mpd_vol_oled\\n\"\r\n                 //\"method = pulse\\n\"\r\n                 //\"source = auto\\n\"\r\n                 \"\\n\"\r\n                 \"[output]\\n\"\r\n                 \"method = raw\\n\"\r\n                 \"data_format = binary\\n\"\r\n                 \"channels = mono\\n\"\r\n                 \"raw_target = %s\\n\"\r\n                 \"bit_format = 8bit\\n\",\r\n          bars, fifo_name.c_str());\r\n  fclose(ofile);\r\n  return templt;\r\n}\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n  int bars = 20;\r\n  const string fifo_name = \"/tmp/cava_fifo\";\r\n  if (argc > 1)\r\n    bars = atoi(argv[1]);\r\n  if (bars < 1) {\r\n    fprintf(stderr, \"error: bars is '%d', must be 1 or more\\n\", bars);\r\n    exit(EXIT_FAILURE);\r\n  }\r\n\r\nunlink(fifo_name.c_str());\r\n  if(mkfifo(fifo_name.c_str(), 0666) == -1) {\r\n    perror(\"could not create cava FIFO for writing\");\r\n    exit(EXIT_FAILURE);\r\n  } \r\n\r\n  string config_file_name = print_config_file(bars, fifo_name);\r\n  if (config_file_name == \"\") {\r\n    perror(\"could not create cava config file\");\r\n    exit(EXIT_FAILURE);\r\n  }\r\n\r\n  string cava_cmd = \"cava -p \" + config_file_name;\r\n  fprintf(stderr, \"cmd ='%s'\\n\", cava_cmd.c_str());\r\n  FILE *from_cava = popen(cava_cmd.c_str(), \"r\");\r\n  if (from_cava == NULL) {\r\n    perror(\"could not start cava program\");\r\n    exit(EXIT_FAILURE);\r\n  }\r\n\r\n  FILE *fifo_file = fopen(fifo_name.c_str(), \"rb\");\r\n  if(fifo_file == NULL) {\r\n    fprintf(stderr, \"could not open cava FIFO for reading\");\r\n    exit(EXIT_FAILURE);\r\n  }\r\n\r\n  unsigned char bar_heights[bars];\r\n  while (true) {\r\n    int num_read = fread(bar_heights, sizeof(unsigned char), bars, fifo_file);\r\n    if(num_read < bars) {\r\n      fprintf(stderr, \"no more data from cava\");\r\n      exit(EXIT_FAILURE);\r\n    }\r\n    for (int i=0; i<bars; i++)\r\n      fprintf(stdout, \"%3d \", bar_heights[i]);\r\n    fprintf(stdout, \"\\n\");\r\n    fflush(stdout); \r\n  }\r\n\r\n  pclose(from_cava);\r\n\r\n  return 0;\r\n}\r\n\r\n\r\n```\r\nUsing the cava_read and the mpd fifo the values are in a low range and do not appear to be correct relative to each other. Running cava on its own and reading from the same fifo the spectrum appears correct.\r\nI have the same results on a Raspberry Pi running Volumio and on an Intel PC running Ubuntu\r\n\r\nUsing cava_read and pulse as a source (on Ubuntu) the values are in a normal range and look like they are probably correct relative to each other.\r\n\r\nHere is the cava config file used by cava_read when reading from the mpd fifo\r\n\r\n```\r\n[general]\r\nbars = 10\r\n\r\n[input]\r\nmethod = fifo\r\nsource = /tmp/mpd_vol_oled\r\n\r\n[output]\r\nmethod = raw\r\ndata_format = binary\r\nchannels = mono\r\nraw_target = /tmp/cava_fifo\r\nbit_format = 8bit\r\n\r\n```\r\n\r\nThis is some typical output for pop music with the mpd fifo, which appears incorrect\r\n\r\n![cava_read_mpd_fifo](https://user-images.githubusercontent.com/2940060/36943934-821471fc-1f92-11e8-96cf-c375783b6881.png)\r\n\r\nAnd this is some typical output for pop music with pulse, which appears correct\r\n\r\n![cava_read_youtube_pulse](https://user-images.githubusercontent.com/2940060/36943935-824a95ca-1f92-11e8-90f0-0e8021f40d64.png)\r\n\r\nThanks\r\n\r\nAdrian."},"comment":{"url":"https://api.github.com/repos/karlstav/cava/issues/comments/370241153","html_url":"https://github.com/karlstav/cava/issues/197#issuecomment-370241153","issue_url":"https://api.github.com/repos/karlstav/cava/issues/197","id":370241153,"user":{"login":"antiprism","id":2940060,"avatar_url":"https://avatars1.githubusercontent.com/u/2940060?v=4","gravatar_id":"","url":"https://api.github.com/users/antiprism","html_url":"https://github.com/antiprism","followers_url":"https://api.github.com/users/antiprism/followers","following_url":"https://api.github.com/users/antiprism/following{/other_user}","gists_url":"https://api.github.com/users/antiprism/gists{/gist_id}","starred_url":"https://api.github.com/users/antiprism/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/antiprism/subscriptions","organizations_url":"https://api.github.com/users/antiprism/orgs","repos_url":"https://api.github.com/users/antiprism/repos","events_url":"https://api.github.com/users/antiprism/events{/privacy}","received_events_url":"https://api.github.com/users/antiprism/received_events","type":"User","site_admin":false},"created_at":"2018-03-04T16:13:32Z","updated_at":"2018-03-04T16:13:32Z","author_association":"NONE","body":"Changing the mpd volume doesn't affect the cava ncurses display or the cava_read output."}}
{"actor":{"display_login":"antiprism"}}