/[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 903 - (hide annotations) (download) (as text)
Sat Jul 22 14:22:53 2006 UTC (17 years, 10 months ago) by persson
File MIME type: text/x-c++hdr
File size: 8537 byte(s)
* real support for 24 bit samples - samples are not truncated to 16
  bits anymore
* support for aftertouch (channel pressure, not polyphonic aftertouch)

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 schoenebeck 563 /** @brief Buffered Disk Stream
40     *
41     * This encapsulation of a disk stream uses a ring buffer to allow
42     * thread safe refilling the stream's buffer with one thread (disk
43     * thread) and actual use / extraction of the audio data from the
44     * stream's buffer with another thread (audio thread).
45     */
46 schoenebeck 53 class Stream {
47     public:
48     // Member Types
49     typedef uint32_t OrderID_t;
50     typedef uint32_t Handle; ///< unique identifier of a relationship between one stream and a consumer (Voice)
51     enum state_t { ///< streams go through severe cyclic state transition (unused->active->end->unused->...)
52     state_unused, ///< stream is not in use, thus can still be launched
53     state_active, ///< stream provides data in it's buffer to be read and hasn't reached the end yet (this is the usual case)
54     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)
55     };
56     struct reference_t { ///< Defines the current relationship between the stream and a client (voice).
57     OrderID_t OrderID; ///< Unique identifier that identifies the creation order of a stream requested by a voice.
58     Handle hStream; ///< Unique identifier of the relationship between stream and client.
59     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).
60     Stream* pStream; ///< Points to the assigned and activated stream or is NULL if the disk thread hasn't launched a stream yet.
61     };
62    
63     // Methods
64 schoenebeck 385 Stream( ::gig::buffer_t* pDecompressionBuffer, uint BufferSize, uint BufferWrapElements);
65 letz 502 virtual ~Stream();
66 schoenebeck 53 int ReadAhead(unsigned long SampleCount);
67     void WriteSilence(unsigned long SilenceSampleWords);
68    
69     inline int GetReadSpace() {
70 persson 903 return (pRingBuffer && State != state_unused) ? pRingBuffer->read_space() / BytesPerSample : 0;
71 schoenebeck 53 }
72    
73     inline int GetWriteSpace() {
74     return (pRingBuffer && State == state_active) ? pRingBuffer->write_space() : 0;
75     }
76    
77     inline int GetWriteSpaceToEnd() {
78 persson 903 return (pRingBuffer && State == state_active) ? pRingBuffer->write_space_to_end_with_wrap() / BytesPerSample : 0;
79 schoenebeck 53 }
80    
81     // adjusts the write space to avoid buffer boundaries which would lead to the wrap space
82     // within the buffer (needed for interpolation) getting filled only partially
83     // for more infos see the docs in ringbuffer.h at adjust_write_space_to_avoid_boundary()
84     inline int AdjustWriteSpaceToAvoidBoundary(int cnt, int capped_cnt) {
85 persson 903 return pRingBuffer->adjust_write_space_to_avoid_boundary(cnt * BytesPerSample, capped_cnt * BytesPerSample) / BytesPerSample;
86 schoenebeck 53 }
87    
88     // gets the current read_ptr within the ringbuffer
89 persson 903 inline uint8_t* GetReadPtr(void) {
90 schoenebeck 53 return pRingBuffer->get_read_ptr();
91     }
92    
93     inline void IncrementReadPos(uint Count) {
94 persson 903 Count *= BytesPerSample;
95 schoenebeck 53 uint leftspace = pRingBuffer->read_space();
96     pRingBuffer->increment_read_ptr(Min(Count, leftspace));
97     if (State == state_end && Count >= leftspace) {
98     Reset(); // quit relation between consumer (voice) and stream and reset stream right after
99     }
100     }
101    
102     // Static Method
103     inline static uint GetUnusedStreams() { return UnusedStreams; }
104     protected:
105     // Methods
106 persson 865 void Launch(Stream::Handle hStream, reference_t* pExportReference, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop);
107 schoenebeck 53 inline void Kill() { pExportReference = NULL; Reset(); } ///< Will be called by disk thread after a 'deletion' command from the audio thread (within the voice class)
108     inline Stream::Handle GetHandle() { return hThis; }
109     inline Stream::state_t GetState() { return State; }
110     friend class DiskThread; // only the disk thread should be able to launch and most important kill a disk stream to avoid race conditions
111     private:
112     // Attributes
113     reference_t* pExportReference;
114     state_t State;
115     Stream::Handle hThis;
116     unsigned long SampleOffset;
117 persson 865 ::gig::DimensionRegion* pDimRgn;
118 schoenebeck 53 ::gig::playback_state_t PlaybackState;
119 persson 903 RingBuffer<uint8_t>* pRingBuffer;
120 schoenebeck 53 bool DoLoop;
121 schoenebeck 385 ::gig::buffer_t* pDecompressionBuffer;
122 persson 903 int BytesPerSample;
123 schoenebeck 53
124     // Static Attributes
125     static uint UnusedStreams; //< Reflects how many stream objects of all stream instances are currently not in use.
126 senkov 333 static uint TotalStreams; //< Reflects how many stream objects currently exist.
127 schoenebeck 53
128     // Methods
129     inline void Reset() {
130     SampleOffset = 0;
131 persson 865 pDimRgn = NULL;
132 schoenebeck 53 PlaybackState.position = 0;
133     PlaybackState.reverse = false;
134     hThis = 0;
135     pRingBuffer->init(); // reset ringbuffer
136     if (State != state_unused) {
137     // we can't do 'SetPos(state_unused)' here, due to possible race conditions)
138     if (pExportReference) {
139     pExportReference->State = state_unused;
140     pExportReference = NULL;
141     }
142     State = state_unused;
143     UnusedStreams++;
144     }
145     }
146     inline void SetState(state_t State) {
147     if (pExportReference) pExportReference->State = State;
148     this->State = State;
149     }
150     inline long Min(long a, long b) { return (a < b) ? a : b; }
151     };
152    
153     }} // namespace LinuxSampler::gig
154    
155     #endif // __LS_GIG_STREAM_H__

  ViewVC Help
Powered by ViewVC