File: bsecxxmodule.cc

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 (522 lines) | stat: -rw-r--r-- 18,644 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/* BSE - Better Sound Engine
 * Copyright (C) 2003 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 "bsecxxmodule.hh"
#include "bseengine.h"
#include "bsemidireceiver.h"
#include "bsesnet.h"

namespace Bse {

static const ClassInfo class_info (NULL, "BseEffect implements an abstract C++ effect base.", __FILE__, __LINE__);
BSE_CXX_TYPE_REGISTER_ABSTRACT (Effect, "BseCxxBase", &class_info);

Effect::Effect() :
  last_module_update (0)
{
}

void
Effect::set_property (guint        prop_id,
                      const Value &value,
                      GParamSpec  *pspec)
{
}

void
Effect::get_property (guint       prop_id,
                      Value      &value,
                      GParamSpec *pspec)
{
}

static void
access_trampoline (BseModule *module,
                   gpointer   data)
{
  SynthesisModule::Closure *clo = static_cast<SynthesisModule::Closure*> (data);
  SynthesisModule *m = static_cast<SynthesisModule*> (module->user_data);
  (*clo) (m);
}

static void
access_data_free (gpointer data)
{
  SynthesisModule::Closure *clo = static_cast<SynthesisModule::Closure*> (data);
  delete clo;
}

void
Effect::update_modules (BseTrans *trans)
{
  BseSource *source = cast (this);
  if (BSE_SOURCE_PREPARED (source))
    {
      SynthesisModule::Closure *clo = make_module_config_closure();
      if (clo)
        {
          BseTrans *atrans = trans ? trans : bse_trans_open();
          bse_source_access_modules (source, access_trampoline, clo, access_data_free, atrans);
          if (!trans)
            last_module_update = bse_trans_commit (atrans);
        }
    }
}

SynthesisModule::SynthesisModule()
{
  intern_module = NULL;
}

void
SynthesisModule::set_module (BseModule *engine_module)
{
  g_return_if_fail (intern_module == NULL);
  g_return_if_fail (engine_module != NULL);
  
  intern_module = engine_module;

  /* assert validity of the above casts */
  BIRNET_STATIC_ASSERT (sizeof   (JStream)                    == sizeof   (BseJStream));
  BIRNET_STATIC_ASSERT (offsetof (JStream, values)            == offsetof (BseJStream, values));
  BIRNET_STATIC_ASSERT (sizeof (((JStream*)0)->values)        == sizeof (((BseJStream*)0)->values));
  BIRNET_STATIC_ASSERT (offsetof (JStream, n_connections)     == offsetof (BseJStream, n_connections));
  BIRNET_STATIC_ASSERT (sizeof (((JStream*)0)->n_connections) == sizeof (((BseJStream*)0)->n_connections));
  BIRNET_STATIC_ASSERT (offsetof (JStream, jcount)            == offsetof (BseJStream, jcount));
  BIRNET_STATIC_ASSERT (sizeof (((JStream*)0)->jcount)        == sizeof (((BseJStream*)0)->jcount));
  
  BIRNET_STATIC_ASSERT (sizeof   (IStream)                == sizeof   (BseIStream));
  BIRNET_STATIC_ASSERT (offsetof (IStream, values)        == offsetof (BseIStream, values));
  BIRNET_STATIC_ASSERT (sizeof (((IStream*)0)->values)    == sizeof (((BseIStream*)0)->values));
  BIRNET_STATIC_ASSERT (offsetof (IStream, connected)     == offsetof (BseIStream, connected));
  BIRNET_STATIC_ASSERT (sizeof (((IStream*)0)->connected) == sizeof (((BseIStream*)0)->connected));
  
  BIRNET_STATIC_ASSERT (sizeof   (OStream)                == sizeof   (BseOStream));
  BIRNET_STATIC_ASSERT (offsetof (OStream, values)        == offsetof (BseOStream, values));
  BIRNET_STATIC_ASSERT (sizeof (((OStream*)0)->values)    == sizeof (((BseOStream*)0)->values));
  BIRNET_STATIC_ASSERT (offsetof (OStream, connected)     == offsetof (BseOStream, connected));
  BIRNET_STATIC_ASSERT (sizeof (((OStream*)0)->connected) == sizeof (((BseOStream*)0)->connected));
}

void
SynthesisModule::ostream_set (unsigned int ostream_index,
                              const float *values)
{
  BseModule *m = engine_module();
  m->ostreams[ostream_index].values = const_cast<float*> (values);
}

const float*
SynthesisModule::const_values (float value)
{
  return bse_engine_const_values (value);
}

SynthesisModule::~SynthesisModule()
{
}

const ProcessCost
SynthesisModule::cost()
{
  return NORMAL;
}

static void
process_module (BseModule *engine_module,
                guint      n_values)
{
  SynthesisModule *m = static_cast<SynthesisModule*> (engine_module->user_data);
  m->process (n_values);
}

static void
reset_module (BseModule *engine_module)
{
  SynthesisModule *m = static_cast<SynthesisModule*> (engine_module->user_data);
  m->reset();
}

static void
delete_module (gpointer        data,
               const BseModuleClass *klass)
{
  SynthesisModule *m = static_cast<SynthesisModule*> (data);
  delete m;
}

static BseCostType
module_flags_from_process_cost (ProcessCost cost)
{
  switch (cost)
    {
    case EXPENSIVE:     return BSE_COST_EXPENSIVE;
    case CHEAP:         return BSE_COST_CHEAP;
    default:
    case NORMAL:        return BSE_COST_NORMAL;
    }
}

const BseModuleClass*
Effect::create_engine_class (SynthesisModule *sample_module,
                             int              cost,
                             int              n_istreams,
                             int              n_jstreams,
                             int              n_ostreams)
{
  BseSource *source = cast (this);
  BseSourceClass *source_class = BSE_SOURCE_GET_CLASS (source);
  if (!source_class->engine_class)
    {
      BseModuleClass klass = {
        0,                      /* n_istreams */
        0,                      /* n_jstreams */
        0,                      /* n_ostreams */
        process_module,         /* process */
        NULL,                   /* process_defer */
        reset_module,           /* reset */
        delete_module,          /* free */
        BSE_COST_NORMAL,        /* mflags */
      };
      klass.mflags = BseCostType (cost >= 0 ? cost : module_flags_from_process_cost (sample_module->cost()));
      klass.n_istreams = n_istreams >= 0 ? n_istreams : (BSE_SOURCE_N_ICHANNELS (source) -
                                                         BSE_SOURCE_N_JOINT_ICHANNELS (source));
      klass.n_jstreams = n_jstreams >= 0 ? n_jstreams : BSE_SOURCE_N_JOINT_ICHANNELS (source);
      klass.n_ostreams = n_ostreams >= 0 ? n_ostreams : BSE_SOURCE_N_OCHANNELS (source);
      bse_source_class_cache_engine_class (source_class, &klass);
    }
  return source_class->engine_class;
}

struct HandlerData {
  SynthesisModule::AutoUpdate auto_update;
  double minimum, maximum;
  bool   bool_quantize;
  Effect *effect;
  HandlerData() :
    auto_update (NULL),
    minimum (0),
    maximum (0),
    bool_quantize (false),
    effect (NULL)
  {
  }
  static void
  destroy (gpointer data)
  {
    HandlerData *hd = reinterpret_cast<HandlerData*> (data);
    delete hd;
  }
};

static void
auto_update_data_free (gpointer data)   /* UserThread */
{
  SynthesisModule::AutoUpdateData *adata = static_cast<SynthesisModule::AutoUpdateData*> (data);
  GObject *object = cast (static_cast<CxxBase*> (adata->effect)); // FIXME: rename cast() and get rid of static_cast<>()
  GObjectClass *klass = (GObjectClass*) g_type_class_peek (adata->pspec->owner_type);
  BseSourceClass *source_class = BSE_SOURCE_CLASS (klass);
  if (source_class->property_updated)
    {
      GParamSpec *pspec = g_param_spec_get_redirect_target (adata->pspec);
      if (!pspec)
        pspec = adata->pspec;
      source_class->property_updated ((BseSource*) object, adata->prop_id, adata->tick_stamp, adata->prop_value, pspec);
      g_object_notify (object, pspec->name);
    }
  delete adata;
}

static void
midi_control_handler (gpointer                  handler_data,  /* MIDI Device Thread (and possibly others) */
                      guint64                   tick_stamp,
                      BseMidiSignalType         signal_type,
                      gfloat                    control_value, /* -1 .. +1 */
                      guint                     n_mcdatas,
                      BseModule          *const*modules,
                      gpointer                  user_data,
                      BseTrans                 *trans)
{
  GParamSpec *pspec = static_cast<GParamSpec*> (handler_data);
  g_return_if_fail (n_mcdatas > 0);
  if (!user_data)
    return;     /* ignore events before bse_midi_receiver_set_control_handler_data() */
  HandlerData *hd = static_cast<HandlerData*> (user_data);
  SynthesisModule::AutoUpdateData *adata = new SynthesisModule::AutoUpdateData;
  adata->prop_id = pspec->param_id;
  if (hd->bool_quantize)
    adata->prop_value = control_value >= 0.5 ? 1 : 0;
  else
    adata->prop_value = 0.5 * (hd->minimum + hd->maximum + control_value * (hd->maximum - hd->minimum));
  adata->tick_stamp = tick_stamp;
  adata->pspec = pspec;
  adata->effect = hd->effect;
  for (guint i = 0; i < n_mcdatas; i++)
    bse_trans_add (trans,
                   bse_job_flow_access (modules[i],
                                        tick_stamp,
                                        hd->auto_update,
                                        adata,
                                        i + 1 >= n_mcdatas ? auto_update_data_free : NULL));
}

static void
get_midi_control_range (GParamSpec *pspec,
                        double     &minimum,
                        double     &maximum,
                        bool       &bool_quantize)
{
  if (SFI_IS_PSPEC_REAL (pspec))
    {
      SfiParamSpecReal *rspec = SFI_PSPEC_REAL (pspec);
      minimum = rspec->minimum;
      maximum = rspec->maximum;
    }
  else if (SFI_IS_PSPEC_INT (pspec))
    {
      SfiParamSpecInt *ispec = SFI_PSPEC_INT (pspec);
      minimum = ispec->minimum;
      maximum = ispec->maximum;
    }
  else if (SFI_IS_PSPEC_BOOL (pspec))
    {
      minimum = 0;
      maximum = 1;
      bool_quantize = true;
    }
  else if (SFI_IS_PSPEC_NUM (pspec))
    {
      SfiParamSpecNum *nspec = SFI_PSPEC_NUM (pspec);
      minimum = nspec->minimum;
      maximum = nspec->maximum;
    }
}

struct HandlerSetup {
  Effect                *effect;
  bool                   add_handler;
  guint                  n_aprops;
  BseAutomationProperty *aprops;
  BseMidiReceiver       *midi_receiver;
  guint                  midi_channel;
  static void
  free (gpointer data)  /* User Thread */
  {
    HandlerSetup *hs = reinterpret_cast<HandlerSetup*> (data);
    g_free (hs->aprops);
    g_free (hs);
  }
};

static void
handler_setup_func (BseModule      *module,   /* Engine Thread */
                    gpointer        data)
{
  HandlerSetup *hs = static_cast<HandlerSetup*> (data);
  guint i;
  for (i = 0; i < hs->n_aprops; i++)
    if (hs->add_handler)
      {
        bool has_data = bse_midi_receiver_add_control_handler (hs->midi_receiver,
                                                               hs->aprops[i].midi_channel ? hs->aprops[i].midi_channel : hs->midi_channel,
                                                               hs->aprops[i].signal_type,
                                                               midi_control_handler,
                                                               hs->aprops[i].pspec,
                                                               module);
        if (!has_data)
          {
            HandlerData *hd = new HandlerData;
            get_midi_control_range (hs->aprops[i].pspec, hd->minimum, hd->maximum, hd->bool_quantize);
            hd->auto_update = hs->effect->get_module_auto_update();
            hd->effect = hs->effect;
            bse_midi_receiver_set_control_handler_data (hs->midi_receiver,
                                                        hs->aprops[i].midi_channel ? hs->aprops[i].midi_channel : hs->midi_channel,
                                                        hs->aprops[i].signal_type,
                                                        midi_control_handler,
                                                        hs->aprops[i].pspec,
                                                        hd, HandlerData::destroy);
          }
      }
    else
      bse_midi_receiver_remove_control_handler (hs->midi_receiver,
                                                hs->aprops[i].midi_channel ? hs->aprops[i].midi_channel : hs->midi_channel,
                                                hs->aprops[i].signal_type,
                                                midi_control_handler,
                                                hs->aprops[i].pspec,
                                                module);
}

BseModule*
Effect::integrate_engine_module (unsigned int   context_handle,
                                 BseTrans      *trans)
{
  BseSource *source = cast (this);
  SynthesisModule *cxxmodule = create_module (context_handle, trans);
  BseModule *engine_module = bse_module_new (create_engine_class (cxxmodule), cxxmodule);
  cxxmodule->set_module (engine_module);
  /* intergrate module into engine */
  bse_trans_add (trans, bse_job_integrate (engine_module));
  /* register MIDI control handlers */
  guint n_props = 0;
  BseAutomationProperty *aprops = bse_source_get_automation_properties (source, &n_props);
  if (n_props)
    {
      HandlerSetup *hs = g_new0 (HandlerSetup, 1);
      hs->effect = this;
      hs->add_handler = true;
      hs->n_aprops = n_props;
      hs->aprops = aprops;
      BseMidiContext mc = bse_snet_get_midi_context (bse_item_get_snet (BSE_ITEM (source)), context_handle);
      hs->midi_receiver = mc.midi_receiver;
      hs->midi_channel = mc.midi_channel;
      bse_trans_add (trans, bse_job_access (engine_module, handler_setup_func, hs, HandlerSetup::free));
    }
  return engine_module;
}

void
Effect::dismiss_engine_module (BseModule       *engine_module,
                               guint            context_handle,
                               BseTrans        *trans)
{
  BseSource *source = cast (this);
  if (engine_module)
    {
      /* unregister MIDI control handlers */
      guint n_props = 0;
      BseAutomationProperty *aprops = bse_source_get_automation_properties (source, &n_props);
      if (n_props)
        {
          HandlerSetup *hs = g_new0 (HandlerSetup, 1);
          hs->effect = this;
          hs->add_handler = false;
          hs->n_aprops = n_props;
          hs->aprops = aprops;
          BseMidiContext mc = bse_snet_get_midi_context (bse_item_get_snet (BSE_ITEM (source)), context_handle);
          hs->midi_receiver = mc.midi_receiver;
          hs->midi_channel = mc.midi_channel;
          bse_trans_add (trans, bse_job_access (engine_module, handler_setup_func, hs, HandlerSetup::free));
        }
      /* discard module */
      bse_trans_add (trans, bse_job_discard (engine_module));
    }
}

unsigned int
Effect::block_size() const
{
  g_return_val_if_fail (is_prepared(), 0);

  return bse_engine_block_size();
}

unsigned int
Effect::max_block_size() const
{
  return BSE_STREAM_MAX_VALUES;
}

void
Effect::class_init (CxxBaseClass *klass)
{
  static gpointer effect_parent_class = NULL;
  struct Trampoline
  {
    static void
    effect_context_create (BseSource *source,
                           guint      context_handle,
                           BseTrans  *trans)
    {
      CxxBase *base = cast (source);
      Effect *self = static_cast<Effect*> (base);
      BseModule *engine_module = self->integrate_engine_module (context_handle, trans);
      
      /* setup module i/o streams with BseSource i/o channels */
      bse_source_set_context_module (source, context_handle, engine_module);
      
      /* reset module */
      bse_trans_add (trans, bse_job_force_reset (engine_module));
      /* configure module */
      SynthesisModule::Closure *clo = self->make_module_config_closure();
      if (clo)
        bse_trans_add (trans, bse_job_access (engine_module, access_trampoline, clo, access_data_free));
      
      /* chain parent class' handler */
      BSE_SOURCE_CLASS (effect_parent_class)->context_create (source, context_handle, trans);
    }
    static void
    effect_context_dismiss (BseSource *source,
                            guint      context_handle,
                            BseTrans  *trans)
    {
      CxxBase *base = cast (source);
      Effect *self = static_cast<Effect*> (base);
      BseModule *engine_module = NULL;
      if (BSE_SOURCE_N_ICHANNELS (source))
        {
          engine_module = bse_source_get_context_imodule (source, context_handle);
          bse_source_set_context_imodule (source, context_handle, NULL);
        }
      if (BSE_SOURCE_N_OCHANNELS (source))
        {
          engine_module = bse_source_get_context_omodule (source, context_handle);
          bse_source_set_context_omodule (source, context_handle, NULL);
        }

      self->dismiss_engine_module (engine_module, context_handle, trans);

      /* chain parent class' handler */
      BSE_SOURCE_CLASS (effect_parent_class)->context_dismiss (source, context_handle, trans);
    }
    static void effect_prepare (BseSource *source)
    {
      CxxBase *base = cast (source);
      Effect *self = static_cast<Effect*> (base);

      /* invoke code that the effect might want to execute before prepare */
      self->prepare1();

      /* chain parent class' handler */
      BSE_SOURCE_CLASS (effect_parent_class)->prepare (source);

      /* invoke code that the effect might want to execute after prepare */
      self->prepare2();
    }
    static void effect_reset (BseSource *source)
    {
      CxxBase *base = cast (source);
      Effect *self = static_cast<Effect*> (base);

      /* invoke code that the effect might want to execute before reset */
      self->reset1();

      /* chain parent class' handler */
      BSE_SOURCE_CLASS (effect_parent_class)->reset (source);

      /* invoke code that the effect might want to execute after reset */
      self->reset2();
    }
  };
  BseSourceClass *source_class = klass;

  effect_parent_class = g_type_class_peek_parent (klass);
  source_class->context_create = Trampoline::effect_context_create;
  source_class->context_dismiss = Trampoline::effect_context_dismiss;
  source_class->prepare = Trampoline::effect_prepare;
  source_class->reset = Trampoline::effect_reset;
}

} // Bse