/[svn]/linuxsampler/trunk/src/engines/gig/Stream.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/engines/gig/Stream.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 505 - (hide annotations) (download) (as text)
Tue May 3 01:00:25 2005 UTC (19 years ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 8144 byte(s)
fixed libgig include rules

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 385 * Copyright (C) 2005 Christian Schoenebeck *
7 schoenebeck 53 * *
8     * This program is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #ifndef __LS_GIG_STREAM_H__
25     #define __LS_GIG_STREAM_H__
26    
27     #include "../../common/global.h"
28    
29     #if DEBUG_HEADERS
30     # warning Stream.h included
31     #endif // DEBUG_HEADERS
32    
33 schoenebeck 505 #include <gig.h>
34    
35 schoenebeck 53 #include "../../common/RingBuffer.h"
36    
37     namespace LinuxSampler { namespace gig {
38    
39     class Stream {
40     public:
41     // Member Types
42     typedef uint32_t OrderID_t;
43     typedef uint32_t Handle; ///< unique identifier of a relationship between one stream and a consumer (Voice)
44     enum state_t { ///< streams go through severe cyclic state transition (unused->active->end->unused->...)
45     state_unused, ///< stream is not in use, thus can still be launched
46     state_active, ///< stream provides data in it's buffer to be read and hasn't reached the end yet (this is the usual case)
47     state_end ///< stream end reached but still providing data in it's buffer to be read (after the client read all remaining data from the stream buffer, state will change automatically to state_unused)
48     };
49     struct reference_t { ///< Defines the current relationship between the stream and a client (voice).
50     OrderID_t OrderID; ///< Unique identifier that identifies the creation order of a stream requested by a voice.
51     Handle hStream; ///< Unique identifier of the relationship between stream and client.
52     state_t State; ///< Current state of the stream that will be pretended to the client (the actual state of the stream might differ though, because the stream might already be in use by another client).
53     Stream* pStream; ///< Points to the assigned and activated stream or is NULL if the disk thread hasn't launched a stream yet.
54     };
55    
56     // Methods
57 schoenebeck 385 Stream( ::gig::buffer_t* pDecompressionBuffer, uint BufferSize, uint BufferWrapElements);
58 letz 502 virtual ~Stream();
59 schoenebeck 53 int ReadAhead(unsigned long SampleCount);
60     void WriteSilence(unsigned long SilenceSampleWords);
61    
62     inline int GetReadSpace() {
63 schoenebeck 323 return (pRingBuffer && State != state_unused) ? pRingBuffer->read_space() : 0;
64 schoenebeck 53 }
65    
66     inline int GetWriteSpace() {
67     return (pRingBuffer && State == state_active) ? pRingBuffer->write_space() : 0;
68     }
69    
70     inline int GetWriteSpaceToEnd() {
71     return (pRingBuffer && State == state_active) ? pRingBuffer->write_space_to_end_with_wrap() : 0;
72     }
73    
74     // adjusts the write space to avoid buffer boundaries which would lead to the wrap space
75     // within the buffer (needed for interpolation) getting filled only partially
76     // for more infos see the docs in ringbuffer.h at adjust_write_space_to_avoid_boundary()
77     inline int AdjustWriteSpaceToAvoidBoundary(int cnt, int capped_cnt) {
78     return pRingBuffer->adjust_write_space_to_avoid_boundary(cnt, capped_cnt);
79     }
80    
81     inline sample_t* GetReadPointer() {
82     return pRingBuffer->get_read_ptr();
83     }
84    
85     // gets the current read_ptr within the ringbuffer
86     inline sample_t* GetReadPtr(void) {
87     return pRingBuffer->get_read_ptr();
88     }
89    
90     inline void IncrementReadPos(uint Count) {
91     uint leftspace = pRingBuffer->read_space();
92     pRingBuffer->increment_read_ptr(Min(Count, leftspace));
93     if (State == state_end && Count >= leftspace) {
94     Reset(); // quit relation between consumer (voice) and stream and reset stream right after
95     }
96     }
97    
98     // Static Method
99     inline static uint GetUnusedStreams() { return UnusedStreams; }
100     protected:
101     // Methods
102     void Launch(Stream::Handle hStream, reference_t* pExportReference, ::gig::Sample* pSample, unsigned long SampleOffset, bool DoLoop);
103     inline void Kill() { pExportReference = NULL; Reset(); } ///< Will be called by disk thread after a 'deletion' command from the audio thread (within the voice class)
104     inline Stream::Handle GetHandle() { return hThis; }
105     inline Stream::state_t GetState() { return State; }
106     friend class DiskThread; // only the disk thread should be able to launch and most important kill a disk stream to avoid race conditions
107     private:
108     // Attributes
109     reference_t* pExportReference;
110     state_t State;
111     Stream::Handle hThis;
112     unsigned long SampleOffset;
113     ::gig::Sample* pSample;
114     ::gig::playback_state_t PlaybackState;
115     RingBuffer<sample_t>* pRingBuffer;
116     bool DoLoop;
117 schoenebeck 385 ::gig::buffer_t* pDecompressionBuffer;
118 schoenebeck 53
119     // Static Attributes
120     static uint UnusedStreams; //< Reflects how many stream objects of all stream instances are currently not in use.
121 senkov 333 static uint TotalStreams; //< Reflects how many stream objects currently exist.
122 schoenebeck 53
123     // Methods
124     inline void Reset() {
125     SampleOffset = 0;
126     pSample = NULL;
127     PlaybackState.position = 0;
128     PlaybackState.reverse = false;
129     hThis = 0;
130     pRingBuffer->init(); // reset ringbuffer
131     if (State != state_unused) {
132     // we can't do 'SetPos(state_unused)' here, due to possible race conditions)
133     if (pExportReference) {
134     pExportReference->State = state_unused;
135     pExportReference = NULL;
136     }
137     State = state_unused;
138     UnusedStreams++;
139     }
140     }
141     inline void SetState(state_t State) {
142     if (pExportReference) pExportReference->State = State;
143     this->State = State;
144     }
145     inline long Min(long a, long b) { return (a < b) ? a : b; }
146     };
147    
148     }} // namespace LinuxSampler::gig
149    
150     #endif // __LS_GIG_STREAM_H__

  ViewVC Help
Powered by ViewVC