Remove all config.h includes from header files, add it to each source file and remove...
[platform/upstream/gstreamer.git] / ext / sndfile / gstsf.c
1 /* GStreamer libsndfile plugin
2  * Copyright (C) 2003 Andy Wingo <wingo at pobox dot com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <string.h>
26 #include <gst/gst.h>
27
28 #include <gst/audio/audio.h>
29
30 #include "gstsf.h"
31
32
33 static GstElementDetails sfsrc_details = {
34   "Sndfile Source",
35   "Source/Audio",
36   "Read audio streams from disk using libsndfile",
37   "Andy Wingo <wingo at pobox dot com>",
38 };
39
40 static GstElementDetails sfsink_details = {
41   "Sndfile Sink",
42   "Sink/Audio",
43   "Write audio streams to disk using libsndfile",
44   "Andy Wingo <wingo at pobox dot com>",
45 };
46
47 enum {
48   ARG_0,
49   ARG_LOCATION,
50   ARG_MAJOR_TYPE,
51   ARG_MINOR_TYPE,
52   ARG_LOOP,
53   ARG_CREATE_PADS
54 };
55
56 GST_PAD_TEMPLATE_FACTORY (sf_src_factory,
57   "src%d",
58   GST_PAD_SRC,
59   GST_PAD_REQUEST,
60   gst_caps_new (
61     "sf_src",
62     "audio/x-raw-float",
63     GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
64   )
65 );
66
67 GST_PAD_TEMPLATE_FACTORY (sf_sink_factory,
68   "sink%d",
69   GST_PAD_SINK,
70   GST_PAD_REQUEST,
71   gst_caps_new (
72     "sf_sink",
73     "audio/x-raw-float",
74     GST_AUDIO_FLOAT_STANDARD_PAD_TEMPLATE_PROPS
75   )
76 );
77
78 #define GST_TYPE_SF_MAJOR_TYPES (gst_sf_major_types_get_type())
79 static GType
80 gst_sf_major_types_get_type (void) 
81 {
82   static GType sf_major_types_type = 0;
83   static GEnumValue *sf_major_types = NULL;
84   
85   if (!sf_major_types_type) 
86   {
87     SF_FORMAT_INFO format_info;
88     int k, count ;
89
90     sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int)) ;
91
92     sf_major_types = g_new0 (GEnumValue, count + 1);
93     
94     for (k = 0 ; k < count ; k++) {
95       format_info.format = k ;
96       sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info));
97       sf_major_types[k].value = format_info.format;
98       sf_major_types[k].value_name = g_strdup (format_info.name);
99       sf_major_types[k].value_nick = g_strdup (format_info.extension);
100
101       /* Irritatingly enough, there exist major_types with the same extension. Let's
102          just hope that sndfile gives us the list in alphabetical order, as it
103          currently does. */
104       if (k > 0 && strcmp (sf_major_types[k].value_nick, sf_major_types[k-1].value_nick) == 0) {
105         g_free (sf_major_types[k].value_nick);
106         sf_major_types[k].value_nick = g_strconcat (sf_major_types[k-1].value_nick, "-",
107                                                     sf_major_types[k].value_name, NULL);
108         g_strcanon (sf_major_types[k].value_nick,
109                     G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
110       }
111     }
112
113     sf_major_types_type = g_enum_register_static ("GstSndfileMajorTypes", sf_major_types);
114   }
115   return sf_major_types_type;
116 }
117
118 #define GST_TYPE_SF_MINOR_TYPES (gst_sf_minor_types_get_type())
119 static GType
120 gst_sf_minor_types_get_type (void) 
121 {
122   static GType sf_minor_types_type = 0;
123   static GEnumValue *sf_minor_types = NULL;
124   
125   if (!sf_minor_types_type) 
126   {
127     SF_FORMAT_INFO format_info;
128     int k, count ;
129
130     sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int)) ;
131
132     sf_minor_types = g_new0 (GEnumValue, count + 1);
133     
134     for (k = 0 ; k < count ; k++) {
135       format_info.format = k ;
136       sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info));
137       sf_minor_types[k].value = format_info.format;
138       sf_minor_types[k].value_name = g_strdup (format_info.name);
139       sf_minor_types[k].value_nick = g_ascii_strdown (format_info.name, -1);
140       g_strcanon (sf_minor_types[k].value_nick, G_CSET_a_2_z G_CSET_DIGITS "-", '-');
141     }
142
143     sf_minor_types_type = g_enum_register_static ("GstSndfileMinorTypes", sf_minor_types);
144   }
145   return sf_minor_types_type;
146 }
147
148 static void             gst_sfsrc_base_init    (gpointer g_class);
149 static void             gst_sfsink_base_init   (gpointer g_class);
150 static void             gst_sf_class_init       (GstSFClass *klass);
151 static void             gst_sf_init             (GstSF *this);
152 static void             gst_sf_dispose          (GObject *object);
153 static void             gst_sf_set_property     (GObject *object, guint prop_id,
154                                                  const GValue *value, GParamSpec *pspec);
155 static void             gst_sf_get_property     (GObject *object, guint prop_id, 
156                                                  GValue *value, GParamSpec *pspec);
157
158 static GstClock*        gst_sf_get_clock        (GstElement *element);
159 static void             gst_sf_set_clock        (GstElement *element, GstClock *clock);
160 static GstPad*          gst_sf_request_new_pad  (GstElement *element, GstPadTemplate *templ,
161                                                  const gchar *unused);
162 static void             gst_sf_release_request_pad (GstElement *element, GstPad *pad);
163 static GstElementStateReturn gst_sf_change_state (GstElement *element);
164
165 static GstPadLinkReturn gst_sf_link             (GstPad *pad, GstCaps *caps);
166
167 static void             gst_sf_loop             (GstElement *element);
168
169 static GstClockTime     gst_sf_get_time         (GstClock *clock, gpointer data);
170
171 static gboolean         gst_sf_open_file        (GstSF *this);
172 static void             gst_sf_close_file       (GstSF *this);
173
174 static GstElementClass *parent_class = NULL;
175
176 GST_DEBUG_CATEGORY_STATIC (gstsf_debug);
177 #define INFO(...) \
178     GST_CAT_LEVEL_LOG (gstsf_debug, GST_LEVEL_INFO, NULL, __VA_ARGS__)
179 #define INFO_OBJ(obj,...) \
180     GST_CAT_LEVEL_LOG (gstsf_debug, GST_LEVEL_INFO, obj, __VA_ARGS__)
181
182 GType
183 gst_sf_get_type (void) 
184 {
185   static GType sf_type = 0;
186
187   if (!sf_type) {
188     static const GTypeInfo sf_info = {
189       sizeof (GstSFClass),      NULL,
190       NULL,
191       (GClassInitFunc)NULL, /* don't even initialize the class */
192       NULL,
193       NULL,
194       sizeof (GstSF),
195       0,
196       (GInstanceInitFunc)NULL /* abstract base class */
197     };
198     sf_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSF", &sf_info, 0);
199   }
200   return sf_type;
201 }
202
203 GType
204 gst_sfsrc_get_type (void) 
205 {
206   static GType sfsrc_type = 0;
207
208   if (!sfsrc_type) {
209     static const GTypeInfo sfsrc_info = {
210       sizeof (GstSFClass),
211       gst_sfsrc_base_init,
212       NULL,
213       (GClassInitFunc) gst_sf_class_init,
214       NULL,
215       NULL,
216       sizeof (GstSF),
217       0,
218       (GInstanceInitFunc) gst_sf_init,
219     };
220     sfsrc_type = g_type_register_static (GST_TYPE_SF, "GstSFSrc", &sfsrc_info, 0);
221   }
222   return sfsrc_type;
223 }
224
225 GType
226 gst_sfsink_get_type (void) 
227 {
228   static GType sfsink_type = 0;
229
230   if (!sfsink_type) {
231     static const GTypeInfo sfsink_info = {
232       sizeof (GstSFClass),
233       gst_sfsink_base_init,
234       NULL,
235       (GClassInitFunc) gst_sf_class_init,
236       NULL,
237       NULL,
238       sizeof (GstSF),
239       0,
240       (GInstanceInitFunc) gst_sf_init,
241     };
242     sfsink_type = g_type_register_static (GST_TYPE_SF, "GstSFSink", &sfsink_info, 0);
243   }
244   return sfsink_type;
245 }
246
247 static void
248 gst_sfsrc_base_init (gpointer g_class)
249 {
250   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
251
252   gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (sf_src_factory));
253   gst_element_class_set_details (element_class, &sfsrc_details);
254 }
255
256 static void
257 gst_sfsink_base_init (gpointer g_class)
258 {
259   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
260
261   gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (sf_sink_factory));
262   gst_element_class_set_details (element_class, &sfsink_details);
263 }
264
265 static void
266 gst_sf_class_init (GstSFClass *klass) 
267 {
268   GObjectClass *gobject_class;
269   GstElementClass *gstelement_class;
270   GParamSpec *pspec;
271
272   gobject_class = (GObjectClass*)klass;
273   gstelement_class = (GstElementClass*)klass;
274
275   /* although this isn't really the parent class, that's ok; GstSF doesn't
276      override any methods */
277   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
278
279   gst_element_class_install_std_props (gstelement_class, "location",
280                                        ARG_LOCATION, G_PARAM_READWRITE, NULL);
281   pspec = g_param_spec_enum
282     ("major-type", "Major type", "Major output type", GST_TYPE_SF_MAJOR_TYPES,
283      SF_FORMAT_WAV, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
284   g_object_class_install_property (gobject_class, ARG_MAJOR_TYPE, pspec);
285   pspec = g_param_spec_enum
286     ("minor-type", "Minor type", "Minor output type", GST_TYPE_SF_MINOR_TYPES,
287      SF_FORMAT_FLOAT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
288   g_object_class_install_property (gobject_class, ARG_MINOR_TYPE, pspec);
289
290   if (G_TYPE_FROM_CLASS (klass) == GST_TYPE_SFSRC) {
291     pspec = g_param_spec_boolean ("loop", "Loop?", "Loop the output?",
292                                   FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
293     g_object_class_install_property (gobject_class, ARG_LOOP, pspec);
294     pspec = g_param_spec_boolean ("create-pads", "Create pads?", "Create one pad for each channel in the sound file?",
295                                   TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
296     g_object_class_install_property (gobject_class, ARG_CREATE_PADS, pspec);
297   }
298  
299   gobject_class->dispose = gst_sf_dispose;
300   gobject_class->set_property = gst_sf_set_property;
301   gobject_class->get_property = gst_sf_get_property;
302
303   gstelement_class->get_clock = gst_sf_get_clock;
304   gstelement_class->set_clock = gst_sf_set_clock;
305   gstelement_class->change_state = gst_sf_change_state;
306   gstelement_class->request_new_pad = gst_sf_request_new_pad;
307   gstelement_class->release_pad = gst_sf_release_request_pad;
308 }
309
310 static void 
311 gst_sf_init (GstSF *this) 
312 {
313   gst_element_set_loop_function (GST_ELEMENT (this), gst_sf_loop);
314   this->provided_clock = gst_audio_clock_new ("sfclock", gst_sf_get_time, this);
315   gst_object_set_parent (GST_OBJECT (this->provided_clock), GST_OBJECT (this));
316 }
317
318 static void
319 gst_sf_dispose (GObject *object)
320 {
321   GstSF *this = (GstSF*)object;
322
323   gst_object_unparent (GST_OBJECT (this->provided_clock));
324
325   G_OBJECT_CLASS (parent_class)->dispose (object);
326 }
327
328 static void
329 gst_sf_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
330 {
331   GstSF *this = GST_SF (object);
332
333   switch (prop_id) {
334     case ARG_LOCATION:
335       if (GST_FLAG_IS_SET (object, GST_SF_OPEN))
336         gst_sf_close_file (this);
337       if (this->filename)
338         g_free (this->filename);
339
340       if (g_value_get_string (value))
341         this->filename = g_strdup (g_value_get_string (value));
342       else
343         this->filename = NULL;
344
345       if (this->filename)
346         gst_sf_open_file (this);
347       break;
348
349     case ARG_MAJOR_TYPE:
350       this->format_major = g_value_get_enum (value);
351       break;
352
353     case ARG_MINOR_TYPE:
354       this->format_subtype = g_value_get_enum (value);
355       break;
356
357     case ARG_LOOP:
358       this->loop = g_value_get_boolean (value);
359       break;
360
361     case ARG_CREATE_PADS:
362       this->create_pads = g_value_get_boolean (value);
363       if (this->file && this->create_pads) {
364         int i;
365         for (i=g_list_length (this->channels); i<this->numchannels; i++)
366           gst_element_get_request_pad ((GstElement*)this, "src%d");
367       }
368       break;
369
370     default:
371       break;
372   }
373 }
374
375 static void   
376 gst_sf_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
377 {
378   GstSF *this = GST_SF (object);
379   
380   switch (prop_id) {
381     case ARG_LOCATION:
382       g_value_set_string (value, this->filename);
383       break;
384
385     case ARG_MAJOR_TYPE:
386       g_value_set_enum (value, this->format_major);
387       break;
388
389     case ARG_MINOR_TYPE:
390       g_value_set_enum (value, this->format_subtype);
391       break;
392
393     case ARG_LOOP:
394       g_value_set_boolean (value, this->loop);
395       break;
396
397     case ARG_CREATE_PADS:
398       g_value_set_boolean (value, this->create_pads);
399       break;
400
401     default:
402       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
403       break;
404   }
405 }
406
407 static GstClock*
408 gst_sf_get_clock (GstElement *element)
409 {
410   GstSF *this = GST_SF (element);
411
412   return this->provided_clock;
413 }
414
415 static void
416 gst_sf_set_clock (GstElement *element, GstClock *clock)
417 {
418   GstSF *this = GST_SF (element);
419   
420   this->clock = clock;
421 }
422
423 static GstClockTime 
424 gst_sf_get_time (GstClock *clock, gpointer data) 
425 {
426   GstSF *this = GST_SF (data);
427
428   return this->time;
429 }
430
431 static GstElementStateReturn
432 gst_sf_change_state (GstElement *element)
433 {
434   GstSF *this = GST_SF (element);
435
436   switch (GST_STATE_TRANSITION (element)) {
437     case GST_STATE_NULL_TO_READY:
438       break;
439     case GST_STATE_READY_TO_PAUSED:
440       break;
441     case GST_STATE_PAUSED_TO_PLAYING:
442       gst_audio_clock_set_active (GST_AUDIO_CLOCK (this->provided_clock), TRUE);
443       break;
444     case GST_STATE_PLAYING_TO_PAUSED:
445       gst_audio_clock_set_active (GST_AUDIO_CLOCK (this->provided_clock), FALSE);
446       break;
447     case GST_STATE_PAUSED_TO_READY:
448       break;
449     case GST_STATE_READY_TO_NULL:
450       if (GST_FLAG_IS_SET (this, GST_SF_OPEN))
451         gst_sf_close_file (this);
452       break;
453   }
454
455   if (GST_ELEMENT_CLASS (parent_class)->change_state)
456     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
457
458   return GST_STATE_SUCCESS;
459 }
460
461 static GstPad*
462 gst_sf_request_new_pad (GstElement *element, GstPadTemplate *templ,
463                         const gchar *unused) 
464 {
465   gchar *name;
466   GstSF *this;
467   GstSFChannel *channel;
468
469   this = GST_SF (element);
470   channel = g_new0 (GstSFChannel, 1);
471   
472   if (templ->direction == GST_PAD_SINK) {
473     /* we have an SFSink */
474     name = g_strdup_printf ("sink%d", this->channelcount);
475     this->numchannels++;
476     if (this->file) {
477       gst_sf_close_file (this);
478       gst_sf_open_file (this);
479     }
480   } else {
481     /* we have an SFSrc */
482     name = g_strdup_printf ("src%d", this->channelcount);
483   }
484   
485   channel->pad = gst_pad_new_from_template (templ, name);
486   gst_element_add_pad (GST_ELEMENT (this), channel->pad);
487   gst_pad_set_link_function (channel->pad, gst_sf_link);
488   
489   this->channels = g_list_append (this->channels, channel);
490   this->channelcount++;
491   
492   INFO_OBJ (element, "added pad %s\n", name);
493
494   g_free (name);
495   return channel->pad;
496 }
497
498 static void
499 gst_sf_release_request_pad (GstElement *element, GstPad *pad)
500 {
501   GstSF *this;
502   GstSFChannel *channel = NULL;
503   GList *l;
504
505   this = GST_SF (element);
506   
507   if (GST_STATE (element) == GST_STATE_PLAYING) {
508     g_warning ("You can't release a request pad if the element is PLAYING, sorry.");
509     return;
510   }
511
512   for (l=this->channels; l; l=l->next) {
513     if (GST_SF_CHANNEL (l)->pad == pad) {
514       channel = GST_SF_CHANNEL (l);
515       break;
516     }
517   }
518
519   g_return_if_fail (channel != NULL);
520
521   INFO_OBJ (element, "Releasing request pad %s", GST_PAD_NAME (channel->pad));
522
523   if (GST_FLAG_IS_SET (element, GST_SF_OPEN))
524     gst_sf_close_file (this);
525
526   gst_element_remove_pad (element, channel->pad);
527   this->channels = g_list_remove (this->channels, channel);
528   this->numchannels--;
529   g_free (channel);
530 }
531
532 static GstPadLinkReturn
533 gst_sf_link (GstPad *pad, GstCaps *caps)
534 {
535   GstSF *this = (GstSF*)GST_OBJECT_PARENT (pad);
536   
537   if (GST_CAPS_IS_FIXED (caps)) {
538     gst_caps_get_int (caps, "rate", &this->rate);
539     gst_caps_get_int (caps, "buffer-frames", &this->buffer_frames);
540
541     INFO_OBJ (this, "linked pad %s:%s with fixed caps, frames=%d, rate=%d",
542               GST_DEBUG_PAD_NAME (pad), this->rate, this->buffer_frames);
543
544     if (this->numchannels) {
545       /* we can go ahead and allocate our buffer */
546       if (this->buffer)
547         g_free (this->buffer);
548       this->buffer = g_malloc (this->numchannels * this->buffer_frames * sizeof (float));
549       memset (this->buffer, 0, this->numchannels * this->buffer_frames * sizeof (float));
550     }
551     return GST_PAD_LINK_OK;
552   }
553
554   return GST_PAD_LINK_DELAYED;
555 }
556
557 static gboolean
558 gst_sf_open_file (GstSF *this)
559 {
560   int mode;
561   SF_INFO info;
562   
563   g_return_val_if_fail (!GST_FLAG_IS_SET (this, GST_SF_OPEN), FALSE);
564
565   this->time = 0;
566
567   if (!this->filename) {
568     gst_element_error (GST_ELEMENT (this), "sndfile: 'location' was not set");
569     return FALSE;
570   }
571     
572   if (GST_IS_SFSRC (this)) {
573     mode = SFM_READ;
574     info.format = 0;
575   } else {
576     if (!this->rate) {
577       INFO_OBJ (this, "Not opening %s yet because caps are not set", this->filename);
578       return FALSE;
579     } else if (!this->numchannels) {
580       INFO_OBJ (this, "Not opening %s yet because we have no input channels", this->filename);
581       return FALSE;
582     }
583
584     mode = SFM_WRITE;
585     this->format = this->format_major | this->format_subtype;
586     info.samplerate = this->rate;
587     info.channels = this->numchannels;
588     info.format = this->format;
589
590     INFO_OBJ (this, "Opening %s with rate %d, %d channels, format 0x%x",
591               this->filename, info.samplerate, info.channels, info.format);
592
593     if (!sf_format_check (&info)) {
594       gst_element_error (GST_ELEMENT (this),
595                          g_strdup_printf ("Input parameters (rate:%d, channels:%d, format:0x%x) invalid",
596                                           info.samplerate, info.channels, info.format));
597       return FALSE;
598     }
599   }
600
601   this->file = sf_open (this->filename, mode, &info);
602
603   if (!this->file) {
604     gst_element_error (GST_ELEMENT (this),
605                        g_strdup_printf ("could not open file \"%s\": %s",
606                                         this->filename, sf_strerror (NULL)));
607     return FALSE;
608   }
609
610   if (GST_IS_SFSRC (this)) {
611     GList *l = NULL;
612     /* the number of channels in the file can be different than the number of
613      * pads */
614     this->numchannels = info.channels;
615     this->rate = info.samplerate;
616
617     if (this->create_pads) {
618       int i;
619       for (i=g_list_length (this->channels); i<this->numchannels; i++)
620         gst_element_get_request_pad ((GstElement*)this, "src%d");
621     }
622   
623     for (l=this->channels; l; l=l->next)
624       /* queue the need to set caps */
625       GST_SF_CHANNEL (l)->caps_set = FALSE;
626   }
627
628   GST_FLAG_SET (this, GST_SF_OPEN);
629
630   return TRUE;
631 }
632
633 static void
634 gst_sf_close_file (GstSF *this)
635 {
636   int err = 0;
637
638   g_return_if_fail (GST_FLAG_IS_SET (this, GST_SF_OPEN));
639
640   INFO_OBJ (this, "Closing file %s", this->filename);
641
642   if ((err = sf_close (this->file)))
643     gst_element_error (GST_ELEMENT (this),
644                        g_strdup_printf ("sndfile: could not close file \"%s\": %s",
645                                         this->filename, sf_error_number (err)));
646   else
647     GST_FLAG_UNSET (this, GST_SF_OPEN);
648
649   this->file = NULL;
650   if (this->buffer)
651     g_free (this->buffer);
652   this->buffer = NULL;
653 }
654
655 static void 
656 gst_sf_loop (GstElement *element) 
657 {
658   GstSF *this;
659   GList *l = NULL;
660
661   this = (GstSF*)element;
662   
663   if (this->channels == NULL) {
664     gst_element_error (element, "You must connect at least one pad to sndfile elements.");
665     return;
666   }
667
668   if (GST_IS_SFSRC (this)) {
669     sf_count_t read;
670     gint i, j;
671     int eos = 0;
672     int buffer_frames = this->buffer_frames;
673     int nchannels = this->numchannels;
674     GstSFChannel *channel = NULL;
675     gfloat *data;
676     gfloat *buf = this->buffer;
677     GstBuffer *out;
678
679     if (!GST_FLAG_IS_SET (this, GST_SF_OPEN))
680       if (!gst_sf_open_file (this))
681         return; /* we've already set gst_element_error */
682
683     if (buffer_frames == 0) {
684       /* we have to set the caps later */
685       buffer_frames = this->buffer_frames = 1024;
686     }
687     if (buf == NULL) {
688       buf = this->buffer = g_malloc (this->numchannels * this->buffer_frames * sizeof (float));
689       memset (this->buffer, 0, this->numchannels * this->buffer_frames * sizeof (float));
690     }
691
692     read = sf_readf_float (this->file, buf, buffer_frames);
693     if (read < buffer_frames)
694       eos = 1;
695
696     if (read)
697       for (i=0,l=this->channels; l; l=l->next,i++) {
698         channel = GST_SF_CHANNEL (l);
699
700         /* don't push on disconnected pads -- useful for ::create-pads=TRUE*/
701         if (!GST_PAD_PEER (channel->pad))
702           continue;
703
704         if (!channel->caps_set) {
705           GstCaps *caps = GST_PAD_CAPS (GST_SF_CHANNEL (l)->pad);
706           if (!caps)
707             caps = gst_caps_copy
708               (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (GST_SF_CHANNEL (l)->pad)));
709           gst_caps_set (caps, "rate", GST_PROPS_INT (this->rate), NULL);
710           gst_caps_set (caps, "buffer-frames", GST_PROPS_INT (this->buffer_frames), NULL);
711           if (!gst_pad_try_set_caps (GST_SF_CHANNEL (l)->pad, caps)) {
712             gst_element_error (GST_ELEMENT (this),
713                                g_strdup_printf ("Opened file with sample rate %d, but could not set caps",
714                                                 this->rate));
715             gst_sf_close_file (this);
716             return;
717           }
718           channel->caps_set = TRUE;
719         }
720
721         out = gst_buffer_new_and_alloc (read * sizeof(float));
722         data = (gfloat*)GST_BUFFER_DATA (out);
723         for (j=0; j<read; j++)
724           data[j] = buf[j * nchannels + i % nchannels];
725         gst_pad_push (channel->pad, GST_DATA (out));
726       }
727
728     this->time += read * (GST_SECOND / this->rate);
729     gst_audio_clock_update_time ((GstAudioClock*)this->provided_clock, this->time);
730
731     if (eos) {
732       if (this->loop) {
733         sf_seek (this->file, (sf_count_t)0, SEEK_SET);
734         eos = 0;
735       } else {
736         for (l=this->channels; l; l=l->next)
737           gst_pad_push (GST_SF_CHANNEL (l)->pad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
738         gst_element_set_eos (element);
739       }
740     }
741   } else {
742     sf_count_t written, num_to_write;
743     gint i, j;
744     int buffer_frames = this->buffer_frames;
745     int nchannels = this->numchannels;
746     GstSFChannel *channel = NULL;
747     gfloat *data;
748     gfloat *buf = this->buffer;
749     GstBuffer *in;
750
751     /* the problem: we can't allocate a buffer for pulled data before caps is
752      * set, and we can't open the file without the sample rate from the
753      * caps... */
754
755     num_to_write = buffer_frames;
756
757     INFO_OBJ (this, "looping, buffer_frames=%d, nchannels=%d", buffer_frames, nchannels);
758
759     for (i=0,l=this->channels; l; l=l->next,i++) {
760       channel = GST_SF_CHANNEL (l);
761       
762       in = GST_BUFFER (gst_pad_pull (channel->pad));
763
764       if (buffer_frames == 0) {
765         /* pulling a buffer from the pad should have caused capsnego to occur,
766            which then would set this->buffer_frames to a new value */
767         buffer_frames = this->buffer_frames;
768         if (buffer_frames == 0) {
769           gst_element_error (element, "Caps were never set, bailing...");
770           return;
771         }
772         buf = this->buffer;
773         num_to_write = buffer_frames;
774       }
775
776       if (!GST_FLAG_IS_SET (this, GST_SF_OPEN))
777         if (!gst_sf_open_file (this))
778           return; /* we've already set gst_element_error */
779
780       if (GST_IS_EVENT (in)) {
781         num_to_write = 0;
782       } else {
783         data = (gfloat*)GST_BUFFER_DATA (in);
784         num_to_write = MIN (num_to_write, GST_BUFFER_SIZE (in) / sizeof (gfloat));
785         for (j=0; j<num_to_write; j++)
786           buf[j * nchannels + i % nchannels] = data[j];
787       }
788       gst_data_unref ((GstData*)in);
789     }
790
791     if (num_to_write) {
792       written = sf_writef_float (this->file, buf, num_to_write);
793       if (written != num_to_write)
794         gst_element_error (element, "Error writing file: %s", sf_strerror (this->file));
795     }
796
797     this->time += num_to_write * (GST_SECOND / this->rate);
798     gst_audio_clock_update_time ((GstAudioClock*)this->provided_clock, this->time);
799
800     if (num_to_write != buffer_frames)
801       gst_element_set_eos (element);
802   }
803 }
804
805 static gboolean
806 plugin_init (GstPlugin *plugin)
807 {
808   if (!gst_library_load ("gstaudio"))
809     return FALSE;
810
811   GST_DEBUG_CATEGORY_INIT (gstsf_debug, "sf",
812                            GST_DEBUG_FG_WHITE | GST_DEBUG_BG_GREEN | GST_DEBUG_BOLD,
813                            "libsndfile plugin");
814
815   if (!gst_element_register (plugin, "sfsrc", GST_RANK_NONE, GST_TYPE_SFSRC))
816     return FALSE;
817
818   if (!gst_element_register (plugin, "sfsink", GST_RANK_NONE, GST_TYPE_SFSINK))
819     return FALSE;
820
821   return TRUE;
822 }
823
824 GST_PLUGIN_DEFINE (
825   GST_VERSION_MAJOR,
826   GST_VERSION_MINOR,
827   "gstsf",
828   "Sndfile plugin library",
829   plugin_init,
830   VERSION,
831   "LGPL",
832   GST_COPYRIGHT,
833   GST_PACKAGE,
834   GST_ORIGIN)