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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (show annotations) (download) (as text)
Fri Mar 5 13:46:15 2004 UTC (20 years, 1 month ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 9558 byte(s)
* implemented parser for the LinuxSampler control protocol (LSCP) by using
  flex / bison (where src/network/lscp.l is the input file for lex / flex
  and src/network/lscp.y is the input file for yacc / bison), parser and
  scanner can be regenerated by 'make parser'
* implemented LSCP network server (only single threaded so far), LSCP
  server will be launched if LinuxSampler was started with "--server" flag,
  implemented the following LSCP commands so far: "LOAD INSTRUMENT", "GET
  CHANNEL VOICE_COUNT", "GET CHANNEL STREAM_COUNT", "GET CHANNEL
  BUFFER_FILL", "SET CHANNEL VOLUME" and "RESET CHANNEL"
* disk thread now started within the engine

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 #ifndef __VOICE_H__
24 #define __VOICE_H__
25
26 #include "global.h"
27 #include "diskthread.h"
28 #include "ringbuffer.h"
29 #include "stream.h"
30 #include "gig.h"
31 #include "eg_vca.h"
32 #include "rtelmemorypool.h"
33 #include "audiothread.h"
34
35 #define MAX_PITCH 4 //FIXME: at the moment in octaves, should be changed into semitones
36 #define USE_LINEAR_INTERPOLATION 1 ///< set to 0 if you prefer cubic interpolation (slower, better quality)
37
38 class Voice {
39 public:
40 // Attributes
41 int MIDIKey; ///< MIDI key number of the key that triggered the voice
42 uint ReleaseVelocity; ///< Reflects the release velocity value if a note-off command arrived for the voice.
43
44 // Static Attributes
45 static DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again
46 static AudioThread* pEngine; ///< Pointer to the engine, to be able to access the event lists.
47
48 // Methods
49 Voice();
50 ~Voice();
51 void Kill();
52 void Render(uint Samples);
53 void Reset();
54 int Trigger(ModulationSystem::Event* pNoteOnEvent, int Pitch, gig::Instrument* pInstrument);
55 inline bool IsActive() { return Active; }
56 inline void SetOutputLeft(float* pOutput, uint MaxSamplesPerCycle) { this->pOutputLeft = pOutput; this->MaxSamplesPerCycle = MaxSamplesPerCycle; }
57 inline void SetOutputRight(float* pOutput, uint MaxSamplesPerCycle) { this->pOutputRight = pOutput; this->MaxSamplesPerCycle = MaxSamplesPerCycle; }
58 private:
59 // Types
60 enum playback_state_t {
61 playback_state_ram,
62 playback_state_disk,
63 playback_state_end
64 };
65
66 // Attributes
67 float Volume; ///< Volume level of the voice
68 float* pOutputLeft; ///< Audio output buffer (left channel)
69 float* pOutputRight; ///< Audio output buffer (right channel)
70 uint MaxSamplesPerCycle; ///< Size of each audio output buffer
71 double Pos; ///< Current playback position in sample
72 double Pitch; ///< Current pitch depth (number of sample points to move on with each render step)
73 gig::Sample* pSample; ///< Pointer to the sample to be played back
74 gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice
75 bool Active; ///< If this voice object is currently in usage
76 playback_state_t PlaybackState; ///< When a sample will be triggered, it will be first played from RAM cache and after a couple of sample points it will switch to disk streaming and at the end of a disk stream we have to add null samples, so the interpolator can do it's work correctly
77 bool DiskVoice; ///< If the sample is very short it completely fits into the RAM cache and doesn't need to be streamed from disk, in that case this flag is set to false
78 Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream
79 unsigned long MaxRAMPos; ///< The upper allowed limit (not actually the end) in the RAM sample cache, after that point it's not safe to chase the interpolator another time over over the current cache position, instead we switch to disk then.
80 bool RAMLoop; ///< If this voice has a loop defined which completely fits into the cached RAM part of the sample, in this case we handle the looping within the voice class, else if the loop is located in the disk stream part, we let the disk stream handle the looping
81 int LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed
82 uint Delay; ///< Number of sample points the rendering process of this voice should be delayed (jitter correction), will be set to 0 after the first audio fragment cycle
83 EG_VCA EG1;
84 ModulationSystem::Event* pTriggerEvent; ///< First event on the key's list the voice should process (only needed for the first audio fragment in which voice was triggered, after that it will be set to NULL).
85
86 // Methods
87 void ProcessEvents(uint Samples);
88 void Interpolate(uint Samples, sample_t* pSrc, uint Skip);
89 void InterpolateAndLoop(uint Samples, sample_t* pSrc, uint Skip);
90 inline void InterpolateOneStep_Stereo(sample_t* pSrc, int& i, float& effective_volume, float& pitch) {
91 int pos_int = double_to_int(this->Pos); // integer position
92 float pos_fract = this->Pos - pos_int; // fractional part of position
93 pos_int <<= 1;
94
95 #if USE_LINEAR_INTERPOLATION
96 // left channel
97 this->pOutputLeft[i] += effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]));
98 // right channel
99 this->pOutputRight[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]));
100 #else // polynomial interpolation
101 // calculate left channel
102 float xm1 = pSrc[pos_int];
103 float x0 = pSrc[pos_int+2];
104 float x1 = pSrc[pos_int+4];
105 float x2 = pSrc[pos_int+6];
106 float a = (3 * (x0 - x1) - xm1 + x2) / 2;
107 float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
108 float c = (x1 - xm1) / 2;
109 this->pOutputLeft[i] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
110
111 //calculate right channel
112 xm1 = pSrc[pos_int+1];
113 x0 = pSrc[pos_int+3];
114 x1 = pSrc[pos_int+5];
115 x2 = pSrc[pos_int+7];
116 a = (3 * (x0 - x1) - xm1 + x2) / 2;
117 b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
118 c = (x1 - xm1) / 2;
119 this->pOutputRight[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
120 #endif // USE_LINEAR_INTERPOLATION
121
122 this->Pos += pitch;
123 }
124 inline void InterpolateOneStep_Mono(sample_t* pSrc, int& i, float& effective_volume, float& pitch) {
125 int pos_int = double_to_int(this->Pos); // integer position
126 float pos_fract = this->Pos - pos_int; // fractional part of position
127
128 #if USE_LINEAR_INTERPOLATION
129 float sample_point = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int]));
130 #else // polynomial interpolation
131 float xm1 = pSrc[pos_int];
132 float x0 = pSrc[pos_int+1];
133 float x1 = pSrc[pos_int+2];
134 float x2 = pSrc[pos_int+3];
135 float a = (3 * (x0 - x1) - xm1 + x2) / 2;
136 float b = 2 * x1 + xm1 - (5 * x0 + x2) / 2;
137 float c = (x1 - xm1) / 2;
138 float sample_point = effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);
139 #endif // USE_LINEAR_INTERPOLATION
140
141 this->pOutputLeft[i] += sample_point;
142 this->pOutputRight[i++] += sample_point;
143
144 this->Pos += pitch;
145 }
146 inline int double_to_int(double f) {
147 #if ARCH_X86
148 int i;
149 __asm__ ("fistl %0" : "=m"(i) : "st"(f - 0.5) );
150 return i;
151 #else
152 return (int) f;
153 #endif // ARCH_X86
154 }
155 };
156
157 #endif // __VOICE_H__

  ViewVC Help
Powered by ViewVC