summaryrefslogtreecommitdiff
path: root/src/examples/ecore
diff options
context:
space:
mode:
authorDaniel Willmann <d.willmann@samsung.com>2012-12-14 23:38:28 +0000
committerDaniel Willmann <daniel@totalueberwachung.de>2012-12-14 23:38:28 +0000
commit21436714202ccb866350a23e04631d66491d7163 (patch)
tree38930e5f24150f75e7d17f339d175c594ac46a28 /src/examples/ecore
parent29931c273f9d4c61c63c204373964281e64e7b1c (diff)
ecore_audio: Add an example - a simple CLI music player
Give it sound files (ogg, wav, not mp3) as parameter or "tone:freq=<f>,duration=<dur_in_s>" to play a lovely sine wave * '<' and '>' seek forward and backward * '+' and '-' change the volume * Space toggles pause * Shows information for the song (remaining, %) * (Un)loop a song with 'l' * 'n' - Next song * 'm' - Next song simultaneous to current * Volume can be set from 0 - 150% Signed-off-by: Daniel Willmann <d.willmann@samsung.com> SVN revision: 81001
Diffstat (limited to 'src/examples/ecore')
-rw-r--r--src/examples/ecore/Makefile.am12
-rw-r--r--src/examples/ecore/ecore_audio_playback.c274
2 files changed, 286 insertions, 0 deletions
diff --git a/src/examples/ecore/Makefile.am b/src/examples/ecore/Makefile.am
index 52b7a584db..170e3b7fb4 100644
--- a/src/examples/ecore/Makefile.am
+++ b/src/examples/ecore/Makefile.am
@@ -8,6 +8,7 @@ AM_CPPFLAGS = \
8-I$(top_srcdir)/src/lib/ecore_input \ 8-I$(top_srcdir)/src/lib/ecore_input \
9-I$(top_srcdir)/src/lib/ecore_con \ 9-I$(top_srcdir)/src/lib/ecore_con \
10-I$(top_srcdir)/src/lib/ecore_evas \ 10-I$(top_srcdir)/src/lib/ecore_evas \
11-I$(top_srcdir)/src/lib/ecore_audio \
11-I$(top_builddir)/src/lib/eina \ 12-I$(top_builddir)/src/lib/eina \
12-I$(top_builddir)/src/lib/eo \ 13-I$(top_builddir)/src/lib/eo \
13-I$(top_builddir)/src/lib/evas \ 14-I$(top_builddir)/src/lib/evas \
@@ -15,6 +16,7 @@ AM_CPPFLAGS = \
15-I$(top_builddir)/src/lib/ecore_input \ 16-I$(top_builddir)/src/lib/ecore_input \
16-I$(top_builddir)/src/lib/ecore_con \ 17-I$(top_builddir)/src/lib/ecore_con \
17-I$(top_builddir)/src/lib/ecore_evas \ 18-I$(top_builddir)/src/lib/ecore_evas \
19-I$(top_builddir)/src/lib/ecore_audio \
18-DPACKAGE_EXAMPLES_DIR=\"$(datadir)/ecore/examples\" \ 20-DPACKAGE_EXAMPLES_DIR=\"$(datadir)/ecore/examples\" \
19@ECORE_CFLAGS@ 21@ECORE_CFLAGS@
20 22
@@ -49,6 +51,15 @@ ecore_thread_example \
49ecore_time_functions_example \ 51ecore_time_functions_example \
50ecore_timer_example 52ecore_timer_example
51 53
54if HAVE_ECORE_AUDIO
55EXTRA_PROGRAMS += \
56ecore_audio_playback
57
58ecore_audio_playback_SOURCES = ecore_audio_playback.c
59ecore_audio_playback_LDADD = $(top_builddir)/src/lib/ecore_audio/libecore_audio.la $(top_builddir)/src/lib/ecore/libecore.la $(top_builddir)/src/lib/eina/libeina.la @ECORE_LIBS@
60
61endif
62
52ecore_animator_example_SOURCES = ecore_animator_example.c 63ecore_animator_example_SOURCES = ecore_animator_example.c
53ecore_animator_example_LDADD = $(top_builddir)/src/lib/ecore_evas/libecore_evas.la $(top_builddir)/src/lib/ecore/libecore.la $(top_builddir)/src/lib/evas/libevas.la @ECORE_LIBS@ 64ecore_animator_example_LDADD = $(top_builddir)/src/lib/ecore_evas/libecore_evas.la $(top_builddir)/src/lib/ecore/libecore.la $(top_builddir)/src/lib/evas/libevas.la @ECORE_LIBS@
54 65
@@ -138,6 +149,7 @@ ecore_timer_example_LDADD = $(top_builddir)/src/lib/ecore/libecore.la @ECORE_LIB
138 149
139SRCS = \ 150SRCS = \
140ecore_animator_example \ 151ecore_animator_example \
152ecore_audio_playback \
141ecore_client_bench \ 153ecore_client_bench \
142ecore_con_client_simple_example \ 154ecore_con_client_simple_example \
143ecore_con_lookup_example \ 155ecore_con_lookup_example \
diff --git a/src/examples/ecore/ecore_audio_playback.c b/src/examples/ecore/ecore_audio_playback.c
new file mode 100644
index 0000000000..1ec7444804
--- /dev/null
+++ b/src/examples/ecore/ecore_audio_playback.c
@@ -0,0 +1,274 @@
1#include <stdio.h>
2#include <string.h>
3#include <libgen.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <termios.h>
7#include <unistd.h>
8#include <fcntl.h>
9#include <Ecore.h>
10#include <Ecore_Audio.h>
11#include <Eina.h>
12
13Ecore_Audio_Object *out = NULL;
14double volume = 1;
15Eina_List *inputs = NULL;
16
17void
18handle_cmd(char *cmd, size_t bread)
19{
20 Eina_List *out_inputs, *input;
21 Ecore_Audio_Object *in;
22 Eina_Bool paused;
23 double pos;
24 int min;
25
26 if (!out)
27 return;
28
29 out_inputs = ecore_audio_output_inputs_get(out);
30 EINA_LIST_FOREACH(out_inputs, input, in)
31 {
32 pos = ecore_audio_input_seek(in, 0, SEEK_CUR);
33 if (!strncmp(cmd, "<", bread))
34 pos = ecore_audio_input_seek(in, -10, SEEK_CUR);
35 else if (!strncmp(cmd, ">", bread))
36 pos = ecore_audio_input_seek(in, 10, SEEK_CUR);
37
38 min = pos / 60;
39 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));
40
41 }
42
43 if (!strncmp(cmd, "p", bread))
44 {
45 }
46 else if (!strncmp(cmd, "n", bread))
47 {
48 in = eina_list_data_get(out_inputs);
49 ecore_audio_output_input_del(out, in);
50 inputs = eina_list_remove(inputs, in);
51
52 if (eina_list_count(inputs) > 0)
53 {
54 in = (Ecore_Audio_Object *)eina_list_data_get(inputs);
55
56 printf("Start: %s (%0.2fs)\n", ecore_audio_input_name_get(in), ecore_audio_input_length_get(in));
57 ecore_audio_output_input_add(out, in);
58 }
59 else
60 {
61 printf("Done\n");
62 ecore_main_loop_quit();
63 }
64 }
65 else if (!strncmp(cmd, "m", bread))
66 {
67 inputs = eina_list_remove(inputs, eina_list_data_get(inputs));
68 if (eina_list_count(inputs) > 0)
69 {
70 in = (Ecore_Audio_Object *)eina_list_data_get(inputs);
71
72 printf("Start: %s (%0.2fs)\n", ecore_audio_input_name_get(in), ecore_audio_input_length_get(in));
73 ecore_audio_output_input_add(out, in);
74 }
75 }
76 else if (!strncmp(cmd, "l", bread))
77 {
78 EINA_LIST_FOREACH(out_inputs, input, in)
79 {
80 Eina_Bool loop = !ecore_audio_input_looped_get(in);
81 printf("%s song %s\n", loop?"Looping":"Not looping", ecore_audio_input_name_get(in));
82 ecore_audio_input_looped_set(in, loop);
83 }
84 }
85 else if (!strncmp(cmd, "+", bread))
86 {
87 if (volume <= 1.5)
88 volume += 0.01;
89 ecore_audio_output_volume_set(out, volume);
90 printf("Volume: %3.0f%%\n", volume * 100);
91 }
92 else if (!strncmp(cmd, "-", bread))
93 {
94 if (volume >= 0)
95 volume -= 0.01;
96 ecore_audio_output_volume_set(out, volume);
97 printf("Volume: %3.0f%%\n", volume * 100);
98 }
99 else if (!strncmp(cmd, " ", bread))
100 {
101 EINA_LIST_FOREACH(out_inputs, input, in)
102 {
103 paused = ecore_audio_input_paused_get(in);
104 ecore_audio_input_paused_set(in, !paused);
105 printf("%s %s\n%0.2f remaining\n", !paused ? "Paused" : "Unpaused", ecore_audio_input_name_get(in), ecore_audio_input_remaining_get(in));
106 }
107 }
108 else if (!strncmp(cmd, "q", bread))
109 {
110 ecore_main_loop_quit();
111 }
112 else
113 printf("Unhandled cmd '%s'\n", cmd);
114
115}
116
117Eina_Bool
118handle_input(void *data, Ecore_Fd_Handler *handler)
119{
120 size_t bread;
121 char buf[20];
122 int fd, i;
123
124 if (!ecore_main_fd_handler_active_get(handler, ECORE_FD_READ))
125 return EINA_TRUE;
126
127 fd = ecore_main_fd_handler_fd_get(handler);
128
129 bread = read(fd, buf, sizeof(buf));
130 if (bread == 0)
131 {
132 printf("Ctrl-D?\n");
133 ecore_main_loop_quit();
134
135 return EINA_FALSE;
136 }
137
138 handle_cmd(buf, bread);
139
140 return EINA_TRUE;
141}
142
143static Eina_Bool _play_started(void *data, int type, void *event)
144{
145 Ecore_Audio_Object *in = (Ecore_Audio_Object *)event;
146 printf("Start: %s\n", ecore_audio_input_name_get(in));
147 return EINA_TRUE;
148}
149
150static Eina_Bool _play_finished(void *data, int type, void *event)
151{
152 Ecore_Audio_Object *in = (Ecore_Audio_Object *)event;
153
154 printf("Done: %s\n", ecore_audio_input_name_get(in));
155
156 inputs = eina_list_remove(inputs, in);
157 ecore_audio_output_input_del(out, in);
158 ecore_audio_input_del(in);
159
160
161 if (eina_list_count(inputs) > 0)
162 {
163 in = (Ecore_Audio_Object *)eina_list_data_get(inputs);
164
165 printf("Start: %s\n", ecore_audio_input_name_get(in));
166 ecore_audio_output_input_add(out, in);
167 }
168 else
169 {
170 printf("Stream done\n");
171 ecore_main_loop_quit();
172 }
173
174 return EINA_TRUE;
175}
176
177Eina_Bool
178output_add(void *data)
179{
180 Ecore_Audio_Object *in = (Ecore_Audio_Object *)eina_list_data_get(inputs);
181
182 printf("Start: %s (%0.2fs)\n", ecore_audio_input_name_get(in), ecore_audio_input_length_get(in));
183 out = ecore_audio_output_add(ECORE_AUDIO_TYPE_PULSE);
184 ecore_audio_output_input_add(out, in);
185 return EINA_FALSE;
186}
187
188int
189main(int argc, const char *argv[])
190{
191 int i;
192
193 struct termios tcorig, tcnew;
194 Ecore_Audio_Object *in;
195 char *tmp, *tmp2, *val;
196
197 if (argc < 2)
198 {
199 printf("Please provide a filename\n");
200 exit(EXIT_FAILURE);
201 }
202
203 ecore_init();
204 ecore_audio_init();
205
206 ecore_event_handler_add(ECORE_AUDIO_INPUT_STARTED, _play_started, NULL);
207 ecore_event_handler_add(ECORE_AUDIO_INPUT_LOOPED, _play_started, NULL);
208 ecore_event_handler_add(ECORE_AUDIO_INPUT_ENDED, _play_finished, NULL);
209
210 for (i=1;i<argc;i++)
211 {
212 if (!strncmp(argv[i], "tone:", 5))
213 {
214 in = ecore_audio_input_add(ECORE_AUDIO_TYPE_TONE);
215 if (!in)
216 {
217 printf("error when creating ecore audio source.\n");
218 goto end;
219 }
220 /* The name has the format "tone:freq=x,duration=y */
221 tmp = strdup(argv[i]);
222 val = strtok_r(tmp, ":", &tmp2);
223
224 while ((val = strtok_r(NULL, ",", &tmp2)) != NULL)
225 {
226 if (!strncmp(val, "freq=", 5))
227 ecore_audio_input_tone_frequency_set(in, atoi(&val[5]));
228 else if (!strncmp(val, "duration=", 9))
229 ecore_audio_input_tone_duration_set(in, atof(&val[9]));
230 }
231 free(tmp);
232 ecore_audio_input_name_set(in, argv[i]);
233 }
234 else
235 {
236 in = ecore_audio_input_add(ECORE_AUDIO_TYPE_SNDFILE);
237 if (!in)
238 {
239 printf("error when creating ecore audio source.\n");
240 goto end;
241 }
242 ecore_audio_input_name_set(in, basename(argv[i]));
243 ecore_audio_input_sndfile_filename_set(in, argv[i]);
244 }
245 inputs = eina_list_append(inputs, in);
246 }
247
248
249 ecore_timer_add(1, output_add, NULL);
250
251 /* Disable canonical mode for stdin */
252 if (tcgetattr(0, &tcorig) == -1)
253 printf("Problem getting termcaps: %s\n", strerror(errno));
254
255 tcnew = tcorig;
256 tcnew.c_lflag &= ~(ICANON | ECHO);
257 if (tcsetattr(0, TCSANOW, &tcnew) == -1)
258 printf("Problem setting termcaps: %s\n", strerror(errno));
259
260 ecore_main_fd_handler_add(0, ECORE_FD_READ, handle_input, NULL, NULL, NULL);
261
262 ecore_main_loop_begin();
263
264end:
265 ecore_audio_shutdown();
266 ecore_shutdown();
267
268 /* Reset stdin */
269 if (tcsetattr(0, TCSANOW, &tcorig) == -1)
270 printf("Problem restoring termcaps: %s\n", strerror(errno));
271
272 return 0;
273}
274