/src/clients/vistest/vistest_fft.c

https://github.com/theefer/xmms2 · C · 196 lines · 142 code · 30 blank · 24 comment · 24 complexity · e66c74da9b6bb44ee5e725859d7b1f81 MD5 · raw file

  1. /* XMMS2 - X Music Multiplexer System
  2. * Copyright (C) 2003-2011 XMMS2 Team
  3. *
  4. * PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. */
  16. /** @file
  17. * Extremly simple example of a visualization for xmms2
  18. * (should probably just be put in /usr/share/doc/xmms2-dev/examples)
  19. */
  20. #include <glib.h>
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include <signal.h>
  24. #include <xmmsclient/xmmsclient.h>
  25. #include <xmmsclient/xmmsclient-glib.h>
  26. static GMainLoop *mainloop;
  27. static xmmsc_connection_t *connection;
  28. static int vis;
  29. static int height;
  30. short data[256];
  31. static void
  32. draw (void) {
  33. int i, z, val;
  34. int base = SHRT_MAX / height;
  35. printf ("\e[1m");
  36. for (z = height; z >= 1; z -= 2) {
  37. for (i = 0; i < 90; i += 1) {
  38. /*val = (data[i] + data[i+1]) / 2;*/
  39. val = data[i];
  40. if (val > (z * base)) {
  41. putchar ('|');
  42. } else if (val > ((z-1) * base)) {
  43. putchar ('.');
  44. } else {
  45. putchar (' ');
  46. }
  47. }
  48. putchar ('\n');
  49. }
  50. printf ("\e[m\e[%dA", height/2);
  51. fflush (stdout);
  52. }
  53. static gboolean
  54. draw_gtk (gpointer stuff)
  55. {
  56. int ret = xmmsc_visualization_chunk_get (connection, vis, data, 0, 0);
  57. if (ret == 256) {
  58. draw ();
  59. }
  60. return (ret >= 0);
  61. }
  62. static void
  63. shutdown_gtk (gpointer stuff)
  64. {
  65. g_main_loop_quit (mainloop);
  66. }
  67. static void
  68. quit (int signum)
  69. {
  70. printf ("\e[%dB", height/2);
  71. xmmsc_visualization_shutdown (connection, vis);
  72. if (connection) {
  73. xmmsc_unref (connection);
  74. }
  75. exit (EXIT_SUCCESS);
  76. }
  77. static void
  78. setup_signal (void)
  79. {
  80. struct sigaction siga;
  81. siga.sa_handler = quit;
  82. sigemptyset (&siga.sa_mask);
  83. siga.sa_flags = SA_RESTART;
  84. sigaction(SIGINT, &siga, (struct sigaction*)NULL);
  85. }
  86. int
  87. main (int argc, char **argv)
  88. {
  89. xmmsc_result_t *res;
  90. xmmsv_t *val;
  91. const char *errmsg;
  92. xmmsv_t *configval;
  93. gchar *path = getenv ("XMMS_PATH");
  94. connection = xmmsc_init ("xmms2-vistest");
  95. if (argc != 2 || (height = atoi (argv[1]) * 2) < 1) {
  96. printf ("Usage: %s <lines>\n", argv[0]);
  97. exit (EXIT_SUCCESS);
  98. }
  99. if (!connection || !xmmsc_connect (connection, path)) {
  100. printf ("Couldn't connect to xmms2d: %s\n",
  101. xmmsc_get_last_error (connection));
  102. exit (EXIT_FAILURE);
  103. }
  104. res = xmmsc_visualization_version (connection);
  105. xmmsc_result_wait (res);
  106. val = xmmsc_result_get_value (res);
  107. if (xmmsv_get_error (val, &errmsg)) {
  108. puts (errmsg);
  109. exit (EXIT_FAILURE);
  110. } else {
  111. int32_t version;
  112. xmmsv_get_int (val, &version);
  113. /* insert the version you need here or instead of complaining,
  114. reduce your feature set to fit the version */
  115. if (version < 1) {
  116. printf ("The server only supports formats up to version %d (needed is %d)!", version, 1);
  117. exit (EXIT_FAILURE);
  118. }
  119. }
  120. xmmsc_result_unref (res);
  121. res = xmmsc_visualization_init (connection);
  122. xmmsc_result_wait (res);
  123. val = xmmsc_result_get_value (res);
  124. if (xmmsv_get_error (val, &errmsg)) {
  125. puts (errmsg);
  126. exit (EXIT_FAILURE);
  127. }
  128. vis = xmmsc_visualization_init_handle (res);
  129. configval = xmmsv_build_dict (XMMSV_DICT_ENTRY_STR ("type", "spectrum"),
  130. XMMSV_DICT_ENTRY_STR ("stereo", "0"),
  131. XMMSV_DICT_END);
  132. res = xmmsc_visualization_properties_set (connection, vis, configval);
  133. xmmsc_result_wait (res);
  134. val = xmmsc_result_get_value (res);
  135. if (xmmsv_get_error (val, &errmsg)) {
  136. puts (errmsg);
  137. exit (EXIT_FAILURE);
  138. }
  139. xmmsc_result_unref (res);
  140. xmmsv_unref (configval);
  141. while (!xmmsc_visualization_started (connection, vis)) {
  142. res = xmmsc_visualization_start (connection, vis);
  143. if (xmmsc_visualization_errored (connection, vis)) {
  144. printf ("Couldn't start visualization transfer: %s\n",
  145. xmmsc_get_last_error (connection));
  146. exit (EXIT_FAILURE);
  147. }
  148. if (res) {
  149. xmmsc_result_wait (res);
  150. xmmsc_visualization_start_handle (connection, res);
  151. xmmsc_result_unref (res);
  152. }
  153. }
  154. setup_signal ();
  155. /* using GTK mainloop */
  156. mainloop = g_main_loop_new (NULL, FALSE);
  157. xmmsc_mainloop_gmain_init (connection);
  158. g_timeout_add_full (G_PRIORITY_DEFAULT, 20, draw_gtk, NULL, shutdown_gtk);
  159. g_main_loop_run (mainloop);
  160. /* not using GTK mainloop */
  161. while (xmmsc_visualization_chunk_get (connection, vis, data, 0, 1000) == 256) {
  162. draw ();
  163. }
  164. quit (0);
  165. return 0;
  166. }