summaryrefslogtreecommitdiff
path: root/lv2/lv2.cc
blob: 41a5d4852f93ff314daec454b9f2916d9892d456 (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            lv2.cc
 *
 *  Wed Jul 13 13:50:33 CEST 2011
 *  Copyright 2011 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include <lv2/lv2plug.in/ns/lv2core/lv2.h>
#include <lv2/lv2plug.in/ns/ext/atom/atom.h>

#include <stdlib.h>
#include <string.h>

#include "lv2_gui.h"
#include "lv2_instance.h"

#include <hugin.hpp>

#define DRUMGIZMO_URI "http://drumgizmo.org/lv2"
#define NS_DG DRUMGIZMO_URI "/atom#"

// Stuff to handle DrumGizmo* transmission from instance to GUI.
static LV2_DrumGizmo_Descriptor dg_descriptor;

static DrumGizmo *dg_get_pci(LV2_Handle instance)
{
  DGLV2 *dglv2 = (DGLV2 *)instance;
  return dglv2->dg;
}

LV2_State_Status
dg_save(LV2_Handle                 instance,
        LV2_State_Store_Function   store,
        LV2_State_Handle           handle,
        uint32_t                   flags,
        const LV2_Feature *const * features)
{
  DGLV2 *dglv2 = (DGLV2 *)instance;

  if(!dglv2 || !dglv2->map || !dglv2->map->map) {
    // Missing urid feature?
    return  LV2_STATE_ERR_NO_FEATURE;
  }

  std::string config = dglv2->dg->configString();

  store(handle,
        dglv2->map->map(dglv2->map->handle, NS_DG "config"),
        config.c_str(),
        config.length() + 1, // Careful!  Need space for terminator
        dglv2->map->map(dglv2->map->handle, LV2_ATOM__Chunk),
        LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);

  return LV2_STATE_SUCCESS;
}

LV2_State_Status
dg_restore(LV2_Handle                  instance,
           LV2_State_Retrieve_Function retrieve,
           LV2_State_Handle            handle,
           uint32_t                    flags,
           const LV2_Feature *const *  features)
{
  DGLV2 *dglv2 = (DGLV2 *)instance;

  if(!dglv2 || !dglv2->map || !dglv2->map->map) {
    // Missing urid feature?
    return  LV2_STATE_ERR_NO_FEATURE;
  }

  size_t size;
  uint32_t type;

  const char* data =
    (const char*)retrieve(handle,
                          dglv2->map->map(dglv2->map->handle, NS_DG "config"),
                          &size, &type, &flags);

  DEBUG(lv2, "Config string size: %d, data*: %p\n", (int)size, data);

  if(data && size) {
    std::string config;
    config.append(data, size - 1);
    dglv2->dg->setConfigString(config);
  }

  return LV2_STATE_SUCCESS;
}

static LV2_State_Interface dg_persist = {
  dg_save,
  dg_restore
};

LV2_Handle instantiate(const struct _LV2_Descriptor *descriptor,
                       double sample_rate,
                       const char *bundle_path,
                       const LV2_Feature *const *features)
{
  DGLV2 *dglv2 = new DGLV2;

  dglv2->map = NULL;
  for (int i = 0 ; features[i] ; i++) {
    if (!strcmp(features[i]->URI,  LV2_URID_URI "#map")) {
      dglv2->map = (LV2_URID_Map*)features[i]->data;
    }
 }

  dg_descriptor.get_pci = dg_get_pci;

  dglv2->in = new InputLV2();
  dglv2->out = new OutputLV2();

  dglv2->buffer = NULL;
  dglv2->buffer_size = 0;

  dglv2->dg = new DrumGizmo(dglv2->out, dglv2->in);
  dglv2->dg->setSamplerate(sample_rate);

  return (LV2_Handle)dglv2;
}

void connect_port(LV2_Handle instance,
                  uint32_t port,
                  void *data_location)
{
  DGLV2 *dglv2 = (DGLV2 *)instance;

  if(port == 0) {// MIDI in
    dglv2->in->eventPort = (LV2_Atom_Sequence*)data_location;
  } else {// Audio Port
    if(port - 1 < NUM_OUTPUTS) {
      dglv2->out->outputPorts[port - 1].samples = (sample_t*)data_location;
      dglv2->out->outputPorts[port - 1].size = 0;
    }
  }
}

void activate(LV2_Handle instance)
{
  // We don't really need to do anything here.
  DGLV2 *dglv2 = (DGLV2 *)instance;
  (void)dglv2;
}

void run(LV2_Handle instance,
         uint32_t sample_count)
{
  static size_t pos = 0;
  DGLV2 *dglv2 = (DGLV2 *)instance;

  dglv2->dg->run(pos, dglv2->buffer, sample_count);

  pos += sample_count;
}

void deactivate(LV2_Handle instance)
{
  // We don't really need to do anything here.
  DGLV2 *dglv2 = (DGLV2 *)instance;
  dglv2->dg->stop();
}

void cleanup(LV2_Handle instance)
{
  DGLV2 *dglv2 = (DGLV2 *)instance;
  delete dglv2->dg;
  delete dglv2->in;
  delete dglv2->out;
}

const void* extension_data(const char *uri)
{
  if(!strcmp(uri, PLUGIN_INSTANCE_URI)) return &dg_descriptor;
  if(!strcmp(uri, LV2_STATE__interface)) return &dg_persist;
  return NULL;
}

#ifdef __cplusplus
extern "C" {
#endif

static const LV2_Descriptor descriptor = {
	DRUMGIZMO_URI,
	instantiate,
	connect_port,
  activate,
	run,
  deactivate,
	cleanup,
	extension_data
};

LV2_SYMBOL_EXPORT
const LV2_Descriptor* lv2_descriptor(uint32_t index)
{
	switch (index) {
	case 0:
		return &descriptor;
	default:
		return NULL;
	}
}

#ifdef __cplusplus
}
#endif