/[svn]/linuxsampler/tags/start/voice.cpp
ViewVC logotype

Annotation of /linuxsampler/tags/start/voice.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (hide annotations) (download)
Sat Oct 25 20:24:31 2003 UTC (20 years, 6 months ago) by schoenebeck
Original Path: linuxsampler/trunk/voice.cpp
File size: 9517 byte(s)
Initial revision

1 schoenebeck 5 /***************************************************************************
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     pRegion = Instrument->GetRegion(MIDIKey);
40     PlaybackState = playback_state_ram; // we always start playback from RAM cache and switch then to disk if needed
41     Pos = 0;
42    
43     if (!pRegion) {
44     std::cerr << "Audio Thread: No Region defined for MIDI key " << MIDIKey << std::endl << std::flush;
45     Active = false;
46     return;
47     }
48    
49     //TODO: current MIDI controller values are not taken into account yet
50     gig::DimensionRegion* pDimRgn = NULL;
51     for (int i = pRegion->Dimensions - 1; i >= 0; i--) { // Check if instrument has a velocity split
52     if (pRegion->pDimensionDefinitions[i].dimension == gig::dimension_velocity) {
53     uint DimValues[5] = {0,0,0,0,0};
54     DimValues[i] = Velocity;
55     pDimRgn = pRegion->GetDimensionRegionByValue(DimValues[4],DimValues[3],DimValues[2],DimValues[1],DimValues[0]);
56     break;
57     }
58     }
59     if (!pDimRgn) { // if there was no velocity split
60     pDimRgn = pRegion->GetDimensionRegionByValue(0,0,0,0,0);
61     }
62    
63     pSample = pDimRgn->pSample; // sample won't change until the voice is finished
64    
65     // Check if the sample needs disk streaming or is too short for that
66     long cachedsamples = pSample->GetCache().Size / pSample->FrameSize;
67     DiskVoice = cachedsamples < pSample->SamplesTotal;
68    
69     if (DiskVoice) {
70     MaxRAMPos = cachedsamples - (OutputBufferSize << MAX_PITCH) / pSample->Channels;
71     pDiskThread->OrderNewStream(&DiskStreamRef, pSample, MaxRAMPos);
72     dmsg(("Disk voice launched (cached samples: %d, total Samples: %d, MaxRAMPos: %d\n", cachedsamples, pSample->SamplesTotal, MaxRAMPos));
73     }
74     else {
75     MaxRAMPos = cachedsamples;
76     dmsg(("RAM only voice launched\n"));
77     }
78    
79     CurrentPitch = pow(2, (double) (MIDIKey - (int) pSample->MIDIUnityNote) / (double) 12);
80    
81     // ************************************************
82     // TODO: ARTICULATION DATA HANDLING IS MISSING HERE
83     // ************************************************
84     }
85    
86     void Voice::RenderAudio() {
87    
88     switch (this->PlaybackState) {
89    
90     case playback_state_ram: {
91     Interpolate((sample_t*) pSample->GetCache().pStart);
92     if (DiskVoice) {
93     // check if we reached the allowed limit of the sample RAM cache
94     if (Pos > MaxRAMPos) {
95     dmsg(("Voice: switching to disk playback (Pos=%f)\n", Pos));
96     this->PlaybackState = playback_state_disk;
97     }
98     }
99     else if (Pos >= pSample->GetCache().Size / pSample->FrameSize) {
100     this->PlaybackState = playback_state_end;
101     }
102     }
103     break;
104    
105     case playback_state_disk: {
106     if (!DiskStreamRef.pStream) {
107     // check if the disk thread created our ordered disk stream in the meantime
108     DiskStreamRef.pStream = pDiskThread->AskForCreatedStream(DiskStreamRef.OrderID);
109     if (!DiskStreamRef.pStream) {
110     std::cout << stderr << "Disk stream not available in time!" << std::endl << std::flush;
111     pDiskThread->OrderDeletionOfStream(&DiskStreamRef);
112     this->Active = false;
113     return;
114     }
115     DiskStreamRef.pStream->IncrementReadPos(pSample->Channels * (double_to_int(Pos) - MaxRAMPos));
116     Pos -= double_to_int(Pos);
117     }
118    
119     // add silence sample at the end if we reached the end of the stream (for the interpolator)
120     if (DiskStreamRef.State == Stream::state_end && DiskStreamRef.pStream->GetReadSpace() < (OutputBufferSize << MAX_PITCH) / pSample->Channels) {
121     DiskStreamRef.pStream->WriteSilence((OutputBufferSize << MAX_PITCH) / pSample->Channels);
122     this->PlaybackState = playback_state_end;
123     }
124    
125     sample_t* ptr = DiskStreamRef.pStream->GetReadPtr(); // get the current read_ptr within the ringbuffer where we read the samples from
126     Interpolate(ptr);
127     DiskStreamRef.pStream->IncrementReadPos(double_to_int(Pos) * pSample->Channels);
128     Pos -= double_to_int(Pos);
129     }
130     break;
131    
132     case playback_state_end:
133     this->Active = false; // free voice
134     break;
135     }
136     }
137    
138     void Voice::Interpolate(sample_t* pSrc) {
139     float effective_volume = 1; // TODO: use the art. data instead
140     int i = 0;
141    
142     // ************************************************
143     // TODO: ARTICULATION DATA HANDLING IS MISSING HERE
144     // ************************************************
145    
146     // FIXME: assuming either mono or stereo
147     if (this->pSample->Channels == 2) { // Stereo Sample
148     while (i < this->OutputBufferSize) {
149     #ifdef USE_LINEAR_INTERPOLATION
150     int pos_int = double_to_int(this->Pos); // integer position
151     float pos_fract = this->Pos - pos_int; // fractional part of position
152     pos_int <<= 1;
153     // left channel
154     this->pOutput[i++] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]));
155     // right channel
156     this->pOutput[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]));
157     #else // polynomial interpolation
158     //FIXME: !!!THIS WON'T WORK!!! needs to be adjusted for stereo, use linear interpolation meanwhile
159     xm1 = pSrc[pos_int];
160     x0 = pSrc[pos_int+1];
161     x1 = pSrc[pos_int+2];
162     x2 = pSrc[pos_int+3];
163     a = (3 * (x0-x1) - xm1 + x2) / 2;
164     b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
165     c = (x1 - xm1) / 2;
166     this->pOutput[u] += effective_volume*((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
167     #endif // USE_LINEAR_INTERPOLATION
168    
169     this->Pos += this->CurrentPitch;
170     }
171     }
172     else { // Mono Sample
173     while (i < this->OutputBufferSize) {
174     #ifdef USE_LINEAR_INTERPOLATION
175     int pos_int = double_to_int(this->Pos); // integer position
176     float pos_fract = this->Pos - pos_int; // fractional part of position
177     float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int]));
178     this->pOutput[i] += sample_point;
179     this->pOutput[i+1] += sample_point;
180     i += 2;
181     #else // polynomial interpolation
182     //FIXME: !!!THIS WON'T WORK!!! needs to be adjusted for stereo, use linear interpolation meanwhile
183     xm1 = pSrc[pos_int];
184     x0 = pSrc[pos_int+1];
185     x1 = pSrc[pos_int+2];
186     x2 = pSrc[pos_int+3];
187     a = (3 * (x0-x1) - xm1 + x2) / 2;
188     b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
189     c = (x1 - xm1) / 2;
190     this->pOutput[u] += effective_volume*((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
191     #endif
192    
193     this->Pos += this->CurrentPitch;
194     }
195     }
196     }
197    
198     void Voice::Kill() {
199     if (DiskVoice && DiskStreamRef.State != Stream::state_unused) {
200     pDiskThread->OrderDeletionOfStream(&DiskStreamRef);
201     }
202     Active = false;
203     }

  ViewVC Help
Powered by ViewVC