1 /*
2  * Sweep, a sound wave editor.
3  *
4  * Copyright (C) 2000 Conrad Parker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef __SWEEP_TYPES_H__
22 #define __SWEEP_TYPES_H__
23 
24 #include <glib.h>
25 #include <gdk/gdktypes.h>  /* XXX: for GdkModifierType */
26 #include <gtk/gtk.h> /* XXX: for info_clist widget */
27 
28 #include <sys/types.h>
29 
30 /*
31 #include <config.h>
32 */
33 
34 /*
35  * Basic types
36  */
37 
38 /* Audio data representation */
39 
40 /* gfloats in the range [-1.0, 1.0] */
41 typedef gfloat sw_audio_t;
42 
43 /* Intermediate audio representation format:
44  * Use this for intermediate values for mixing etc. */
45 typedef gdouble sw_audio_intermediate_t;
46 
47 #define SW_AUDIO_T_MAX (1.0)
48 #define SW_AUDIO_T_MIN (-1.0)
49 
50 /* Time, in seconds */
51 typedef gfloat sw_time_t;
52 
53 /* Frame Counts */
54 #if 0
55 
56 #if (SIZEOF_OFF_T == 8)
57 /* For libsndfile version 1 */
58 typedef off_t sw_framecount_t;
59 #else
60 typedef int sw_framecount_t;
61 #endif
62 
63 #else
64 
65 typedef int sw_framecount_t;
66 #define FRAMECOUNT_MAX INT_MAX
67 
68 #endif
69 
70 
71 /*
72  * Core datatypes
73  */
74 
75 typedef struct _sw_sel sw_sel;
76 typedef struct _sw_format sw_format;
77 typedef struct _sw_sounddata sw_sounddata;
78 typedef struct _sw_sample sw_sample;
79 
80 /*
81  * sw_sel: a region in a selection.
82  *
83  * Caution: Potential off-by-one error.
84  *    The selected region is defined as going from
85  *    sel_start to (sel_end - 1).
86  *    Thus the length of the selection is always
87  *    (sel_end - sel_start).
88  *
89  * Units are frame offsets from start of sample.
90  *
91  */
92 struct _sw_sel {
93   sw_framecount_t sel_start;
94   sw_framecount_t sel_end;
95 };
96 
97 /*
98  * sw_format: a sampling format.
99  *
100  * Multichannel data is interleaved: Stereo is stored LR.
101  */
102 struct _sw_format {
103   gint channels;  /* nr channels per frame */
104   gint rate;      /* sampling rate (Hz) */
105 };
106 
107 struct _sw_sounddata {
108   int refcount;
109 
110   sw_format * format;
111   sw_framecount_t nr_frames;    /* nr frames */
112 
113   gpointer data;
114   GMutex * data_mutex; /* Mutex for access to sample data */
115 
116   GList * sels;     /* selection: list of sw_sels */
117   GMutex * sels_mutex; /* Mutex for access to sels */
118 };
119 
120 #define SW_DIR_LEN 256
121 
122 
123 typedef enum {
124   SWEEP_EDIT_MODE_READY,
125   SWEEP_EDIT_MODE_META, /* modifying metadata: sels etc. */
126   SWEEP_EDIT_MODE_FILTER,
127   SWEEP_EDIT_MODE_ALLOC,
128 } sw_edit_mode;
129 
130 typedef enum {
131   SWEEP_EDIT_STATE_IDLE,
132   SWEEP_EDIT_STATE_PENDING,
133   SWEEP_EDIT_STATE_BUSY,
134   SWEEP_EDIT_STATE_DONE,
135   SWEEP_EDIT_STATE_CANCEL,
136 } sw_edit_state;
137 
138 typedef enum {
139   SWEEP_TRANSPORT_STOP,
140   SWEEP_TRANSPORT_PLAY,
141   SWEEP_TRANSPORT_PLAY_SEL,
142 } sw_transport_type;
143 
144 /* File formats */
145 typedef enum {
146   SWEEP_FILE_FORMAT_NONE=0,
147   SWEEP_FILE_FORMAT_BY_EXTENSION=0,
148   SWEEP_FILE_FORMAT_RAW,
149   SWEEP_FILE_FORMAT_WAV,
150   SWEEP_FILE_FORMAT_AIFF,
151   SWEEP_FILE_FORMAT_AU,
152   SWEEP_FILE_FORMAT_PAF,
153   SWEEP_FILE_FORMAT_SVX,
154   SWEEP_FILE_FORMAT_IRCAM,
155   SWEEP_FILE_FORMAT_VOC,
156   SWEEP_FILE_FORMAT_MAX
157 } sw_file_format_t;
158 
159 
160 typedef struct _sw_edit_region sw_edit_region;
161 typedef struct _sw_edit_buffer sw_edit_buffer;
162 
163 /*
164  * A region of data. Units are frames.
165  * The length of data available *data is (end - start)
166  */
167 struct _sw_edit_region {
168   sw_framecount_t start;
169   sw_framecount_t end;
170 
171   gpointer data;
172 };
173 
174 struct _sw_edit_buffer {
175   sw_format * format;
176   GList * regions;
177   gint refcount;
178 };
179 
180 
181 typedef void (*SweepFunction) (gpointer data);
182 typedef void (*SweepCallback) (sw_sample * sample, gpointer data);
183 
184 
185 typedef struct _sw_operation sw_operation;
186 typedef struct _sw_op_instance sw_op_instance;
187 
188 struct _sw_operation {
189   sw_edit_mode edit_mode;
190   SweepCallback _do_;
191   SweepFunction purge_do;
192   SweepCallback undo;
193   SweepFunction purge_undo;
194   SweepCallback redo;
195   SweepFunction purge_redo;
196 };
197 
198 struct _sw_op_instance {
199   sw_sample * sample;
200   char * description;
201   sw_operation * op;
202   gpointer do_data;
203   gpointer undo_data;
204   gpointer redo_data;
205 };
206 
207 /*
208  * Basic types for parameters
209  */
210 typedef enum {
211   SWEEP_TYPE_BOOL = 0,
212   SWEEP_TYPE_INT,
213   SWEEP_TYPE_FLOAT,
214   SWEEP_TYPE_STRING,
215 } sw_param_type;
216 
217 typedef gboolean sw_bool;
218 typedef gint sw_int;
219 typedef gdouble sw_float;
220 typedef gchar * sw_string;
221 
222 
223 /*
224  * Instances of Parameter Sets
225  */
226 typedef union _sw_param sw_param;
227 typedef sw_param * sw_param_set;
228 
229 union _sw_param {
230   sw_bool b;
231   sw_int  i;
232   sw_float f;
233   sw_string s;
234 };
235 
236 /*
237  * Specifications for Parameter Sets
238  *
239  */
240 
241 /*
242  * Constraint types. These are used within the param_spec to indicate
243  * the usage of the sw_constraint.
244  */
245 typedef enum {
246 /*
247  * SW_PARAM_CONSTRAINED_NOT indicates that the parameter is completely
248  * unconstrained.
249  *
250  * Useful in verse; with iambic pentameter, accent the "-ED" eg.
251  *
252  *     This 'x' is SW_CONSTRAINED_NOT!
253  *     How free is its life, how wretched its lot!
254  */
255   SW_PARAM_CONSTRAINED_NOT=0,
256 
257 /*
258  * SW_PARAM_CONSTRAINED_LIST indicates that the parameter is constrained
259  * to values given in a param list.
260  */
261   SW_PARAM_CONSTRAINED_LIST,
262 
263 /*
264  * SW_PARAM_CONSTRAINED_RANGE indicates that the parameter is constrained
265  * to a given range.
266  */
267   SW_PARAM_CONSTRAINED_RANGE,
268 
269 } sw_constraint_type;
270 
271 
272 /* VALID PORTIONS OF RANGES */
273 
274 /*
275  * SW_RANGE_LOWER_BOUND_VALID indicates that the 'lower' field
276  * of the constraint, if interpreted as a range, is valid. If this bit
277  * is not set, then the parameter is known to have no lower bound.
278  * If the constraint is valid and the 'step' field is set, then the
279  * value of 'lower' is used to determine a basis for the parameter,
280  * even if SW_RANGE_LOWER_BOUND_VALID is not set.
281  *
282  * Note that the constraint is not interpreted as a range if
283  * SW_PARAM_CONSTRAINED_LIST is set.
284  */
285 #define SW_RANGE_LOWER_BOUND_VALID (1<<0)
286 
287 /*
288  * SW_RANGE_UPPER_BOUND_VALID indicates that the 'upper' field
289  * of the constraint, if interpreted as a range, is valid. If this bit
290  * is not set, then the parameter is known to have no upper bound.
291  */
292 #define SW_RANGE_UPPER_BOUND_VALID (1<<1)
293 
294 /*
295  * SW_RANGE_STEP_VALID indicates that the 'step' field of
296  * the constraint, if interpreted as a range, is valid. If this bit
297  * is not set, then the parameter is assumed to be continuous.
298  * If this field is valid, then the parameter will only have values
299  * equal to  (lower + n*step) for integer n.
300  *
301  * This constraint is ignored for string paramters.
302  */
303 #define SW_RANGE_STEP_VALID (1<<2)
304 
305 #define SW_RANGE_ALL_VALID (SW_RANGE_LOWER_BOUND_VALID|SW_RANGE_UPPER_BOUND_VALID|SW_RANGE_STEP_VALID)
306 
307 /*
308  * HINTS for user interface semantics
309  */
310 
311 typedef int sw_hints;
312 
313 /*
314  * SW_PARAM_HINT_DEFAULT indicates that the parameter has no special
315  * interpretation; eg. if a number, it's linear
316  */
317 
318 #define SW_PARAM_HINT_DEFAULT      (0)
319 
320 /*
321  * SW_PARAM_HINT_LOGARITHMIC indicates that the parameter should be
322  * interpreted as logarithmic.
323  */
324 #define SW_PARAM_HINT_LOGARITHMIC  (1<<1)
325 
326 /*
327  * SW_PARAM_HINT_TIME indicates that the parameter should be
328  * interpreted as a time
329  */
330 #define SW_PARAM_HINT_TIME         (1<<2)
331 
332 /*
333  * SW_PARAM_HINT_FILENAME indicates that the parameter should be
334  * interpreted as a valid filename on the user's system.
335  */
336 #define SW_PARAM_HINT_FILENAME     (1<<3)
337 
338 
339 typedef struct _sw_param_spec sw_param_spec;
340 typedef struct _sw_param_range sw_param_range;
341 typedef union _sw_constraint sw_constraint;
342 
343 /*
344  * sw_param_range: a range of acceptable parameter values.
345  *
346  * NB: this is a hard limit. Values <lower and values >upper
347  * need not be expected by plugins.
348  *
349  * The first parameter is a mask consisting of a bitwise or of between
350  * zero and three of SW_RANGE_LOWER_BOUND_VALID,
351  * SW_RANGE_UPPER_BOUND_VALID, and SW_RANGE_STEP_VALID.
352  *
353  * The three following parameters are interpreted as the type of the
354  * parameter they constrain. The 'step' parameter is never valid for
355  * string parameters.
356  */
357 struct _sw_param_range {
358   int valid_mask;
359   sw_param lower;
360   sw_param upper;
361   sw_param step;
362 };
363 
364 /*
365  * sw_constraint
366  *
367  * Constraints on parameters. Constraints, if valid, are hard limits.
368  *
369  * All constraints are disregarded for boolean parameters.
370  */
371 union _sw_constraint {
372   /*
373    * param_list: Values are constrained to those within a list of
374    * parameters. NB: the length of this list is given by the
375    * value of the first parameter, interpreted as an integer.
376    * ie. this length = constraint->param_list[0].i
377    */
378   sw_param * list;
379 
380   /*
381    * param_range, as described above.
382    */
383   sw_param_range * range;  /* param range */
384 };
385 
386 
387 /*
388  * sw_param_spec: specification for a parameter.
389  */
390 struct _sw_param_spec {
391   /* A short name for this parameter */
392   gchar * name;
393 
394   /* A longer description of the parameter's purpose and usage */
395   gchar * desc;
396 
397   /* The type of the parameter */
398   sw_param_type type;
399 
400   /* Constraints */
401   sw_constraint_type constraint_type;
402   sw_constraint constraint;
403 
404   /* Hints */
405   sw_hints hints;
406 };
407 
408 
409 /*
410  * Plugins and procedures
411  */
412 
413 typedef struct _sw_procedure sw_procedure;
414 typedef struct _sw_plugin sw_plugin;
415 
416 struct _sw_procedure {
417   gchar * name;
418   gchar * description;
419   gchar * author;
420   gchar * copyright;
421   gchar * url;
422 
423   gchar * identifier;
424 
425   /* Key bindings */
426   guint accel_key;
427   GdkModifierType accel_mods;
428 
429   gint nr_params;
430   sw_param_spec * param_specs;
431 
432   /* suggest sets suggested values for the members of pset,
433    * possibly using the sample.
434    *
435    * If nr_params is 0 then this function will not be called.
436    * If this function is NULL then default values (zero,FALSE,"")
437    * will be used.
438    */
439   void (*suggest) (sw_sample * sample, sw_param_set pset,
440 		   gpointer custom_data);
441 
442   /* This is the function that actually does the work!
443    *
444    * apply applies the parameter set pset to a sample
445    *
446    * If nr_params is 0 then this function will be passed a NULL pset.
447    */
448   sw_op_instance * (*apply) (sw_sample * sample,
449 			     sw_param_set pset, gpointer custom_data);
450 
451   /* custom data to pass to the suggest and apply functions */
452   gpointer custom_data;
453 };
454 
455 struct _sw_plugin {
456   /* plugin_init () returns a list of procedures */
457   GList * (*plugin_init) (void);
458 
459   /* plugin_cleanup() frees the plugin's private data structures */
460   void (*plugin_cleanup) (void);
461 };
462 
463 typedef sw_sample * (*SweepFilter) (sw_sample * sample,
464 				    sw_param_set pset,
465 				    gpointer custom_data);
466 
467 
468 #endif  /* __SWEEP_TYPES_H__ */
469 
470 
471 
472 
473 
474 
475