plugin.h 2.91 KB
Newer Older
jhammen's avatar
jhammen committed
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
/*
 * This file is part of handrumr.lv2.
 *
 * handrumr.lv2 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 3 of the License, or
 * (at your option) any later version.
 *
 * handrumr.lv2 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 handrumr.lv2.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef PLUGIN_H
#define PLUGIN_H

#include <stdarg.h>
#include <stdlib.h>
#include <sndfile.h>

#include "lv2/lv2plug.in/ns/lv2core/lv2.h"
#include "lv2/lv2plug.in/ns/lv2core/lv2_util.h"
#include "lv2/lv2plug.in/ns/ext/atom/util.h"
#include "lv2/lv2plug.in/ns/ext/atom/forge.h"
#include "lv2/lv2plug.in/ns/ext/midi/midi.h"
#include "lv2/lv2plug.in/ns/ext/state/state.h"
#include "lv2/lv2plug.in/ns/ext/urid/urid.h"
#include "lv2/lv2plug.in/ns/ext/parameters/parameters.h"

#include "instrument.h"

35
36
#define HANDRUMR_URI          "http://j2page.org/lv2/handrumr"
#define HANDRUMR2_URI         HANDRUMR_URI "2"
jhammen's avatar
jhammen committed
37
#define HANDRUMR16_URI        HANDRUMR_URI "16"
jhammen's avatar
jhammen committed
38
39
40
41
42
43
44
45
#define HANDRUMR__config      HANDRUMR_URI "#config"

#define MAX_POLYPHONY 1024

namespace handrumr {

class SamplePlay
{
46
    SampleFileBuffer *mSample;
jhammen's avatar
jhammen committed
47
48
49
50
51
    int32_t index;
    uint32_t offset;
public:
    SamplePlay() : index(-1) { }
    bool active() { return index >= 0; }
52
53
    void activate(SampleFileBuffer *s, uint32_t off) {
        mSample = s, offset = off, index = 0;
jhammen's avatar
jhammen committed
54
55
56
57
    }
    float next(uint32_t i) {
        if(i < offset) { return 0; }
        float &ret = mSample->data()[index++];
58
        if(index == mSample->size()) { index = -1; }
jhammen's avatar
jhammen committed
59
60
61
        offset = 0;
        return ret;
    }
62
63
    uint16_t channel() { return mSample->channel(); }
    void cancel() { index = -1; }
jhammen's avatar
jhammen committed
64
65
66
67
68
69
70
71
72
73
74
75
};

struct PluginURIs {
    LV2_URID midi_Event;
    LV2_URID atom_string;
    LV2_URID hd_config;
};

class Plugin
{
  bool mActivated;
  const LV2_Atom_Sequence* controlPort;
76
  float **outputPort;
jhammen's avatar
jhammen committed
77
78
  LV2_URID_Map *map;
  PluginURIs uris;
79
  uint16_t channelCount;
jhammen's avatar
jhammen committed
80
81
  // runtime
  SamplePlay active[MAX_POLYPHONY];
82
  uint16_t maxActive;
jhammen's avatar
jhammen committed
83
84
85
  SamplePlay *nextSlot();
  // config
  Instrument *instruments;
86
  uint16_t instrumentCount;
jhammen's avatar
jhammen committed
87
  string sampleBase;
jhammen's avatar
jhammen committed
88
public:
89
  Plugin(const LV2_Feature* const*, uint16_t channelCount);
jhammen's avatar
jhammen committed
90
91
92
93
94
95
96
97
98
99
100
101
  void activated(bool state) { mActivated = state; }
  void connectPort(uint32_t port, void *data);
  LV2_State_Status restore(LV2_State_Retrieve_Function, LV2_State_Handle,
                           uint32_t, const LV2_Feature* const*);
  LV2_State_Status save(LV2_State_Store_Function, LV2_State_Handle,
                        uint32_t, const LV2_Feature* const*);
  void run(uint32_t sampleCount);
};

}

#endif // PLUGIN_H