ecore_audio: Implement ecore_audio_playback example with eo
authorDaniel Willmann <d.willmann@samsung.com>
Wed, 17 Apr 2013 17:56:50 +0000 (18:56 +0100)
committerDaniel Willmann <d.willmann@samsung.com>
Thu, 18 Apr 2013 18:15:37 +0000 (19:15 +0100)
Signed-off-by: Daniel Willmann <d.willmann@samsung.com>
src/examples/ecore/Makefile.am
src/examples/ecore/ecore_audio_playback.c

index cba995e..c147757 100644 (file)
@@ -74,6 +74,7 @@ $(top_builddir)/src/lib/eina/libeina.la \
 
 if HAVE_ECORE_AUDIO
 EXTRA_PROGRAMS += \
+ecore_audio_playback \
 ecore_audio_to_ogg
 
 ECORE_AUDIO_COMMON_LDADD = \
index 86ff8c2..bdfd7db 100644 (file)
 #include <Ecore_Audio.h>
 #include <Eina.h>
 
-Ecore_Audio_Object *out = NULL;
+Eo *out = NULL;
 double volume = 1;
 Eina_List *inputs = NULL;
 
 void
 handle_cmd(char *cmd, size_t bread)
 {
+   const char *name;
    Eina_List *out_inputs, *input;
-   Ecore_Audio_Object *in;
+   Eo *in;
    Eina_Bool paused;
-   double pos;
+   double pos, length;
    int min;
 
    if (!out)
      return;
 
-   out_inputs = ecore_audio_output_inputs_get(out);
+   eo_do(out, ecore_audio_obj_out_inputs_get(&out_inputs));
    EINA_LIST_FOREACH(out_inputs, input, in)
      {
-        pos = ecore_audio_input_seek(in, 0, SEEK_CUR);
+        eo_do(in, ecore_audio_obj_in_seek(0, SEEK_CUR, &pos));
         if (!strncmp(cmd, "<", bread))
-          pos = ecore_audio_input_seek(in, -10, SEEK_CUR);
+          eo_do(in, ecore_audio_obj_in_seek(-10, SEEK_CUR, &pos));
         else if (!strncmp(cmd, ">", bread))
-          pos = ecore_audio_input_seek(in, 10, SEEK_CUR);
+          eo_do(in, ecore_audio_obj_in_seek(10, SEEK_CUR, &pos));
 
         min = pos / 60;
-        printf("Position: %2im %5.02fs (%0.2f%%) - %s\n", min, pos - min * 60, pos/ecore_audio_input_length_get(in)*100, ecore_audio_input_name_get(in));
+
+        eo_do(in, ecore_audio_obj_name_get(&name));
+        eo_do(in, ecore_audio_obj_in_length_get(&length));
+        printf("Position: %2im %5.02fs (%0.2f%%) - %s\n", min, pos - min * 60, pos/length*100, name);
 
      }
 
@@ -49,15 +53,18 @@ handle_cmd(char *cmd, size_t bread)
    else if (!strncmp(cmd, "n", bread))
      {
         in = eina_list_data_get(out_inputs);
-        ecore_audio_output_input_del(out, in);
+        eo_do(out, ecore_audio_obj_out_input_detach(in));
         inputs = eina_list_remove(inputs, in);
 
         if (eina_list_count(inputs) > 0)
           {
-             in = (Ecore_Audio_Object *)eina_list_data_get(inputs);
+             in = (Eo *)eina_list_data_get(inputs);
+
+             eo_do(in, ecore_audio_obj_name_get(&name),
+                   ecore_audio_obj_in_length_get(&length));
 
-             printf("Start: %s (%0.2fs)\n", ecore_audio_input_name_get(in), ecore_audio_input_length_get(in));
-             ecore_audio_output_input_add(out, in);
+             printf("Start: %s (%0.2fs)\n", name, length);
+             eo_do(out, ecore_audio_obj_out_input_attach(in));
           }
        else
          {
@@ -70,33 +77,39 @@ handle_cmd(char *cmd, size_t bread)
         inputs = eina_list_remove(inputs, eina_list_data_get(inputs));
         if (eina_list_count(inputs) > 0)
           {
-             in = (Ecore_Audio_Object *)eina_list_data_get(inputs);
+             in = (Eo *)eina_list_data_get(inputs);
 
-             printf("Start: %s (%0.2fs)\n", ecore_audio_input_name_get(in), ecore_audio_input_length_get(in));
-             ecore_audio_output_input_add(out, in);
+             eo_do(in, ecore_audio_obj_name_get(&name),
+                   ecore_audio_obj_in_length_get(&length));
+
+             printf("Start: %s (%0.2fs)\n", name, length);
+             eo_do(out, ecore_audio_obj_out_input_attach(in));
           }
      }
    else if (!strncmp(cmd, "l", bread))
      {
         EINA_LIST_FOREACH(out_inputs, input, in)
           {
-             Eina_Bool loop = !ecore_audio_input_looped_get(in);
-             printf("%s song %s\n", loop?"Looping":"Not looping", ecore_audio_input_name_get(in));
-             ecore_audio_input_looped_set(in, loop);
+             Eina_Bool loop;
+             eo_do(in, ecore_audio_obj_in_looped_get(&loop),
+                   ecore_audio_obj_name_get(&name));
+
+             printf("%s song %s\n", !loop?"Looping":"Not looping", name);
+             eo_do(in, ecore_audio_obj_in_looped_set(!loop));
           }
      }
    else if (!strncmp(cmd, "+", bread))
      {
         if (volume < 1.5)
           volume += 0.01;
-        ecore_audio_output_volume_set(out, volume);
+        eo_do(out, ecore_audio_obj_volume_set(volume));
         printf("Volume: %3.0f%%\n", volume * 100);
      }
    else if (!strncmp(cmd, "-", bread))
      {
         if (volume > 0)
           volume -= 0.01;
-        ecore_audio_output_volume_set(out, volume);
+        eo_do(out, ecore_audio_obj_volume_set(volume));
         printf("Volume: %3.0f%%\n", volume * 100);
      }
    else if (!strncmp(cmd, "*", bread))
@@ -104,11 +117,12 @@ handle_cmd(char *cmd, size_t bread)
         double speed;
         EINA_LIST_FOREACH(out_inputs, input, in)
           {
-             speed = ecore_audio_input_speed_get(in);
+             eo_do(in, ecore_audio_obj_in_speed_get(&speed));
              if (speed < 2.0)
                speed += 0.01;
-             ecore_audio_input_speed_set(in, speed);
-             printf("Speed: %3.0f%% (%s)\n", speed * 100, ecore_audio_input_name_get(in));
+             eo_do(in, ecore_audio_obj_in_speed_set(speed),
+                   ecore_audio_obj_name_get(&name));
+             printf("Speed: %3.0f%% (%s)\n", speed * 100, name);
           }
      }
    else if (!strncmp(cmd, "/", bread))
@@ -116,20 +130,23 @@ handle_cmd(char *cmd, size_t bread)
         double speed;
         EINA_LIST_FOREACH(out_inputs, input, in)
           {
-             speed = ecore_audio_input_speed_get(in);
+             eo_do(in, ecore_audio_obj_in_speed_get(&speed));
              if (speed > 0.5)
                speed -= 0.01;
-             ecore_audio_input_speed_set(in, speed);
-             printf("Speed: %3.0f%% (%s)\n", speed * 100, ecore_audio_input_name_get(in));
+             eo_do(in, ecore_audio_obj_in_speed_set(speed),
+                   ecore_audio_obj_name_get(&name));
+             printf("Speed: %3.0f%% (%s)\n", speed * 100, name);
           }
      }
    else if (!strncmp(cmd, " ", bread))
      {
         EINA_LIST_FOREACH(out_inputs, input, in)
           {
-             paused = ecore_audio_input_paused_get(in);
-             ecore_audio_input_paused_set(in, !paused);
-             printf("%s %s\n%0.2f remaining\n", !paused ? "Paused" : "Unpaused", ecore_audio_input_name_get(in), ecore_audio_input_remaining_get(in));
+             eo_do(in, ecore_audio_obj_paused_get(&paused),
+                   ecore_audio_obj_name_get(&name),
+                   ecore_audio_obj_in_remaining_get(&length));
+             printf("%s %s\n%0.2f remaining\n", !paused ? "Paused" : "Unpaused", name, length);
+             eo_do(in, ecore_audio_obj_paused_set(!paused));
           }
      }
    else if (!strncmp(cmd, "q", bread))
@@ -146,7 +163,7 @@ handle_input(void *data, Ecore_Fd_Handler *handler)
 {
    size_t bread;
    char buf[20];
-   int fd, i;
+   int fd;
 
    if (!ecore_main_fd_handler_active_get(handler, ECORE_FD_READ))
      return EINA_TRUE;
@@ -169,28 +186,35 @@ handle_input(void *data, Ecore_Fd_Handler *handler)
 
 static Eina_Bool _play_started(void *data, int type, void *event)
 {
-  Ecore_Audio_Object *in = (Ecore_Audio_Object *)event;
-  printf("Start: %s\n", ecore_audio_input_name_get(in));
+  const char *name;
+  Eo *in = event;
+
+  eo_do(in, ecore_audio_obj_name_get(&name));
+  printf("Start: %s\n", name);
+
   return EINA_TRUE;
 }
 
-static Eina_Bool _play_finished(void *data, int type, void *event)
+static Eina_Bool _play_finished(void *data EINA_UNUSED, Eo *in, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
 {
-  Ecore_Audio_Object *in = (Ecore_Audio_Object *)event;
+  const char *name;
 
-  printf("Done: %s\n", ecore_audio_input_name_get(in));
+  eo_do(in, ecore_audio_obj_name_get(&name));
+  printf("Done: %s\n", name);
 
   inputs = eina_list_remove(inputs, in);
-  ecore_audio_output_input_del(out, in);
-  ecore_audio_input_del(in);
+  eo_do(out, ecore_audio_obj_out_input_detach(in));
+  eo_del(in);
 
 
   if (eina_list_count(inputs) > 0)
     {
-      in = (Ecore_Audio_Object *)eina_list_data_get(inputs);
+      const char *name;
+      in = (Eo *)eina_list_data_get(inputs);
 
-      printf("Start: %s\n", ecore_audio_input_name_get(in));
-      ecore_audio_output_input_add(out, in);
+      eo_do(in, ecore_audio_obj_name_get(&name));
+      printf("Start: %s\n", name);
+      eo_do(out, ecore_audio_obj_out_input_attach(in));
     }
   else
     {
@@ -201,24 +225,13 @@ static Eina_Bool _play_finished(void *data, int type, void *event)
   return EINA_TRUE;
 }
 
-Eina_Bool
-output_add(void *data)
-{
-   Ecore_Audio_Object *in = (Ecore_Audio_Object *)eina_list_data_get(inputs);
-
-   printf("Start: %s (%0.2fs)\n", ecore_audio_input_name_get(in), ecore_audio_input_length_get(in));
-   out = ecore_audio_output_add(ECORE_AUDIO_TYPE_PULSE);
-   ecore_audio_output_input_add(out, in);
-   return EINA_FALSE;
-}
-
 int
 main(int argc, const char *argv[])
 {
-   int i;
+   int i, freq;
 
    struct termios tcorig, tcnew;
-   Ecore_Audio_Object *in;
+   Eo *in;
    char *tmp, *tmp2, *val;
 
    if (argc < 2)
@@ -230,15 +243,13 @@ main(int argc, const char *argv[])
    ecore_init();
    ecore_audio_init();
 
-   ecore_event_handler_add(ECORE_AUDIO_INPUT_STARTED, _play_started, NULL);
-   ecore_event_handler_add(ECORE_AUDIO_INPUT_LOOPED, _play_started, NULL);
-   ecore_event_handler_add(ECORE_AUDIO_INPUT_ENDED, _play_finished, NULL);
+   ecore_app_args_set(argc, argv);
 
    for (i=1;i<argc;i++)
      {
        if (!strncmp(argv[i], "tone:", 5))
          {
-            in = ecore_audio_input_add(ECORE_AUDIO_TYPE_TONE);
+            in = eo_add(ECORE_AUDIO_OBJ_IN_TONE_CLASS, NULL);
             if (!in)
               {
                  printf("error when creating ecore audio source.\n");
@@ -250,30 +261,43 @@ main(int argc, const char *argv[])
 
             while ((val = strtok_r(NULL, ",", &tmp2)) != NULL)
               {
-                 if (!strncmp(val, "freq=", 5))
-                   ecore_audio_input_tone_frequency_set(in, atoi(&val[5]));
-                 else if (!strncmp(val, "duration=", 9))
-                   ecore_audio_input_tone_duration_set(in, atof(&val[9]));
+                 if (!strncmp(val, "freq=", 5)) {
+                   freq = atoi(&val[5]);
+                   eo_do(in, eo_base_data_set(ECORE_AUDIO_ATTR_TONE_FREQ, &freq, NULL));
+                 } else if (!strncmp(val, "duration=", 9)) {
+                   eo_do(in, ecore_audio_obj_in_length_set(atof(&val[9])));
+                 }
               }
             free(tmp);
-            ecore_audio_input_name_set(in, argv[i]);
+            eo_do(in, ecore_audio_obj_name_set(argv[i]));
          }
        else
          {
-            in = ecore_audio_input_add(ECORE_AUDIO_TYPE_SNDFILE);
+            in = eo_add(ECORE_AUDIO_OBJ_IN_SNDFILE_CLASS, NULL);
             if (!in)
               {
                  printf("error when creating ecore audio source.\n");
                  goto end;
               }
-            ecore_audio_input_name_set(in, basename(argv[i]));
-            ecore_audio_input_sndfile_filename_set(in, argv[i]);
+            eo_do(in, ecore_audio_obj_name_set(basename(argv[i])));
+            eo_do(in, ecore_audio_obj_source_set(argv[i]));
          }
+       eo_do(in, eo_event_callback_add(ECORE_AUDIO_EV_IN_STOPPED, _play_finished, NULL));
        inputs = eina_list_append(inputs, in);
      }
 
+   const char *name;
+   double length;
+   in = (Eo *)eina_list_data_get(inputs);
+
+   eo_do(in, ecore_audio_obj_name_get(&name),
+         ecore_audio_obj_in_length_get(&length));
+
+   printf("Start: %s (%0.2fs)\n", name, length);
+
+   out = eo_add(ECORE_AUDIO_OBJ_OUT_PULSE_CLASS, NULL);
+   eo_do(out, ecore_audio_obj_out_input_attach(in));
 
-   ecore_timer_add(1, output_add, NULL);
 
    /* Disable canonical mode for stdin */
    if (tcgetattr(0, &tcorig) == -1)