diff options
author | Daniel Willmann <d.willmann@samsung.com> | 2013-04-12 19:11:15 +0100 |
---|---|---|
committer | Daniel Willmann <d.willmann@samsung.com> | 2013-04-18 19:14:32 +0100 |
commit | e865d5b0a4b8eb41def9c5f6412e650b7660a7d9 (patch) | |
tree | fced5dd7ad118f7eeb4ea6da49a4da7ec7f81625 /src/lib/ecore_audio | |
parent | 542b8743bbae5bba73a7431df570dd034016956f (diff) |
ecore_audio: Add tone input class
Signed-off-by: Daniel Willmann <d.willmann@samsung.com>
Diffstat (limited to 'src/lib/ecore_audio')
-rw-r--r-- | src/lib/ecore_audio/Ecore_Audio.h | 2 | ||||
-rw-r--r-- | src/lib/ecore_audio/ecore_audio_obj_in_tone.c | 199 | ||||
-rw-r--r-- | src/lib/ecore_audio/ecore_audio_obj_in_tone.h | 57 | ||||
-rw-r--r-- | src/lib/ecore_audio/ecore_audio_private.h | 8 |
4 files changed, 258 insertions, 8 deletions
diff --git a/src/lib/ecore_audio/Ecore_Audio.h b/src/lib/ecore_audio/Ecore_Audio.h index ddf34a0497..8a542176e1 100644 --- a/src/lib/ecore_audio/Ecore_Audio.h +++ b/src/lib/ecore_audio/Ecore_Audio.h | |||
@@ -574,4 +574,6 @@ EAPI void ecore_audio_input_callback_setup(Ecore_Audio_Object *in | |||
574 | #include <ecore_audio_obj_in_sndfile.h> | 574 | #include <ecore_audio_obj_in_sndfile.h> |
575 | #include <ecore_audio_obj_out_sndfile.h> | 575 | #include <ecore_audio_obj_out_sndfile.h> |
576 | 576 | ||
577 | #include <ecore_audio_obj_in_tone.h> | ||
578 | |||
577 | #endif | 579 | #endif |
diff --git a/src/lib/ecore_audio/ecore_audio_obj_in_tone.c b/src/lib/ecore_audio/ecore_audio_obj_in_tone.c new file mode 100644 index 0000000000..53286cb762 --- /dev/null +++ b/src/lib/ecore_audio/ecore_audio_obj_in_tone.c | |||
@@ -0,0 +1,199 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | #include <config.h> | ||
3 | #endif | ||
4 | |||
5 | #include <stdlib.h> | ||
6 | #include <stdio.h> | ||
7 | #include <string.h> | ||
8 | |||
9 | #ifdef HAVE_FEATURES_H | ||
10 | #include <features.h> | ||
11 | #endif | ||
12 | |||
13 | #include <Eo.h> | ||
14 | #include "ecore_audio_private.h" | ||
15 | #include <math.h> | ||
16 | |||
17 | EAPI Eo_Op ECORE_AUDIO_OBJ_IN_TONE_BASE_ID = EO_NOOP; | ||
18 | |||
19 | #define MY_CLASS ECORE_AUDIO_OBJ_IN_TONE_CLASS | ||
20 | #define MY_CLASS_NAME "ecore_audio_obj_in_tone" | ||
21 | |||
22 | struct _Ecore_Audio_Tone | ||
23 | { | ||
24 | int freq; | ||
25 | int phase; | ||
26 | }; | ||
27 | |||
28 | typedef struct _Ecore_Audio_Tone Ecore_Audio_Tone; | ||
29 | |||
30 | static void _read(Eo *eo_obj, void *_pd, va_list *list) | ||
31 | { | ||
32 | int i, remain; | ||
33 | Ecore_Audio_Tone *obj = _pd; | ||
34 | Ecore_Audio_Input *in_obj = eo_data_get(eo_obj, ECORE_AUDIO_OBJ_IN_CLASS); | ||
35 | |||
36 | |||
37 | void *data = va_arg(*list, void *); | ||
38 | int len = va_arg(*list, int); | ||
39 | int *ret = va_arg(*list, int *); | ||
40 | |||
41 | float *val = data; | ||
42 | |||
43 | remain = in_obj->length * in_obj->samplerate * 4 - obj->phase * 4; | ||
44 | if (remain > len) | ||
45 | remain = len; | ||
46 | |||
47 | for (i=0; i<remain/4; i++) { | ||
48 | val[i] = sin(2* M_PI * obj->freq * (obj->phase + i) / in_obj->samplerate); | ||
49 | } | ||
50 | |||
51 | obj->phase += i; | ||
52 | |||
53 | if (ret) | ||
54 | *ret = remain; | ||
55 | } | ||
56 | |||
57 | static void _seek(Eo *eo_obj, void *_pd, va_list *list) | ||
58 | { | ||
59 | int tmp; | ||
60 | Ecore_Audio_Tone *obj = _pd; | ||
61 | Ecore_Audio_Input *in_obj = eo_data_get(eo_obj, ECORE_AUDIO_OBJ_IN_CLASS); | ||
62 | |||
63 | double offs = va_arg(*list, double); | ||
64 | int mode = va_arg(*list, int); | ||
65 | double *ret = va_arg(*list, double *); | ||
66 | |||
67 | switch (mode) { | ||
68 | case SEEK_SET: | ||
69 | tmp = offs * in_obj->samplerate; | ||
70 | break; | ||
71 | case SEEK_CUR: | ||
72 | tmp = obj->phase + offs * in_obj->samplerate; | ||
73 | break; | ||
74 | case SEEK_END: | ||
75 | tmp = (in_obj->length + offs) * in_obj->samplerate; | ||
76 | break; | ||
77 | default: | ||
78 | goto err; | ||
79 | } | ||
80 | if ((tmp < 0) || (tmp > in_obj->length * in_obj->samplerate)) | ||
81 | goto err; | ||
82 | |||
83 | obj->phase = tmp; | ||
84 | |||
85 | if (ret) | ||
86 | *ret = (double)obj->phase / in_obj->samplerate; | ||
87 | |||
88 | err: | ||
89 | if (ret) | ||
90 | *ret = -1.0; | ||
91 | } | ||
92 | |||
93 | static void _source_set(Eo *eo_obj, void *_pd, va_list *list) | ||
94 | { | ||
95 | Ecore_Audio_Tone *obj = _pd; | ||
96 | |||
97 | Ecore_Audio_Object *ea_obj = eo_data_get(eo_obj, ECORE_AUDIO_OBJ_CLASS); | ||
98 | Ecore_Audio_Input *in_obj = eo_data_get(eo_obj, ECORE_AUDIO_OBJ_IN_CLASS); | ||
99 | |||
100 | const char *source = va_arg(*list, const char *); | ||
101 | |||
102 | eina_stringshare_replace(&ea_obj->source, source); | ||
103 | |||
104 | if (!ea_obj->source) | ||
105 | return; | ||
106 | |||
107 | ea_obj->format = ECORE_AUDIO_FORMAT_AUTO; | ||
108 | } | ||
109 | |||
110 | static void _source_get(Eo *eo_obj, void *_pd EINA_UNUSED, va_list *list) | ||
111 | { | ||
112 | Ecore_Audio_Object *obj = eo_data_get(eo_obj, ECORE_AUDIO_OBJ_CLASS); | ||
113 | |||
114 | const char **ret = va_arg(*list, const char **); | ||
115 | |||
116 | if (ret) | ||
117 | *ret = obj->source; | ||
118 | } | ||
119 | |||
120 | static void _format_set(Eo *eo_obj, void *_pd EINA_UNUSED, va_list *list) | ||
121 | { | ||
122 | Ecore_Audio_Object *ea_obj = eo_data_get(eo_obj, ECORE_AUDIO_OBJ_CLASS); | ||
123 | |||
124 | Ecore_Audio_Format format= va_arg(*list, Ecore_Audio_Format); | ||
125 | |||
126 | if (ea_obj->source) { | ||
127 | ERR("Input is already open - cannot change format"); | ||
128 | return; | ||
129 | } | ||
130 | |||
131 | switch (format) { | ||
132 | default: | ||
133 | ERR("Format not supported!"); | ||
134 | return; | ||
135 | } | ||
136 | ea_obj->format = format; | ||
137 | } | ||
138 | |||
139 | static void _format_get(Eo *eo_obj, void *_pd EINA_UNUSED, va_list *list) | ||
140 | { | ||
141 | Ecore_Audio_Object *obj = eo_data_get(eo_obj, ECORE_AUDIO_OBJ_CLASS); | ||
142 | |||
143 | Ecore_Audio_Format *ret = va_arg(*list, Ecore_Audio_Format *); | ||
144 | |||
145 | if (ret) | ||
146 | *ret = obj->format; | ||
147 | } | ||
148 | |||
149 | static void _constructor(Eo *eo_obj, void *_pd, va_list *list EINA_UNUSED) | ||
150 | { | ||
151 | Ecore_Audio_Tone *obj = _pd; | ||
152 | Ecore_Audio_Input *in_obj = eo_data_get(eo_obj, ECORE_AUDIO_OBJ_IN_CLASS); | ||
153 | |||
154 | eo_do_super(eo_obj, MY_CLASS, eo_constructor()); | ||
155 | |||
156 | in_obj->channels = 1; | ||
157 | in_obj->samplerate = 44100; | ||
158 | in_obj->length = 1; | ||
159 | |||
160 | obj->freq = 1000; | ||
161 | } | ||
162 | |||
163 | static void _class_constructor(Eo_Class *klass) | ||
164 | { | ||
165 | const Eo_Op_Func_Description func_desc[] = { | ||
166 | /* Virtual functions of parent class implemented in this class */ | ||
167 | EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_CONSTRUCTOR), _constructor), | ||
168 | //EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_DESTRUCTOR), _destructor), | ||
169 | |||
170 | EO_OP_FUNC(ECORE_AUDIO_OBJ_ID(ECORE_AUDIO_OBJ_SUB_ID_SOURCE_SET), _source_set), | ||
171 | EO_OP_FUNC(ECORE_AUDIO_OBJ_ID(ECORE_AUDIO_OBJ_SUB_ID_SOURCE_GET), _source_get), | ||
172 | EO_OP_FUNC(ECORE_AUDIO_OBJ_ID(ECORE_AUDIO_OBJ_SUB_ID_FORMAT_SET), _format_set), | ||
173 | EO_OP_FUNC(ECORE_AUDIO_OBJ_ID(ECORE_AUDIO_OBJ_SUB_ID_FORMAT_GET), _format_get), | ||
174 | |||
175 | EO_OP_FUNC(ECORE_AUDIO_OBJ_IN_ID(ECORE_AUDIO_OBJ_IN_SUB_ID_SEEK), _seek), | ||
176 | EO_OP_FUNC(ECORE_AUDIO_OBJ_IN_ID(ECORE_AUDIO_OBJ_IN_SUB_ID_READ_INTERNAL), _read), | ||
177 | |||
178 | EO_OP_FUNC_SENTINEL | ||
179 | }; | ||
180 | |||
181 | eo_class_funcs_set(klass, func_desc); | ||
182 | } | ||
183 | |||
184 | static const Eo_Op_Description op_desc[] = { | ||
185 | EO_OP_DESCRIPTION_SENTINEL | ||
186 | }; | ||
187 | |||
188 | static const Eo_Class_Description class_desc = { | ||
189 | EO_VERSION, | ||
190 | MY_CLASS_NAME, | ||
191 | EO_CLASS_TYPE_REGULAR, | ||
192 | EO_CLASS_DESCRIPTION_OPS(&ECORE_AUDIO_OBJ_IN_TONE_BASE_ID, op_desc, ECORE_AUDIO_OBJ_IN_TONE_SUB_ID_LAST), | ||
193 | NULL, | ||
194 | sizeof(Ecore_Audio_Tone), | ||
195 | _class_constructor, | ||
196 | NULL | ||
197 | }; | ||
198 | |||
199 | EO_DEFINE_CLASS(ecore_audio_obj_in_tone_class_get, &class_desc, ECORE_AUDIO_OBJ_IN_CLASS, NULL); | ||
diff --git a/src/lib/ecore_audio/ecore_audio_obj_in_tone.h b/src/lib/ecore_audio/ecore_audio_obj_in_tone.h new file mode 100644 index 0000000000..a02669118b --- /dev/null +++ b/src/lib/ecore_audio/ecore_audio_obj_in_tone.h | |||
@@ -0,0 +1,57 @@ | |||
1 | #ifndef ECORE_AUDIO_IN_TONE_H | ||
2 | #define ECORE_AUDIO_IN_TONE_H | ||
3 | |||
4 | #include <Eina.h> | ||
5 | #include <Eo.h> | ||
6 | |||
7 | #ifdef EAPI | ||
8 | #undef EAPI | ||
9 | #endif | ||
10 | |||
11 | #ifdef __GNUC__ | ||
12 | #if __GNUC__ >= 4 | ||
13 | #define EAPI __attribute__ ((visibility("default"))) | ||
14 | #else | ||
15 | #define EAPI | ||
16 | #endif | ||
17 | #else | ||
18 | #define EAPI | ||
19 | #endif | ||
20 | |||
21 | /** | ||
22 | * @file ecore_audio_obj_in_tone.h | ||
23 | * @brief Audio Module | ||
24 | */ | ||
25 | |||
26 | #ifdef __cplusplus | ||
27 | extern "C" | ||
28 | { | ||
29 | #endif | ||
30 | |||
31 | /** | ||
32 | * @addtogroup Ecore_Audio_Group | ||
33 | * @{ | ||
34 | */ | ||
35 | |||
36 | #define ECORE_AUDIO_OBJ_IN_TONE_CLASS ecore_audio_obj_in_tone_class_get() | ||
37 | |||
38 | const Eo_Class *ecore_audio_obj_in_tone_class_get() EINA_CONST; | ||
39 | |||
40 | extern EAPI Eo_Op ECORE_AUDIO_OBJ_IN_TONE_BASE_ID; | ||
41 | |||
42 | enum Ecore_Audio_Obj_In_Tone_Sub_Ids | ||
43 | { | ||
44 | ECORE_AUDIO_OBJ_IN_TONE_SUB_ID_LAST | ||
45 | }; | ||
46 | |||
47 | #define ECORE_AUDIO_OBJ_IN_TONE_ID(sub_id) (ECORE_AUDIO_OBJ_IN_TONE_BASE_ID + EO_TYPECHECK(enum Ecore_Audio_Obj_In_Tone_Sub_Ids, sub_id) | ||
48 | |||
49 | /** | ||
50 | * @} | ||
51 | */ | ||
52 | |||
53 | #ifdef __cplusplus | ||
54 | } | ||
55 | #endif | ||
56 | |||
57 | #endif | ||
diff --git a/src/lib/ecore_audio/ecore_audio_private.h b/src/lib/ecore_audio/ecore_audio_private.h index 7d74215a19..ef08b74a9b 100644 --- a/src/lib/ecore_audio/ecore_audio_private.h +++ b/src/lib/ecore_audio/ecore_audio_private.h | |||
@@ -184,14 +184,6 @@ Ecore_Audio_Module *ecore_audio_sndfile_init(void); | |||
184 | void ecore_audio_sndfile_shutdown(void); | 184 | void ecore_audio_sndfile_shutdown(void); |
185 | #endif /* HAVE_SNDFILE */ | 185 | #endif /* HAVE_SNDFILE */ |
186 | 186 | ||
187 | /* ecore_audio_tone */ | ||
188 | struct _Ecore_Audio_Tone | ||
189 | { | ||
190 | int freq; | ||
191 | double duration; | ||
192 | int phase; | ||
193 | }; | ||
194 | |||
195 | Ecore_Audio_Module *ecore_audio_tone_init(void); | 187 | Ecore_Audio_Module *ecore_audio_tone_init(void); |
196 | void ecore_audio_tone_shutdown(void); | 188 | void ecore_audio_tone_shutdown(void); |
197 | 189 | ||