File: bseeditablesample.proc

package info (click to toggle)
beast 0.7.4-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 33,500 kB
  • sloc: ansic: 197,348; cpp: 59,114; sh: 11,030; perl: 2,523; makefile: 2,474; xml: 1,026; lisp: 549; awk: 270; sed: 8
file content (287 lines) | stat: -rw-r--r-- 10,131 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* BSE - Better Sound Engine	-*-mode: c;-*-
 * Copyright (C) 2002 Tim Janik
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * A copy of the GNU Lesser General Public License should ship along
 * with this library; if not, see http://www.gnu.org/copyleft/.
 */
#include        <bse/bseplugin.h>
#include        <bse/bseprocedure.h>
#include        <bse/bseeditablesample.h>
#include        <bse/gsldatahandle.h>


AUTHORS	= "Tim Janik <timj@gtk.org>";
LICENSE = "GNU Lesser General Public License";


METHOD (BseEditableSample, open) {
  HELP	= ("Open the sample for reading.");
  IN	= bse_param_spec_object ("esample", "Editable Sample", NULL,
				 BSE_TYPE_EDITABLE_SAMPLE, SFI_PARAM_STANDARD);
  OUT   = bse_param_spec_genum ("error", "Error", NULL,
				BSE_TYPE_ERROR_TYPE, BSE_ERROR_NONE,
				SFI_PARAM_STANDARD);
} BODY (BseProcedureClass *proc,
	const GValue      *in_values,
	GValue            *out_values)
{
  /* extract parameter values */
  BseEditableSample *esample = bse_value_get_object (in_values++);
  BseErrorType error;
  
  /* check parameters */
  if (!BSE_IS_EDITABLE_SAMPLE (esample))
    return BSE_ERROR_PROC_PARAM_INVAL;
  
  /* action */
  if (!esample->wchunk)
    error = BSE_ERROR_WAVE_NOT_FOUND;
  else if (esample->open_count)
    {
      esample->open_count++;
      error = BSE_ERROR_NONE;
    }
  else
    {
      error = gsl_wave_chunk_open (esample->wchunk);
      if (!error)
	esample->open_count++;
    }
  
  /* set output parameters */
  g_value_set_enum (out_values++, error);
  
  return BSE_ERROR_NONE;
}

METHOD (BseEditableSample, close) {
  HELP	= ("Close an opened sample.");
  IN	= bse_param_spec_object ("esample", "Editable Sample", NULL,
				 BSE_TYPE_EDITABLE_SAMPLE, SFI_PARAM_STANDARD);
} BODY (BseProcedureClass *proc,
	const GValue      *in_values,
	GValue            *out_values)
{
  /* extract parameter values */
  BseEditableSample *esample = bse_value_get_object (in_values++);
  
  /* check parameters */
  if (!BSE_IS_EDITABLE_SAMPLE (esample) || !esample->wchunk || !esample->open_count)
    return BSE_ERROR_PROC_PARAM_INVAL;
  
  /* action */
  esample->open_count--;
  if (!esample->open_count)
    gsl_wave_chunk_close (esample->wchunk);
  
  return BSE_ERROR_NONE;
}

METHOD (BseEditableSample, get-length) {
  HELP	= ("Return the number of values in the sample.");
  IN	= bse_param_spec_object ("esample", "Editable Sample", NULL,
				 BSE_TYPE_EDITABLE_SAMPLE, SFI_PARAM_STANDARD);
  OUT	= sfi_pspec_int ("length", NULL, "Number of values",
			 1, 0, G_MAXINT, 1, SFI_PARAM_STANDARD);
} BODY (BseProcedureClass *proc,
	const GValue      *in_values,
	GValue            *out_values)
{
  /* extract parameter values */
  BseEditableSample *esample = bse_value_get_object (in_values++);
  GslDataCache *dcache = NULL;
  
  /* check parameters */
  if (!BSE_IS_EDITABLE_SAMPLE (esample))
    return BSE_ERROR_PROC_PARAM_INVAL;
  
  /* set output parameters */
  if (BSE_EDITABLE_SAMPLE_OPENED (esample) && esample->wchunk)
    dcache = esample->wchunk->dcache;
  sfi_value_set_int (out_values++, dcache ? gsl_data_handle_length (dcache->dhandle) : 0);
  
  return BSE_ERROR_NONE;
}

METHOD (BseEditableSample, get-n-channels) {
  HELP	= ("Return the number of channels in the sample.");
  IN	= bse_param_spec_object ("esample", "Editable Sample", NULL,
				 BSE_TYPE_EDITABLE_SAMPLE, SFI_PARAM_STANDARD);
  OUT	= sfi_pspec_int ("n-channels", NULL, "Number of channels",
			 0, 0, G_MAXINT, 1, SFI_PARAM_STANDARD);
} BODY (BseProcedureClass *proc,
	const GValue      *in_values,
	GValue            *out_values)
{
  /* extract parameter values */
  BseEditableSample *esample = bse_value_get_object (in_values++);
  
  /* check parameters */
  if (!BSE_IS_EDITABLE_SAMPLE (esample))
    return BSE_ERROR_PROC_PARAM_INVAL;
  
  /* set output parameters */
  sfi_value_set_int (out_values++, esample->wchunk ? esample->wchunk->n_channels : 1);
  
  return BSE_ERROR_NONE;
}

METHOD (BseEditableSample, get-osc-freq) {
  HELP	= ("Return the oscillator frequency for the sample.");
  IN	= bse_param_spec_object ("esample", "Editable Sample", NULL,
				 BSE_TYPE_EDITABLE_SAMPLE, SFI_PARAM_STANDARD);
  OUT	= bse_param_spec_freq_simple ("osc-freq", NULL, "Oscillator Frequency",
				      SFI_PARAM_STANDARD);
} BODY (BseProcedureClass *proc,
	const GValue      *in_values,
	GValue            *out_values)
{
  /* extract parameter values */
  BseEditableSample *esample = bse_value_get_object (in_values++);
  
  /* check parameters */
  if (!BSE_IS_EDITABLE_SAMPLE (esample))
    return BSE_ERROR_PROC_PARAM_INVAL;
  
  /* set output parameters */
  sfi_value_set_real (out_values++, esample->wchunk ? esample->wchunk->osc_freq : BSE_KAMMER_FREQUENCY);
  
  return BSE_ERROR_NONE;
}

METHOD (BseEditableSample, read-samples) {
  HELP	= ("Read a set of samples from a specific offset.");
  IN	= bse_param_spec_object ("esample", "Editable Sample", NULL,
				 BSE_TYPE_EDITABLE_SAMPLE, SFI_PARAM_STANDARD);
  IN	= sfi_pspec_int ("voffset", NULL, "Value offset",
			 0, 0, G_MAXINT, 1, SFI_PARAM_STANDARD);
  OUT	= sfi_pspec_fblock ("sample_block", NULL, "Block of samples", SFI_PARAM_STANDARD);
} BODY (BseProcedureClass *proc,
        const GValue      *in_values,
        GValue            *out_values)
{
  /* extract parameter values */
  BseEditableSample *esample = bse_value_get_object (in_values++);
  guint              voffset = sfi_value_get_int (in_values++);
  GslDataCache *dcache = NULL;
  SfiFBlock *fblock;
  
  /* check parameters */
  if (!BSE_IS_EDITABLE_SAMPLE (esample))
    return BSE_ERROR_PROC_PARAM_INVAL;
  
  if (BSE_EDITABLE_SAMPLE_OPENED (esample) && esample->wchunk)
    dcache = esample->wchunk->dcache;
  if (!dcache || voffset >= gsl_data_handle_length (dcache->dhandle))
    fblock = sfi_fblock_new_sized (1024);
  else
    {
      GslDataCacheNode *dnode = gsl_data_cache_ref_node (dcache, voffset, TRUE);
      guint i, l, dnode_length = dcache->node_size;
      
      l = dnode_length - (voffset - dnode->offset) + dcache->padding;
      l = MIN (l, gsl_data_handle_length (dcache->dhandle) - voffset);
      fblock = sfi_fblock_new_sized (l);
      for (i = 0; i < l; i++)
	fblock->values[i] = dnode->data[voffset - dnode->offset + i];
      gsl_data_cache_unref_node (dcache, dnode);
    }
  
  /* set output parameters */
  sfi_value_take_fblock (out_values++, fblock);
  
  return BSE_ERROR_NONE;
}

METHOD (BseEditableSample, collect-stats) {
  HELP	= ("Collect statistics from sample blocks as (minimum, maximum) pairs.");
  IN	= bse_param_spec_object ("esample", "Editable Sample", NULL,
				 BSE_TYPE_EDITABLE_SAMPLE, SFI_PARAM_STANDARD);
  IN	= sfi_pspec_int ("voffset", NULL, "Offset of first stat block",
			 0, 0, G_MAXINT, 1, SFI_PARAM_STANDARD);
  IN	= sfi_pspec_real ("offset_scale", NULL, "Factor to scale voffset increments with",
			  0, 0, G_MAXINT, 1, SFI_PARAM_STANDARD);
  IN	= sfi_pspec_int ("block_size", NULL, "Block size to compute stat pairs from",
			 0, 0, G_MAXINT, 1, SFI_PARAM_STANDARD);
  IN	= sfi_pspec_int ("stepping", NULL, "Stepping within a stat block",
			 0, 0, G_MAXINT, 1, SFI_PARAM_STANDARD);
  IN	= sfi_pspec_int ("max_pairs", NULL, "Maximum number of (min, max) pairs to collect",
			 1, 0, G_MAXINT, 1, SFI_PARAM_STANDARD);
  OUT	= sfi_pspec_fblock ("sample_block", NULL, "Block of samples", SFI_PARAM_STANDARD);
} BODY (BseProcedureClass *proc,
        const GValue      *in_values,
        GValue            *out_values)
{
  /* extract parameter values */
  BseEditableSample *esample    = bse_value_get_object (in_values++);
  guint              voffset    = sfi_value_get_int (in_values++);
  double             offs_scale = g_value_get_double (in_values++);
  guint              block_size = sfi_value_get_int (in_values++);
  guint              stepping   = sfi_value_get_int (in_values++);
  guint              max_pairs  = sfi_value_get_int (in_values++);
  GslDataCache *dcache = NULL;
  SfiFBlock *fblock;
  
  /* check parameters */
  if (!BSE_IS_EDITABLE_SAMPLE (esample) || stepping < 1)
    return BSE_ERROR_PROC_PARAM_INVAL;
  
  if (BSE_EDITABLE_SAMPLE_OPENED (esample) && esample->wchunk)
    dcache = esample->wchunk->dcache;
  if (!dcache || voffset + block_size > gsl_data_handle_length (dcache->dhandle))
    fblock = sfi_fblock_new_sized (max_pairs * 2);
  else
    {
      GslDataCacheNode *dnode = gsl_data_cache_ref_node (dcache, voffset, GSL_DATA_CACHE_DEMAND_LOAD);
      guint j, dnode_length = dcache->node_size;
      
      fblock = sfi_fblock_new_sized (max_pairs * 2);
      for (j = 0; j < max_pairs; j++)
	{
	  guint i, cur_offset = j * offs_scale;
	  gfloat min = +1, max = -1;
	  
	  /* keep alignment across offset scaling */
	  cur_offset /= stepping;
	  cur_offset = voffset + cur_offset * stepping;
	  
	  /* collect stats for one block */
	  for (i = cur_offset; i < cur_offset + block_size; i += stepping)
	    {
	      guint pos;
	      
	      if (i < dnode->offset || i >= dnode->offset + dnode_length)
		{
		  gsl_data_cache_unref_node (dcache, dnode);
		  /* demand_load the first block only */
		  dnode = gsl_data_cache_ref_node (dcache, i, j == 0 ? GSL_DATA_CACHE_DEMAND_LOAD : GSL_DATA_CACHE_PEEK);
		  if (!dnode)
		    goto break_loops;
		}
	      pos = i - dnode->offset;
	      min = MIN (min, dnode->data[pos]);
	      max = MAX (max, dnode->data[pos]);
	    }
	  fblock->values[j * 2] = min;
	  fblock->values[j * 2 + 1] = max;
	}
      gsl_data_cache_unref_node (dcache, dnode);
    break_loops:
      sfi_fblock_resize (fblock, j * 2);
    }
  
  /* set output parameters */
  sfi_value_take_fblock (out_values++, fblock);
  
  return BSE_ERROR_NONE;
}