1 /*
2  * libInstPatch
3  * Copyright (C) 1999-2014 Element Green <element@elementsofsound.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; version 2.1
8  * of the License only.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA or on the web at http://www.gnu.org.
19  */
20 #ifndef __IPATCH_SAMPLE_H__
21 #define __IPATCH_SAMPLE_H__
22 
23 #include <glib.h>
24 #include <glib-object.h>
25 
26 #include <libinstpatch/IpatchSampleTransform.h>
27 
28 /* forward type declarations */
29 
30 typedef struct _IpatchSample IpatchSample; /* dummy typedef */
31 typedef struct _IpatchSampleIface IpatchSampleIface;
32 typedef struct _IpatchSampleHandle IpatchSampleHandle;
33 
34 #include <libinstpatch/IpatchSampleData.h>
35 
36 #define IPATCH_TYPE_SAMPLE   (ipatch_sample_get_type ())
37 #define IPATCH_SAMPLE(obj) \
38   (G_TYPE_CHECK_INSTANCE_CAST ((obj), IPATCH_TYPE_SAMPLE, \
39    IpatchSample))
40 #define IPATCH_SAMPLE_CLASS(klass) \
41   (G_TYPE_CHECK_CLASS_CAST ((klass), IPATCH_TYPE_SAMPLE, \
42    IpatchSampleIface))
43 #define IPATCH_IS_SAMPLE(obj) \
44   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), IPATCH_TYPE_SAMPLE))
45 #define IPATCH_SAMPLE_GET_IFACE(obj) \
46   (G_TYPE_INSTANCE_GET_INTERFACE ((obj), IPATCH_TYPE_SAMPLE, \
47    IpatchSampleIface))
48 
49 /**
50  * IpatchSampleHandleOpenFunc:
51  * @handle: Caller supplied structure to initialize
52  * @err: Location to store error information
53  *
54  * #IpatchSample interface method function type to open a sample for reading
55  * or writing.  This method is optional for an #IpatchSample interface and if
56  * not specified then it is assumed that the open was successful and nothing
57  * additional need be done.  All fields of @handle structure are already
58  * initialized, except <structfield>data1</structfield>,
59  * <structfield>data2</structfield> and <structfield>data3</structfield>
60  * which are available for the interface implementation.
61  *
62  * Returns: %TRUE on success, %FALSE otherwise (in which case an error should
63  *   optionally be stored in @err).
64  */
65 typedef gboolean(*IpatchSampleHandleOpenFunc)(IpatchSampleHandle *handle,
66         GError **err);
67 /**
68  * IpatchSampleHandleCloseFunc:
69  * @handle: Sample handle to close (as returned from #IpatchSampleHandleOpenFunc)
70  *
71  * #IpatchSample interface method to free any resources allocated in
72  * #IpatchSampleOpenFunc to @handle.  This method is optional for an
73  * #IpatchSample interface and need not be specified if nothing needs to be done
74  * to release any resources allocated by #IpatchSampleHandleOpenFunc.
75  */
76 typedef void (*IpatchSampleHandleCloseFunc)(IpatchSampleHandle *handle);
77 
78 /**
79  * IpatchSampleHandleReadFunc:
80  * @handle: Handle to read from (as returned from #IpatchSampleOpenFunc)
81  * @offset: Offset in sample to start read from (in frames), use
82  *   #IPATCH_SAMPLE_CUROFS to use current offset (starts at 0 if not specified)
83  * @frames: Size of sample data to read (in frames)
84  * @buf: Buffer to store sample data in
85  * @err: Location to store error information
86  *
87  * #IpatchSample interface method function type to read data from a
88  * sample handle.  Can be %NULL in #IpatchSampleIface if sample data is not
89  * readable.  Sample data should be stored in its native format.
90  *
91  * Returns: Should return %TRUE on success, %FALSE otherwise (in which case
92  * an error should optionally be stored in @err).
93  */
94 typedef gboolean(*IpatchSampleHandleReadFunc)(IpatchSampleHandle *handle,
95         guint offset, guint frames,
96         gpointer buf, GError **err);
97 /**
98  * IpatchSampleHandleWriteFunc:
99  * @handle: Handle to write to (as returned from #IpatchSampleOpenFunc)
100  * @offset: Offset in sample to start write to (in frames), use
101  *   #IPATCH_SAMPLE_CUROFS to use current offset (starts at 0 if not specified)
102  * @frames: Size of sample data to write (in frames)
103  * @buf: Buffer to store sample data in
104  * @err: Location to store error information
105  *
106  * #IpatchSample interface method function type to write data to a
107  * sample handle.  Can be %NULL in #IpatchSampleIface if sample data is not
108  * writable.  Sample data will be supplied in its native format.
109  *
110  * Returns: Should return %TRUE on success, %FALSE otherwise (in which case
111  * an error should optionally be stored in @err).
112  */
113 typedef gboolean(*IpatchSampleHandleWriteFunc)(IpatchSampleHandle *handle,
114         guint offset, guint frames,
115         gconstpointer buf, GError **err);
116 /* Sample interface */
117 struct _IpatchSampleIface
118 {
119     GTypeInterface parent_class;
120 
121     IpatchSampleHandleOpenFunc open;
122     IpatchSampleHandleCloseFunc close;
123     IpatchSampleHandleReadFunc read;
124     IpatchSampleHandleWriteFunc write;
125 
126     int *loop_types; /* -1 terminated array of supp. loop types (NULL = none) */
127 };
128 
129 /* Sample handle for I/O operations */
130 struct _IpatchSampleHandle
131 {
132     IpatchSample *sample;		      /* The sample which this handle applies to */
133     IpatchSampleTransform *transform;   /* Set if sample is being converted */
134     IpatchSampleHandleReadFunc read;    /* Read method pointer (copied from IpatchItem interface) */
135     IpatchSampleHandleWriteFunc write;  /* Write method pointer (copied from IpatchItem interface) */
136     IpatchSampleHandleCloseFunc close;  /* Close method pointer (copied from IpatchItem interface) */
137     guint32 read_mode : 1;              /* TRUE if read mode, FALSE if write mode */
138     guint32 manual_transform : 1;       /* Methods handle sample transform */
139     guint32 release_transform : 1;      /* TRUE if transform should be released from transform pool */
140     guint32 format : 12;                /* Format to transform to */
141     guint32 reserved : 17;
142     guint32 channel_map;                /* Channel map for multi-channel audio transform */
143     gpointer data1;		      /* sample interface defined */
144     gpointer data2;		      /* sample interface defined */
145     gpointer data3;		      /* sample interface defined */
146     gpointer data4;		      /* sample interface defined */
147     guint32 reserved2;
148 };
149 
150 /**
151  * IpatchSampleLoopType:
152  * @IPATCH_SAMPLE_LOOP_NONE: No loop.
153  * @IPATCH_SAMPLE_LOOP_STANDARD: Standard loop.
154  * @IPATCH_SAMPLE_LOOP_RELEASE: Loop till note release stage.
155  * @IPATCH_SAMPLE_LOOP_PINGPONG: Play forward and then in reverse continously.
156  *
157  * Sample looping type.
158  */
159 typedef enum
160 {
161     IPATCH_SAMPLE_LOOP_NONE,
162     IPATCH_SAMPLE_LOOP_STANDARD,
163     IPATCH_SAMPLE_LOOP_RELEASE,
164     IPATCH_SAMPLE_LOOP_PINGPONG
165 } IpatchSampleLoopType;
166 
167 /**
168  * IPATCH_SAMPLE_FORMAT_DEFAULT:
169  *
170  * Default sample format for #IpatchSample interface.
171  */
172 #define IPATCH_SAMPLE_FORMAT_DEFAULT  (IPATCH_SAMPLE_16BIT | IPATCH_SAMPLE_MONO \
173                 		       | IPATCH_SAMPLE_LENDIAN | IPATCH_SAMPLE_SIGNED)
174 
175 /**
176  * IPATCH_SAMPLE_RATE_MIN: (skip)
177  *
178  * Minimum sample rate.
179  * SoundFont spec says 8000 Hz is minimum guaranteed, seen lots of smaller values though.
180  */
181 #define IPATCH_SAMPLE_RATE_MIN     100
182 
183 /**
184  * IPATCH_SAMPLE_RATE_MAX: (skip)
185  *
186  * Maximum sample rate.
187  */
188 #define IPATCH_SAMPLE_RATE_MAX     192000
189 
190 /**
191  * IPATCH_SAMPLE_RATE_DEFAULT: (skip)
192  *
193  * Default sample rate.
194  */
195 #define IPATCH_SAMPLE_RATE_DEFAULT 44100
196 
197 /**
198  * IPATCH_SAMPLE_ROOT_NOTE_DEFAULT: (skip)
199  *
200  * Default root note.
201  */
202 #define IPATCH_SAMPLE_ROOT_NOTE_DEFAULT  60
203 
204 /**
205  * IPATCH_SAMPLE_LOOP_TYPE_TERM:
206  *
207  * Value used for terminating list of supported loop types.
208  */
209 #define IPATCH_SAMPLE_LOOP_TYPE_TERM (-1)
210 
211 /**
212  * IPATCH_SAMPLE_HANDLE_FORMAT:
213  * @handle: Sample handle
214  *
215  * Macro to access transform sample format of a sample handle.
216  *
217  * Returns: Sample transform format.
218  */
219 #define IPATCH_SAMPLE_HANDLE_FORMAT(handle)  ((handle)->format)
220 
221 /* some convenient loop_type arrays for use by IpatchSample interfaces */
222 extern int ipatch_sample_loop_types_standard[];
223 extern int ipatch_sample_loop_types_standard_release[];
224 
225 GType ipatch_sample_get_type(void);
226 int *ipatch_sample_get_loop_types(IpatchSample *sample);
227 int *ipatch_sample_type_get_loop_types(GType type);
228 int *ipatch_sample_get_loop_types_len(IpatchSample *sample, int *len);
229 int *ipatch_sample_type_get_loop_types_len(GType type, int *len);
230 
231 void ipatch_sample_set_format(IpatchSample *sample, int format);
232 int ipatch_sample_get_format(IpatchSample *sample);
233 void ipatch_sample_set_size(IpatchSample *sample, guint size);
234 guint ipatch_sample_get_size(IpatchSample *sample, guint *bytes);
235 int ipatch_sample_get_frame_size(IpatchSample *sample);
236 IpatchSampleData *ipatch_sample_get_sample_data(IpatchSample *sample);
237 gboolean ipatch_sample_set_sample_data(IpatchSample *sample,
238                                        IpatchSampleData *sampledata);
239 
240 gboolean ipatch_sample_read(IpatchSample *sample, guint offset, guint frames,
241                             gpointer buf, GError **err);
242 gpointer ipatch_sample_read_size(IpatchSample *sample, guint offset, guint size, GError **err);
243 gboolean ipatch_sample_write(IpatchSample *sample, guint offset, guint frames,
244                              gconstpointer buf, GError **err);
245 gboolean ipatch_sample_write_size(IpatchSample *sample, guint offset,
246                                   gconstpointer buf, guint size, GError **err);
247 gboolean ipatch_sample_read_transform(IpatchSample *sample, guint offset, guint frames,
248                                       gpointer buf, int format, guint32 channel_map,
249                                       GError **err);
250 gpointer ipatch_sample_read_transform_size(IpatchSample *sample, guint offset, guint size,
251         int format, guint32 channel_map, GError **err);
252 gboolean ipatch_sample_write_transform(IpatchSample *sample, guint offset, guint frames,
253                                        gconstpointer buf, int format, guint32 channel_map,
254                                        GError **err);
255 gboolean ipatch_sample_write_transform_size(IpatchSample *sample, guint offset,
256         gconstpointer buf, guint size, int format,
257         guint32 channel_map, GError **err);
258 gboolean ipatch_sample_copy(IpatchSample *dest_sample, IpatchSample *src_sample,
259                             guint32 channel_map, GError **err);
260 gboolean ipatch_sample_save_to_file(IpatchSample *sample, const char *filename,
261                                     int file_format, int sub_format, GError **err);
262 
263 gboolean ipatch_sample_handle_open(IpatchSample *sample,
264                                    IpatchSampleHandle *handle, char mode,
265                                    int format, guint32 channel_map, GError **err);
266 void ipatch_sample_handle_close(IpatchSampleHandle *handle);
267 IpatchSampleTransform *ipatch_sample_handle_get_transform(IpatchSampleHandle *handle);
268 void ipatch_sample_handle_set_transform(IpatchSampleHandle *handle,
269                                         IpatchSampleTransform *transform);
270 int ipatch_sample_handle_get_format(IpatchSampleHandle *handle);
271 int ipatch_sample_handle_get_frame_size(IpatchSampleHandle *handle);
272 guint ipatch_sample_handle_get_max_frames(IpatchSampleHandle *handle);
273 gpointer ipatch_sample_handle_read(IpatchSampleHandle *handle, guint offset, guint frames,
274                                    gpointer buf, GError **err);
275 gpointer
276 ipatch_sample_handle_read_size(IpatchSampleHandle *handle, guint offset,
277                                guint size, GError **err);
278 gboolean ipatch_sample_handle_write(IpatchSampleHandle *handle, guint offset, guint frames,
279                                     gconstpointer buf, GError **err);
280 gboolean
281 ipatch_sample_handle_write_size(IpatchSampleHandle *handle, guint offset,
282                                 gconstpointer buf, guint size, GError **err);
283 
284 gboolean ipatch_sample_handle_cascade_open(IpatchSampleHandle *handle,
285         IpatchSample *sample, GError **err);
286 
287 GParamSpec *ipatch_sample_install_property(GObjectClass *oclass,
288         guint property_id,
289         const char *property_name);
290 GParamSpec *ipatch_sample_install_property_readonly(GObjectClass *oclass,
291         guint property_id,
292         const char *property_name);
293 GParamSpec *ipatch_sample_new_property_param_spec(const char *property_name,
294         GParamFlags flags);
295 
296 #endif
297