/[svn]/linuxsampler/trunk/src/voice.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/voice.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (show annotations) (download)
Sun Nov 23 21:16:49 2003 UTC (20 years, 5 months ago) by schoenebeck
File size: 9609 byte(s)
* rewrote sustain pedal handling: instead of just queuing the note-offs I
  added a sustain pointer for each midi key which starts to point to the
  first active voice on the respective key and increments to the next voice
  on the key when a note-off arrived, the release velocity value will
  immediately be stored in the respective voice object (this also fixes the
  segmentation fault issue when the sustain pool was full)

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 ***************************************************************************/
22
23 #include "voice.h"
24
25 // FIXME: no support for layers (nor crossfades) yet
26
27 DiskThread* Voice::pDiskThread = NULL;
28
29 Voice::Voice(DiskThread* pDiskThread) {
30 Active = false;
31 Voice::pDiskThread = pDiskThread;
32 }
33
34 Voice::~Voice() {
35 }
36
37 void Voice::Trigger(int MIDIKey, uint8_t Velocity, gig::Instrument* Instrument) {
38 Active = true;
39 this->MIDIKey = MIDIKey;
40 pRegion = Instrument->GetRegion(MIDIKey);
41 PlaybackState = playback_state_ram; // we always start playback from RAM cache and switch then to disk if needed
42 Pos = 0;
43 ReleaseVelocity = 127; // default release velocity value
44
45 if (!pRegion) {
46 std::cerr << "Audio Thread: No Region defined for MIDI key " << MIDIKey << std::endl << std::flush;
47 Active = false;
48 return;
49 }
50
51 //TODO: current MIDI controller values are not taken into account yet
52 gig::DimensionRegion* pDimRgn = NULL;
53 for (int i = pRegion->Dimensions - 1; i >= 0; i--) { // Check if instrument has a velocity split
54 if (pRegion->pDimensionDefinitions[i].dimension == gig::dimension_velocity) {
55 uint DimValues[5] = {0,0,0,0,0};
56 DimValues[i] = Velocity;
57 pDimRgn = pRegion->GetDimensionRegionByValue(DimValues[4],DimValues[3],DimValues[2],DimValues[1],DimValues[0]);
58 break;
59 }
60 }
61 if (!pDimRgn) { // if there was no velocity split
62 pDimRgn = pRegion->GetDimensionRegionByValue(0,0,0,0,0);
63 }
64
65 pSample = pDimRgn->pSample; // sample won't change until the voice is finished
66
67 // Check if the sample needs disk streaming or is too short for that
68 long cachedsamples = pSample->GetCache().Size / pSample->FrameSize;
69 DiskVoice = cachedsamples < pSample->SamplesTotal;
70
71 if (DiskVoice) {
72 MaxRAMPos = cachedsamples - (OutputBufferSize << MAX_PITCH) / pSample->Channels;
73 pDiskThread->OrderNewStream(&DiskStreamRef, pSample, MaxRAMPos);
74 dmsg(5,("Disk voice launched (cached samples: %d, total Samples: %d, MaxRAMPos: %d\n", cachedsamples, pSample->SamplesTotal, MaxRAMPos));
75 }
76 else {
77 MaxRAMPos = cachedsamples;
78 dmsg(5,("RAM only voice launched\n"));
79 }
80
81 CurrentPitch = pow(2, (double) (MIDIKey - (int) pSample->MIDIUnityNote) / (double) 12);
82
83 // ************************************************
84 // TODO: ARTICULATION DATA HANDLING IS MISSING HERE
85 // ************************************************
86 }
87
88 void Voice::RenderAudio() {
89
90 switch (this->PlaybackState) {
91
92 case playback_state_ram: {
93 Interpolate((sample_t*) pSample->GetCache().pStart);
94 if (DiskVoice) {
95 // check if we reached the allowed limit of the sample RAM cache
96 if (Pos > MaxRAMPos) {
97 dmsg(5,("Voice: switching to disk playback (Pos=%f)\n", Pos));
98 this->PlaybackState = playback_state_disk;
99 }
100 }
101 else if (Pos >= pSample->GetCache().Size / pSample->FrameSize) {
102 this->PlaybackState = playback_state_end;
103 }
104 }
105 break;
106
107 case playback_state_disk: {
108 if (!DiskStreamRef.pStream) {
109 // check if the disk thread created our ordered disk stream in the meantime
110 DiskStreamRef.pStream = pDiskThread->AskForCreatedStream(DiskStreamRef.OrderID);
111 if (!DiskStreamRef.pStream) {
112 std::cout << stderr << "Disk stream not available in time!" << std::endl << std::flush;
113 pDiskThread->OrderDeletionOfStream(&DiskStreamRef);
114 this->Active = false;
115 return;
116 }
117 DiskStreamRef.pStream->IncrementReadPos(pSample->Channels * (double_to_int(Pos) - MaxRAMPos));
118 Pos -= double_to_int(Pos);
119 }
120
121 // add silence sample at the end if we reached the end of the stream (for the interpolator)
122 if (DiskStreamRef.State == Stream::state_end && DiskStreamRef.pStream->GetReadSpace() < (OutputBufferSize << MAX_PITCH) / pSample->Channels) {
123 DiskStreamRef.pStream->WriteSilence((OutputBufferSize << MAX_PITCH) / pSample->Channels);
124 this->PlaybackState = playback_state_end;
125 }
126
127 sample_t* ptr = DiskStreamRef.pStream->GetReadPtr(); // get the current read_ptr within the ringbuffer where we read the samples from
128 Interpolate(ptr);
129 DiskStreamRef.pStream->IncrementReadPos(double_to_int(Pos) * pSample->Channels);
130 Pos -= double_to_int(Pos);
131 }
132 break;
133
134 case playback_state_end:
135 Kill(); // free voice
136 break;
137 }
138 }
139
140 void Voice::Interpolate(sample_t* pSrc) {
141 float effective_volume = 1; // TODO: use the art. data instead
142 int i = 0;
143
144 // ************************************************
145 // TODO: ARTICULATION DATA HANDLING IS MISSING HERE
146 // ************************************************
147
148 // FIXME: assuming either mono or stereo
149 if (this->pSample->Channels == 2) { // Stereo Sample
150 while (i < this->OutputBufferSize) {
151 #ifdef USE_LINEAR_INTERPOLATION
152 int pos_int = double_to_int(this->Pos); // integer position
153 float pos_fract = this->Pos - pos_int; // fractional part of position
154 pos_int <<= 1;
155 // left channel
156 this->pOutput[i++] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]));
157 // right channel
158 this->pOutput[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]));
159 #else // polynomial interpolation
160 //FIXME: !!!THIS WON'T WORK!!! needs to be adjusted for stereo, use linear interpolation meanwhile
161 xm1 = pSrc[pos_int];
162 x0 = pSrc[pos_int+1];
163 x1 = pSrc[pos_int+2];
164 x2 = pSrc[pos_int+3];
165 a = (3 * (x0-x1) - xm1 + x2) / 2;
166 b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
167 c = (x1 - xm1) / 2;
168 this->pOutput[u] += effective_volume*((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
169 #endif // USE_LINEAR_INTERPOLATION
170
171 this->Pos += this->CurrentPitch;
172 }
173 }
174 else { // Mono Sample
175 while (i < this->OutputBufferSize) {
176 #ifdef USE_LINEAR_INTERPOLATION
177 int pos_int = double_to_int(this->Pos); // integer position
178 float pos_fract = this->Pos - pos_int; // fractional part of position
179 float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int]));
180 this->pOutput[i] += sample_point;
181 this->pOutput[i+1] += sample_point;
182 i += 2;
183 #else // polynomial interpolation
184 //FIXME: !!!THIS WON'T WORK!!! needs to be adjusted for stereo, use linear interpolation meanwhile
185 xm1 = pSrc[pos_int];
186 x0 = pSrc[pos_int+1];
187 x1 = pSrc[pos_int+2];
188 x2 = pSrc[pos_int+3];
189 a = (3 * (x0-x1) - xm1 + x2) / 2;
190 b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
191 c = (x1 - xm1) / 2;
192 this->pOutput[u] += effective_volume*((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
193 #endif
194
195 this->Pos += this->CurrentPitch;
196 }
197 }
198 }
199
200 void Voice::Kill() {
201 if (DiskVoice && DiskStreamRef.State != Stream::state_unused) {
202 pDiskThread->OrderDeletionOfStream(&DiskStreamRef);
203 }
204 Active = false;
205 }

  ViewVC Help
Powered by ViewVC