summaryrefslogtreecommitdiff
path: root/attic/muse_qt4_evolution/muse/ticksynth.cpp
blob: 861861de8a2fa27d01cc9e44f6a8b090b9039c62 (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
//=============================================================================
//  MusE
//  Linux Music Editor
//  $Id:$
//
//  Copyright (C) 2002-2006 by Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  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., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include "audio.h"
#include "ticksynth.h"
#include "default_click.h"

SynthI* metronome = 0;

class MetronomeSynth;
static MetronomeSynth* metronomeSynth;

//---------------------------------------------------------
//   MetronomeSynth
//---------------------------------------------------------

class MetronomeSynth : public Synth {
   public:
      MetronomeSynth(const QFileInfo* fi) : Synth(fi, QString("Metronome")) {}
      virtual ~MetronomeSynth() {}
      virtual void incInstances(int) {}
      virtual void* instantiate();
      virtual SynthIF* createSIF(SynthI*);
      };

//---------------------------------------------------------
//   instantiate
//---------------------------------------------------------

void* MetronomeSynth::instantiate()
      {
      return 0;
      }

//---------------------------------------------------------
//   MetronomeSynthIF
//---------------------------------------------------------

class MetronomeSynthIF : public SynthIF
      {
      const float* data;
      int pos;
      int len;
      void process(float** buffer, int offset, int n);

   public:
      MetronomeSynthIF(SynthI* s) : SynthIF(s) {
            data = 0;
            }
      virtual bool guiVisible() const { return false; }
      virtual void showGui(bool) {}
      virtual bool hasGui() const { return false; }
      virtual void getGeometry(int*, int*, int*, int*) const {}
      virtual void setGeometry(int, int, int, int) {}
      virtual void getData(MidiEventList*, unsigned pos, int ports, unsigned n, float** buffer);
      virtual bool putEvent(const MidiEvent& ev);
      virtual MidiEvent receiveEvent() { return MidiEvent(); }
      virtual int eventsPending() const { return 0; }
      virtual int channels() const { return 1; }
      virtual void deactivate2() {}
      virtual void deactivate3() {}
      virtual QString getPatchName(int, int, int) const { return ""; }
      virtual QString getPatchName(int, int) { return ""; }
      virtual void populatePatchPopup(QMenu*, int) {};
      virtual void write(Xml&) const {}
      virtual void setParameter(int, float) {}
      virtual int getControllerInfo(int, const char**, int*, int*, int*) { return 0; }
      virtual bool hasAuxSend() const  { return false; }
      };

//---------------------------------------------------------
//   getData
//---------------------------------------------------------

void MetronomeSynthIF::getData(MidiEventList* el, unsigned pos, int/*ports*/, unsigned n, float** buffer)
      {
      unsigned curPos      = pos;
      unsigned endPos      = pos + n;
      unsigned off         = pos;

      iMidiEvent i = el->begin();
      for (; i != el->end(); ++i) {
            unsigned frame = i->time();
            if (frame >= endPos)
                  break;
            if (frame > curPos) {
                  if (frame < pos)
                        printf("should not happen: missed event %d\n", pos -frame);
                  else
                        process(buffer, curPos-pos, frame - curPos);
                  curPos = frame;
                  }
            putEvent(*i);
            }
      if (endPos - curPos)
            process(buffer, curPos - off, endPos - curPos);
      el->erase(el->begin(), i);
      }

//---------------------------------------------------------
//   putEvent
//---------------------------------------------------------

bool MetronomeSynthIF::putEvent(const MidiEvent& ev)
      {
      if (ev.dataA() == 0) {
            data = defaultClickEmphasis;
            len  = defaultClickEmphasisLength;
            }
      else {
            data = defaultClick;
            len  = defaultClickLength;
            }
      pos = 0;
      return false;
      }

//---------------------------------------------------------
//   createSIF
//---------------------------------------------------------

SynthIF* MetronomeSynth::createSIF(SynthI* s)
      {
      return new MetronomeSynthIF(s);
      }

//---------------------------------------------------------
//   process
//    synthesize n samples into buffer+offset
//---------------------------------------------------------

void MetronomeSynthIF::process(float** buffer, int offset, int n)
      {
      if (data == 0) {
            memset(buffer[0], 0, n * sizeof(float));
            return;
            }
      const float* s = data + pos;
      float* d       = *buffer + offset;
      int l          = std::min(n, len);
      int i;
      for (i = 0; i < l; ++i)
            *d++ = *s++;
      for (; i < n; ++i)
            *d++ = 0.0f;
      pos += l;
      len -= l;
      if (len <= 0)
            data = 0;
      }

//---------------------------------------------------------
//   initMetronome
//---------------------------------------------------------

void initMetronome()
      {
      QFileInfo fi;     // dummy
      metronomeSynth = new MetronomeSynth(&fi);
      metronome = new SynthI();
      metronome->setName("metronome");
      metronome->initInstance(metronomeSynth);
      }