1 /*
2   ZynAddSubFX - a software synthesizer
3 
4   Part.h - Part implementation
5   Copyright (C) 2002-2005 Nasca Octavian Paul
6   Author: Nasca Octavian Paul
7 
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12 
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License (version 2 or later) for more details.
17 
18   You should have received a copy of the GNU General Public License (version 2)
19   along with this program; if not, write to the Free Software Foundation,
20   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 
22 */
23 
24 #ifndef PART_H
25 #define PART_H
26 
27 #define MAX_INFO_TEXT_SIZE 1000
28 
29 #include "../globals.h"
30 #include "../Params/Controller.h"
31 #include "../Misc/Microtonal.h"
32 
33 #include <pthread.h>
34 #include <list> // For the monomemnotes list.
35 
36 class EffectMgr;
37 class ADnoteParameters;
38 class SUBnoteParameters;
39 class PADnoteParameters;
40 class SynthNote;
41 class XMLWrapper;
42 class FFTwrapper;
43 
44 /** Part implementation*/
45 class Part
46 {
47     public:
48         /**Constructor
49          * @param microtonal_ Pointer to the microtonal object
50          * @param fft_ Pointer to the FFTwrapper
51          * @param mutex_ Pointer to the master pthread_mutex_t*/
52         Part(Microtonal *microtonal_, FFTwrapper *fft_, pthread_mutex_t *mutex_);
53         /**Destructor*/
54         ~Part();
55 
56         // Midi commands implemented
57         void NoteOn(unsigned char note,
58                     unsigned char velocity,
59                     int masterkeyshift);
60         void NoteOff(unsigned char note);
61         void PolyphonicAftertouch(unsigned char note,
62                                   unsigned char velocity,
63                                   int masterkeyshift);
64         void AllNotesOff(); //panic
65         void SetController(unsigned int type, int par);
66         void RelaseSustainedKeys(); //this is called when the sustain pedal is relased
67         void RelaseAllKeys(); //this is called on AllNotesOff controller
68 
69         /* The synthesizer part output */
70         void ComputePartSmps(); //Part output
71 
72         //instrumentonly: 0 - save all, 1 - save only instrumnet, 2 - save only instrument without the name(used in bank)
73 
74 
75         //saves the instrument settings to a XML file
76         //returns 0 for ok or <0 if there is an error
77         int saveXML(const char *filename);
78         int loadXMLinstrument(const char *filename);
79 
80         void add2XML(XMLwrapper *xml);
81         void add2XMLinstrument(XMLwrapper *xml);
82 
83         void defaults();
84         void defaultsinstrument();
85 
86         void applyparameters(bool lockmutex = true);
87 
88         void getfromXML(XMLwrapper *xml);
89         void getfromXMLinstrument(XMLwrapper *xml);
90 
91         void cleanup(bool final = false);
92 
93         //the part's kit
94         struct {
95             unsigned char      Penabled, Pmuted, Pminkey, Pmaxkey;
96             unsigned char     *Pname;
97             unsigned char      Padenabled, Psubenabled, Ppadenabled;
98             unsigned char      Psendtoparteffect;
99             ADnoteParameters  *adpars;
100             SUBnoteParameters *subpars;
101             PADnoteParameters *padpars;
102         } kit[NUM_KIT_ITEMS];
103 
104 
105         //Part parameters
106         void setkeylimit(unsigned char Pkeylimit);
107         void setkititemstatus(int kititem, int Penabled_);
108 
109         unsigned char Penabled; /**<if the part is enabled*/
110         unsigned char Pvolume; /**<part volume*/
111         unsigned char Pminkey; /**<the minimum key that the part receives noteon messages*/
112         unsigned char Pmaxkey; //the maximum key that the part receives noteon messages
113         void setPvolume(char Pvolume);
114         unsigned char Pkeyshift; //Part keyshift
115         unsigned char Prcvchn; //from what midi channel it receive commnads
116         unsigned char Ppanning; //part panning
117         void setPpanning(char Ppanning);
118         unsigned char Pvelsns; //velocity sensing (amplitude velocity scale)
119         unsigned char Pveloffs; //velocity offset
120         unsigned char Pnoteon; //if the part receives NoteOn messages
121         unsigned char Pkitmode; //if the kitmode is enabled
122         unsigned char Pdrummode; //if all keys are mapped and the system is 12tET (used for drums)
123 
124         unsigned char Ppolymode; //Part mode - 0=monophonic , 1=polyphonic
125         unsigned char Plegatomode; // 0=normal, 1=legato
126         unsigned char Pkeylimit; //how many keys are alowed to be played same time (0=off), the older will be relased
127 
128         unsigned char *Pname; //name of the instrument
129         struct { //instrument additional information
130             unsigned char Ptype;
131             unsigned char Pauthor[MAX_INFO_TEXT_SIZE + 1];
132             unsigned char Pcomments[MAX_INFO_TEXT_SIZE + 1];
133         } info;
134 
135 
136         float *partoutl; //Left channel output of the part
137         float *partoutr; //Right channel output of the part
138 
139         float *partfxinputl[NUM_PART_EFX + 1], //Left and right signal that pass thru part effects;
140         *partfxinputr[NUM_PART_EFX + 1];          //partfxinput l/r [NUM_PART_EFX] is for "no effect" buffer
141 
142         enum NoteStatus {
143             KEY_OFF, KEY_PLAYING, KEY_RELASED_AND_SUSTAINED, KEY_RELASED
144         };
145 
146         float volume, oldvolumel, oldvolumer; //this is applied by Master
147         float panning; //this is applied by Master, too
148 
149         Controller ctl; //Part controllers
150 
151         EffectMgr    *partefx[NUM_PART_EFX]; //insertion part effects (they are part of the instrument)
152         unsigned char Pefxroute[NUM_PART_EFX]; //how the effect's output is routed(to next effect/to out)
153         bool Pefxbypass[NUM_PART_EFX]; //if the effects are bypassed
154 
155 
156         pthread_mutex_t *mutex;
157         pthread_mutex_t load_mutex;
158 
159         int lastnote;
160 
161     private:
162         void RunNote(unsigned k);
163         void KillNotePos(int pos);
164         void RelaseNotePos(int pos);
165         void MonoMemRenote(); // MonoMem stuff.
166 
167         int killallnotes; //is set to 1 if I want to kill all notes
168 
169         struct PartNotes {
170             NoteStatus status;
171             int note; //if there is no note playing, the "note"=-1
172             int itemsplaying;
173             struct {
174                 SynthNote *adnote,
175                    *subnote,
176                    *padnote;
177                 int sendtoparteffect;
178             } kititem[NUM_KIT_ITEMS];
179             int time;
180         };
181 
182         int  lastpos, lastposb; // To keep track of previously used pos and posb.
183         bool lastlegatomodevalid; // To keep track of previous legatomodevalid.
184 
185         // MonoMem stuff
186         std::list<unsigned char> monomemnotes; // A list to remember held notes.
187         struct {
188             unsigned char velocity;
189             int mkeyshift; // I'm not sure masterkeyshift should be remembered.
190         } monomem[256];
191         /* 256 is to cover all possible note values.
192            monomem[] is used in conjunction with the list to
193            store the velocity and masterkeyshift values of a given note (the list only store note values).
194            For example 'monomem[note].velocity' would be the velocity value of the note 'note'.*/
195 
196         PartNotes partnote[POLIPHONY];
197 
198         float oldfreq;    //this is used for portamento
199         Microtonal *microtonal;
200         FFTwrapper *fft;
201 };
202 
203 #endif
204