summaryrefslogtreecommitdiff
path: root/muse2/synti/simpledrums2/simpledrums.h
blob: f4bf27ee8b334239b14dc5bec637c1386cc1b090 (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
//
// C++ Interface: simplesynth
//
// Description:
//
//
// Author: Mathias Lundgren <lunar_shuttle@users.sf.net>, (C) 2004
//  Contributer: (C) Copyright 2011 Tim E. Real (terminator356 at users.sourceforge.net)
//
//  This program 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; version 2 of
//  the License, or (at your option) any later version.
//
//  This program 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 this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//
//
#ifndef SIMPLESYNTH_H
#define SIMPLESYNTH_H

#include <sndfile.h>
#include "libsynti/mess.h"
#include "common.h"
#include "common_defs.h"
//#include "libsynti/mpevent.h"
#include "muse/mpevent.h"   
#include "simpledrumsgui.h"
#include "ssplugin.h"

#define SS_NO_SAMPLE       0
#define SS_NO_PLUGIN       0

#define SS_PROCESS_BUFFER_SIZE 4096 //TODO: Add initialization method for nr of frames in each process from MusE - if nr of frames > than this, this will fail
#define SS_SENDFX_BUFFER_SIZE  SS_PROCESS_BUFFER_SIZE

enum SS_ChannelState
   {
      SS_CHANNEL_INACTIVE=0,
      SS_SAMPLE_PLAYING,
   };

enum SS_State
   {
      SS_INITIALIZING=0,
      SS_LOADING_SAMPLE,
      SS_CLEARING_SAMPLE,
      SS_RUNNING,
   };

enum SS_SendFXState
   {
      SS_SENDFX_OFF=0,
      SS_SENDFX_ON
   };

struct SS_SendFx
   {
      SS_SendFXState state;
      LadspaPlugin*  plugin;
      int            inputs;
      int            outputs;
      int            retgain_ctrlval;
      double         retgain;
      int            nrofparameters;
   };

struct SS_Sample
   {
      SS_Sample() { data = 0; }
      float*      data;
      int         samplerate;
      //int         bits;
      std::string filename;
      long        samples;
      long        frames;
      int         channels;
      //SF_INFO     sfinfo;
   };

struct SS_Channel
   {
      SS_ChannelState state;
      const char*     name;
      SS_Sample*      sample;
      SS_Sample*      originalSample;
      int             playoffset;
      bool            noteoff_ignore;

      double          volume;
      int             volume_ctrlval;

      double          cur_velo;
      double          gain_factor;

      int             pan;
      double          balanceFactorL;
      double          balanceFactorR;
      int             pitchInt;

      bool            channel_on;

      //Send fx:
      double          sendfxlevel[SS_NR_OF_SENDEFFECTS];
   };

struct SS_Controller
   {
      std::string name;
      int num;
      int min, max;
   };

struct SS_SampleLoader
   {
      SS_Channel*  channel;
      std::string  filename;
      int          ch_no;
   };

double rangeToPitch(int value);
//int pitchToRange(double pitch);

class SimpleSynth : public Mess
   {
   public:
      SimpleSynth(int);

      virtual ~SimpleSynth();

      //virtual bool guiVisible() const;
      //virtual bool hasGui() const;
      virtual bool nativeGuiVisible() const;
      virtual bool hasNativeGui() const;
      virtual bool playNote(int arg1, int arg2, int arg3);
      virtual bool processEvent(const MusECore::MidiPlayEvent& arg1);
      virtual bool setController(int arg1, int arg2, int arg3);
      virtual bool sysex(int arg1, const unsigned char* arg2);
      virtual QString getPatchName(int arg1, int arg2, bool arg3) const; 
      virtual const MidiPatch* getPatchInfo(int arg1, const MidiPatch* arg2) const;
      virtual int getControllerInfo(int arg1, const char** arg2, int* arg3, int* arg4, int* arg5, int* arg6) const;
      virtual void processMessages();
      virtual void process(float** data, int offset, int len);
      //virtual void showGui(bool arg1);
      virtual void showNativeGui(bool arg1);
      ///virtual void getInitData(int*, const unsigned char**) const;
      virtual void getInitData(int*, const unsigned char**);
      // This is only a kludge required to support old songs' midistates. Do not use in any new synth.
      virtual int oldMidiStateHeader(const unsigned char** data) const;
      bool init(const char* name);
      void guiSendSampleLoaded(bool success, int ch, const char* filename);
      void guiSendError(const char* errorstring);

      static const char* synth_state_descr[];
      static const char* channel_state_descr[];

private:
      SimpleSynthGui* gui;

      byte* initBuffer;
      int initLen;
      void setupInitBuffer(int len);
      
      SS_Channel channels[SS_NR_OF_CHANNELS];
      SS_Controller controllers[SS_NR_OF_CONTROLLERS];
      bool setController(int channel, int id, int val, bool fromGui);
      bool loadSample(int ch_no, const char* filename);
      void parseInitData(const unsigned char* data);
      void updateVolume(int ch, int in_volume_ctrlval);
      void updatePitch(int ch, int inpitch_ctrlval);
      void updateBalance(int ch, int pan);
      void guiNotifySampleCleared(int ch);
      void guiUpdateBalance(int ch, int bal);
      void guiUpdatePitch(int ch, int bal);
      void guiUpdateVolume(int ch, int val);
      void guiUpdateNoff(int ch, bool b);
      void guiUpdateChoff(int ch, bool b);
      void guiUpdateMasterVol(int val);
      void guiUpdateFxParameter(int fxid, int param, float val);
      void guiUpdateSendFxLevel(int channel, int fxid, int level);
      bool initSendEffect(int sendeffectid, QString lib, QString name);
      void setSendFxLevel(int channel, int effectid, double val);
      void cleanupPlugin(int id);
      void setFxParameter(int fxid, int param, float val);
      void clearSample(int ch);

      double master_vol;
      int master_vol_ctrlval;

      //Send effects:
      SS_SendFx sendEffects[SS_NR_OF_SENDEFFECTS];
      float* sendFxLineOut[SS_NR_OF_SENDEFFECTS][2]; //stereo output (fed into LADSPA inputs),sent from the individual channels -> LADSPA fx
      float* sendFxReturn[SS_NR_OF_SENDEFFECTS][2];  //stereo inputs, from LADSPA plugins, sent from LADSPA -> SS and added to the mix
      double* processBuffer[2];
   };

void resample(SS_Sample *origSmp, SS_Sample* newSample, double pitch);
static void* loadSampleThread(void*);
static pthread_mutex_t SS_LoaderMutex;
static SS_State synth_state;
static SimpleSynth* simplesynth_ptr;

#endif