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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (hide annotations) (download)
Fri Jan 2 00:02:56 2004 UTC (20 years, 3 months ago) by schoenebeck
File size: 11677 byte(s)
* src/gig.cpp: fixed looping informations ('LoopStart', 'LoopEnd' and
  'LoopSize')
* src/voice.cpp: pitch sample according to keyboard position only if
  keyrange > 1 key, this was the reason that some gig files were horrible
  detuned
* src/audioio.cpp: bigendian specific fix
* src/ringbuffer.h: removed kernel header dependency by adding own atomic.h

1 schoenebeck 9 /***************************************************************************
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 schoenebeck 18 /**
38     * Initializes and triggers the voice, a disk stream will be launched if
39     * needed.
40     *
41     * @returns 0 on success, a value < 0 if something failed
42     */
43     int Voice::Trigger(int MIDIKey, uint8_t Velocity, gig::Instrument* Instrument) {
44 schoenebeck 15 Active = true;
45     this->MIDIKey = MIDIKey;
46     pRegion = Instrument->GetRegion(MIDIKey);
47     PlaybackState = playback_state_ram; // we always start playback from RAM cache and switch then to disk if needed
48     Pos = 0;
49     ReleaseVelocity = 127; // default release velocity value
50 schoenebeck 9
51     if (!pRegion) {
52     std::cerr << "Audio Thread: No Region defined for MIDI key " << MIDIKey << std::endl << std::flush;
53 schoenebeck 18 Kill();
54     return -1;
55 schoenebeck 9 }
56    
57     //TODO: current MIDI controller values are not taken into account yet
58     gig::DimensionRegion* pDimRgn = NULL;
59     for (int i = pRegion->Dimensions - 1; i >= 0; i--) { // Check if instrument has a velocity split
60     if (pRegion->pDimensionDefinitions[i].dimension == gig::dimension_velocity) {
61     uint DimValues[5] = {0,0,0,0,0};
62     DimValues[i] = Velocity;
63     pDimRgn = pRegion->GetDimensionRegionByValue(DimValues[4],DimValues[3],DimValues[2],DimValues[1],DimValues[0]);
64     break;
65     }
66     }
67     if (!pDimRgn) { // if there was no velocity split
68     pDimRgn = pRegion->GetDimensionRegionByValue(0,0,0,0,0);
69     }
70    
71     pSample = pDimRgn->pSample; // sample won't change until the voice is finished
72    
73     // Check if the sample needs disk streaming or is too short for that
74     long cachedsamples = pSample->GetCache().Size / pSample->FrameSize;
75     DiskVoice = cachedsamples < pSample->SamplesTotal;
76    
77 schoenebeck 26 if (DiskVoice) { // voice to be streamed from disk
78 schoenebeck 9 MaxRAMPos = cachedsamples - (OutputBufferSize << MAX_PITCH) / pSample->Channels;
79 schoenebeck 26
80     // check if there's a loop defined which completely fits into the cached (RAM) part of the sample
81     if (pSample->Loops && pSample->LoopEnd <= MaxRAMPos) {
82     RAMLoop = true;
83     LoopCyclesLeft = pSample->LoopPlayCount;
84     }
85     else RAMLoop = false;
86    
87     if (pDiskThread->OrderNewStream(&DiskStreamRef, pSample, MaxRAMPos, !RAMLoop) < 0) {
88 schoenebeck 18 dmsg(1,("Disk stream order failed!\n"));
89     Kill();
90     return -1;
91     }
92 schoenebeck 26 dmsg(4,("Disk voice launched (cached samples: %d, total Samples: %d, MaxRAMPos: %d, RAMLooping: %s\n", cachedsamples, pSample->SamplesTotal, MaxRAMPos, (RAMLoop) ? "yes" : "no"));
93 schoenebeck 9 }
94 schoenebeck 26 else { // RAM only voice
95 schoenebeck 9 MaxRAMPos = cachedsamples;
96 schoenebeck 26 if (pSample->Loops) {
97     RAMLoop = true;
98     LoopCyclesLeft = pSample->LoopPlayCount;
99     }
100     else RAMLoop = false;
101     dmsg(4,("RAM only voice launched (Looping: %s)\n", (RAMLoop) ? "yes" : "no"));
102 schoenebeck 9 }
103    
104 schoenebeck 28 // Pitch according to keyboard position (if keyrange > 1 key)
105     CurrentPitch = (pRegion->KeyRange.high != pRegion->KeyRange.low) ?
106     pow(2, (double) (MIDIKey - (int) pSample->MIDIUnityNote) / (double) 12) : 1.0;
107 schoenebeck 17 Volume = pDimRgn->GetVelocityAttenuation(Velocity);
108 schoenebeck 9
109     // ************************************************
110     // TODO: ARTICULATION DATA HANDLING IS MISSING HERE
111     // ************************************************
112 schoenebeck 18
113     return 0; // success
114 schoenebeck 9 }
115    
116 schoenebeck 26 /**
117     * Renders the audio data for this voice for the current audio fragment.
118     * The sample input data can either come from RAM (cached sample or sample
119     * part) or directly from disk. The output signal will be rendered by
120     * resampling / interpolation. If this voice is a disk streaming voice and
121     * the voice completely played back the cached RAM part of the sample, it
122     * will automatically switch to disk playback for the next RenderAudio()
123     * call.
124     */
125 schoenebeck 9 void Voice::RenderAudio() {
126    
127     switch (this->PlaybackState) {
128    
129     case playback_state_ram: {
130 schoenebeck 26 if (RAMLoop) InterpolateAndLoop((sample_t*) pSample->GetCache().pStart);
131     else Interpolate((sample_t*) pSample->GetCache().pStart);
132 schoenebeck 9 if (DiskVoice) {
133     // check if we reached the allowed limit of the sample RAM cache
134     if (Pos > MaxRAMPos) {
135 schoenebeck 12 dmsg(5,("Voice: switching to disk playback (Pos=%f)\n", Pos));
136 schoenebeck 9 this->PlaybackState = playback_state_disk;
137     }
138     }
139     else if (Pos >= pSample->GetCache().Size / pSample->FrameSize) {
140     this->PlaybackState = playback_state_end;
141     }
142     }
143     break;
144    
145     case playback_state_disk: {
146     if (!DiskStreamRef.pStream) {
147     // check if the disk thread created our ordered disk stream in the meantime
148     DiskStreamRef.pStream = pDiskThread->AskForCreatedStream(DiskStreamRef.OrderID);
149     if (!DiskStreamRef.pStream) {
150     std::cout << stderr << "Disk stream not available in time!" << std::endl << std::flush;
151 schoenebeck 18 Kill();
152 schoenebeck 9 return;
153     }
154     DiskStreamRef.pStream->IncrementReadPos(pSample->Channels * (double_to_int(Pos) - MaxRAMPos));
155     Pos -= double_to_int(Pos);
156     }
157    
158     // add silence sample at the end if we reached the end of the stream (for the interpolator)
159     if (DiskStreamRef.State == Stream::state_end && DiskStreamRef.pStream->GetReadSpace() < (OutputBufferSize << MAX_PITCH) / pSample->Channels) {
160     DiskStreamRef.pStream->WriteSilence((OutputBufferSize << MAX_PITCH) / pSample->Channels);
161     this->PlaybackState = playback_state_end;
162     }
163    
164     sample_t* ptr = DiskStreamRef.pStream->GetReadPtr(); // get the current read_ptr within the ringbuffer where we read the samples from
165     Interpolate(ptr);
166     DiskStreamRef.pStream->IncrementReadPos(double_to_int(Pos) * pSample->Channels);
167     Pos -= double_to_int(Pos);
168     }
169     break;
170    
171     case playback_state_end:
172 schoenebeck 13 Kill(); // free voice
173 schoenebeck 9 break;
174     }
175     }
176    
177 schoenebeck 26 /**
178     * Interpolates the input audio data (no loop).
179     *
180     * @param pSrc - pointer to input sample data
181     */
182 schoenebeck 9 void Voice::Interpolate(sample_t* pSrc) {
183 schoenebeck 17 float effective_volume = this->Volume;
184 schoenebeck 9 int i = 0;
185    
186     // ************************************************
187     // TODO: ARTICULATION DATA HANDLING IS MISSING HERE
188     // ************************************************
189    
190     // FIXME: assuming either mono or stereo
191     if (this->pSample->Channels == 2) { // Stereo Sample
192     while (i < this->OutputBufferSize) {
193 schoenebeck 26 InterpolateOneStep_Stereo(pSrc, i, effective_volume);
194 schoenebeck 9 }
195     }
196     else { // Mono Sample
197     while (i < this->OutputBufferSize) {
198 schoenebeck 26 InterpolateOneStep_Mono(pSrc, i, effective_volume);
199     }
200     }
201     }
202 schoenebeck 18
203 schoenebeck 26 /**
204     * Interpolates the input audio data, this method honors looping.
205     *
206     * @param pSrc - pointer to input sample data
207     */
208     void Voice::InterpolateAndLoop(sample_t* pSrc) {
209     float effective_volume = this->Volume;
210     int i = 0;
211 schoenebeck 9
212 schoenebeck 26 // ************************************************
213     // TODO: ARTICULATION DATA HANDLING IS MISSING HERE
214     // ************************************************
215 schoenebeck 18
216 schoenebeck 26 // FIXME: assuming either mono or stereo
217     if (pSample->Channels == 2) { // Stereo Sample
218     if (pSample->LoopPlayCount) {
219     // render loop (loop count limited)
220     while (i < OutputBufferSize && LoopCyclesLeft) {
221     InterpolateOneStep_Stereo(pSrc, i, effective_volume);
222     if (Pos > pSample->LoopEnd) {
223     Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);;
224     LoopCyclesLeft--;
225     }
226     }
227     // render on without loop
228     while (i < OutputBufferSize) {
229     InterpolateOneStep_Stereo(pSrc, i, effective_volume);
230     }
231 schoenebeck 9 }
232 schoenebeck 26 else { // render loop (endless loop)
233     while (i < OutputBufferSize) {
234     InterpolateOneStep_Stereo(pSrc, i, effective_volume);
235     if (Pos > pSample->LoopEnd) {
236     Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);
237     }
238     }
239     }
240 schoenebeck 9 }
241 schoenebeck 26 else { // Mono Sample
242     if (pSample->LoopPlayCount) {
243     // render loop (loop count limited)
244     while (i < OutputBufferSize && LoopCyclesLeft) {
245     InterpolateOneStep_Mono(pSrc, i, effective_volume);
246     if (Pos > pSample->LoopEnd) {
247     Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);;
248     LoopCyclesLeft--;
249     }
250     }
251     // render on without loop
252     while (i < OutputBufferSize) {
253     InterpolateOneStep_Mono(pSrc, i, effective_volume);
254     }
255     }
256     else { // render loop (endless loop)
257     while (i < OutputBufferSize) {
258     InterpolateOneStep_Mono(pSrc, i, effective_volume);
259     if (Pos > pSample->LoopEnd) {
260     Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);;
261     }
262     }
263     }
264     }
265 schoenebeck 9 }
266    
267 schoenebeck 26 /**
268     * Immediately kill the voice.
269     */
270 schoenebeck 9 void Voice::Kill() {
271     if (DiskVoice && DiskStreamRef.State != Stream::state_unused) {
272     pDiskThread->OrderDeletionOfStream(&DiskStreamRef);
273     }
274 schoenebeck 18 DiskStreamRef.pStream = NULL;
275     DiskStreamRef.hStream = 0;
276     DiskStreamRef.State = Stream::state_unused;
277     DiskStreamRef.OrderID = 0;
278 schoenebeck 9 Active = false;
279     }

  ViewVC Help
Powered by ViewVC