3a723b891b72abc113835c1f6c8fa66136c36fa7
1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 */
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "audio.h"
26 int
27 gst_audio_frame_byte_size (GstPad* pad)
28 {
29 /* calculate byte size of an audio frame
30 * this should be moved closer to the gstreamer core
31 * and be implemented for every mime type IMO
32 * returns -1 if there's an error (to avoid division by zero),
33 * or the byte size if everything's ok
34 */
36 int width = 0;
37 int channels = 0;
39 GstCaps *caps = NULL;
41 /* get caps of pad */
42 caps = GST_PAD_CAPS (pad);
44 if (caps == NULL)
45 {
46 /* ERROR: could not get caps of pad */
47 g_warning ("gstaudio: could not get caps of pad %s:%s\n",
48 GST_ELEMENT_NAME (gst_pad_get_parent (pad)), GST_PAD_NAME (pad));
49 return 0;
50 }
52 gst_caps_get_int (caps, "width", &width);
53 gst_caps_get_int (caps, "channels", &channels);
54 return (width / 8) * channels;
55 }
57 long
58 gst_audio_frame_length (GstPad* pad, GstBuffer* buf)
59 /* calculate length of buffer in frames
60 * this should be moved closer to the gstreamer core
61 * and be implemented for every mime type IMO
62 * returns 0 if there's an error, or the number of frames if everything's ok
63 */
64 {
65 int frame_byte_size = 0;
67 frame_byte_size = gst_audio_frame_byte_size (pad);
68 if (frame_byte_size == 0)
69 /* error */
70 return 0;
71 /* FIXME: this function assumes the buffer size to be a whole multiple
72 * of the frame byte size
73 */
74 return GST_BUFFER_SIZE (buf) / frame_byte_size;
75 }
77 long
78 gst_audio_frame_rate (GstPad *pad)
79 /*
80 * calculate frame rate (based on caps of pad)
81 * returns 0 if failed, rate if success
82 */
83 {
84 GstCaps *caps = NULL;
85 gint rate;
87 /* get caps of pad */
88 caps = GST_PAD_CAPS (pad);
90 if (caps == NULL) {
91 /* ERROR: could not get caps of pad */
92 g_warning ("gstaudio: could not get caps of pad %s:%s\n",
93 GST_ELEMENT_NAME (gst_pad_get_parent (pad)), GST_PAD_NAME (pad));
94 return 0;
95 }
96 else {
97 gst_caps_get_int (caps, "rate", &rate);
98 return rate;
99 }
100 }
102 double
103 gst_audio_length (GstPad* pad, GstBuffer* buf)
104 {
105 /* calculate length in seconds
106 * of audio buffer buf
107 * based on capabilities of pad
108 */
110 long bytes = 0;
111 int width = 0;
112 int channels = 0;
113 int rate = 0;
115 double length;
117 GstCaps *caps = NULL;
119 g_assert (GST_IS_BUFFER (buf));
120 /* get caps of pad */
121 caps = GST_PAD_CAPS (pad);
122 if (caps == NULL)
123 {
124 /* ERROR: could not get caps of pad */
125 g_warning ("gstaudio: could not get caps of pad %s:%s\n",
126 GST_ELEMENT_NAME (gst_pad_get_parent (pad)), GST_PAD_NAME (pad));
127 length = 0.0;
128 }
129 else
130 {
131 bytes = GST_BUFFER_SIZE (buf);
132 gst_caps_get_int (caps, "width", &width);
133 gst_caps_get_int (caps, "channels", &channels);
134 gst_caps_get_int (caps, "rate", &rate);
136 g_assert (bytes != 0);
137 g_assert (width != 0);
138 g_assert (channels != 0);
139 g_assert (rate != 0);
140 length = (bytes * 8.0) / (double) (rate * channels * width);
141 }
142 /* g_print ("DEBUG: audio: returning length of %f\n", length); */
143 return length;
144 }
146 long
147 gst_audio_highest_sample_value (GstPad* pad)
148 /* calculate highest possible sample value
149 * based on capabilities of pad
150 */
151 {
152 gboolean is_signed = FALSE;
153 gint width = 0;
154 GstCaps *caps = NULL;
156 caps = GST_PAD_CAPS (pad);
157 if (caps == NULL)
158 {
159 g_warning ("gstaudio: could not get caps of pad %s:%s\n",
160 GST_ELEMENT_NAME (gst_pad_get_parent (pad)), GST_PAD_NAME (pad));
161 }
163 gst_caps_get_int (caps, "width", &width);
164 gst_caps_get_boolean (caps, "signed", &is_signed);
166 if (is_signed) --width;
167 /* example : 16 bit, signed : samples between -32768 and 32767 */
168 return ((long) (1 << width));
169 }
171 gboolean
172 gst_audio_is_buffer_framed (GstPad* pad, GstBuffer* buf)
173 /* check if the buffer size is a whole multiple of the frame size */
174 {
175 if (GST_BUFFER_SIZE (buf) % gst_audio_frame_byte_size (pad) == 0)
176 return TRUE;
177 else
178 return FALSE;
179 }
181 static gboolean
182 plugin_init (GstPlugin *plugin)
183 {
184 return TRUE;
185 }
187 GST_PLUGIN_DEFINE (
188 GST_VERSION_MAJOR,
189 GST_VERSION_MINOR,
190 "gstaudio",
191 "Support services for audio plugins",
192 plugin_init,
193 VERSION,
194 GST_LICENSE,
195 GST_COPYRIGHT,
196 GST_PACKAGE,
197 GST_ORIGIN
198 );