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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations) (download)
Sat Nov 29 15:17:48 2003 UTC (20 years, 5 months ago) by schoenebeck
File size: 9646 byte(s)
* implemented velocity->volume mapping: samples are now played back with a
  volume appropriate to the velocity of the triggered key, the velocity
  curve transformation functions (in src/gig.h) used for this are so far
  only an approximation to the ones from Gigasampler

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 Volume = pDimRgn->GetVelocityAttenuation(Velocity);
83
84 // ************************************************
85 // TODO: ARTICULATION DATA HANDLING IS MISSING HERE
86 // ************************************************
87 }
88
89 void Voice::RenderAudio() {
90
91 switch (this->PlaybackState) {
92
93 case playback_state_ram: {
94 Interpolate((sample_t*) pSample->GetCache().pStart);
95 if (DiskVoice) {
96 // check if we reached the allowed limit of the sample RAM cache
97 if (Pos > MaxRAMPos) {
98 dmsg(5,("Voice: switching to disk playback (Pos=%f)\n", Pos));
99 this->PlaybackState = playback_state_disk;
100 }
101 }
102 else if (Pos >= pSample->GetCache().Size / pSample->FrameSize) {
103 this->PlaybackState = playback_state_end;
104 }
105 }
106 break;
107
108 case playback_state_disk: {
109 if (!DiskStreamRef.pStream) {
110 // check if the disk thread created our ordered disk stream in the meantime
111 DiskStreamRef.pStream = pDiskThread->AskForCreatedStream(DiskStreamRef.OrderID);
112 if (!DiskStreamRef.pStream) {
113 std::cout << stderr << "Disk stream not available in time!" << std::endl << std::flush;
114 pDiskThread->OrderDeletionOfStream(&DiskStreamRef);
115 this->Active = false;
116 return;
117 }
118 DiskStreamRef.pStream->IncrementReadPos(pSample->Channels * (double_to_int(Pos) - MaxRAMPos));
119 Pos -= double_to_int(Pos);
120 }
121
122 // add silence sample at the end if we reached the end of the stream (for the interpolator)
123 if (DiskStreamRef.State == Stream::state_end && DiskStreamRef.pStream->GetReadSpace() < (OutputBufferSize << MAX_PITCH) / pSample->Channels) {
124 DiskStreamRef.pStream->WriteSilence((OutputBufferSize << MAX_PITCH) / pSample->Channels);
125 this->PlaybackState = playback_state_end;
126 }
127
128 sample_t* ptr = DiskStreamRef.pStream->GetReadPtr(); // get the current read_ptr within the ringbuffer where we read the samples from
129 Interpolate(ptr);
130 DiskStreamRef.pStream->IncrementReadPos(double_to_int(Pos) * pSample->Channels);
131 Pos -= double_to_int(Pos);
132 }
133 break;
134
135 case playback_state_end:
136 Kill(); // free voice
137 break;
138 }
139 }
140
141 void Voice::Interpolate(sample_t* pSrc) {
142 float effective_volume = this->Volume;
143 int i = 0;
144
145 // ************************************************
146 // TODO: ARTICULATION DATA HANDLING IS MISSING HERE
147 // ************************************************
148
149 // FIXME: assuming either mono or stereo
150 if (this->pSample->Channels == 2) { // Stereo Sample
151 while (i < this->OutputBufferSize) {
152 #ifdef USE_LINEAR_INTERPOLATION
153 int pos_int = double_to_int(this->Pos); // integer position
154 float pos_fract = this->Pos - pos_int; // fractional part of position
155 pos_int <<= 1;
156 // left channel
157 this->pOutput[i++] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]));
158 // right channel
159 this->pOutput[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]));
160 #else // polynomial interpolation
161 //FIXME: !!!THIS WON'T WORK!!! needs to be adjusted for stereo, use linear interpolation meanwhile
162 xm1 = pSrc[pos_int];
163 x0 = pSrc[pos_int+1];
164 x1 = pSrc[pos_int+2];
165 x2 = pSrc[pos_int+3];
166 a = (3 * (x0-x1) - xm1 + x2) / 2;
167 b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
168 c = (x1 - xm1) / 2;
169 this->pOutput[u] += effective_volume*((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
170 #endif // USE_LINEAR_INTERPOLATION
171
172 this->Pos += this->CurrentPitch;
173 }
174 }
175 else { // Mono Sample
176 while (i < this->OutputBufferSize) {
177 #ifdef USE_LINEAR_INTERPOLATION
178 int pos_int = double_to_int(this->Pos); // integer position
179 float pos_fract = this->Pos - pos_int; // fractional part of position
180 float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int]));
181 this->pOutput[i] += sample_point;
182 this->pOutput[i+1] += sample_point;
183 i += 2;
184 #else // polynomial interpolation
185 //FIXME: !!!THIS WON'T WORK!!! needs to be adjusted for stereo, use linear interpolation meanwhile
186 xm1 = pSrc[pos_int];
187 x0 = pSrc[pos_int+1];
188 x1 = pSrc[pos_int+2];
189 x2 = pSrc[pos_int+3];
190 a = (3 * (x0-x1) - xm1 + x2) / 2;
191 b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
192 c = (x1 - xm1) / 2;
193 this->pOutput[u] += effective_volume*((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
194 #endif
195
196 this->Pos += this->CurrentPitch;
197 }
198 }
199 }
200
201 void Voice::Kill() {
202 if (DiskVoice && DiskStreamRef.State != Stream::state_unused) {
203 pDiskThread->OrderDeletionOfStream(&DiskStreamRef);
204 }
205 Active = false;
206 }

  ViewVC Help
Powered by ViewVC