1 /* This file is part of GEGL
2  *
3  * GEGL is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 3 of the License, or (at your option) any later version.
7  *
8  * GEGL is distributed in the hope that it will be useful,
9  * Lesser General Public License for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public
12  * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
13  */
14 
15 #include "config.h"
16 
17 #include <string.h>
18 
19 #include <glib-object.h>
20 
21 #include "gegl.h"
22 #include "gegl-types-internal.h"
23 #include "gegl-audio-fragment.h"
24 
25 enum
26 {
27   PROP_0,
28   PROP_STRING
29 };
30 
31 struct _GeglAudioFragmentPrivate
32 {
33   int     max_samples;
34   int     sample_count;
35   int     channels;
36   int     channel_layout;
37   int     sample_rate;
38   int     pos;
39 };
40 
41 static void      set_property (GObject      *gobject,
42                                guint         prop_id,
43                                const GValue *value,
44                                GParamSpec   *pspec);
45 static void      get_property (GObject    *gobject,
46                                guint       prop_id,
47                                GValue     *value,
48                                GParamSpec *pspec);
49 
G_DEFINE_TYPE_WITH_PRIVATE(GeglAudioFragment,gegl_audio_fragment,G_TYPE_OBJECT)50 G_DEFINE_TYPE_WITH_PRIVATE (GeglAudioFragment, gegl_audio_fragment,
51                             G_TYPE_OBJECT)
52 
53 
54 static void deallocate_data (GeglAudioFragment *audio)
55 {
56   int i;
57   for (i = 0; i < GEGL_MAX_AUDIO_CHANNELS; i++)
58   {
59     g_clear_pointer (&audio->data[i], g_free);
60   }
61 }
62 
allocate_data(GeglAudioFragment * audio)63 static void allocate_data (GeglAudioFragment *audio)
64 {
65   int i;
66   deallocate_data (audio);
67   if (audio->priv->channels <= 0 ||
68       audio->priv->max_samples <= 0)
69     return;
70   for (i = 0; i < audio->priv->channels; i++)
71   {
72     audio->data[i] = g_malloc (sizeof (float) * audio->priv->max_samples);
73   }
74 }
75 
76 #define GEGL_MAX_AUDIO_SAMPLES  2400
77 
78 static void
gegl_audio_fragment_init(GeglAudioFragment * self)79 gegl_audio_fragment_init (GeglAudioFragment *self)
80 {
81   self->priv = gegl_audio_fragment_get_instance_private ((self));
82   self->priv->max_samples = GEGL_MAX_AUDIO_SAMPLES;
83   allocate_data (self);
84 }
85 
86 static void
gegl_audio_fragment_finalize(GObject * self)87 gegl_audio_fragment_finalize (GObject *self)
88 {
89   GeglAudioFragment *audio = (void*)self;
90   deallocate_data (audio);
91 }
92 
93 static void
gegl_audio_fragment_class_init(GeglAudioFragmentClass * klass)94 gegl_audio_fragment_class_init (GeglAudioFragmentClass *klass)
95 {
96   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
97 
98   gobject_class->set_property = set_property;
99   gobject_class->get_property = get_property;
100   gobject_class->finalize = gegl_audio_fragment_finalize;
101 
102   g_object_class_install_property (gobject_class, PROP_STRING,
103                                    g_param_spec_string ("string",
104                                                         "String",
105                                                         "A String representation of the GeglAudioFragment",
106                                                         "",
107                                                         G_PARAM_READWRITE |
108                                                         G_PARAM_STATIC_STRINGS));
109 }
110 
111 static void
set_property(GObject * gobject,guint property_id,const GValue * value,GParamSpec * pspec)112 set_property (GObject      *gobject,
113               guint         property_id,
114               const GValue *value,
115               GParamSpec   *pspec)
116 {
117   //GeglAudioFragment *audio = GEGL_AUDIO_FRAGMENT (gobject);
118 
119   switch (property_id)
120     {
121       default:
122         G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, property_id, pspec);
123         break;
124     }
125 }
126 
127 static void
get_property(GObject * gobject,guint property_id,GValue * value,GParamSpec * pspec)128 get_property (GObject    *gobject,
129               guint       property_id,
130               GValue     *value,
131               GParamSpec *pspec)
132 {
133   //GeglAudioFragment *audio = GEGL_AUDIO_FRAGMENT (gobject);
134 
135   switch (property_id)
136     {
137       default:
138         G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, property_id, pspec);
139         break;
140     }
141 }
142 
143 
144 GeglAudioFragment *
gegl_audio_fragment_new(int sample_rate,int channels,int channel_layout,int max_samples)145 gegl_audio_fragment_new (int sample_rate, int channels, int channel_layout, int max_samples)
146 {
147   GeglAudioFragment *ret = g_object_new (GEGL_TYPE_AUDIO_FRAGMENT, NULL);
148   gegl_audio_fragment_set_sample_rate (ret, sample_rate);
149   gegl_audio_fragment_set_channel_layout (ret, channel_layout);
150   gegl_audio_fragment_set_max_samples (ret, max_samples);
151   gegl_audio_fragment_set_channels (ret, channels);
152   return ret;
153 }
154 
155 void
gegl_audio_fragment_set_max_samples(GeglAudioFragment * audio,int max_samples)156 gegl_audio_fragment_set_max_samples (GeglAudioFragment *audio,
157                                      int                max_samples)
158 {
159   if (audio->priv->max_samples == max_samples)
160     return;
161   audio->priv->max_samples = max_samples;
162   allocate_data (audio);
163 }
164 
165 void
gegl_audio_fragment_set_sample_rate(GeglAudioFragment * audio,int sample_rate)166 gegl_audio_fragment_set_sample_rate (GeglAudioFragment *audio,
167                                      int                sample_rate)
168 {
169   audio->priv->sample_rate = sample_rate;
170 }
171 
172 void
gegl_audio_fragment_set_channels(GeglAudioFragment * audio,int channels)173 gegl_audio_fragment_set_channels (GeglAudioFragment *audio,
174                                   int                channels)
175 {
176   if (audio->priv->channels == channels)
177     return;
178   audio->priv->channels = channels;
179   allocate_data (audio);
180 }
181 
182 void
gegl_audio_fragment_set_channel_layout(GeglAudioFragment * audio,int channel_layout)183 gegl_audio_fragment_set_channel_layout (GeglAudioFragment *audio,
184                                         int                channel_layout)
185 {
186   audio->priv->channel_layout = channel_layout;
187 }
188 
189 void
gegl_audio_fragment_set_sample_count(GeglAudioFragment * audio,int samples)190 gegl_audio_fragment_set_sample_count (GeglAudioFragment *audio,
191                                       int                samples)
192 {
193   audio->priv->sample_count = samples;
194 }
195 
196 void
gegl_audio_fragment_set_pos(GeglAudioFragment * audio,int pos)197 gegl_audio_fragment_set_pos (GeglAudioFragment *audio,
198                              int                pos)
199 {
200   audio->priv->pos = pos;
201 }
202 
203 int
gegl_audio_fragment_get_max_samples(GeglAudioFragment * audio)204 gegl_audio_fragment_get_max_samples (GeglAudioFragment *audio)
205 {
206   return audio->priv->max_samples;
207 }
208 
209 
210 int
gegl_audio_fragment_get_sample_rate(GeglAudioFragment * audio)211 gegl_audio_fragment_get_sample_rate (GeglAudioFragment *audio)
212 {
213   return audio->priv->sample_rate;
214 }
215 
216 int
gegl_audio_fragment_get_channels(GeglAudioFragment * audio)217 gegl_audio_fragment_get_channels (GeglAudioFragment *audio)
218 {
219   return audio->priv->channels;
220 }
221 
222 int
gegl_audio_fragment_get_sample_count(GeglAudioFragment * audio)223 gegl_audio_fragment_get_sample_count (GeglAudioFragment *audio)
224 {
225   return audio->priv->sample_count;
226 }
227 
228 int
gegl_audio_fragment_get_pos(GeglAudioFragment * audio)229 gegl_audio_fragment_get_pos     (GeglAudioFragment *audio)
230 {
231   return audio->priv->pos;
232 }
233 
234 int
gegl_audio_fragment_get_channel_layout(GeglAudioFragment * audio)235 gegl_audio_fragment_get_channel_layout (GeglAudioFragment *audio)
236 {
237   return audio->priv->channel_layout;
238 }
239 
240 /* --------------------------------------------------------------------------
241  * A GParamSpec class to describe behavior of GeglAudioFragment as an object property
242  * follows.
243  * --------------------------------------------------------------------------
244  */
245 
246 #define GEGL_PARAM_AUDIO_FRAGMENT (obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEGL_TYPE_PARAM_AUDIO_FRAGMENT, GeglParamAudioFragment))
247 #define GEGL_IS_PARAM_AUDIO_FRAGMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEGL_TYPE_PARAM_AUDIO_FRAGMENT))
248 
249 typedef struct _GeglParamAudioFragment GeglParamAudioFragment;
250 
251 struct _GeglParamAudioFragment
252 {
253   GParamSpec parent_instance;
254 };
255 
256 static void
gegl_param_audio_fragment_init(GParamSpec * self)257 gegl_param_audio_fragment_init (GParamSpec *self)
258 {
259 }
260 
261 static void
gegl_param_audio_fragment_finalize(GParamSpec * self)262 gegl_param_audio_fragment_finalize (GParamSpec *self)
263 {
264   //GeglParamAudioFragment  *param_audio  = GEGL_PARAM_AUDIO (self);
265   GParamSpecClass *parent_class = g_type_class_peek (g_type_parent (GEGL_TYPE_PARAM_AUDIO_FRAGMENT));
266 
267   parent_class->finalize (self);
268 }
269 
270 static void
gegl_param_audio_fragment_set_default(GParamSpec * param_spec,GValue * value)271 gegl_param_audio_fragment_set_default (GParamSpec *param_spec,
272                               GValue     *value)
273 {
274   //GeglParamAudioFragment *gegl_audio_fragment = GEGL_PARAM_AUDIO (param_spec);
275 }
276 
277 GType
gegl_param_audio_fragment_get_type(void)278 gegl_param_audio_fragment_get_type (void)
279 {
280   static GType param_audio_fragment_type = 0;
281 
282   if (G_UNLIKELY (param_audio_fragment_type == 0))
283     {
284       static GParamSpecTypeInfo param_audio_fragment_type_info = {
285         sizeof (GeglParamAudioFragment),
286         0,
287         gegl_param_audio_fragment_init,
288         0,
289         gegl_param_audio_fragment_finalize,
290         gegl_param_audio_fragment_set_default,
291         NULL,
292         NULL
293       };
294       param_audio_fragment_type_info.value_type = GEGL_TYPE_AUDIO_FRAGMENT;
295 
296       param_audio_fragment_type = g_param_type_register_static ("GeglParamAudioFragment",
297                                                        &param_audio_fragment_type_info);
298     }
299 
300   return param_audio_fragment_type;
301 }
302 
303 GParamSpec *
gegl_param_spec_audio_fragment(const gchar * name,const gchar * nick,const gchar * blurb,GParamFlags flags)304 gegl_param_spec_audio_fragment (const gchar *name,
305                                 const gchar *nick,
306                                 const gchar *blurb,
307                                 GParamFlags  flags)
308 {
309   GeglParamAudioFragment *param_audio;
310 
311   param_audio = g_param_spec_internal (GEGL_TYPE_PARAM_AUDIO_FRAGMENT,
312                                        name, nick, blurb, flags);
313 
314   return G_PARAM_SPEC (param_audio);
315 }
316 
317 
318